OpenStreetMap to GeoJSON


Posted on 2023-12-19, by Racum. Tags: GIS OpenStreetMap GeoJSON JSON

Use OpenStreetMap as your repository of geometries, and export them in the more convenient GeoJSON format. This technique only works with geometries with area (Polygon and MultiPolygon), points and lines break the exporting tool used here.

Features on OpenStreetMap

OpenStreetMap (OSM for short) has its own file format (OSM XML): this is an open format, but it was designed for OSM’s needs, it is not a good generic geometry format like Shapefile, KML or GeoJSON (specially popular for APIs and databases).

To understand how OSM data is organized, you first need to understand their 3 element types: nodes (points), ways (lines) and relations (groups of nodes and/or lines). There is no native “polygon” type, for example, if you see the limits of a city, the data behind looks like a group of ways (like “north border”, “east border”, etc) and a relation connecting them together. So, it is not a direct match, but “relation” is closest abstraction OSM has to a polygon.

Searching Relation IDs

On an OSM map view, right-click on the area you want to export, and click ”Query features”; if this menu is disabled you need keep zooming-in until it appears clickable:

Right-click on OpenStreepMap to open the 'Query features' menu.

This will open the “Query Features” panel, inside it, on “Enclosing features”, pick the feature you want, it usually goes smaller to bigger, like: location, neighborhood, city, state, country.

Links from 'Query Features' opening features of many zoom levels.

Notice that every feature open a relation page with name, ID, tags and a map. For exporting, the most important information on this page is the relation ID (in parenthesis after the relation name). For the example above on St. Mark's Square in Venice, you’ll get:

  • St. Mark's Square: 7932512
  • Venezia: 4817103
  • Veneto: 43648
  • Italian Peninsula: 4056532

Online converter at OSM France

The french OSM community created an online tool to convert relations into polygons: https://polygons.openstreetmap.fr.

Web

Just paste the relation ID into it, submit, and save the GeoJSON file that it generates:

Screenshot of the home page of the Polygon creation tool from OSM France. Screenshot of the result pare of the Polygon creation tool from OSM France.

The output is always a GeoJSON with a MultiPolygon in its root, even if the feature could fit in a single Polygon.

Command line

You can automate the operation above accessing via curl:

$ curl http://polygons.openstreetmap.fr/get_geojson.py?id=7932512 > st_mark_square.geojson
$ curl http://polygons.openstreetmap.fr/get_geojson.py?id=4817103 > venice.geojson
$ curl http://polygons.openstreetmap.fr/get_geojson.py?id=43648 > veneto.geojson
$ curl http://polygons.openstreetmap.fr/get_geojson.py?id=4056532 > italy.geojson

GeoJSON editors

Most complete GIS tools like QGIS and ArcGIS have support to many formats including GeoJSON. But, if you don’t want to install a complex application and just want to see a preview and do basic changes, I recommend the online tool geojson.io:

Screenshot of the geojson.io UI, showing a map of Venice in the left and its GeoJSON code in the right.

Notice that geojson.io coerces the GeoJSON into a FeatureCollection.

PostGIS

If you are creating your own GIS solution based on PostGIS, you can handle GeoJSON import/export with the functions ST_GeomFromGeoJSON and ST_AsGeoJSON:

-- Creating a table with geometry field:
create table cities (
  name text,
  geometry geometry(MultiPolygon)
);

-- Inserting a GeoJSON string into a geometry field:
insert into cities (name, geometry)
values ('Venice', ST_GeomFromGeoJSON('{"type":"MultiPolygon", ...'));

-- Returning a GeoJSON as string from a geometry field:
select ST_AsGeoJSON(geometry) from cities where name = 'Venice';