0

我想用我的国家苏里南的数据制作地图。该图应显示地图上的位置以及学校的数量。我唯一的数据是:

度假村 坐标 学校代码
帕拉马里博 比克惠岑 5°30'29,88"NB, 55°10'44,04"WL A1
帕拉马里博 比克惠岑 5°30'29,88"NB, 55°10'44,04"WL A2
帕拉马里博 中枢 5°30'29,9"NB, 55°10'44,0"WL A3
帕拉马里博 比克惠岑 5°30'29,88"NB, 55°10'44,04"WL A4
帕拉马里博 韦尔格勒根 5°50'33"NB,55°13'7"WL A5
瓦尼卡 新格隆德 5°45'NB,55°13'WL A6
瓦尼卡 栋堡 5°42'NB,55°5'WL A7
瓦尼卡 栋堡 5°42'NB,55°5'WL A8

我查看了说明视频,找到了有关使用邮政编码以及经度和纬度构建地图的说明。是否可以在 r 中使用这些数据来构建地图?如果是的话,你能给我一些指导如何做到这一点吗?如果答案是否定的,是否可以将此列转换为数据的经纬度甚至邮政编码?如果是,我该怎么做?

先感谢您。

4

1 回答 1

0

这是应该让您入门的东西;附加注释是内联的。

加载必要的库

# Load libraries
library(tidyverse)
library(magrittr)
library(sf)

下载苏里南形状文件

# Download Suriname shape files of country, district and resort boundaries
# Source: https://data.humdata.org/dataset/suriname-administrative-level-0-1-and-2-boundaries
url <- "https://data.humdata.org/dataset/ab35e673-76f2-43e5-a6a4-a5b81f9e093c/resource/fd31ebfd-bf77-4b7d-902f-3ea3a9c2d7a2/download/sur_adm_2017_shp.zip"
download.file(url, "suriname_data.zip")
unzip("suriname_data.zip", exdir = "data")

数据存储在文件夹 ./data 中。ZIP 文件还包含带有元数据信息的 PDF。我建议看看。

阅读苏里南区 shapefile

# Read shapefile of Suriname districts
# Note: Geodetic CRS = WGS84
suriname_district <- st_read("data/sur_admbnda_adm1_2017.shp")

解析本地数据

# Define custom function to parse coordinates from your data.frame
parse_coordinaten <- function(x) {

    x %>%
    str_remove_all("(NB|WL)") %>%        # Remove NB and WL suffixes
    str_split(", ") %>%                  # Split into lat/long string
    map(~ .x %>%
        str_replace_all(",", ".") %>%    # Replace , with . decimal separator
        str_split("[^(\\d|\\.)]") %>%    # Split each lat/long on non-digit char
        map_dbl(function(w) w %>%        # Convert deg/min/sec to fractional degree
            as.numeric() %>%
            multiply_by(c(1, 1/60, 1/3600)) %>%
            sum(na.rm = TRUE)
        ) %>%
        multiply_by(c(1, -1)) %>%        # Multiply long by -1 since west of prime meridian 
        setNames(c("lat", "lon")) %>%   
        bind_rows()
    ) %>%
        bind_rows()
}

# Read data; I assume you have a `data.frame` named `df` 
# Note: `dput(df)` included in the appendix
data <- df %>%
    mutate(Coordinaten = map(Coordinaten, parse_coordinaten)) %>%
    unnest(Coordinaten) %>%
    st_as_sf(coords = c("lon", "lat"), crs = 4326) %>%
    mutate(label = paste(District, Ressort, Schoolcode, sep = "<br>"))

阴谋

# Plot using `leaflet`
library(leaflet)
pal <- colorFactor("Spectral", levels = suriname_district$ADM1_NL)
leaflet() %>%
    addTiles() %>% 
    addPolygons(
        fillColor = ~pal(ADM1_NL),
        fillOpacity = 0.6,
        weight = 0,
        data = suriname_district) %>%
    addMarkers(
        data = data,
        label = ~map(label, htmltools::HTML))

在此处输入图像描述

leaflet允许大量定制;例如,您可以调整标记标签、添加度假村边界等。


附录:样本数据

df <- structure(list(District = c("Paramaribo", "Paramaribo", "Paramaribo", 
"Paramaribo", "Paramaribo", "Wanica", "Wanica", "Wanica"), Ressort = c("Beekhuizen", 
"Beekhuizen", "Centrum", "Beekhuizen", "Welgelegen", "Nieuwe Grond", 
"Domburg", "Domburg"), Coordinaten = c("5°30'29,88NB, 55°10'44,04WL", 
"5°30'29,88NB, 55°10'44,04WL", "5°30'29,9NB, 55°10'44,0WL", 
"5°30'29,88NB, 55°10'44,04WL", "5°50'33NB, 55°13'7WL", "5°45'NB, 55°13'WL", 
"5°42'NB, 55°5'WL", "5°42'NB, 55°5'WL"), Schoolcode = c("A1", 
"A2", "A3", "A4", "A5", "A6", "A7", "A8")), class = "data.frame", row.names = c(NA, 
-8L))
于 2021-05-19T12:28:59.577 回答