我对 osmnx.features 模块的用法感到困惑。
我的目标是找到瑞士指定城市的所有酒店和汽车旅馆。首先,我寻找市政府的几何形状,然后在这个几何形状内寻找所有酒店和汽车旅馆。显然,在我的城市列表中,有些城市没有酒店(如 Acquarossa,见下文)或汽车旅馆(如 Aarau)。
下面的代码获取几何图形,但会生成错误,因为某些城市没有结果:
osmnx._errors.InsufficientResponseError: No data elements in server response. Check log and query location/tags.
这是预期的行为吗?如果是,我应该如何正确使用该包来获取空数据框?尝试/例外?我的期望是一个空的数据框,它是某种信息(“无”是一个有趣的输出)。
还是我的代码有错误?
我感觉 osmnx 之前(比如 2023 年)返回了一个空数据帧。可以吗?
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"}
)
下面似乎提供了一个可行的替代版本。
两部分:
下面的简化代码似乎为“酒店”标签提供了预期的响应;对于“汽车旅馆”标签,只有未来警告 - “osmnx/features.py:1030: FutureWarning:
keepdims
参数 gdf.dropna(axis="columns", how="全部“,就地= 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')
如 OSMnx 示例笔记本所示:“使用 OSMnx 下载任何 OSM 地理空间特征” 可以直接使用 'ox.features_from_place()' 来获取 OSM 功能,如示例所示:
# 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
exception osmnx._errors.InsufficientResponseError
Exception for empty or too few results in server response.
注意:直接在 OSMnx 查询中使用列表推导式的最初灵感:https://stackoverflow.com/a/71278021
无论如何希望这对你有帮助