I want to write a script that parses OpenStreetMap (OSM) XML files and builds a database of towns and cities in a hierarchical fashion. I want the resulting data set to have a hierarchy that might look like this in the US:
USA -> California -> San Francisco County -> San Francisco
and maybe like this in the UK:
United Kingdom -> England -> Middlesex -> London -> Soho
The output will be a JSON document that describes a hierarchy for all cities in the OSM file, with a structure like the examples above.
I'm using Python and the "imposm" parser library and I can load and parse the file without a problem; my issue is a lack of understanding of how the OSM data is structured: I don't know how to know the parent/child relationship between nodes in OSM's data. For example, if I locate the node for "Soho", how can I tie it back to the nodes for "City of Westminster", "Greater London", "Middlesex" and "England"?
I know that some nodes have an "is_in" tag that might give some of this information, but
- A) this is inconsistent and
- B) it seems to be a free-form text field, not a link to an OSM node (ie. is_in: "City of Westminster" does not give me any link to the Westminster node).
Please let me know if you have any suggestions for how to link these nodes hierarchically.