I have an oddity question, that I’m wondering if someone can figure out how to answer.
So you have country boundaries tagged boundary=administrative + admin_level=2, and then states at admin_level=4 and so forth. So, if you have a city within a county within a state within a country, that’s four total levels.
I am wondering what is the deepest nesting that exists in the database? Does anyone have a technique that might work to be able to determine this?
I’m not entirely sure what the specific issue is with your question.
As I know that the ‘admin_level’ in OpenStreetMap varies by country.
Currently, the maximum documented value for
(administrative) admin_level is 11, as detailed in the wiki.
I am asking what is the deepest level of nesting that exists in the database. I am looking for specific cases where a boundary is hierarchically contained within N successively higher-level administrative boundaries.
What is the largest value of N?
Sure there are large values of admin_level, but not every number from 2-11 is necessarily used.
Looking at the cases where high numbers are used in taginfo, I found a 5-stack:
Poland > Kuyavian–Pomeranian Voivodeship > Bydgoszcz County > Gmina Białe Błota > A rope in someone’s garden.
Not sure how valid the last step is, but it definitely exists in the database. Not much else interesting in the 13+ numbers.
The Philippines regularly goes to 5: Philippines > Region > Province > Municipality > Barangay
In Greece, we have 7 layers in total (and currently the maximum we can map, meaning no other administrative layer is missing from it):
For example the railway station of Patras is within Greece > Decentralized Administration of Peloponnese, West Greece and Ionian > Region of West Greece > Regional Unit of Achaea > Municipality of Patras > Municipal Unit of Patras > 1st Commune of Patras
I used spatial queries (which admin boundary is within which one?) to construct a directed acyclic graph of admin boundaries. I made each admin boundary a graph node, then connected them with an edge if they were within another admin boundary. The answer is the longest path in the graph.
Edit: Here is the code, in case someone would like to do something similar. Takes only a few minutes to run (once you have downloaded and converted the planet file into the format required by Geodesk) !
Code
from geodesk import *
import networkx as nx
planet = Features("planet.gol") # this file has to be created from an .osm.pbf file first
# create empty graph and a root node
G = nx.DiGraph()
G.add_node("world")
# make each admin boundary into a graph node
admin_boundaries = planet('a[boundary=administrative][admin_level][type!=multilinestring]').relations
G.add_nodes_from(admin_boundaries)
# connect each country node to the root node
for country in planet('a[boundary=administrative][admin_level=2][type!=multilinestring]').relations:
G.add_edge("world", country)
# connect each admin area to all the higher-level admin areas within it
for a in admin_boundaries:
if str(a.admin_level).isdigit():
for sub_boundary in planet('ar[boundary=administrative][admin_level > ' + str(a.admin_level) + '][type!=multilinestring]').relations.within(a):
G.add_edge(a, sub_boundary)
nx.dag_longest_path(G)