这是应该让您入门的东西;附加注释是内联的。
加载必要的库
# 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))