GISR

GGMap – a map background from Google Maps

Today we’ll take a look at the ggmap library, which allows us to retrieve a map background from Google Maps to present our data there (but more on that in the next posts). First, let’s install the ggmap package and activate it:

install.packages("ggmap")
library(ggmap)

The basic function in this package is get_map, which allows us to download a map fragment from Google Maps. Interestingly, we can select it by the place name specified in the location attribute:

map = get_map(location = "Warsaw, Copernicus Science Centre")

To display the map, we can use the ggmap function or a regular plot. The former additionally inserts coordinates into the map frame:

ggmap(map)

The result is a map printout with the center at the specified position:

Unfortunately, the zoom of the map is so small that the location we specify is barely visible.We can change the zoom by setting the value of the zoom attribute, which defaults to 10 (city scale). The zoom value can be set from 3 (continent scale) to 21 (single building scale). In our example, we set the value to 18 and display the map:

map = get_map(location = "Warsaw, Copernicus Science Centre", zoom = 18)
ggmap(map)

As a result, we get a printout of the map with the selected object visible in the center:

W mapach Google mapy możliwość przełączania różnych typów podkładów, np. mapowy, satelita lub hybrydowy. W get_map możemy zdefiniować, który z dostępnych podkładów chcemy pobrać poprzez atrybut maptype. Domyślnie ma on wartość “roadmap”. Zmiana wartości na “satellite” pobierze fragment mapy z podkładem ortofotomapy:

In the Google Maps it is possible to switch different types of underlay, e.g. map, satellite or hybrid. In get_map, we can specify which of the available underlays we want to retrieve via the maptype attribute. By default, it has the value “roadmap”. If you change the value to “satellite”, the map fragment with the orthophoto underlay will be retrieved:

map = get_map(location = "Warsaw, Copernicus Science Centre", zoom = 18, maptype = "satellite")
ggmap(map)

If you change the value to “hybrid”, the map fragment with hybrid underlay (roadmap + satellite) will be retrieved:

The attribute “location” defined as the name of the location based on the search for Google Maps retrieves its geolocation. We can also provide the location of the object directly using a vector of geographic coordinates, which is useful for more precise localization of an object that may not always be in the Google database:

map = get_map(location = c(lon = 20.0701, lat = 49.1971), zoom = 15, maptype = "satellite")
ggmap(map)

For large objects, such as a municipality, the location is best defined as a range in the form of a vector with four elements.

map = get_map(location = c(left = 20.068, bottom = 49.198,right = 20.073,top = 49.203), maptype = "hybrid")
ggmap(map)

The get_map function has many other attributes that we can use to change the appearance of the downloaded primes. For more sophisticated users, we refer to the documentation of this tool:

help(get_map)

Leave a Reply

Your email address will not be published. Required fields are marked *