Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Map with specified boundary coordinates

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

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.

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

like image 474
lbedogni Avatar asked Sep 03 '14 05:09

lbedogni


People also ask

What is LatLng in Google Map?

A LatLng is a point in geographical coordinates: latitude and longitude. Latitude ranges between -90 and 90 degrees, inclusive.


2 Answers

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.

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

like image 185
jazzurro Avatar answered Oct 03 '22 19:10

jazzurro


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:

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

you get:

enter image description here

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

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.

like image 21
Jaap Avatar answered Oct 03 '22 19:10

Jaap