2014-09-03 53 views
14

我想從R獲得具有特定座標邊界的RgoogleMaps的地圖。獲取具有指定邊界座標的地圖

我可以調用的是GetMap,並指定一箇中心,我必須添加一個縮放級別。一切工作正常,除了我沒有得到與我選擇的座標有界的圖像映射。

下面是一個例子:

lat <- c(44.49,44.5)     
lon <- c(11.33,11.36)    
center = c(mean(lat), mean(lon))  
zoom <- 14       
mmap <- GetMap(center = center, zoom=zoom, maptype= "satellite", destfile = "m.png") 

的問題是,只有中心作爲參數傳遞,從而對整個圖像我看到的是依賴於縮放級別。所以,我無法真正理解我得到的圖像的邊界是什麼。我想要做的是讓圖像與我定義的座標完全一致。這是可能的(也與其他地圖包)?

回答

19

這是一種方法。首先,你得到一個具有一定縮放比例的地圖。然後,當您繪製一個圖形時,您可以添加lon和lat限制,您可以使用scale_x_continuousscale_y_continuous

library(ggmap) 
library(ggplot2) 

### Set a range 
lat <- c(44.49, 44.5)     
lon <- c(11.33, 11.36) 

### Get a map 
map <- get_map(location = c(lon = mean(lon), lat = mean(lat)), zoom = 14, 
       maptype = "satellite", source = "google") 

### When you draw a figure, you limit lon and lat.  
foo <- ggmap(map)+ 
     scale_x_continuous(limits = c(11.33, 11.36), expand = c(0, 0)) + 
     scale_y_continuous(limits = c(44.49, 44.5), expand = c(0, 0)) 

foo 

enter image description here

+0

這工作,謝謝:) – lbedogni 2014-09-05 13:32:07

+0

快樂,隊友。 – jazzurro 2014-09-06 04:02:42

+1

此過程以低縮放級別(14)獲取圖像,然後裁剪圖像,從而產生低分辨率圖像。更好的方法是直接將範圍對象更改爲谷歌質心和縮放級別。 – Faridcher 2016-05-14 12:30:11

6

另一種選擇是使用OpenStreetMap的作爲地圖的來源。使用ggmap包中的get_map函數,可以在使用OpenStreetMap作爲源時指定地圖的邊界。附:

mmap <- get_map(location = c(11.33,44.49,11.36,44.50), source = "osm") 
ggmap(mmap) 

你:

enter image description here

然而,這種方法不能與谷歌地圖的工作。指定與GoogleMaps的邊界作爲源會給你以下警告:

警告:給定的邊界框谷歌 - 空間範圍只有 近似。將邊界框轉換爲中心/縮放規格。 (實驗)

使用OpenStreetMap的缺點是你將無法訪問衛星圖像。

1

另一種方式的實際互動谷歌地圖是我的googleway

library(googleway) 

lat <- c(44.49,44.5)     
lon <- c(11.33,11.36) 
zoom <- 14 

mapKey <- 'your_api_key' 

google_map(location = c(mean(lat), mean(lon)), zoom = zoom, key = mapKey) 

enter image description here

其中,是一個谷歌地圖,配備了衛星imagary標準

enter image description here

0

我希望我早點見過這個問題。 RgoogleMaps包提供了兩種檢索地圖的方式:GetMap(center,zoom) and GetMap.bbox(lonR,latR)它只是將邊界框作爲參數。縮放級別是自動計算的。我認爲後者的功能可能就是你要找的。 Markus