1

What works

If I need a GeoJSON file with the OpenStreetMap borders of Liechtenstein, I download liechtenstein-latest.osm.pbf from GeoFabrik:

curl https://download.geofabrik.de/europe/liechtenstein-latest.osm.pbf -o liechtenstein-latest.osm.pbf

Search for Liechtenstein OpenStreetMap and get its relation id (1155955):

enter image description here

Then, using ogr2ogr, I create liechtenstein_borders.geojson:

ogr2ogr -f GeoJSON \
        -dialect sqlite \
        -sql "SELECT * FROM multipolygons WHERE osm_id=='1155955'" \
        liechtenstein_borders.geojson liechtenstein-latest.osm.pbf \
        -nln main

The resulting GeoJSON nicely has a Liechtenstein shape:

enter image description here


What does not work

The same method does not work for Norway. Relation id: 2978650.

Download data:

curl https://download.geofabrik.de/europe/norway-latest.osm.pbf -o norway-latest.osm.pbf

Attempt extraction:

ogr2ogr -f GeoJSON \
        -dialect sqlite \
        -sql "SELECT * FROM other_relations WHERE osm_id=='2978650'" \
        norway_borders.geojson norway-latest.osm.pbf \
        -nln main

Result is an empty GeoJSON:

enter image description here


Why?

What didn't I take into account when expecting the Norway-case to work just like the Liechtenstein one, and how can I fix the method? Ideally, I would like to be able to extract the geometry after having found the relation id.

2 Answers 2

2

The relation is outside the cropped area from Geofabrik extract e.g right map at https://download.geofabrik.de/europe/norway.html That's why you do not get it.

Two alternatives approaches using jq, curl, ogr2ogr and query-overpass (Node.js based CLI)

  1. Using Overpass
echo "[out:json][timeout:300];(relation(2978650););(._;>>;);out body;>;out skel qt;" | query-overpass | jq -c '{"type": "FeatureCollection", "name": "my_selection", "features": [.features[] | select( .geometry.type == "Polygon" or .geometry.type == "MultiPolygon")]}' | ogr2ogr -dialect SQLite -f GeoJSON /tmp/output_norway_approach1.geojson /vsistdin/ -sql "SELECT 'Norway' AS name, ST_Union(geometry) AS geometry FROM my_selection"
  1. Using polygons export from relation
curl -s "https://polygons.openstreetmap.fr/get_geojson.py?id=2978650&params=0" | jq -c '{"type": "FeatureCollection", "name": "my_selection", "features": [.geometries[] | {"type":"Feature","properties":{"name": "Norway"},"geometry": .}]}' >| output_norway_approach2.geojson

Second approach could be easily automated within Python if necessary

PS: There are some ways for improvements as in both cases, I do not bother about attributes of the relation and only care about geometry.

0
0

The accepted answer works. If one prefers ogr2ogr over jq, they can do:

curl "https://polygons.openstreetmap.fr/get_geojson.py?id=2978650&params=0" -o borders.geojsonseq \
    && ogr2ogr borders.geojson borders.geojsonseq

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.