osmcode/pyosmium

Extracting Relation members (ways) and Way Nodes from and o5m file

freekvh opened this issue · 2 comments

Dear devs,

I have been struggling to extract all ways for a relation and all nodes for a way from an .o5m file. I simply don't understand how I should use this module.

I made this class:

import osmium as osm
import pandas as pd

class OSMHandler(osm.SimpleHandler):
    def __init__(self):
        osm.SimpleHandler.__init__(self)
        self.osm_data = []

    def tag_inventory(self, elem, elem_type):
        for tag in elem.tags:
            self.osm_data.append([elem_type, 
                                   elem.id, 
                                   elem.version,
                                   elem.visible,
                                   pd.Timestamp(elem.timestamp),
                                   elem.uid,
                                   elem.user,
                                   elem.changeset,
                                   len(elem.tags),
                                   tag.k, 
                                   tag.v])
    def node(self, n):
        self.tag_inventory(n, "node")

    def way(self, w):
        self.tag_inventory(w, "way")

    def relation(self, r):
        self.tag_inventory(r, "relation")

And I then proceed to go through my file (which only contains data from the tag:route=mtb

osmhandler = OSMHandler()
osmhandler.apply_file("../data/world_mtb_routes.o5m")


# transform the list into a pandas DataFrame
data_colnames = ['type', 'id', 'version', 'visible', 'ts', 'uid',
                 'user', 'chgset', 'ntags', 'tagkey', 'tagvalue']
df_osm = pd.DataFrame(osmhandler.osm_data, columns=data_colnames)

source: https://stackoverflow.com/questions/45771809/how-to-extract-and-visualize-data-from-osm-file-in-python

However, the resulting dataframe does not contain the members of the relation (the ways). I need those to get the nodes per way, and plot the entire relation.

The nodes are in the data frame, but without a list of the ways per relation and the nodes per way I can't plot the relations. What am I missing? I really don't understand the logic behind the module I'm afraid, my apologies.

I says here that during a pass through the file I need store the members lists, but how are they addressed in the code above?

Closing because I found I should post this on OSM help, and I did: https://help.openstreetmap.org/questions/81521/how-to-extract-relation-members-from-o5m-files