Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example for OR condition in custom filter #90

Open
EwoutH opened this issue Jul 3, 2024 · 5 comments
Open

Add example for OR condition in custom filter #90

EwoutH opened this issue Jul 3, 2024 · 5 comments

Comments

@EwoutH
Copy link

EwoutH commented Jul 3, 2024

I would be curious on how to do more advanced custom filters, for example how to combine two (in and OR fasion).

custom_filter = '["highway"~"motorway|motorway_link"]'
construction_filter = '["construction"~"motorway|motorway_link"]'
full_filter = ???

Possible syntax and examples of more advanced custom filter could be added to 08-custom-filters-infrastructure.ipynb

@EwoutH
Copy link
Author

EwoutH commented Jul 3, 2024

Taking a look back at an old notebook I once applied this trick:

road_types = '["highway"~"motorway|trunk|primary|secondary|tertiary|motorway_link|trunk_link|primary_link|secondary_link|tertiary_link|construction"]["construction"!~"unclassified|residential|living_street|road|raceway|pedestrian|service|track|bridleway|cycleway|footway|path|steps"]'

But I think this should be able to done more elegant and robust.

@gboeing
Copy link
Owner

gboeing commented Jul 14, 2024

So an Overpass Turbo query to perform a union operation looks something like this:

(way["railway"="light_rail"]({{bbox}});way["highway"="primary"]({{bbox}}););(._;>;);out;

This example will return all the ways tagged railway=light_rail and all the ways tagged highway=primary. As you might notice, this doesn't work with the OSMnx custom_filter function argument, because that argument is only the ["railway"="light_rail"] or the ["highway"="primary"] part of the above query.

So then how to you create a graph of the union of multiple infrastructure types? See here and here. The short story is you use networkx.compose like:

import networkx as nx
import osmnx as ox
place = 'Some City'
G1 = ox.graph_from_place(place, custom_filter='["highway"~"primary"]')
G2 = ox.graph_from_place(place, custom_filter='["railway"~"light_rail"]')
G = nx.compose(G1, G2)

This will generate essentially the same result as the OverpassQL union above.

All that said, it's worth noting that the OSMnx features module allows you to pass a tags dict argument that does allow unions in the query. The graph module in contrast queries by predefined network_type strings or by inserting a single custom_filter string into the query. It may be possible to enhance the graph module's functions to accept a tags argument (perhaps instead of the custom_filter argument), like the features module does. But that functionality doesn't currently exist and its feasibility has not been verified.

@gboeing
Copy link
Owner

gboeing commented Aug 6, 2024

See also gboeing/osmnx#1199

@HDKMP
Copy link

HDKMP commented Aug 6, 2024 via email

@gboeing
Copy link
Owner

gboeing commented Aug 14, 2024

I will add an example here next time I update the repo to demonstrate the enhancement (gboeing/osmnx#1204) of passing a list to custom_filter to perform a union of multiple queries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants