Another Python interface for OSM files

Here’s a work-in-progress OSM file parser & generator:

I’ve been doing my own project that manipulates some stuff, and needed to convert from/to OSM files.
I saw the other project on GitHub, but didn’t like the quality of the code. It’s not DRY, so I decided to complete my own tool that I started in May 2011.

As a result, my code is 7K, while the other guy’s is 13K. Plus, hope it’s clear how to extend my classes. The dumb one returns everything as dictionaries. The smart class returns nice objects, although right now it’s impossible to generate new ones (init expects dictionaries).

The script you should use is The other scripts (osm2mongo, osm_json) do not work in the new branch yet.

It requires Python 2.7 (or 2.6 if you get rid of dictionary comprehensions) and generates correct OSM files. (Although, empty XML elements are closed in an ugly way, like .) It can work with any file-like object (stdin/out, bz2 file, etc).

You’re welcome to try, fork it, write your comments.


without having tried your piece of software, maybe a hint for you:

Doing a fulltext serach about thze OSM wiki, there is no entry for osm2phyton anywhere.

If you would like to spread your software and even get more feedback, why not creating an own article in the wiki about osm2phyton?

Thus, more people get to know and can try it …

I’m getting close to that. Didn’t want to release a half-baked solution.

Today, I simplified the module a lot. No more need to instantiate a class just to run a single method in it.

>>> from osm2py import load_osm
>>> load_osm(open('my_file.osm'))

Will output a list of dictionaries representing the XML elements. To process it and save as you need, define a callback function. For example this is the default one, which just adds the element to parent[‘children’] list:

def load_callback(current, parent=None):

You can define your function so that it records the data into a db.
To work with only a certain kind of elements, define an arbitrary filter callback. For example, we want to get all the tags (filter) and print them (callback):

def cb(elt, parent=None):
    print "{k}: {v}".format(elt['attrs'])

flt = lambda elt: elt['name'] == 'tag'

load_osm(open('my_file.osm'), cb, flt)

This will print every tag value consecutively. A homework: modify this callback to count tags usage in a file and print them in the end.