简体   繁体   English

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

[英]Get longitude and latitude from a shapefile with Leaflet in R

I've been trying to map with leaflet to add addCircles() but my shapefile doesn't seem to have the latitude lat and longitude lng parameters so I got the centroids for each city as is the code below but it doesn't seem to show me the centroid points so I put in a df these values but I can't automate to get the points for n cities.我一直在尝试使用 map 和leaflet添加addCircles()但我的 shapefile 似乎没有纬度lat和经度lng参数所以我得到了每个城市的质心,如下面的代码所示,但它似乎没有向我展示质心点,所以我输入了 df 这些值,但我无法自动获取 n 个城市的点。

The data is 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)

[updated] There's a way to get the longitude and latitude of my shapefile automatically. [更新]有一种方法可以自动获取我的 shapefile 的经度和纬度。

在此处输入图像描述

I think you can do this by adding the centroid geometry to your polygon layer with st_centroid .我认为您可以通过使用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)

You will get a warning indicating that the centroids won't be accurate, because you are in a geographic rather than projected coordinate system and st_centroid assumes projected geometry.您将收到一条警告,指出质心将不准确,因为您处于地理而非投影坐标系中,并且 st_centroid 假定为投影几何。 I would guess this isn't going to cause problems with reasonably small sized polygons at lower or middle latitudes, but you should be aware of the potential for distortion.我想这不会对低纬度或中纬度的相当小尺寸的多边形造成问题,但您应该意识到失真的可能性。 If you need more accuracy, you could calculate your centroids before you warp into EPSG:4326 (WGS84).如果您需要更高的精度,您可以在变形为 EPSG:4326 (WGS84) 之前计算您的质心。 If you are going this route, you probably want to create the centroids as separate points, warp them separately and either join or use the separate points as your dataset to add the circles to your map.如果您要走这条路线,您可能希望将质心创建为单独的点,分别扭曲它们,然后连接或使用单独的点作为数据集,将圆添加到 map。

At this point you can continue along the script as written until the last line, when you need to specify the geom2 column we created earlier as the data source此时您可以继续编写脚本直到最后一行,此时您需要指定我们之前创建的 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 

在此处输入图像描述

I don't know best practices to share the leaflet here, but static image shown我不知道在此处分享 leaflet 的最佳实践,但显示了 static 图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM