crsobj <- CRS("+proj=longlat +datum=WGS84") #Web Mercator projection system
shpfile_t <- spTransform(shpfile,crsobj) #Applying projection transformation
df <- as.data.frame(coordinates(shpfile_t)) #Converting to a data frame
head(df,5)
coords.x1<dbl>coords.x2<dbl>
1 103.8391 1.281441
2 103.8462 1.274459
3 103.8443 1.272756
4 103.8497 1.284547
5 103.8374 1.282626
我们注意到变换后的投影系统。让我们将其绘制在底图之上。
sgmap <- get_map(location="Singapore", zoom=11,
maptype="roadmap", source="google") #Using Osm base map of Singapore
p <- ggmap(sgmap) +
geom_point(data = df,
aes(x = coords.x1, y = coords.x2),
color = 'orange',size = 1)
p
The data is from data.gov.sg. Loading the packages required to plot the maps
We observe that the coordinates are in a different projection system. So we have to convert this to web mercator projection system. We transform the shapefile using spTransform.
crsobj <- CRS("+proj=longlat +datum=WGS84") #Web Mercator projection system
shpfile_t <- spTransform(shpfile,crsobj) #Applying projection transformation
df <- as.data.frame(coordinates(shpfile_t)) #Converting to a data frame
head(df,5)
coords.x1<dbl>coords.x2<dbl>
1 103.8391 1.281441
2 103.8462 1.274459
3 103.8443 1.272756
4 103.8497 1.284547
5 103.8374 1.282626
We notice the transformed projection system. Let us plot it on top of a base map.
sgmap <- get_map(location="Singapore", zoom=11,
maptype="roadmap", source="google") #Using Osm base map of Singapore
p <- ggmap(sgmap) +
geom_point(data = df,
aes(x = coords.x1, y = coords.x2),
color = 'orange',size = 1)
p
发布评论
评论(2)
从开发者的角度来看,它似乎运行良好。
From the developer, it seems to work nicely.
数据来自data.gov.sg。加载绘制地图所需的包
使用 readOGR 读取 shapefile
我们观察到坐标位于不同的投影系统中。所以我们必须将其转换为网络墨卡托投影系统。我们使用 spTransform 转换 shapefile。
我们注意到变换后的投影系统。让我们将其绘制在底图之上。
The data is from data.gov.sg. Loading the packages required to plot the maps
Reading the shapefile using readOGR
We observe that the coordinates are in a different projection system. So we have to convert this to web mercator projection system. We transform the shapefile using spTransform.
We notice the transformed projection system. Let us plot it on top of a base map.