leaflet()将shapefile绘制为巨型矩形

时间:2018-12-06 21:45:03

标签: r gis sp rgdal r-leaflet

我遇到leaflet的问题,它正在绘制一个巨大的矩形而不是形状。我确定shapefile的格式存在问题,但是我无法确定出了什么问题。绘制文件效果很好。

文件:https://upload.cat/8c8ade09a3489b47

原始文件来源:http://sites.psu.edu/psucz/data/(在页面底部)

require(tidyverse)
require(leaflet)
require(rgdal)


ers_shp <- readOGR("ERS10.shp")

#Doesn't work, produces rectangle:
leaflet() %>% addProviderTiles("CartoDB.Positron") %>% addPolygons(data = ers_shp)

#Works, indicating the data is there.
plot(ers_shp, col="#f2f2f2", fill=TRUE, bg="skyblue", lwd=0.25, mar=rep(0,4), border=0 )

1 个答案:

答案 0 :(得分:2)

这是因为您需要先将多边形转换为经度/纬度,然后再将其传递给传单:

library(sf)
inv <- sf::st_read("ERS10.rep.shp") %>% 
  sf::st_transform(4326)
leaflet() %>% addProviderTiles("CartoDB.Positron") %>% addPolygons(data = inv)

OR

library(sp)
inv <- rgdal::readOGR("ERS10.rep.shp") %>%  
  spTransform(CRS("+proj=longlat +datum=WGS84"))
leaflet() %>% addProviderTiles("CartoDB.Positron") %>% addPolygons(data = inv)