我在创建地图时面临问题。我从 - 下载了一个形状文件
提取数据后,我找到了一个形状文件。我试图使用R代码在谷歌地图中绘制这个形状文件。但它没有显示任何东西?
library(maptools)
library(ggmap)
counties.mpl <- readShapePoints("bgd_poi_healthfacilities_lged")
#Coordinates looks like:
counties.mpl@coords
coords.x1 coords.x2
0 531533.8 2524464
1 531004.7 2531410
2 533228.5 2525061
3 531723.1 2488972
4 532347.8 2492098
5 518104.8 2520361
#map code:
mymap <- get_map(location="Bangladesh", zoom=6)
ggmap(mymap)+
geom_point(data=counties.mpl@coords,
aes(x=counties.mpl@coords[,1], y=counties.mpl@coords[,2]))
有人可以帮我解决这个问题吗?提前致谢。
正如其他人已经注意到的那样,你的shapefile使用不同的坐标系,你需要将它转换为纬度/经度才能使geom_point()
层能够很好地坐在mymap
上。
你的shapefile的.prj文件以:
PROJCS["BangladeshTM WGS1984",GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984", ...
链接here解释了每个部分的含义,但为了我们的目的,你只需要知道shapefile的投影坐标系是“BangladeshTM WGS1984”,即孟加拉国横向墨卡托,编码为EPSG:3106。
ggmap()
期望的典型纬度/经度坐标系是WGS 84,编码为EPSG:4326。
TLDR:将您的数据投影从EPSG:3106转换为EPSG:4326,并相应地进行绘图。
counties.mpl <- readShapePoints("bgd_poi_healthfacilities_lged")
# define current projection
proj4string(counties.mpl) <- CRS("+init=epsg:3106") # Bangladesh Transverse Mercator system
# remap to lat / long projection
counties.mpl.remapped <- spTransform(counties.mpl, CRS("+init=epsg:4326"))
# extract the coordinates as a data frame.
df <- as.data.frame(counties.mpl.remapped@coords)
colnames(df) <- c("lon", "lat")
# plot results
mymap <- get_map(location="Bangladesh", zoom=6)
ggmap(mymap) +
geom_point(data = df)