mapsapi
The mapsapi
package provides an interface to the Google Maps APIs, currently three of them -
Functions mp_directions
, mp_matrix
and mp_geocode
are used to access the APIs. They return an xml_document
object (package xml2
) with the response contents.
Given a directions response, functions mp_get_routes
and mp_get_segments
can be used to process the response document into a spatial layer. Function mp_get_routes
gives each alternative as a separate line, while function mp_get_segments
gives each segment (that is, a portion of the route associated with specific driving instructions) as a separate line.
Given a distance matrix response, function mp_get_matrix
can be used to obtain distance/duration matrices.
Given a geocode response, functions mp_get_points
and mp_get_bounds
can be used to obtain geocoded locations as a point or polygon (bounds) layer.
The CRAN version can be installed with -
The development version can be installed using devtools
-
And loaded with library
-
The following expression queries the Directions API for driving directions from Tel-Aviv and Haifa. Note that locations can be specified as a coordinate pair, a textual address or an sf
spatial object.
Or using the sample response data included in the packages -
Given the response object, we can use mp_get_routes
to create a spatial layer of route lines -
Here is the resulting object -
## Simple feature collection with 2 features and 8 fields
## geometry type: LINESTRING
## dimension: XY
## bbox: xmin: 34.76389 ymin: 31.88796 xmax: 35.10844 ymax: 32.7944
## epsg (SRID): 4326
## proj4string: +proj=longlat +datum=WGS84 +no_defs
## alternative_id summary distance_m distance_text
## 1 1 Yitzhak Rabin Hwy/Route 6 125960 126 km
## 2 2 Hwy 2/Kvish HaHof 121014 121 km
## duration_s duration_text duration_in_traffic_s duration_in_traffic_text
## 1 5382 1 hour 30 mins NA NA
## 2 5484 1 hour 31 mins NA NA
## geometry
## 1 LINESTRING (34.81144 31.892...
## 2 LINESTRING (34.81144 31.892...
And a visualization using leaflet
-
library(leaflet)
pal = colorFactor(palette = "Dark2", domain = r$alternative_id)
leaflet() %>%
addProviderTiles("CartoDB.DarkMatter") %>%
addPolylines(data = r, opacity = 1, weight = 7, color = ~pal(alternative_id))
Separate segments can be extracted from the same response using mp_get_segments
-
Here are the first six features of the resulting object -
## Simple feature collection with 6 features and 10 fields
## geometry type: LINESTRING
## dimension: XY
## bbox: xmin: 34.80859 ymin: 31.88963 xmax: 34.82019 ymax: 31.89275
## epsg (SRID): 4326
## proj4string: +proj=longlat +datum=WGS84 +no_defs
## alternative_id leg_id segment_id summary
## 1-1-1 1 1 1 Yitzhak Rabin Hwy/Route 6
## 1-1-2 1 1 2 Yitzhak Rabin Hwy/Route 6
## 1-1-3 1 1 3 Yitzhak Rabin Hwy/Route 6
## 1-1-4 1 1 4 Yitzhak Rabin Hwy/Route 6
## 1-1-5 1 1 5 Yitzhak Rabin Hwy/Route 6
## 1-1-6 1 1 6 Yitzhak Rabin Hwy/Route 6
## travel_mode
## 1-1-1 driving
## 1-1-2 driving
## 1-1-3 driving
## 1-1-4 driving
## 1-1-5 driving
## 1-1-6 driving
## instructions
## 1-1-1 Head <b>southwest</b> on <b>Bnei Moshe St</b> toward <b>Negba St</b>
## 1-1-2 Turn <b>left</b> onto <b>Negba St</b>
## 1-1-3 Turn <b>left</b> onto <b>Rachel Hirshenzon St</b>
## 1-1-4 Continue onto <b>Ezra St</b>
## 1-1-5 Continue onto <b>Ha-Hagana St</b>
## 1-1-6 Slight <b>right</b> toward <b>Ha-Hagana St</b>
## distance_m distance_text duration_s duration_text
## 1-1-1 322 0.3 km 70 1 min
## 1-1-2 180 0.2 km 42 1 min
## 1-1-3 325 0.3 km 82 1 min
## 1-1-4 512 0.5 km 126 2 mins
## 1-1-5 214 0.2 km 54 1 min
## 1-1-6 18 18 m 4 1 min
## geometry
## 1-1-1 LINESTRING (34.81144 31.892...
## 1-1-2 LINESTRING (34.80859 31.890...
## 1-1-3 LINESTRING (34.80968 31.889...
## 1-1-4 LINESTRING (34.81256 31.891...
## 1-1-5 LINESTRING (34.81783 31.892...
## 1-1-6 LINESTRING (34.82004 31.892...
And a visualization -
The following expression queries the Distance Matrix API to obtain a matrix of driving distance and duration between all combinations of three locations: Tel-Aviv, Jerusalem and Beer-Sheva.
Or using the sample response data included in the packages -
The mp_get_matrix
function can then be used to process the XML reposne into a matrix
. Possible values of the matrix include -
distance_m
- Distance, in metersdistance_text
- Distance, textual descriptionduration_s
- Duration, in secondsduration_text
- Duration, textual description## Tel-Aviv Jerusalem Beer-Sheva
## Tel-Aviv 0 66532 111641
## Jerusalem 67009 0 120608
## Beer-Sheva 109723 106299 0
The following expression queries the Directions API for geocoding a single address.
Or using the sample response data included in the packages -
Given the response object, we can use mp_get_points
to create a spatial layer of geocoded point locations -
## Simple feature collection with 1 feature and 4 fields
## geometry type: POINT
## dimension: XY
## bbox: xmin: 34.78177 ymin: 32.0853 xmax: 34.78177 ymax: 32.0853
## epsg (SRID): 4326
## proj4string: +proj=longlat +datum=WGS84 +no_defs
## id status address address_google pnt
## 1 1 OK Tel-Aviv Tel Aviv-Yafo, Israel POINT (34.78177 32.0853)
Here is a visualization using leaflet
-
Or the bounds -
## Simple feature collection with 1 feature and 3 fields
## geometry type: POLYGON
## dimension: XY
## bbox: xmin: 34.74252 ymin: 32.02925 xmax: 34.85198 ymax: 32.14661
## epsg (SRID): 4326
## proj4string: +proj=longlat +datum=WGS84 +no_defs
## status address address_google geometry
## 1 OK Tel-Aviv Tel Aviv-Yafo, Israel POLYGON ((34.74252 32.02925...
And a visualization using leaflet
-