使用R在leaflet地图上投影我的shapefile数据

8

我(在Win7上)尝试使用leaflet包显示我的shapefile数据(这里是数据文件),但没有成功。我只能看到背景地图瓷砖,而看不到我的数据。然而,在QGIS中,我能够看到我的数据。下面的代码和描述来自这里

library(rgdal)
shapeData <- readOGR(".",'myGIS')
ogrInfo(".",'myGIS')
leaflet()  %>% addTiles() %>% setView(lng = -106.363590, lat=31.968483,zoom=11) %>% addPolygons(data=shapeData,weight=5,col = 'red') %>% addMarkers(lng = -106.363590,lat=31.968483,popup="Hi there") 

这是我在互联网浏览器中看到的。我没有看到shapeData被投影在上面:

2个回答

13

Victorp的回答可行。不过我建议使用:

shapeData <- spTransform(shapeData, CRS("+proj=longlat +datum=WGS84 +no_defs"))

这个CRS对应于EPSG:4326。Leaflet实际上会负责将其从EPSG:4326转换为EPSG:3857(即“Google Mercator”)。

现在,差异(GRS80与WGS84)微不足道(在其中一个轴上为0.1毫米)。而且似乎leaflet认为两者都是相同的椭球体。但是出于测试目的,使用EPSG:4326会更好。


酷,我不知道那个。 - Victorp
这是一个很好的答案;不幸的是,当接收到具有外部CRS的数据时,leaflet没有返回错误消息。 - Konrad
这是对我有效的方法。使用sf包中的函数。使用x = st_read("shape file")读取形状文件,然后使用st_transform(x, '+proj=longlat +datum=WGS84')进行转换。 - Tony Ladson

8

你需要更改投影方式:

library("rgdal")
shapeData <- readOGR(".",'myGIS')
shapeData <- spTransform(shapeData, CRS("+proj=longlat +ellps=GRS80"))
library("leaflet")
leaflet()  %>% addTiles() %>% 
  setView(lng = -106.363590, lat=31.968483,zoom=11) %>% 
  addPolygons(data=shapeData,weight=5,col = 'red') %>% 
  addMarkers(lng = -106.363590,lat=31.968483,popup="Hi there") 

但我不能告诉你为什么这样做有效,我只了解一点地理和proj。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接