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

Projecting my shapefile data on leaflet map using R

我(在 Win7 上)试图获取我的 shapefile 数据(Here are the data files.) to be displayed using leaflet package. But without any success. I only get the background map tile but not my data. I am able to see my data in QGIS though. I got the following code and description from here

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:

您好,您必须更改投影:

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") 

但是我不能告诉你为什么这有效,我对geo和proj只知道一点。

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 毫米)。而且传单似乎认为两者都是同一个椭圆体。但是出于测试目的,建议使用 EPSG:4326.