|Scripted imports and automated edits should only be carried out by those with experience and understanding of the way the OpenStreetMap community creates maps, and only with careful planning and consultation with the local community.
See Import/Guidelines and Automated Edits/Code of Conduct for more information.
ogr2osm is a Python script which converts any OGR supported vector format into .osm format, with an emphasis on converting polygon boundaries into relations. First created in 2009, the original edition, authored by Iván Sánchez Ortega, was last updated in January 2012.
Unlike most (if not all) other scripts, such as shp2osm and polyshp2osm, it can save a processing step by converting from any coordinate system supported by PROJ.4 to the system used by the OpenStreetMap database (EPSG:4326).
There are three versions of ogr2osm: The original in OSM SVN, the UVM rewrite by Andrew Guertin, and pnorman's updated version based on the UVM rewrite.
ogr module from the gdal library (From Debian/Ubuntu install package
Download ogr2osm via SVN (or manually download at least ogr2osm.py and SimpleXMLWriter.py):
Download via git
pnorman's updated version
git clone git://github.com/pnorman/ogr2osm.git cd ogr2osm git submodule update --init
cd ogr2osm git pull git submodule update
The basic usage is the same for all three versions.
Run the script without any parameters or with
--help to see the usage instructions. By default all attributes in the source file will be added to the resulting OSM.XML file as is, however it can be supplied a Python-based conversion function to convert the attributes into OSM tags. This allows the user to take full advantage of the Python language for processing attributes, such as fixing abbreviations or capitalization.
Example usage producing my-shapefile.osm, ready to be loaded (i.e. into JOSM).
python ogr2osm.py my-shapefile.shp [-t my-translation]
See the translations/ directory for sample translation control files. The translations for pnorman's version are kept in the ogr2osm-translations repo and should be automatically downloaded.
How it works
The script has three separate phases: parse, merge, and output.
During the parse phase, the script loops through all the geometries in the OGR datasource, and adds them to internal data structures. All lines and polygons get converted into a set of 2-node segments. Duplicated or re-used nodes and segments are detected and treated.
During the merge phase, 2-node segments will be merged into longer chains of nodes. Two adjacent segments or chains will be merged as one only if both of them are part of the same geometries. This algorithm will effectively detect the node chains that are the boundary lines between polygons.
After the merge phase, all lines and polygons will be sets of node chains (ways). Geometries consisting of one way will be output as ways, and geometries consisting of more will be output as relations.
"Beerware" - see source.
SVN ogr2osm fails to convert a dataset when it is very big (e.g. a 500MB shapefile). In March 2010 Ivansanchez was reported to be working on a revamped version of ogr2osm that would be much slower (10x) but would hold all the data in a SQLite database instead of in memory.
pnorman's ogr2osm will work on very large files, given enough ram
Running under Windows
This thread http://lists.osgeo.org/pipermail/gdal-dev/2009-December/022992.html highlights a problem with running ogr2osm under Microsoft Windows. It will work with the OSGeo4W shell (download) provided that python-gdal bindings are installed. An easier way to install GDAL and python bindings for running ogr2osm on Windows is to use the Windows binaries from gisinternals.com. It is enough to install GDAL core components and python bindings for the right python version by running the corresponding MSI installer packages. Some people may feel it simpler to use a virtual machine using Ubuntu then to run ogr2osm under Windows.