5

I have downloaded an OpenStreetMap file on my desktop , and I have used my OSM file in the jupyter notebook.

My code:

import xml.etree.cElementTree as ET
osm_file = "ahmedabad_india.osm"

for event, elem in ET.iterparse(osm_file, events=("start",)):
     print(elem)
     # prints the Element 'osm' at 0x03A7DC08>
     #<Element 'bounds' at 0x03A7DDA0>
     #<Element 'node' at 0x03A7DE90>
     #<Element 'tag' at 0x03A7DF08> and so on ...

I'd like to see the contents of all the tags i.e. <'node', 'id', 'name', ...> and so on.

I tried using elem tag but, it prints nothing.

Can anyone help me to figure out, who to get the contents of tags like node, ways etc.

1 Answer 1

21

You can extract all the data from an .osm file through PyOsmium (A fast and flexible C++ library for working with OpenStreetMap data) and then handle it with Pandas:

Code:

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")


osmhandler = OSMHandler()
# scan the input file and fills the handler list accordingly
osmhandler.apply_file("muenchen.osm")

# 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)
df_osm = tag_genome.sort_values(by=['type', 'id', 'ts'])

Output:

enter image description here

6
  • my concern is how do you get the .osm file in the first place. Is there an automated pythonic way to get such a file? Also is there no direct way to access the data without have to download some physical file in the process which would eat up system resources?
    – user32882
    Oct 25, 2018 at 10:35
  • 2
    you can simply go to the osm website, select a small region and export it.
    – ashunigion
    Aug 26, 2019 at 14:23
  • How to get latitude and longitude of elements? Jun 28, 2020 at 16:30
  • @VictorMarconi if you write print(elem) below the definition of tag_inventory you can see that the coordinates are stored like this location=8.786805/53.074942 this means that elem.location will give you the value
    – mjeshtri
    Oct 29, 2020 at 16:55
  • Thank you for this answer, but I am looking to plot relations. As far as I can tell the resulting dataframe does not contain the members of a relation and also does not contain coordinates for nodes. How can this information be added?
    – Freek
    Jul 30, 2021 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.