DEV Community

Cover image for How To Get OpenStreetMap (OSM) Data For Your App
Geoapify
Geoapify

Posted on

How To Get OpenStreetMap (OSM) Data For Your App

OpenStreetMap is an editable global map. It means that on the map, you can add new places or fix mistakes, or even you can make any changes to it. OpenStreetMap (OSM) is a successful open data project that is driven by a global mapper community that aspires to make all geographic data openly available to everyone. With millions of registered members, it is a community-driven effort.

The OSM has become a popular resource for mapping data for developers for their apps and websites. Anyone can maintain all the data about roads, places, buildings, railway tracks, etc. However, converting OpenStreetMap data into data objects and using them in your project requires skill and specific understanding.

How can you access or download data from OpenStreetMap (OSM)?

There are a number of ways to download OSM data, including utilizing a command-line tool like Curl or Wget to export the data from the OSM website and third-party servers such as Planet OSM or Geofabrik. We also introduce you to Geoapify Places API, which allows you to query pre-formatted OSM data by categories. Let's take a closer look, shall we?

However, before learning about downloading data from OSM and querying, let us look at the OpenStreetMap data structure. This knowledge will help you to understand the query samples.

OSM Objects and Their Properties: nodes, ways, relations, and tags

In the OSM database, all the objects have an OSM ID and an OSM type. Nodes, Ways, and Relations are the three types of the OSM database objects.
Each object may also contain Tags. These tags specify the object's attributes and properties, such as the address, opening hours, amenity type, or other data.

Nodes

Nodes are items that specify the position, such as a building number, postbox, public transport stops, and so on are all examples of nodes.
Nodes are specific positions that can exist independently or as Relation members. A subway station, for example, can include a subway entrance.

Subway entrance, 57th Street, New York

Ways

To represent streets, rivers, buildings, roads, or a polygon is used that is a way. Ways, like Nodes, can exist independently or as part of a Relation. Streets and rivers are frequently represented by a set of the Ways, allowing each piece to have its own set of tags, such as speed restrictions, number of lanes, surface, and so on.

Railway platform, 57th Street, New York

Relations

Relations are combinations of nodes, paths, and maybe other relations. Relations define a world structure. For example, the relation Stop Area describes the structure of the station - platforms, underground station entrances, elevators.

Subway station, 57th Street, New York

Tags

Tags describe the characteristics and properties of all the objects in the OSM database.
An example of how to tag a subway station is as follows:

Tag Meaning
name=57th Street (F,) Primary name of the OSM object
network=NYC Subway Indicates the system the OSM object belongs to
public_transport=stop_area Type of object related to public transport
subway=yes Indicates that the object belongs to subway
type=public_transport Indicates that object belongs to the public transport

The OpenStreetMap (OSM) community has worked hard to ensure that the tags are balanced and understandable. The documentation explains not only what each Tag does, but also why it does it, so you can understand what you are looking at and have some context for your position or route while looking at a map on the web or using an app created with OSM data.

Now, let's take a look at how we can leverage data from OpenStreetMap to create new applications:

Option 1: Get A Snapshot Of OSM Data.

The following are some of the most popular methods for obtaining OSM original data:

Planet OSM provides you with weekly OSM data snapshots. You can find all of the objects in OSM without any hurdles or any delay.
There is much reliable third-party software to convert the data to different formats and databases. For example, with Osm2pgsql software, you can transfer all the data into a Postgis database. You can work with the data by using SQL queries once you get it from the database.

Here is an example of a query that lets you find subway stations (nodes only) in a particular area:

with filterGeom as (select ST_Transform(ST_SetSRID(st_geomfromgeojson('{"type":"Polygon","coordinates":[[[-74.0091, 40.7747], [-73.9417, 40.7747], [-73.9417, 40.7455], [-74.0091, 40.7455], [-74.0091, 40.7747]]]}'), 4326), 3857) as w)
(select 
pop.osm_id,
pop.tags,
ST_AsGeoJSON(st_transform(way, 4326))::jsonb as geojson
from planet_osm_point pop, filterGeom
where 
((railway = 'station' and tags ? 'subway' and tags->'subway' = 'yes') 
    or (tags ? 'public_transport' and tags->'public_transport' = 'station' and tags ? 'subway' and tags->'subway' = 'yes') 
    or (tags ? 'public_transport' and tags->'public_transport' = 'station' and tags ? 'station' and tags->'station' = 'subway') ) 
and ST_Intersects(way, filtergeom.w))
Enter fullscreen mode Exit fullscreen mode

Geofabrik is a website that provides daily OSM data downloading. Geofabrik website provides you API data Openstreetmap in PBF format per continent. The website shares data without violating data protection laws. You will not receive metadata such as personal information, user IDs, etc.

With the BBBike website, you can download OSM data of cities and regions. The size of the data is limited to only 2 to 50 Mbs. You can choose many different data formats to download the data, such as PBF, XML, shape files, and vector map tiles.

Option 2: Use The Overpass API In Place Of The OSM API.

You can query OpenStreetMap data based on specific criteria with the help of the Overpass API. It is designed for reading data, and you may build queries with Overpass QL (or Overpass XML as an alternative). On the Overpass QL documentation page, you can discover query examples. With the Overpass Turbo interactive tool, you can perform short queries and test your requests. You can use the Overpass Turbo interactive to test your requests on a live map. You can search for locations, use tags, change things, or only play with the map. Thanks to the simple and easy interface.

Here's an Overpass query that returns subway stations similar to the one above:

(
  node[railway=station][subway=yes]( 40.7455, -74.0091, 40.7747, -73.9417);
  node[public_transport=station][subway=yes]( 40.7455, -74.0091, 40.7747, -73.9417);
  node[public_transport=station][station=subway]( 40.7455, -74.0091, 40.7747, -73.9417);
);
out;
Enter fullscreen mode Exit fullscreen mode

It is worth noting that you can query data from multiple tables in a single request. Use node, rel, and ways to get data from a single table. Use nw, nwr, wr, or nr to query data from many tables quickly. We have a list of open Overpass API instances that you can use for free. They do, however, have usage restrictions, so you can not use them in your apps for production.

Want a commercial version of the Overpass API? Companies like Geoapify offer the Overpass API at affordable rates. Contact us if you want more information.

Option 3: Places API As The Simplest Method For Obtaining OSM Data

It is simple integrating OpenStreetMap (OSM) data into your project with Places API that returns places by categories for the selected area. For example, if you want information on restaurants in a specific area, you need to run an HTTP request specifying the kind of establishment you want and an area where you need to search.

The Places API provides processed and validated data, so you will not have to deal with any odd cases or errors that open data might have. Accommodations, advertising, restaurants & cafés, leisure, entertainment, tourism, and more categories, are all supported by the Geoapify Places API.

You can get Places data by requesting with an HTTP get. Here is an example of a URL that returns subway stations in New York:

https://api.geoapify.com/v2/places?categories=public_transport.subway&filter=rect:-74.0091,40.7455,-73.9417,40.7747&limit=100&apiKey=YOUR_API_KEY
Enter fullscreen mode Exit fullscreen mode

The API returns a GeoJSON object. This can be easily visualized by map libraries, including Leaflet, MapLibre GL, and others. Here is JSFiddle that demonstrates getting subway stations data and adding them to a MapLibre GL map:

Conclusion

We can safely conclude that OSM is a game-changer. If you want to create open-source applications for various platforms, OSM is a good place to start. The data is open source, and quick updates and plenty of software to get you started. You now have at least three options for integrating OSM data into your project. The software you use is determined by the type of data you are looking for and your skills.

Top comments (0)