使用 R 中的 Leaflet 从 shapefile 获取经度和纬度

Get longitude and latitude from a shapefile with Leaflet in R

我一直在尝试使用 leaflet 进行映射以添加 addCircles(),但我的 shapefile 似乎没有纬度 lat 和经度 lng 参数,所以我得到了每个城市的质心,如下面的代码所示,但它似乎没有显示质心点,所以我输入了 df 这些值,但我无法自动获取 n 个城市的点。

数据为here.

library(stringr)
library(leaflet)
library(sf)
library(dplyr)
quito = st_read("C:/Users/crist/Downloads/Administraciones Zonales/Administración_Zonal.shp") %>% 
  st_simplify(dTolerance = 1000) %>% 
  sf::st_transform('+init=epsg:4326')

sectores = read.csv("C:/Users/crist/Downloads/sector.csv", header = T,sep = ";", dec = ",", row.names = 1)
sectores

full_data = inner_join(quito, sectores, by = 'NOMBRE') %>%
  mutate(label_map = sprintf("<strong>%s </strong><br/>Valor: %g<br/>",NOMBRE, TARIFA_PROMEDIO_POR_HAB_DISPONIBLE_...) %>% lapply(htmltools::HTML))

bins = c(0, 10, 50, 100, 150,250)
pal_quito <- colorBin("Oranges", domain = full_data$TARIFA_PROMEDIO_POR_HAB_OCUPADA_..., 
                      bins = bins)

#I tried to do this because my shp didn't have the longitude and latitude variables 

full_data$centroids <- st_transform(full_data, 29101) %>% 
  st_centroid() %>% 
  st_geometry()

lngt_q = c(-78.41782, -78.67333, -78.4823, -78.60407, -78.36822, -78.50851, -78.56278, -78.3023)
lat_q = c(-0.08668143, -0.2179538, -0.1585809, 0.09029626, -0.4124271, -0.2112893, -0.311081, -0.2025039)

full_data$lngt_q =lngt_q
full_data$lat_q =lat_q

leaflet(data = full_data) %>%
  addTiles() %>% 
  addProviderTiles(providers$CartoDB.Positron) %>%
  addPolygons(data = full_data,
              color = "#444444", 
              weight = 1,
              smoothFactor = 0.5, 
              fillOpacity = 0.2,
              label = full_data$label_map, 
              fillColor = pal_quito(full_data$TARIFA_PROMEDIO_POR_HAB_OCUPADA_...),
              stroke = T,
              highlightOptions = highlightOptions(
                weight = 5,
                color = "#666666",
                fillOpacity = 0.7)) %>% 
  addCircles(full_data$centroids, lng = lngt_q, lat = lat_q, color = "#045a8d", weight = 1, radius =1500,
             fillOpacity = 0.2)

[更新] 有一种方法可以自动获取我的 shapefile 的经度和纬度。

我认为您可以通过使用 st_centroid 将质心几何体添加到多边形图层来实现。

library(stringr)
library(leaflet)
library(sf)
library(dplyr)
library(mapview)  # I believe this is needed to make code above function

quito = st_read("C:/Users/Brian/Downloads/Administraciones Zonales/Administración_Zonal.shp") %>% 
      st_simplify(dTolerance = 1000) %>%
      #logintud y latitud   # this produced an error, thin it is intended as comment
      sf::st_transform('+init=epsg:4326')

## Adding the centroid of each polygon as a separate geometry column. This will not be active but can be accessed as needed
quito$geom2 = st_centroid(quito)

您将收到一条警告,指出质心将不准确,因为您处于地理坐标系而非投影坐标系中,并且 st_centroid 假定投影几何。我猜这不会对中低纬度的相当小的多边形造成问题,但您应该意识到变形的可能性。如果您需要更高的准确性,您可以在变形为 EPSG:4326 (WGS84) 之前计算质心。如果您要走这条路,您可能希望将质心创建为单独的点,单独扭曲它们,然后加入或使用单独的点作为数据集以将圆添加到地图中。

这时候你可以继续写脚本直到最后一行,这时你需要指定我们之前创建的geom2列作为数据源

      addCircles(data = full_data$geom2, fill = TRUE, stroke = TRUE, color = "#000", fillColor = "blue", radius = 800, weight = 1)   ## I increased the radius to get it to display 

我不知道在此处分享传单的最佳做法,但显示的是静态图片