Search code examples
overpass-api

Extract all polygons states of a country


I need polygons of states in my Country.

After many tries, I got to this. It does return the states of my country, but there are no paths and many items that I did not want.

I've been using http://overpass-turbo.eu/ to test my queries.

[out:json][timeout:25];
{{geocodeArea:Czechia}}->.searchArea;
(
  relation["boundary"="administrative"]["admin_level"="7"](area.searchArea);
);

// print results
out body;
>;
out skel qt;

I want to know:

  • What I need to do to get polygons of results (the states in my case)
  • Why am I getting roads and other stuff when I am specifying boundary and admin_level.
  • How to do a similar query next time (I will be getting at least the same for Slovakia)

Solution

  • To get the polygons, you can convert the json result of your query to GeoJSON.

    You can test it in http://overpass-turbo.eu/ by running the query and then clicking the Export button and choosing the GeoJSON format.

    The output will contain the states and administrative centers as items of the features array. Each item will contains its polygon coordinates in geometry.coordinates.
    I don't know how to filter out the administrative centers via a query, but you can easily filter out those items on the client side when processing the GeoJSON. I did not see any other unwanted data other than that.

    Sample output (abbreviated for readability):

    {
        ...
        "features": [
          {
            "type": "Feature",
            "properties": {
              "@id": "relation/435509",
              "ISO3166-2": "CZ-806",
              "admin_level": "7",
              "boundary": "administrative",
              "name": "okres Ostrava-město",
              ...
            },
            "geometry": {
              "type": "Polygon",
              "coordinates": [
                [
                  [
                    18.3400673,
                    49.7592689
                  ],
                  [
                    18.3403166,
                    49.7590688
                  ],
                  [
                    18.3406238,
                    49.7586477
                  ],
                  ...
    

    Regarding similar queries - it depends on the data available and is country specific.
    E.g. for Slovakia, the lowest admin_level which returns areas covering the whole state is 4 and that setting returns only the regions (which is a partitioning of a higher level than the one that you are using for the Czech republic).