0

What would be the code to easily get all the states (second subdivisions) of a country? The pattern from OSMNX is, more or less:

division admin_level
country 2
region 3
state 4
city 8
neighborhood 10

For an example, to get all the neighborhoods from a city:

import pandas as pd
import geopandas as gpd
import osmnx as ox

place = 'Rio de Janeiro'
tags = {'admin_level': '10'}

gdf = ox.geometries_from_place(place, tags)

The same wouldn't apply if one wants the states from a country?

place = 'Brasil'
tags = {'admin_level': '4'}

gdf = ox.geometries_from_place(place, tags)

I'm not even sure this snippet doesn't work, because I let it run for 4 hours and it didn't stop running. Maybe the package isn't made for downloading big chunks of data, or there's a solution more efficient than ox.geometries_from_place() for that task, or there's more information I could add to the tags. Help is appreciated.

enriicoo
  • 31
  • 4

1 Answers1

2

OSMnx can potentially get all the states or provinces from some country, but this isn't a use case it's optimized for, and your specific use creates a few obstacles. You can see your query reproduced on Overpass Turbo.

  1. You're using the default query area size, so it's making thousands of requests
  2. Brazil's bounding box intersects portions of overseas French territory, which in turn pulls in all of France (spanning the entire globe)
  3. OSMnx uses an r-tree to filter the final results, but globe-spanning results make this index perform very slowly

OSMnx can acquire geometries either via the geometries module (as you're doing) or via the geocode_to_gdf function in the geocoder module. You may want to try the latter if it fits your use case, as it's extremely more efficient.

With that in mind, if you must use the geometries module, you can try a few things to improve performance. First off, adjust the query area so you're downloading everything with one single API request. You're downloading relatively few entities, so the huge query area should still be ok within the timeout interval. The "intersecting overseas France" and "globe-spanning r-tree" problems are harder to solve. But as a demonstration, here's a simple example with Uruguay instead. It takes 20 something seconds to run everything on my machine:

import osmnx as ox
ox.settings.log_console = True
ox.settings.max_query_area_size = 25e12

place = 'Uruguay'
tags = {'admin_level': '4'}

gdf = ox.geometries_from_place(place, tags)
gdf = gdf[gdf["is_in:country"] == place]
gdf.plot()
gboeing
  • 5,691
  • 2
  • 15
  • 41