pyrosm icon indicating copy to clipboard operation
pyrosm copied to clipboard

Fix: Reading PBF from osmfilter -> osmconvert fails #176

Open eracle opened this issue 4 months ago • 0 comments

Pull Request: Fix for Pyrosm KeyError when Filtering PBF Files with Osmium

Summary

This pull request addresses an issue where the pyrosm library throws a KeyError: 'tags' when filtering certain PBF files using osmium and processing them with custom tag criteria. The error occurs specifically for certain key-value pairs like highway=motorhead, which cause the tags key to be missing in some OSM relationships.

Problem

The following error traceback is observed when attempting to process OSM data using custom filter criteria:

tests/test_custom_filter.py:55: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
pyrosm/pyrosm.py:767: in get_data_by_custom_criteria
    gdf = get_user_defined_data(
pyrosm/user_defined.py:36: in get_user_defined_data
    nodes, ways, relation_ways, relations = get_osm_data(
pyrosm/data_manager.pyx:175: in pyrosm.data_manager.get_osm_data
    cpdef get_osm_data(node_arrays, way_records, relations, tags_as_columns, data_filter, filter_type, osm_keys=None):
pyrosm/data_manager.pyx:176: in pyrosm.data_manager.get_osm_data
    return _get_osm_data(node_arrays, way_records, relations, tags_as_columns, data_filter, filter_type, osm_keys)
pyrosm/data_manager.pyx:172: in pyrosm.data_manager._get_osm_data
    ways, relation_ways, filtered_relations = get_osm_ways_and_relations(way_records, relations, osm_keys, tags_as_columns, data_filter, filter_type)
pyrosm/data_manager.pyx:115: in pyrosm.data_manager.get_osm_ways_and_relations
    filtered_relations = get_relation_arrays(relations, osm_keys, data_filter, filter_type)
pyrosm/data_manager.pyx:48: in pyrosm.data_manager.get_relation_arrays
    indices = filter_relation_indices(relations, osm_keys, data_filter, filter_type)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

>   cdef int i, n = len(relations["tags"])
E   KeyError: 'tags'

pyrosm/data_filter.pyx:268: KeyError

The error originates from the pyrosm library, which attempts to access the tags key in relationships that do not have any tags. This is observed when using the following Osmium command to filter tags in PBF files:

osmium tags-filter --overwrite -o {filtered_output_file} {osm_input_file} nwr/{query_tag}

For instance, filtering with highway=motorhead leads to the error due to some relations not having the tags key.

Testing

  • Added a unit test that downloads a filtered PBF file from my personal repository. The unit test ensures that the fix works correctly by running the filter process on a known dataset with missing tags keys.

Btw Fixing #176

Thank you for reviewing this PR!

eracle avatar Oct 11 '24 12:10 eracle