Skip to content

JsLth/tidypass

Repository files navigation

tidypass

Lifecycle: experimental

A tidy R interface to the Postpass service. Allows you to use dplyr syntax to retrieve OpenStreetMap data using {dbplyr}. Postpass is a nice alternative to Overpass Turbo that is faster and more versatile because it uses a Postgres database in the background.

Installation

You can install the development version of tidypass from GitHub with:

# install.packages("pak")
pak::pak("jslth/tidypass")

Example

This is a basic example which extracts all fast food restaurants in Karlsruhe along with their stated cuisine. As you can see, this is mostly written using dplyr. Spatial filters are a bit more complicated but also mostly follow dplyr syntax.

library(tidypass)
library(sf)

# Boundaries of Karlsruhe
bbox <- st_as_sfc(st_bbox(c(
  xmin = 8.34,
  xmax = 8.46,
  ymin = 48.97,
  ymax = 49.03
)))

query <- pp_tbl("point") |>
  filter(amenity == "fast_food" & geom %&&% !!pg_bbox(bbox)) |>
  select(cuisine, name, geom)

Since Postpass works with SQL, the query is translated to Postgres SQL through dbplyr.

query
#> <SQL>
#> SELECT tags ->> 'cuisine' AS `cuisine`, tags ->> 'name' AS `name`, `geom`
#> FROM (
#>   SELECT `postpass_point`.*, tags ->> 'amenity' AS `amenity`
#>   FROM `postpass_point`
#> ) AS `q01`
#> WHERE (`amenity` = 'fast_food' AND `geom` && st_setsrid(st_makebox2d(st_makepoint(8.34, 48.97), st_makepoint(8.46, 49.03)), 4326))

Postpass employs a queue system where you are assigned a different priority depending on the estimated size of your query. If the total amount of scanned data is too high, you are assigned a medium or slow queue, which can take a while. Fast food restaurants in Karlsruhe are very fast.

explain(query)
#> Data scanned: 1.48 megabytes
#> Result size:  96 bytes
#> Queue:        fast queue

Data can be extracted using the collect method. This sends the SQL query to the Postpass server.

fast_food <- collect(query)
plot(bbox)
plot(st_geometry(fast_food), add = TRUE)

About

Use dplyr to query OpenStreetMap

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages