I want to download country borders as vector data from OpenStreetMap. The best would be in a common vector geodata format like GeoJSON that can be used e.g. in QGIS. I there a simple way to do this?
-
1See gis.stackexchange.com/a/379763/88814 and gis.stackexchange.com/a/401024/88814– BabelCommented Dec 26, 2023 at 18:55
-
Thank you! I know that the answer can be found at GIS SE already, but it seems that no one has asked explicitly how to download country vector data from OSM - and a separate Q&A seemed valuable due to this post.– winnewoerpCommented Dec 26, 2023 at 19:00
-
1I'll just add a note to download OSM data in QGIS for set areas use the OSM Downloader Plugin.– JohnCommented Dec 27, 2023 at 19:53
1 Answer
To download country borders from OpenStreetMap, you can use overpass-turbo.eu. I used the following expression:
[out:json][timeout:25];
relation[boundary=administrative][admin_level=2]["name:en"=Switzerland];
out geom;
Here's a direct link to the query (example country: Switzerland).
Replace the value after "name:en"=
with the English name of your country or use name=
to query it using the official (short) country name. Don't forget to insert quotes if the name contains spaces or special characters.
You can get the data as GeoJSON via Export > GeoJSON Download. When you load the layer into QGIS, only load the polygons.
A note on country borders including territorial waters: If your country includes a coastline to international waters (maritime borders), the national territory in OSM includes the territorial waters (usually 12 nautical miles from the low-tide coastline). To query only the landmass you might need a workaround when using OSM data. In some countries, downloading all level 4 administrative areas might work. Here's an example for Italy:
[out:json][timeout:25];
{{geocodeArea:Italy}};
relation[admin_level=4][boundary=administrative]["ISO3166-2"~"IT-"](area);
out geom;
Here's a direct link to the query
"ISO3166-2"
is used here because without a unique country identifier parts of neighboring countries might be included due to inaccuracies.
Dissolve the layer to one Polygon/MultiPolygon in QGIS to reduce data volume.
However, this does not work for all countries either because no admin level exists that covers only the landmass or because level 4 administrative units also include territorial waters (e.g. Germany).
-
2This was an excellent answer, I imported a .GeoJSON into QGIS and exported it out of QGIS as a .SHP– MapDeathCommented Dec 26, 2023 at 22:13
-
How would I do this for all the smaller admin levels (districts) in Malawi or to get Malawi with all its districts in one geojson? I tried changing the "admin_level=" to a different value but that doesn't seem to have worked. I know OSM has all the sub levels because I can see them on the OSM world basemap in QGIS– MapDeathCommented Dec 26, 2023 at 22:40
-
You can get all districts like this:
[out:json][timeout:25]; relation[admin_level=4][boundary=administrative]["ISO3166-2"~"MW-"]; out geom;
Lower levels (e.g.admin_level=8
for municipal level) don't seem to exist for Malawi. Commented Dec 26, 2023 at 23:10