使用 R 在 google 地图中定位位置点的问题
Problem in positioning the location points in google map using R
我在创建地图时遇到问题。我已经从-
下载了一个形状文件
提取数据后,我找到了一个形状文件。我正在尝试使用 R 代码在 google 地图中绘制此形状文件。但它没有显示任何内容?
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", ...
link here explains what each part means, but for our purpose, you just need to know that the shapefile's projected coordinate system is "BangladeshTM WGS1984", i.e. Bangladesh Transverse Mercator, coded as 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)
我在创建地图时遇到问题。我已经从-
下载了一个形状文件提取数据后,我找到了一个形状文件。我正在尝试使用 R 代码在 google 地图中绘制此形状文件。但它没有显示任何内容?
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", ...
link here explains what each part means, but for our purpose, you just need to know that the shapefile's projected coordinate system is "BangladeshTM WGS1984", i.e. Bangladesh Transverse Mercator, coded as 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)