For the past month I’ve been working on a small side project which I’m pleased to launch for Open Data Day 2021.
I’ve long been a fan of OpenStreetMap. I’ve contributed to the map, coordinated a local crowd-mapping project and used OSM tiles to help build web based maps. But I’ve only done a small amount of work with the actual data. Not much more than running a few Overpass API queries and playing with some of the exports available from Geofabrik.
I recently started exploring the Overpass API again to learn how to write useful queries. I wanted to see if I could craft some queries to help me contribute more effectively. For example by helping me to spot areas that might need updating. Or identify locations where I could add links to Wikidata.
There’s a quite a bit of documentation about the Overpass API and the query language it uses, which is called OverpassQL. But I didn’t find them that accessible. The documentation is more of a reference than a tutorial.
And, while, there’s quite a few example queries to find across the OSM wiki and other websites, there isn’t always a great deal of context to those examples that explain how they work or when you might use them.
So I’ve been working on two things to address what I think is a gap in helping people learn how to get more from the OpenStreetMap API.
The first is a simple tool that will take a collection of Overpass queries and build a set of HTML pages from them. It’s based on a similar tool I built for SPARQL queries a few years ago. Both are inspired by Javadoc and other code documentation tools.
The idea was to encourage the publication of collections of useful, documented queries. E.g. to be shared amongst members of a community or people working on a project. The OSM wiki can be used to share queries, but it might not always be a suitable home for this type of content.
The tool is still at quite an early stage. It’s buggy, but functional.
To test it out I’ve been working on my own collection of Overpass queries. I initially started to pull together some simple examples that illustrated a few features of the language. But then realised that I should just use the tool to write a proper tutorial. So that’s what I’ve been doing for the last week or so.
Announcing OSM Queries
OSM Queries is the result. As of today the website contains four collections of queries. The main collection of queries is a 26 part tutorial that covers the basic features of Overpass QL.
By working through the tutorial you’ll learn:
- some basics of the OpenStreetMap data model
- how to write queries to extract nodes, ways and relations from the OSM database using a variety of different methods
- how to filtering data to extract just the features of interest
- how to write spatial queries to find features based on whether they are within specific areas or are within proximity to one another
- how to output data as CSV and JSON for use in other tools
Every query in the tutorial has its own page containing an embedded syntax highlighted version of the query. This makes them easier to share with others. You can click a button to load and run the query using the Overpass Turbo IDE. So you can easily view the results and tinker with the query.
I think the tutorial covers all the basic options for querying and filtering data. Many of the queries include comments that illustrate variations of the syntax, encouraging you to further explore the language.
I’ve also been compiling an Overpass QL syntax reference that provides a more concise view of some of the information in the OSM wiki. There’s a lot of advanced features (like this) which I will likely cover in a separate tutorial.
Writing a tutorial against the live OpenStreetMap database is tricky. The results can change at any time. So I opted to focus on demonstrating the functionality using mostly natural features and administrative boundaries.
In the end I chose to focus on an area around Uluru in Australia. Not just because it provides an interesting and stable backdrop for the tutorial. But because I also wanted to encourage a tiny bit of reflection in the reader about what gets mapped, who does the mapping, and how things get tagged.
A bit of map art, and a request
The three other query collections are quite small:
- a set of miscellaneous queries that are based on my own experiments
- a small set of queries which might be useful for local mappers
- and a little showcase of how to style query results using MapCSS.
I ended up getting a bit creative with the MapCSS queries.
For example, to show off the functionality I’ve written a query that shows the masonic symbol hidden in the streets of Bath, styled Brøndby Haveby like a bunch of flowers and the Lotus Bahai Temple as, well, a lotus flower.
These were all done by styling the existing OSM data. No edits were done to change the map. I wouldn’t encourage you to do that.
I’ve put all the source files and content for the website into the public domain so you’re free to adapt, use and share however you see fit.
While I’ll continue to improve the tutorial and add some more examples I’m also hoping that I can encourage others to contribute to the site. If you have useful queries that you could be added to the site then submit them via Github. I’ve provided a simple issue template to help you do that.
I’m hoping this provides a useful resource for people in the OSM community and that we can collectively improve it over time. I’ve love to get some feedback, so feel free to drop me an email, comment on this post or message me on twitter.
And if you’ve never explored the data behind OpenStreetMap then Open Data Day is a great time to dive in. Enjoy.
7 thoughts on “OSM Queries”
I’ll have a look to see if it has answers to my questions.
A fairly minor point, but I would avoid a file extension of “.osm” as this is widely used for XML data in the OSM schema. I don’t know of any convention for overpass-turbo queries, although I suspect some people will already have personal ones.
Thanks, its easy enough to change, so I will do that.
I had looked for conventions for queries but couldn’t find any. I forgot to look wider for other file conventions. You’re right that this is confusing.
The Overpass API code has a couple of “osm3s” files which are queries, but there they are using the XML syntax. Do you think that might be a better option?
x`I’m not really sure, but would opt for something along the lines of “.oql” or “.otql” for Overpass Query Language with the additional “t” for turbo as there are client-side aspects of overpass-turbo (area assembly & MapCSS which dont apply to raw overpass ql).
Good suggestions, thanks. I think I’ll go with OQL for now.
Comments are closed.