Get objects matching at least one of the specified expressions from the input and write them to the output. All objects matching the expressions will be kept in the output, and objects referenced by them will also be added to the output (unless omit_referenced = TRUE). Objects will be written out in the order they are found in the input. Please note that the function will only work correctly on history files if omit_referenced is TRUE, and it cannot be used on change files.

tags_filter(
  input_path,
  filters,
  output_path,
  invert_match = FALSE,
  omit_referenced = FALSE,
  remove_tags = FALSE,
  overwrite = FALSE,
  echo_cmd = FALSE,
  echo = TRUE,
  spinner = TRUE,
  verbose = FALSE,
  progress = FALSE
)

Arguments

input_path

A string. The path to the OSM data/history file to which the filters should be applied. Please see file_formats for a list of supported file formats.

filters

A string. The filter expressions that should be applied to the input file. Please see the "Filter expressions" section for a description of the filter expression format.

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.

invert_match

A logical. Whether to invert the sense of matching - i.e. to exclude objects with matching tags. Defaults to FALSE.

omit_referenced

A logical. Whether to omit the nodes referenced from matching ways and members referenced from matching relations. Defaults to FALSE.

remove_tags

A logical. Whether to remove tags from objects that are not matching the filter expression but are included to complete references (nodes in ways and members of relations). Defaults to FALSE. Please note that if an object is both matching the filter and used as a reference it will keep its tags.

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.

progress

A logical. Whether to display a progress bar while running the command. Defaults to FALSE.

Value

The normalized path to the output file.

Filter expressions

A filter expression specifies a tag or tags that should be found in the data and the type of object (node, way or relation) that should be matched.

The object type(s) comes first, then a slash (/) and then the rest of the expression. Object types are specified as 'n' (for nodes), 'w' (for ways), 'r' (for relations) and 'a' (for areas - closed ways with 4 or more nodes and relations with type=multipolygon or type=boundary tag). Any combination of them can be used. If the object type is not specified, the expression matches all object types.

Some examples:

  • n/amenity - matches all nodes with the key "amenity".

  • nw/highway - matches all nodes or ways with the key "highway".

  • /note - matches objects of any type with the key "note".

  • note - matches objects of any type with the key "note".

  • w/highway=primary - matches all ways with the key "highway" and value "primary".

  • w/highway!=primary - matches all ways with the key "highway" and a value other than "primary".

  • r/type=multipolygon,boundary - matches all relations with key "type" and value "multipolygon" or "boundary".

  • w/name,name:de=Kastanienallee,Kastanienstrasse - matches any way with a "name" or "name:de" tag with the value "Kastanienallee" or "Kastanienstrasse".

  • n/addr:* - matches all nodes with any key starting with "addr:"

  • n/name=*Paris - matches all nodes with a name that contains the word "Paris".

  • a/building - matches any closed ways with 4 or more nodes or relations tagged "building". Relations must also have a tag "type=multipolygon" or "type=boundary".

If there is no equal sign ("=") in the expression, only keys are matched and values can be anything. If there is an equal sign ("=") in the expression, the key is to the left and the value to the right. An exclamation sign ("!") before the equal sign means: a tag with that key, but not the value(s) to the right of the equal sign. A leading or trailing asterisk ("*") can be used for substring or prefix matching, respectively. Commas (",") can be used to separate several keys or values.

All filter expressions are case-sensitive. There is no way to escape the special characters such as "=", "" and ",". You can not mix comma-expressions and ""-expressions.

The specified filter expressions are matched in the order they are given. To achieve best performance, put expressions expected to match more often first.

Area matches (with leading "a/") do not check whether the matched object is a valid (multi)polygon, they only check whether an object might possibly be turned into a (multi)polygon. This is the case for all closed ways (where the first and last node are the same) with 4 or more nodes and for all relations that have an additional "type=multipolygon" or "type=boundary" tag.

Examples

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

# get all amenity nodes
output <- tags_filter(pbf_path, "n/amenity", tempfile(fileext = ".osm.pbf"))
nodes <- sf::st_read(output, layer = "points", quiet = TRUE)
head(nodes$other_tags)
#> [1] "\"amenity\"=>\"university\",\"wikidata\"=>\"Q10278991\""                                                                  
#> [2] "\"amenity\"=>\"telephone\",\"operator\"=>\"OI\",\"payment:telephone_cards\"=>\"yes\""                                     
#> [3] "\"amenity\"=>\"kindergarten\""                                                                                            
#> [4] "\"addr:housenumber\"=>\"45\",\"addr:street\"=>\"Rua Amintas de Barros\",\"amenity\"=>\"restaurant\",\"cuisine\"=>\"arab\""
#> [5] "\"addr:city\"=>\"Curitiba\",\"addr:housenumber\"=>\"1250\",\"addr:street\"=>\"Rua 7 de Abril\",\"amenity\"=>\"pub\""      
#> [6] "\"amenity\"=>\"taxi\""                                                                                                    

# get all objects (nodes, ways or relations) with an addr:* tag
output <- tags_filter(
  pbf_path,
  "addr:*",
  tempfile(fileext = ".osm.pbf"),
  omit_referenced = TRUE
)
nodes <- sf::st_read(output, layer = "points", quiet = TRUE)
head(nodes$other_tags)
#> [1] "\"addr:city\"=>\"Curitiba\",\"addr:housenumber\"=>\"358\",\"addr:postcode\"=>\"80510-190\",\"addr:street\"=>\"Rua Mateus Leme\",\"addr:suburb\"=>\"Centro Cívico\",\"brand\"=>\"Ibis\",\"brand:wikidata\"=>\"Q920166\",\"brand:wikipedia\"=>\"en:Ibis (hotel)\",\"tourism\"=>\"hotel\""
#> [2] "\"addr:city\"=>\"Curitiba\",\"addr:country\"=>\"BR\",\"addr:housenumber\"=>\"1975\",\"addr:street\"=>\"Avenida Marechal Deodoro\""                                                                                                                                                     
#> [3] "\"addr:city\"=>\"Curitiba\",\"addr:country\"=>\"BR\",\"addr:housenumber\"=>\"2085\",\"addr:street\"=>\"Avenida Marechal Deodoro\""                                                                                                                                                     
#> [4] "\"addr:city\"=>\"Curitiba\",\"addr:country\"=>\"BR\",\"addr:housenumber\"=>\"1976\",\"addr:street\"=>\"Avenida Marechal Deodoro\""                                                                                                                                                     
#> [5] "\"addr:city\"=>\"Curitiba\",\"addr:country\"=>\"BR\",\"addr:housenumber\"=>\"2086\",\"addr:street\"=>\"Avenida Marechal Deodoro\""                                                                                                                                                     
#> [6] "\"addr:city\"=>\"Curitiba\",\"addr:country\"=>\"BR\",\"addr:housenumber\"=>\"965\",\"addr:street\"=>\"Rua José de Alencar\""                                                                                                                                                           

# get all nodes and ways with a highway tag and all relations tagged with
# type=restriction plus all referenced objects
output <- tags_filter(
  pbf_path,
  "nw/highway r/type=restriction",
  tempfile(fileext = ".osm.pbf")
)
ways <- sf::st_read(output, layer = "lines", quiet = TRUE)
head(ways$highway)
#> [1] "service"  "service"  "service"  "service"  "tertiary" "tertiary"
relations <- sf::st_read(output, layer = "other_relations", quiet = TRUE)
head(relations$other_tags)
#> [1] "\"restriction\"=>\"no_right_turn\"" "\"restriction\"=>\"no_left_turn\"" 
#> [3] "\"restriction\"=>\"no_right_turn\"" "\"restriction\"=>\"no_right_turn\""
#> [5] "\"restriction\"=>\"no_right_turn\"" "\"restriction\"=>\"no_u_turn\""