3 Ways to get OpenStreetMap(OSM) Data

Using OpenStreetMap data as database
Using OpenStreetMap data as database

OpenStreetMap is a map of the world that you can edit. This means you can correct errors, add new places, and even contribute to the map yourself! It's a community-driven project with millions of registered users. It is a community-driven project that aims to make all geographical data available to everyone under an open license.

OpenStreetMap data is the most current and detailed spatial database available. As a result, OSM has become a trendy resource for developers looking to use mapping data in their applications and websites. However, it requires experience and specific knowledge to turn OpenStreetMap data into an OSM objects and use it in your project.

In this article, we will go over the basics of getting OpenStreetMap data and how to most conveniently query the data with Geoapify Places API.

OpenStreetMap data structure

Let's look at the OpenStreetMap data structure before we talk about extracting data and querying examples because you will need this knowledge to understand the query samples better.

Each object in the OSM database is assigned a type associated with its table. There are three types of objects: nodes, ways, and relations. Objects for each kind have unique IDs. So each object in OSM has an ID and type. For example, osm_type="way" and osm_id=67104773.

In addition, each object may have tags that describe its properties, such as the address, opening hours, amenity type, color, references, Wikipedia page, or other information.

Nodes

Nodes are objects with a position (coordinates). Nodes can represent a postbox, tree, subway entrance, city center, building number, etc.

OpenStreetMap nodes example

Nodes can be standalone or part of a relation. For example, a subway entrance can be a part of a subway station.

Ways

A way is a line or polygon, for example, for streets, rivers, buildings, or boundaries.

OpenStreetMap way example

Similar to nodes, ways can be standalone or part of a relation. Often, streets and rivers are represented by a set of ways so that each piece can have its own tags, like speed limits, number of lanes, surface, etc.

Relations

Relations are groups of nodes, ways and possibly other relations that describe some structure in the world (e.g., the relation "cycleway" describes the structure of a bicycle path alongside a road).

OpenStreetMap relation example

Relations can have tags too – these are usually called "tags on relation" because they're applied directly to the relation instead of individual members like node or way tags.

Tags

Every object in the OSM database may have a set of tags that describe the object's features and properties. The following is an example of how to tag a building:

  • building=yes (this tells us that this is a building)

  • name=Old Marylebone Town Hall (this is the name of the building)

  • operator=Westminster City Council (this tells us who operates this building)

  • addr:street=Marylebone Road (this tells us where this building can be found)

The OpenStreetMap (OSM) community has worked hard to create a way to keep the tags balanced and easy to understand. The documentation portal explains the tags - https://taginfo.openstreetmap.org/.

The documentation explains not only what each tag does but also why it does what it does so that when you're looking at a map on the web or using an app built from OSM data, you can understand what you're looking at and have some context for your location or route.

The OSM community has also created a list of guidelines for tagging roads and other features on the map. These guidelines are called the "style guide" in OSM lingo, and they explain how to tag different types of things so that your data will be consistent with everyone else's data. The style guide is very important because it helps ensure that all maps using OSM data look the same!

Let's take a look at how we can retrieve data from the OSM project and use it to build new applications:

Option 1. Get OSM data snapshot

The original OpenStreetMap files use XML-based formats. The XML format is designed to add new information about nodes, ways and relations, and to keep track of changes made to the map.

The alternative formats that are best for data read are produced by third-party tools and extractors.

Here are some of the most popular ways to get OSM original data:

Through Planet OSM website

Planet OSM offers weekly snapshots of OSM data. It contains all the objects that were in OSM at that time.

You can convert the files to different formats or databases using 3d party tools. For example, Osm2pgsql lets you import the data to the Postgis database.

Once you have the data from the database, you can use SQL queries to work with it. For example, this query extracts all restaurants within a specific view box in Paris, France:

with filterGeom as (select ST_Transform(ST_SetSRID(st_geomfromgeojson('{"type":"Polygon","coordinates":[[[2.2795, 48.8810], [2.4142, 48.8810], [2.4142, 48.8325], [2.2795, 48.8325], [2.2795, 48.8810]]]}'), 4326), 3857) as w)
(select 
pop.osm_id,
pop.tags,
'node' as osm_type,
ST_AsGeoJSON(st_transform(way, 4326))::jsonb as geojson
from planet_osm_point pop, filterGeom
where pop.amenity = 'restaurant' and ST_Intersects(way, filtergeom.w))
union all
(select 
pol.osm_id,
pol.tags,
'polygon' as osm_type,
ST_AsGeoJSON(st_transform(way, 4326))::jsonb as geojson
from planet_osm_polygon pol, filterGeom
where pol.amenity = 'restaurant' and ST_Intersects(way, filtergeom.w))

Note that some restaurants are mapped as nodes (planet_osm_point), and others are ways (planet_osm_polygon). So you have to query from 2 tables.

Through Geofabrik website

Geofabrik offers daily OSM data extracts by continents in PBF format. The data is already cleaned up from metadata - personal data, user ID, changesets. So you can use it with no concerns about data protection regulations.

Through BBBike website

BBBike provides OSM extracts by cities and regions. The extracts are only 2-50 MB large, and you can choose the data format between PBF, XML, shape files, vector map tiles, and others.

ProsCons
  • Flexible and powerful
  • Can be adapted to any needs
  • Can be used only on the server part of applications
  • Requires specific technical skills
  • Need to be updated and maintained

Option 2. Overpass API as the OSM API

Overpass API lets you query OpenStreetMap data by criteria. It's optimized for reading data, and you can use Overpass QL (or Overpass XML as an alternative) to write queries. You can find query examples on the Overpass QL documentation page.

You run small queries and test your requests with Overpass Turbo interactive tool. Overpass Turbo interactive allows you to test your requests on a live map. With the simple and intuitive interface, you can easily search for addresses, use tags, edit objects or just play with the map.

Here is an example of a query that does the same job as the example in the previous section - queries Parisian restaurants:

nw
  [amenity=restaurant]
  (48.8330,2.2792,48.8800,2.4140);
out;

Note that you can query data from a few tables in one request. To get information from one table, use 'node', 'rel' and 'way'. To query data from several tables at once, use 'nw', 'nwr', 'wr', or 'nr'.

There is a list of available open Overpass API instances that can be used for free. However, they have limits on usage, so you cannot use them for production purposes in your applications. If you need a commercial version of Overpass API, please contact us.

However, it's important to note that even if Overpass API is great for small and simple queries, it's not the best choice for more complicated ones.

ProsCons
  • Flexible
  • Perfect for one-time jobs
  • Great documentation and a large number of examples
  • Requires Overpass QL programming language knowledge
  • Strict volume limits on free servers
  • Not suitable for heavy requests

Option 3. Places API as the easiest way to get OSM data with API

Geoapify Places API makes it easy to get OpenStreetMap (OSM) data into your project. For example, if you want information about restaurants in a particular area, all you need to do is specify the category of place you want and the area where to search and run an HTTP request.

Here is a URL to get restaurants in Paris (register and get an API key on MyProjects Geoapify):

https://api.geoapify.com/v2/places?categories=catering.restaurant&filter=rect:2.2792,48.8330,2.4140,48.8800&limit=100&apiKey=YOUR_API_KEY

You don't need to operate with nodes, ways, and relations by using Places API. We've already done it for you! Furthermore, the Places API contains processed and checked data, so you won't have to deal with worn cases or mistakes that you might find in open data.

Here's an example of how to run a query in your JS application:


fetch('https://api.geoapify.com/v2/places?categories=catering.restaurant&filter=rect:2.2792,48.8330,2.4140,48.8800&limit=100&apiKey=YOUR_API_KEY')
.then(resp => resp.json())
.then((places) => {
	console.log(places);
});

Geoapify Places API supports 500+ categories - accommodations, commercials, restaurants & cafes, leisure, entertainment, tourism, etc.

You can try the Places API on the Playground page without needing to register.

ProsCons
  • Easy to use
  • No special knowledge required
  • Can be used to download data
  • Can be used to query data in customer-facing (Front-End) apps
  • Free tier for one-time jobs and small volumes
  • Limited to existing categories, new categories are available on request
  • Requires registration and API key

Conclusion

What we can conclude is that OSM is a game-changer. If you want to develop open source applications for different platforms, OSM is the source where you should start looking. The data is freely available, updates are fast, and there are tons of tools to get started with. Now you know at least three ways to get OSM data into your project. The choice of the tool depends on what kind of data you are looking for and what capabilities you have.

We at Geoapify love OpenStreetMap, and we're happy to help you with it. So contact us, and we'll advise you on what we think is the best solution for your project.