16

In order to do reverse geocoding lookups to find a country (as ISO-3166-1) and the subdivision (as ISO-3166-2) for given coordinates I am looking for a way to get all polygons of all countries and their subdivisions.

Reverse geocoding lookups with Google Maps or other services either do not always return the country and subdivision (Google Maps), or sometimes they are incorrect (geonames.org). As I understand it all countries and subdivisons are available in the openstreetmap data, but I do not know how to only retrieve the polygons - no map data, no points of interest or cities are necessary, only the outlines of all subdivisions with the given country and subdivision codes. Then I can check myself if a given point is within any given polygon, or near it.

Does anyone know how to retrieve this data from the openstreetmap data, or is there any other source for this data?

8

5 Answers 5

6

This site may have what you are looking for:

3
  • I've checked out gadm and was able to test it - the provided data is really good, and most importantly, the ISO 3166-2 codes are contained (usually in the hasc_1 column, sometimes in the hasc_2 column). Some manual processing is still necessary depending on what kind of details one wants, but the source material seems great so far.
    – iquito
    Commented Apr 11, 2012 at 2:42
  • @iquito: FYI those values in the HASC_* columns are not ISO 3166, they're a totally different thing. Commented Apr 15, 2013 at 18:16
  • Unfortunately it is only available for academic and non-commercial use.
    – Wouter
    Commented Jul 17 at 9:08
2

You can find a script inspired by MvG's answer and a few examples here : https://github.com/laem/regions-topojson

Sadly, not all geonames administrative relations are tagged with iso-3166-2...

1

Since you requested how to extract this data from openstreetmap: theoretically you can use the XAPI to match boundary descriptions. E.g. using a query like this: (warning, will take quite a while, so I'm not formatting this as a link!)

www.overpass-api.de/api/xapi?way[bbox=2,49,7.5,54][boundary=administrative][admin_level=2]

The result will be a list of polygons, in this case denoting the boundaries of the Benelux countries. You'd probably have to repeat this for other bounding boxes, perhaps with overlap. And for other levels. Lot of work. You'd want a script for that.

Or download Planet.osm, and simply use suitable OSM libraries or XML tools to locally extract the relevant data.

All of this will come without uniform ISO-3166-2 identifiers, as far as I can tell, simply because that standard isn't available for free.

1

Other sources include Second Administrative Level Boundaries (SALB) and DIVA-GIS.

0

You can download a complete set of country boundaries, with first-level administrative subdivisions for most countries from GADM, here: https://gadm.org/download_world.html

2
  • I would love to know why this answer was downvoted, considering the ultimate question is "Does anyone know how to retrieve this data from the openstreetmap data, or is there any other source for this data?" I mean, what answer could be better than providing a link directly to the exact data they want? Commented Apr 30, 2018 at 4:27
  • probably the reason could be that the first (top) answer corresponds GADM data Commented May 10, 2018 at 12:35

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.