Search code examples
pythongisosmnx

osmnx features_from_polygon search tags using AND condition


osmnx provides a function features_from_polygon which can search based on a tag parameter. For example:

import osmnx as ox
r = ox.features.features_from_polygon(polygon, tags={'railway': True})

When adding multiple tags the results are a union of all tags, so for example this will return all the rail features and all the bridges features in the polygon.

r = ox.features.features_from_polygon(polygon, tags={'railway': True, 'bridge': True})

Is it possible to perform an AND search to return all the railway features that are also bridges in the polygon?


Solution

  • Is it possible to perform an AND search to return all the railway features that are also bridges in the polygon?

    Yes. Just search for the union of both tags, then filter the result to retain only those with non-null values for both:

    import osmnx as ox
    import pandas as pd
    ox.settings.log_console = True
    
    tags = {"railway": True, "bridge": True}
    gdf = ox.features.features_from_place("Long Beach, CA, USA", tags=tags)
    gdf[pd.notnull(gdf["bridge"]) & pd.notnull(gdf["railway"])]