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

问题描述 投票:0回答:1

我一直在尝试用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的经度和纬度。

enter image description here

r leaflet maps centroid
1个回答
0
投票

[我认为您可以通过使用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 

enter image description here

我不知道在此处共享传单的最佳做法,但显示了静态图像

© www.soinside.com 2019 - 2024. All rights reserved.