Polygons.Rmd
Isochrone polygons are a great way of visualising complex travel-time datasets in a powerful way - what does “half an hour away” really look like? With TargomoR
you can add them straight into a leaflet
map, or get the data in a convenient form for analysis such as testing intersections with known boundaries (i.e. is any part of this region within half an hour of that point?).
For the isochrone polygon service, getTargomoPolygons()
returns a Simple Feature Collection of 2 fields. The fields are:
Each feature is a POLYGON (or MULTIPOLYGON). There is one feature per time specified in the travelTimes
argument of targomoOptions()
- see the vignette on Options for more information.
# set some source data
source <- data.frame(id = "Big Ben", lat = 51.5007, lng = -0.1246)
# query the API
polygons <- getTargomoPolygons(source_data = source,
source_lat = ~lat, source_lng = ~lng,
options = targomoOptions(travelType = "bike"))
# inspect the output
polygons
#> Simple feature collection with 3 features and 2 fields
#> geometry type: MULTIPOLYGON
#> dimension: XY
#> bbox: xmin: -0.2218 ymin: 51.4434 xmax: -0.0317 ymax: 51.5577
#> epsg (SRID): 4326
#> proj4string: +proj=longlat +datum=WGS84 +no_defs
#> time area geometry
#> 2 1800 116951364 MULTIPOLYGON (((-0.1712 51....
#> 1 1200 46732859 MULTIPOLYGON (((-0.1483 51....
#> 3 600 10007331 MULTIPOLYGON (((-0.1285 51....
If we want to integrate this into a leaflet map, you can either:
drawTargomoPolygons()
, or;addTargomoPolygons()
, or;leaflet::addPolygons
The second option looks like:
# set some source data
source <- data.frame(id = "Big Ben", lat = 51.5007, lng = -0.1246)
# combine the API call and drawing using `addTargomoPolygons`
leaflet() %>%
addTargomoTiles() %>%
addTargomoPolygons(source_data = source,
options = targomoOptions(travelType = "bike"))
#> Assuming "lng" and "lat" are longitude and latitude, respectively
Using addTargomoPolygons()
you can customise the way the polygons appear on the map with a call to polygonDrawOptions()
. The options available are:
Option | Default | Meaning |
---|---|---|
stroke | TRUE |
Should the polygons have a border? |
weight | 5 |
The border stroke-weight |
color | list("red", "orange", "green") |
The border color |
opacity | 0.5 |
The border opacity |
fill | TRUE |
Should the interior of the polygons be filled? |
fillColor | color |
The interior fill color (defaults to border color) |
fillOpacity | 0.2 |
The fill opacity |
dashArray | NULL |
The border dash pattern |
smoothFactor | 1 |
How much to simplify the polylines |
noClip | FALSE |
Disable polyline clipping? |
For example, if we wish to re-create the map above, but with opaque polygons in shades of blue with thin black outlines, we can do the following (this time using drawTargomoPolygons()
):