Osm2gml.py

From OpenStreetMap Wiki
Jump to: navigation, search

Osm2gml.py python script converts OSM XML into GML. This is an alternative to the OSM to GML XSLT. Using Python and xml.sax, it is possible to convert from the Planet.osm dump to GML much more quickly. There is a script available from http://www.chzsoft.com.ar/geo/osm2gml.py.txt which shows how to do it. (A script for the old version 0.4 API can be found at http://london.freemap.in/osm2gml_simple.py.txt)

Syntax for running this script:

cat yourfile.osm | ./osm2gml.py > yourfile.gml

A few important things to notice about this code:

  • The OSM namespace is the base namespace. This is so that attributes, when they come out, come out as lanes, not osm:lanes, which some tools might not completely understand.
  • You can chose via exportAll whether all tags should be exported. If exportAll is set to 0 then exportTags defines a set of tags which are exported. The reason that we don't just export all tags is that when exporting to shapefiles, attributes are fixed width. This means that for every attribute, even if it's null 99.9% of the time, you're increasing your attributes table significantly. Exporting only useful tags lets you select which data are important, and limit the size of your files. This is only a concern if you are converting to shapefiles, however.
  • By default only ways are exported. After setting exportNodes to 1 all nodes which have a name tag will be exported as well.

From here, you can use standard GIS tools to conver the GML to something more useful:

ogr2ogr planet.shp planet.gml

And from there, you can set it up in whatever you want: qgis, for example, will now display it.

Output GML file from May Planet.osm (produced by v.0.4 API script) --