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

Union filters #1204

Merged
merged 3 commits into from
Aug 14, 2024
Merged

Union filters #1204

merged 3 commits into from
Aug 14, 2024

Conversation

arkty
Copy link
Contributor

@arkty arkty commented Aug 13, 2024

Union filters

Discussion: #1199

Problem

When developing city graph models, the size of the resulting graph might be important. Thanks to OSMnx, it's possible to use a pre-defined filters to request a network, based on desired types of OSM ways. But sometimes, it's needed to add an additional ways from OSM into network or union them based on multiple filters.

Technical details

When using custom_filter, it's not possible to request additional roads by Overpass union condition.

Example

We need to request additional road by name (The Road), ignoring all other conditions.

In Overpass this request will look like this:

(way[...](poly:'***'); way[name="The Road"](poly:'***'););

OSMnx constructs Overpass request like this, where $filter is pre-defined filter or provided by user.

[out:json][timeout:180];
(way[$filter](poly:'***');>;);
out;

Proposal

Make custom_filter accepts not only string, but a list of filters and then combine the result into single graph.
Because OSMnx already supports sub-divided requests, it's easy to implement this without changing logic on actual graph creation (simplification, connectivity, etc).

Usage example

On example of city Petrozavodsk we will use graph_from_place with network_type = "drive". This will give us a graph on a perfect level of details. But for some reason, two additional roads are required to be in model.

The request below correctly adds these roads.

ptz_graph = ox.graph_from_place(
    'Petrozavodsk, Russia',
    custom_filter=[
        op._get_network_filter("drive"),
        f'[name="улица Достоевского"]',
        f'[name="улица Калевалы"]'
    ]
)

@arkty arkty mentioned this pull request Aug 13, 2024
3 tasks
@EwoutH
Copy link
Contributor

EwoutH commented Aug 13, 2024

Would something like this be possible? How would that work API wise in with this PR? (see gboeing/osmnx-examples#90)

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

@arkty
Copy link
Contributor Author

arkty commented Aug 13, 2024

@EwoutH for OR filter it will be

highway_filter = '["highway"~"motorway|motorway_link"]'
construction_filter = '["construction"~"motorway|motorway_link"]'

# Full OR filter
custom_filter = [highway_filter, construction_filter]

@EwoutH
Copy link
Contributor

EwoutH commented Aug 13, 2024

That’s a really clean syntax, love it!

@gboeing
Copy link
Owner

gboeing commented Aug 13, 2024

@arkty tests are failing. Run the pre-commit hooks locally then push again.

@arkty
Copy link
Contributor Author

arkty commented Aug 14, 2024

@gboeing i've added a commit with style fix, can you please run wf again.

Copy link

codecov bot commented Aug 14, 2024

Codecov Report

Attention: Patch coverage is 88.88889% with 1 line in your changes missing coverage. Please review.

Project coverage is 98.27%. Comparing base (8df6d7a) to head (7dca7f5).
Report is 9 commits behind head on tests.

Files Patch % Lines
osmnx/_overpass.py 88.88% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            tests    #1204      +/-   ##
==========================================
- Coverage   98.31%   98.27%   -0.04%     
==========================================
  Files          24       24              
  Lines        2367     2373       +6     
==========================================
+ Hits         2327     2332       +5     
- Misses         40       41       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@gboeing gboeing changed the base branch from main to tests August 14, 2024 20:40
@gboeing gboeing merged commit c094bdc into gboeing:tests Aug 14, 2024
6 of 7 checks passed
@gboeing gboeing mentioned this pull request Aug 14, 2024
@EwoutH
Copy link
Contributor

EwoutH commented Aug 14, 2024

Awesome! Looking forward to using this in the next pre-release!

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

Successfully merging this pull request may close these issues.

3 participants