从带有R中的传单的形状文件中获取经度和纬度

从带有R中的传单的形状文件中获取经度和纬度,r,leaflet,maps,centroid,R,Leaflet,Maps,Centroid,我一直在尝试使用单张来添加addCircles()但我的shapefile似乎没有纬度lat和经度lng参数,因此我获得了每个城市的质心,如下代码所示,但它似乎没有显示质心点,因此我输入了这些值的df,但我无法自动获得n个城市的点 数据是 库(stringr) 图书馆(单张) 图书馆(sf) 图书馆(dplyr) 基多=st_read(“C:/Users/crist/Downloads/Administraciones Zonales/Administración_Zonal.shp”)%>%

我一直在尝试使用
单张
来添加
addCircles()
但我的shapefile似乎没有纬度
lat
和经度
lng
参数,因此我获得了每个城市的质心,如下代码所示,但它似乎没有显示质心点,因此我输入了这些值的df,但我无法自动获得n个城市的点

数据是

库(stringr)
图书馆(单张)
图书馆(sf)
图书馆(dplyr)
基多=st_read(“C:/Users/crist/Downloads/Administraciones Zonales/Administración_Zonal.shp”)%>%
st_simplify(dtoleance=1000)%>%
sf::st_变换('+init=epsg:4326')
sectores=read.csv(“C:/Users/crist/Downloads/sector.csv”,header=T,sep=“;”,dec=“,”,row.names=1)
宗派
完整数据=内部联接(基多,扇区,by='NOMBRE')%>%
变异(label_map=sprintf(“%s
勇气:%g
”,NOMBRE,TARIFA_PROMEDIO_POR_HAB_disposable)%>%lapply(htmltools::HTML)) 垃圾箱=c(0、10、50、100、150250)
pal_quito我认为可以通过使用
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_质心采用投影几何体。我想这不会对低纬度或中纬度的小多边形造成问题,但你应该意识到变形的可能性。如果您需要更高的精度,可以在扭曲到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 


我不知道在这里分享传单的最佳做法,但显示的静态图像

哇,这太棒了,我知道我可以让这更简单。要共享地图,只需将其像普通绘图一样显示在查看器中。我有一个问题,为什么我不能使用
mutate(geom2=st_centroid(quito))
函数?我不是dplyr软件包的专家。@cdcarrion我认为这是因为tidyverse/dplyr中的mutate谓词期望为每行提供一个输出,例如均值或求和类型的函数,而st_形心从行开始运行并返回data.frame,因此dplyr不知道如何合并data.frame——看看
st_centroid(基多)
的输出——它返回所有属性。我怀疑dplyr不知道它只需要geometry列。也许有办法让它发挥作用,但我对sf软件包了解不够,无法让它发挥作用。
      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