簡體   English   中英

在R中將shapefile添加到ggmap中

[英]add shapefile to a ggmap in R

我目前正在嘗試使用R中的ggmap將shapefile(代表河流)繪制到googlemap圖上。

我得到我的地圖:

map <- get_googlemap(center = c(lon = 13.40, lat = 52.50), zoom = 11, scale = 2,
                     size = c(640,480), color = "bw", maptype = "satellite")

我可以使用ggmap(map)但是在添加shapefile時遇到了麻煩。 我正在嘗試使用+ geom_polygon()來做到這一點,但ggmap希望我輸入坐標作為數據框。

我使用spTranform轉換了shapefile的坐標,但我不知道如何獲取轉換后的坐標以將其放入數據幀並將其傳遞給ggmap 如果我使用coordinates(riv) (其中“ riv”是shapefile的名稱),我會得到類似

     [,1]    [,2]
0 1483815 6898445
1 1484915 6898293
2 1485984 6898123
3 1486659 6898201
4 1487148 6898012
5 1487569 6897824

這不是我想要的格式。 dhapefile是使用Qgis生成的,但是我不能使用Qgis來轉換那里的坐標。 有什么幫助嗎?

您的問題似乎是坐標系。 對於Googlemaps,您將需要EPSG3857(請參閱此處 ),但是您還需要知道您的原始參考系統是什么(一旦知道,您可以從EPSG.io獲得Proj字符串描述符)。

library(sp)
# Proj description of Marcator 3857
crs3857 = '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs'
# Find out your coordinate system and get the proj description
original_crs = "<proj description>"
# Make sure that the CRS is correctly referenced in yor polygon
mypolygon@proj4string = CRS(original_crs)
# Convert the polygon to 3857
mypolygon3857 = spTransform(mypolygon, CRS(crs3857))
# Get the coordinates
mydata = coordinates(mypolygon3857)
names(mydata) = c("lon", "lat")
# Plot
map = get_googlemap(center = c(lon = 13.40, lat = 52.50), zoom = 11, scale = 2, size = c(640,480), color = "bw", maptype = "satellite")
ggmap(map) + geom_polygon(mydata, aes(x=lon, y=lat))

在您的情況下,使用組合坐標,可以使它像這樣工作:

library(sp)
library(ggmap)
map = get_googlemap(center = c(lon = 13.40, lat = 52.50), zoom = 11, scale = 2, size = c(640,480), color = "bw", maptype = "satellite")
lat = c(52.5 , 52.5, 52.55,52.55)
lon = c(13.3, 13.4, 13.4, 13.3)
mydata = data.frame(lon,lat)
ggmap(map) + geom_polygon(data=mydata, aes(x=lon, y=lat), col="red", fill=NA)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM