OSMNX: Dealing with empty dataframe of attributes when using the features module

37 views Asked by At

I am confused with the usage of the osmnx.features module.

My goal is to find all hotels and motels in specified municipalities of Switzerland. First, I look for the geometry of the municipality and then - inside this geometry - for all hotels and motels. And obviously, in my list of municipalities, some have no hotels (like Acquarossa, see below) nor motels (like Aarau).

The code below gets the geometry but generates an error because some municipalities have no results: osmnx._errors.InsufficientResponseError: No data elements in server response. Check log and query location/tags. Is it the expected behavior? And if yes, how should I properly use the package to get an empty dataframe? With try/except? My expectation would be an empty dataframe, which is some kind of information ("none" is an interesting output). Or do I have an error in my code?

I have the feeling that osmnx was returning an empty dataframe before (say, in 2023). Could it be?

for municipalities in ['Aarau', 'Acquarossa']:
    # Specifying "city" in the request to avoid looking for hotels in the district or canton of the same name, but only in the municipality
    request_dict = {
        "city": commune_with_hotels,
        "country": "Switzerland",
        "countrycodes": "ch",
    }
    request = ox.geocode_to_gdf(request_dict)
    boundary_polygon = request["geometry"][0]
    # Get OSM data for "hotels" and "motels"
    hotels_per_commune = ox.features_from_polygon(
        boundary_polygon, tags={"tourism": "hotel"}
    )
    motels_per_commune = ox.features_from_polygon(
        boundary_polygon, tags={"tourism": "motel"}
    )
1

There are 1 answers

0
dmb On

The below seems to provide a viable alternative version.

Two parts:

  1. Simplify code
  2. Discuss, but not resolve, occasional occurrence of the specific error mentioned

The simplified code below seems to provide expected response for the 'hotel' tag; and for the 'motel' tag only a future warning -- "osmnx/features.py:1030: FutureWarning: <class 'geopandas.array.GeometryArray'>._reduce will require a keepdims parameter in the future gdf.dropna(axis="columns", how="all", inplace=True)".

def fetch_tourism_features(tag):
    cities = ['Aarau', 'Acquarossa'] 
    tags = {"tourism" :f'{tag}'}
    gdf = ox.features_from_place([{"city": city, "country": "Switzerland", "countrycodes": "ch"} for city in cities], tags)
    return gdf

hotels = fetch_tourism_features('hotel')
motels = fetch_tourism_features('motel')
  1. Simplify code:

As shown in the OSMnx example notebook: 'Download Any OSM Geospatial Features with OSMnx' one can use directly 'ox.features_from_place()' to fetch OSM features as in its example:

# get everything tagged amenity,
# and everything tagged landuse = retail or commercial,
# and everything tagged highway = bus_stop
tags = {"amenity": True, "landuse": ["retail", "commercial"], "highway": "bus_stop"}
gdf = ox.features_from_place("Piedmont, California, USA", tags)
gdf.shape
  1. As for the error message, it's listed in the OSMnx (1.9.1 Internals Reference' osmnx._errors module -- and does occur in some queries (just a few ad hoc ones tested; not enough to look for any pattern):
exception osmnx._errors.InsufficientResponseError
Exception for empty or too few results in server response.

Note: Initial inspiration for using dictionary comprehensions directly in OSMnx queries: https://stackoverflow.com/a/71278021

Anyway hope this is of some help