Hello
I use overpass turbo not often and have absolutely no experience. Maybe someone can help me with an example?
I need the administrative borders (admin_level 5) of Germany. Only the borders and only the cost lines (not the area on the sea). I need no perfect resolution (reduce data, maybe 200m between points).
My first try is:
[out:json];
// gather results
rel[boundary=administrative][admin_level=5]({{bbox}});
// print results
out geom;
But it shows boundaries on the sea and not only Germany.
Any hint?
That’s because this part of the sea is part of Germany. There has been a polygon, which is the one you are looking for, called the landmass polygon. It has been removed in January this year. The reason was that it was a lot of work to maintain it and it can be calculated from other data.
My opinion: It’s very difficult to create it from other data. I didn’t manage to do so yet and I doubt that it is possible using overpass. Instead I’m using an old version, I once saved as a poly file.
First of all, only few German states have polygons of admin level 5 (Regierungsbezirk) at all. Almost all abolished that level of administration. But you asked for admin level 5 and I will respond assuming that this level exists all over Germany.
You can’t achieve this with Overpass. Use PostgreSQL with PostGIS instead:
Download the extract for Germany in .osm.pbf format from Geofabrik Downloads
Install PostgreSQL and PostGIS. Instructions depend on your operating system. There are lots of instructions on the internet how to do this.
Install GDAL (includes the og2ogr).
Create a database after you have PostgreSQL installed.
Load the OpenStreetMap data extract into the database using osm2pgsql -d gis --output pgsql --hstore --multi-geometry path/to/osm_file.osm.pbf
You might have to add more command line options for username, password, port etc.
Load them into the database using shp2pgsql -s 4326 -cDI land_polygons.shp land gis | psql -d gis
As an alternative, you can use ogr2ogr as well.
Log into the database and execute following SQL commands:
CREATE TABLE al5 AS
SELECT osm_id, name, way AS geom
FROM planet_osm_polygon
WHERE osm_id < 0 AND boundary = 'administrative' AND admin_level = '5';
CREATE INDEX ON al5 USING gist(geom);
CREATE TABLE al5_on_land AS
SELECT a.osm_id, a.name, ST_CollectionExtract(ST_Intersection(a.geom, ST_Union(l.geom)), 3) AS geom
FROM al5 AS a
JOIN land AS l
ON a.geom && l.geom AND ST_Intersects(a.geom, l.geom)
GROUP BY a.osm_id, a.name;
Now you have a database table al5_on_land containing the admin level 5 polygons on land. You can export them using ogr2ogr (adding username, password, port etc. might be necessary):
Guide at switch2osm.org to install a tile server on Debian 12. The guide covers setup of Osm2pgsql, PostgreSQL and PostGIS. Other guides on that page cover other versions of Debian and Ubuntu. LearnOSM
I recommend to run PostgreSQL on the same host as you run Osm2pgsql. Avoid network based storage in virtual (cloud) environments.
Note for experienced readers of this guide: You could use the new flex output of Osmp2gsql. There is an example and extensive, excellent documentation covering it. In order to keep the instructions simple, I used the old pgsql output.
As a side note, it would be really awesome if Postpass or something similar came with some conveniences for this sort of query. People very commonly want boundaries without water for cartography and data visualization. Apparently there are paid services for this data but nothing particularly geared toward the OSM community.
Thank you. Well it looks like not so easy I think.
I solved it with QGIS and hand working. Maybe not the best move or finest work, but I get what I need.