Creates geographical extracts from an OSM data file or an OSM history file. The geographical extent can be given either as a bounding box or as a (multi)polygon.

extract(
  input_path,
  extent,
  output_path,
  strategy = c("complete_ways", "smart", "simple"),
  overwrite = FALSE,
  echo_cmd = FALSE,
  echo = TRUE,
  spinner = TRUE,
  verbose = FALSE
)

Arguments

input_path

A string. The path to the OSM data/history file whose extent should be extracted from. Please see file_formats for a list of supported file formats.

extent

Either a POLYGON or a MULTIPOLYGON sf object with only one feature or a bbox object, created by sf::st_bbox.

output_path

A string. The path to the file where the output should be written to. Please see file_formats for a list of supported file formats.

strategy

A string. The strategy to use when creating the extract. Available strategies are "complete_ways", "smart" and "simple". Defaults to "complete_ways". Please see the "Strategies" section for a description of each one of them.

overwrite

A logical. Whether existing files should be overwritten by the output. Defaults to FALSE.

echo_cmd

A logical. Whether to print the Osmium command generated by the function call to the screen. Defaults to FALSE.

echo

A logical. Whether to print the standard output and error generated by the Osmium call to the screen. Defaults to TRUE.

spinner

A logical. Whether to show a reassuring spinner while the Osmium call is being executed. Defaults to TRUE.

verbose

A logical. Whether to display detailed information on the running command. Defaults to FALSE.

Value

The normalized path to the output file.

Strategies

Different strategies can be used when creating extracts. Depending on the strategy, different objects will end up in the extracts. The strategies differ in how much memory they need and how often they need to read the input file. The choice of strategy depends on how you want to use the generated extracts and how much memory and time you have.

  • "simple" - runs in a single pass. The extract will contain all nodes inside the region and all ways referencing those nodes, as well as all relations referencing any nodes or ways already included. Ways crossing the region boundary will not be reference-complete. Relations will not be reference-complete. This strategy is fast, because it reads the input only once, but the result is not enough for most use cases. This strategy will not work for history files.

  • "complete_ways" - runs in two passes. The extract will contain all nodes inside the region and all ways referencing those nodes, as well as all nodes referenced by those ways. The extract will also contain all relations referenced by nodes inside the region or ways already included and, recursively, their parent relations. The ways are reference-complete, but the relations are not.

  • "smart" - runs in three passes. The extract will contain all nodes inside the region and all ways referencing those nodes, as well as all nodes referenced by those ways. The extract will also contain all relations referenced by nodes inside the region or ways already included and, recursively, their parent relations. The extract will also contain all nodes and ways (and the nodes they reference) referenced by relations tagged "type=multipolygon" directly referencing any nodes in the region or ways referencing nodes in the region. The ways are reference-complete, and all multipolygon relations referencing nodes in the regions or ways that have nodes in the region are reference-complete. Other relations are not reference-complete.

Examples

pbf_path <- system.file("extdata/cur.osm.pbf", package = "rosmium")

file.size(pbf_path)
#> [1] 525406

# buffering the pbf bounding box 4000 meters inward and using the result
# extent to extract the osm data inside it. transforming the crs because
# inward buffers only work with projected crs

lines <- sf::st_read(pbf_path, layer = "lines", quiet = TRUE)
bbox <- sf::st_bbox(lines)
bbox_polygon <- sf::st_as_sf(sf::st_as_sfc(bbox))
smaller_bbox_poly <- sf::st_buffer(
  sf::st_transform(bbox_polygon, 5880),
  -4000
)
smaller_bbox_poly <- sf::st_transform(smaller_bbox_poly, 4326)

output_path <- extract(
  pbf_path,
  smaller_bbox_poly,
  tempfile(fileext = ".osm.pbf")
)

file.size(output_path)
#> [1] 42503