Introduction to package mapsapi

Michael Dorman

2017-09-27

Introduction

The mapsapi package provides an interface to the Google Maps APIs, currently two of them -

Functions google_directions and google_matrix are used to access the API. They return an xml_2_document object (package xml2) with the response contents.

Given a directions response, functions extract_routes and extract_segments can be used to process the response document into a spatial layer. Function extract_routes gives each alternative as a separate line, while function extract_segments gives each segment (that is, a portion of the route associated with specific driving instructions) as a separate line.

Installation

The package can be installed from GitHub -

install.packages("devtools")
devtools::install_github("michaeldorman/mapsapi")

And loaded with library -

library(mapsapi)

Directions

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.

doc = google_directions(
  origin = c(34.81127, 31.89277),
  destination = "Haifa",
  alternatives = TRUE
)

Or using the sample response data included in the packages -

library(xml2)
doc = as_xml_document(response_directions)

Given the response object, we can use extract_routes to create a spatial layer of route lines -

r = extract_routes(doc)

Here is the resulting object -

r
## Simple feature collection with 3 features and 4 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 duration_s
## 1              1     Yitzhak Rabin Hwy/Route 6     125960       5382
## 2              2             Hwy 2/Kvish HaHof     121014       5484
## 3              3 Route 4 and Hwy 2/Kvish HaHof     123693       5857
##                         geomerty
## 1 LINESTRING(34.81144 31.8925...
## 2 LINESTRING(34.81144 31.8925...
## 3 LINESTRING(34.81144 31.8925...

And a visualization using leaflet -

library(leaflet)
pal = colorFactor(palette = "Dark2", domain = r$alternative_id)
leaflet() %>% 
  addProviderTiles(provider = providers$Stamen.TonerLite) %>% 
  addPolylines(data = r, opacity = 1, weight = 7, color = ~pal(alternative_id))

Separate segments can be extracted from the same response using extract_segments -

seg = extract_segments(doc)

Here are the first six features of the resulting object -

head(seg)
## Simple feature collection with 6 features and 8 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 segment_id                   summary
## 1              1          1 Yitzhak Rabin Hwy/Route 6
## 2              1          2 Yitzhak Rabin Hwy/Route 6
## 3              1          3 Yitzhak Rabin Hwy/Route 6
## 4              1          4 Yitzhak Rabin Hwy/Route 6
## 5              1          5 Yitzhak Rabin Hwy/Route 6
## 6              1          6 Yitzhak Rabin Hwy/Route 6
##                                                           instructions
## 1 Head <b>southwest</b> on <b>Bnei Moshe St</b> toward <b>Negba St</b>
## 2                                Turn <b>left</b> onto <b>Negba St</b>
## 3                    Turn <b>left</b> onto <b>Rachel Hirshenzon St</b>
## 4                                         Continue onto <b>Ezra St</b>
## 5                                    Continue onto <b>Ha-Hagana St</b>
## 6                       Slight <b>right</b> toward <b>Ha-Hagana St</b>
##   distance_m distance_text duration_s duration_text
## 1        322        0.3 km         70         1 min
## 2        180        0.2 km         42         1 min
## 3        325        0.3 km         82         1 min
## 4        512        0.5 km        126        2 mins
## 5        214        0.2 km         54         1 min
## 6         18          18 m          4         1 min
##                         geomerty
## 1 LINESTRING(34.81144 31.8925...
## 2 LINESTRING(34.80859 31.8909...
## 3 LINESTRING(34.80968 31.8896...
## 4 LINESTRING(34.81256 31.8912...
## 5 LINESTRING(34.81783 31.8922...
## 6 LINESTRING(34.82004 31.8927...

And a visualization -

pal = colorFactor(palette = sample(colors(), length(unique(seg$segment_id))), domain = seg$segment_id)
leaflet(seg) %>% 
  addProviderTiles(provider = providers$Stamen.TonerLite) %>% 
  addPolylines(opacity = 1, weight = 7, color = ~pal(segment_id), popup = ~instructions)

Distance Matrix

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.

locations = c("Tel-Aviv", "Jerusalem", "Beer-Sheva")
doc = google_matrix(
  origins = locations,
  destinations = locations
)

Or using the sample response data included in the packages -

doc = as_xml_document(response_matrix)

The extract_matrix function can then be used to process the XML reposne into a matrix. Possible values of the matrix include -

m = extract_matrix(doc, value = "distance_m")
colnames(m) = locations
rownames(m) = locations
m
##            Tel-Aviv Jerusalem Beer-Sheva
## Tel-Aviv          0     66532     111641
## Jerusalem     67009         0     120608
## Beer-Sheva   109723    106299          0