-
I have been trying to replicate the output of the following bash command with pyosmium: osmium tags-filter osm_data/aachen.osm.pbf \
w/highway w/public_transport=platform w/railway=platform w/park_ride r/type=restriction \
-o aachen-filtered2.osm.pbf Here's my python code: import osmium
# Define the filters for the desired tags
filters = [
osmium.filter.KeyFilter('highway').enable_for(osmium.osm.WAY),
osmium.filter.TagFilter(('public_transport', 'platform')).enable_for(osmium.osm.WAY),
osmium.filter.TagFilter(('railway', 'platform')).enable_for(osmium.osm.WAY),
osmium.filter.KeyFilter('park_ride').enable_for(osmium.osm.WAY),
osmium.filter.TagFilter(('type', 'restriction')).enable_for(osmium.osm.RELATION)
]
# Apply filters using a ForwardReferenceWriter to maintain referential completeness
writer = osmium.ForwardReferenceWriter('aachen-filtered.osm.pbf', ref_src='osm_data/aachen.osm.pbf', overwrite=True)
fp = osmium.FileProcessor('osm_data/aachen.osm.pbf')
# Add filters to the file processor
for f in filters:
fp = fp.with_filter(f)
# Process the file and write filtered data
with writer:
for obj in fp:
writer.add(obj) I would have expected these two to result in the same output file, but that does not seem to be the case.
Could anyone provide an explaination for the difference, to make a working python version of the bash command? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
For reference, the osm file I tried this with comes from here: https://github.com/motis-project/test-data/raw/aachen/aachen.osm.pbf |
Beta Was this translation helpful? Give feedback.
-
Thanks for trying out the new filters. Your filters are not quite right. When you chain filters, then they applied one after the other. That effectively is a conjunction of conditions. You are thus filtering for ways with The other problem with the filters is that all of them are restricted to either ways or relations. That means that all nodes will pass your filter chain. They will all end up in your output file. If you are only interested in ways and relations, then use the EntityFilter to restrict to these types first. Finally, there is the tricky part regarding referential completeness. To get the members and nodes of ways and relations, you need the backward references, not the forward references. See the user manual for an attempt to define these terms. Putting it all together, this is what the script should look like:
For additional information on tag filtering with pyosmium, have a look at the tag filter cookbook. It gives some background on what is happening here. |
Beta Was this translation helpful? Give feedback.
Thanks for trying out the new filters.
Your filters are not quite right. When you chain filters, then they applied one after the other. That effectively is a conjunction of conditions. You are thus filtering for ways with
highway
andpublic_transport=platform
andrailway=platform
andpark_ride
tags. You can do or-filtering of tags or keys by adding multiple parameters toKeyFilter()
orTagFilter()
. However, it is currently not possible to or-chain filters. So if you have a mix of keys and tags you want to filter for, then you need to pre-filter by tag keys only and then further filter for the appropriate key-value combinations in the reader loop.The other problem with the filters is that…