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 osm2py.py. 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.
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
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:
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):