21 - Type agnostic queries (nwr)

Objects in the OpenStreetMap database can be represented in different ways.

For example a shop might initially be added as a node (a point location) and later changed to be represented by a way. This might happen if a mapper adds an outline for the building in which the business operates.

Sometimes we want to query using node, way or relation because we know what data we want. But how do we find things that might be represented as any of these types?

One approach is to union the results of multiple queries that fetch all the possible variations, e.g:

( node["amenity"]; way["amenity"]; rel["amenity"]; );

But this is repetitive. There are variations of the node, way and relation queries we can use that are more succinct:

  • nw - to find nodes and ways
  • nr - to find nodes and relations
  • wr - to find ways and relations
  • nwr - to find nodes, ways and relations

We can apply filters to these statements just like their single type equivalents.

This is a much more succinct way of finding objects of various types. These are fairly recent additions to the language, so you will still encounter many queries that build up results by querying for each type in turn (see the comments in the example below).

This example extracts all of the nodes, ways and relations in our bounding box.

Source File21-type-agnostic-queries.osm
  • Leigh Dodds