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"}
)
The below seems to provide a viable alternative version.
Two parts:
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
keepdimsparameter in the future gdf.dropna(axis="columns", how="all", inplace=True)".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:
Note: Initial inspiration for using dictionary comprehensions directly in OSMnx queries: https://stackoverflow.com/a/71278021
Anyway hope this is of some help