osm2python /

Filename Size Date modified Message
sample
42 B
4.7 KB
898 B
1.6 KB
6.1 KB

osm2python : A Simple Python OSM XML Converter

Usage is extremely simple:

>>> from osm2python import load_osm, dump_osm
>>> load_osm(open('my_file.osm'))
[<a list of all the XML elements as dictionaries (see help(load_osm) for more info)>]

Dump it back:

>>> dump_osm(open('another_file.osm', 'w'), new_doc_dictionary)

If you want to save the elements into a custom storage, define a callback:

>>> def cb(current, parent):
        print current

>>> load_osm(open('my_file.osm'), cb)
(will just print all the dictionaries)

You can also create an arbitrary filter for the elements:

>>> flt = lambda elt: (elt['name'] == 'node' and
       (54 < int(elt['attrs']['lat']) < 56) and
       (82 < int(elt['attrs']['lon']) < 84))

>>> load_osm(open('my_file.osm'), element_filter=flt)
<list of dictionaries>

Tests with bzipped files show that the scripts run twice as faster in PyPy 1.8 than in cPython 2.7.2, but since this script keeps all the data in memory, the memory usage with PyPy becomes impractically big on real OSM files. So, to run with PyPy, make sure your callback does not store references to the new elements, so that they could be garbage-collected. To run such a test, do

$ python __init__.py sample/chistooz.osm.bz2 x
$ path/to/pypy __init__.py sample/chistooz.osm.bz2 x

(the second argument 'x' triggers profiling)

tree.py can import from OSM XML in Python objects representing the document and linking correctly one another.

osm_json.py dumps these dictionaries in json format.

Note: This project is not a PyPI package yet.

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.