Search code examples
pythongeopandasosmnx

Trying to use the OSMnx tag "element_type" to retrieve all the "way" objects contained within a polygon


I'm trying to get all the structures (polygons) contained within a set polygon by calling for the tag "element_type":"way" but I can't seem to get it to work.

I've tried the code below to try to get all the element_type:way inside the polygon but it returns an empty gdf.

polygon = disolved_ellipses_gpd.iloc[0]["geometry"]
tags = {"element_type":"way"}
new_gdf = ox.geometries_from_polygon(polygon, tags)
new_gdf.shape

I also tried this code in an attempt to just filter out the non element_type:way but I get a KeyError: "element_type".

polygon = disolved_ellipses_gpd.iloc[0]["geometry"]
tags = {"building":True}
new_gdf = ox.geometries_from_polygon(polygon, tags)
new_gdf.shape
new_gdf[new_gdf["element_type"] == "way"].dropna(axis=1, how="any")

I'm not sure if I'm screwed up somewhere or if you just can't call based on the "element_type". I tried searching through the documentation but couldn't find anything.


Solution

  • There is almost certainly a better way, but I blundered into this solution:

    gdf = ox.geometries_from_point( (lat, lon), dist=distance, tags=tags)
    print ( len ( gdf ) )   # Returns the number of rows?
    
    # Try to extract the OSM ID - surprisingly diffculty
    element_type = gdf.axes[0].values[0][0] 
    osmid = gdf.axes[0].values[0][1]
    

    For some reason, this syntax simply fails:

    gdf [ 'element_type' ]
    

    Perhaps @gboeing may offer a better solution.