获取具有指定边界坐标的地图

时间:2022-11-20 22:48:35

I want to get a map with RgoogleMaps from R, with a specific coordinates boundary.

我想从R获得带有RgoogleMaps的地图,具有特定的坐标边界。

What I can call is GetMap, and specify a center, I must add a zoom level. Everything works fine, except that I am not getting an image map bounded with the coordinates I choose.

我可以称之为GetMap,并指定一个中心,我必须添加一个缩放级别。一切都很好,除了我没有得到一个与我选择的坐标有界的图像地图。

Here's an example:

这是一个例子:

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") 

The problem is that only the center is passed as a parameter, and thus the whole image I see is dependant on the zoom level. So, I cannot really understand what are the boundaries of the image I get. What I want to do is to get an image bounded exactly with the coordinates I am defining. Is this possible (also with other map packages)?

问题是只有中心作为参数传递,因此我看到的整个图像取决于缩放级别。所以,我无法真正理解我得到的图像的边界。我想要做的是使用我定义的坐标精确限制图像。这是可能的(还有其他地图包)吗?

4 个解决方案

#1


24  

Here is one way. First, you get a map with a certain zoom. Then, you add the lon and lat limit when you draw a figure, which you can do with scale_x_continuous and scale_y_continuous.

这是一种方式。首先,您将获得具有特定缩放的地图。然后,在绘制图形时添加lon和lat限制,可以使用scale_x_continuous和scale_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

获取具有指定边界坐标的地图

#2


6  

Another option is using OpenStreetMap as a source for your map. With the get_map function from the ggmap package, you can specify the boundaries of your map when you use OpenStreetMap as a source. With:

另一个选择是使用OpenStreetMap作为地图的源。使用ggmap包中的get_map函数,可以在使用OpenStreetMap作为源时指定地图的边界。附:

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

you get:

你得到:

获取具有指定边界坐标的地图

However, this method does not work with GoogleMaps. Specifying the boundaries with GoogleMaps as a source will give you the following warning:

但是,此方法不适用于GoogleMaps。使用GoogleMaps作为源指定边界将给出以下警告:

Warning: bounding box given to google - spatial extent only approximate. converting bounding box to center/zoom specification. (experimental)

警告:给谷歌的边界框 - 空间范围只是近似值。将边界框转换为中心/缩放规范。 (实验)

A drawback of using OpenStreetMap is that you won't have access to satelite images.

使用OpenStreetMap的一个缺点是您无法访问卫星图像。

#3


2  

Another way for an actual interactive Google Map is with my googleway package

实际交互式Google Map的另一种方式是使用我的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)

获取具有指定边界坐标的地图

Which, being a Google Map, comes with satellite imagary as standard

作为谷歌地图,标准配备了卫星图像

获取具有指定边界坐标的地图

#4


1  

I wish I had seen this question earlier. The RgoogleMaps package offers two ways of retrieving a map: GetMap(center, zoom) and GetMap.bbox(lonR,latR) which simply takes the bounding box as parameters. The zoom level is automatically computed. I think that the latter function could be what you are looking for. Markus

我希望我早些时候见过这个问题。 RgoogleMaps包提供了两种检索地图的方法:GetMap(中心,缩放)和GetMap.bbox(lonR,latR),它只是将边界框作为参数。自动计算缩放级别。我认为后者的功能可能就是你想要的。马库斯

#1


24  

Here is one way. First, you get a map with a certain zoom. Then, you add the lon and lat limit when you draw a figure, which you can do with scale_x_continuous and scale_y_continuous.

这是一种方式。首先,您将获得具有特定缩放的地图。然后,在绘制图形时添加lon和lat限制,可以使用scale_x_continuous和scale_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

获取具有指定边界坐标的地图

#2


6  

Another option is using OpenStreetMap as a source for your map. With the get_map function from the ggmap package, you can specify the boundaries of your map when you use OpenStreetMap as a source. With:

另一个选择是使用OpenStreetMap作为地图的源。使用ggmap包中的get_map函数,可以在使用OpenStreetMap作为源时指定地图的边界。附:

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

you get:

你得到:

获取具有指定边界坐标的地图

However, this method does not work with GoogleMaps. Specifying the boundaries with GoogleMaps as a source will give you the following warning:

但是,此方法不适用于GoogleMaps。使用GoogleMaps作为源指定边界将给出以下警告:

Warning: bounding box given to google - spatial extent only approximate. converting bounding box to center/zoom specification. (experimental)

警告:给谷歌的边界框 - 空间范围只是近似值。将边界框转换为中心/缩放规范。 (实验)

A drawback of using OpenStreetMap is that you won't have access to satelite images.

使用OpenStreetMap的一个缺点是您无法访问卫星图像。

#3


2  

Another way for an actual interactive Google Map is with my googleway package

实际交互式Google Map的另一种方式是使用我的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)

获取具有指定边界坐标的地图

Which, being a Google Map, comes with satellite imagary as standard

作为谷歌地图,标准配备了卫星图像

获取具有指定边界坐标的地图

#4


1  

I wish I had seen this question earlier. The RgoogleMaps package offers two ways of retrieving a map: GetMap(center, zoom) and GetMap.bbox(lonR,latR) which simply takes the bounding box as parameters. The zoom level is automatically computed. I think that the latter function could be what you are looking for. Markus

我希望我早些时候见过这个问题。 RgoogleMaps包提供了两种检索地图的方法:GetMap(中心,缩放)和GetMap.bbox(lonR,latR),它只是将边界框作为参数。自动计算缩放级别。我认为后者的功能可能就是你想要的。马库斯