r.klingeresri-de-esridist

OSMQuery or how to query OpenStreetMap spatially

Blog Post created by r.klingeresri-de-esridist Employee on Aug 29, 2018

There are multiple ways how you might use OpenStreetMap. The most commonly used might be the OSM dataset as a basemap. Some others might already used the ArcGIS Editor for OSM. But I haven't found a soultion to query OSM directly from within ArcGIS. The question to answer was: "Where are bakeries in Northumberland?" The Overpass API offers a great  query tool to define queries and get the result on a map. I wanted to have a comparable but easy to use tool in ArcGIS so I created OSMQuery. You can use OSMQuery for free.

The Inputs of OSMQuery

OSMQuery takes only a simple set of inputs at the moment:

- a tag like "building", "shop" or "amenity"

- a key like "farm", "bakery" or "place_of_worship"

- an area which will be used as a "related" feature

- or a bounding box / spatial extent

The tool uses the name of the region and gets an area ID from the Nominatim Geocoder. This area ID or the bounding box of the spatial extent will be used to create a query for the Overpass API.

OSMQuery in ArcMAP 10.6

(the above image is already outdated after one day. See the end of this post!)

The query is send to the Overpass API which responds with a JSON object. The JSON object contains elements like nodes, ways and relations.

The Logic of OSMQuery

The processing logic is splitted in two parts. First I needed to determine whether points, line and/or polygon feature classes will be needed to store the features. This is a bit tricky as polygons are also "ways" in terms of OpenStreetMap logic. The response of a way not only contains the tags of a way but also the id's of nodes defining the way geometry. A polygon can be created if the first nodeID in the list of nodes is also the last nodeID for the way:

As you can see, the result way has 71 nodes and the response looks like this:

{
  "version": 0.6,
  "generator": "Overpass API 0.7.55.4 3079d8ea",
  "osm3s": {
    "timestamp_osm_base": "2018-08-29T08:10:02Z",
    "copyright": "The data included in this document is from www.openstreetmap.org. The data is made available under ODbL."
  },
  "elements": [

{
  "type": "node",
  "id": 2496059820,
  "lat": 51.0703208,
  "lon": 4.9863343
},
{
  "type": "node",
  "id": 2496059823,
  "lat": 51.0703401,
  "lon": 4.9865653
},
...
{
  "type": "node",
  "id": 2496059818,
  "lat": 51.0702779,
  "lon": 4.9863413
},
{
  "type": "way",
  "id": 242035363,
  "nodes": [
    2496059820,
    2496059823,
...
    2496059818,
    2496059820
  ],
  "tags": {
    "OnroerendErfgoed:criteria": "M",
    "addr:city": "Veerle",
    "amenity": "place_of_worship",
    "building": "house",
    "denomination": "roman_catholic",
    "description": "Parochiekerk",
    "heritage": "4",
    "heritage:operator": "OnroerendErfgoed",
    "heritage:website": "https://inventaris.onroerenderfgoed.be/dibe/relict/41142",
    "image": "https://commons.wikimedia.org/wiki/File:Veerle_-_Onze-Lieve-Vrouw-in-de-Wijngaardkerk.jpg",
    "name": "Onze-Lieve-Vrouw-in-de-Wijngaardkerk",
    "ref:OnroerendErfgoed": "41142",
    "religion": "christian",
    "source": "AGIV",
    "wikimedia_commons": "Category:OLV in de Wijngaardkerk (Veerle)"
  }
}
  ]
}

Furthermore the result has not a defined datamodel. Each node may come with a very different set of tags and keys. So I needed to create a union of tags for each feature class type. The adding fields algorithm is the slowest part in my toolbox as the number of tgas can be as high as 40...

After I created the feature classes and added the fields accrodiung the list of attributes/tags I can add the features from the Overpass repsonse.

This is quite easy in the end but it is a pain for the polylines and polygons as I needed to read the lat/lon attributes for each node recursively.

But in the end: There are 33 bakeries in Northumberland, UK according to the OpenStreetMap dataset:

result for bakeries in Northumberland

Here is the compared dataset in the Overpass API:

result for bakeries in Northumberland using the Overpass API

If you want to test/download/develop the toolbo, go ahead and use the OSMQuery repo at GitHub.

Here is a short video about the usage:

Thanks to the support of GitHub user rastrau OSMQuery also supports multiple queries now:

Outcomes