Ogr2osm

From OpenStreetMap Wiki
Jump to: navigation, search

ogr2osm is a Python script which converts any OGR supported vector format into .osm format, with an emphasis on converting polygon boundaries into relations.

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 OpenStreetMap, (EPSG:4326).

Using a mechanism known as "translation", the script also allows the conversion of source tags and meta-data into properly formatted OpenStreetMap tags.

The current version is maintained by Paul Norman, released under a MIT license, and located at http://www.github.com/pnorman/ogr2osm. Its development and documentation are focused for users of Ubuntu >12.04 or other Debian-based system.

There are two other relatively deprecated versions of ogr2osm: 'Original/SVN' - developed in 2009-12 by Iván Sánchez Ortega (hosted in the OSM SVN) and 'UVM' by Andrew Guertin.

Dependencies

Requires the ogr module from the gdal library. (Debian/Ubuntu users will need to install the python-gdal package).

To install

git clone git://github.com/pnorman/ogr2osm.git
cd ogr2osm
git submodule update --init

To update ogr2osm:

cd ogr2osm
git pull
git submodule update

Installation instructions for older editions:

SVN Version

Download ogr2osm via SVN (or manually download at least ogr2osm.py and SimpleXMLWriter.py):

svn export http://svn.openstreetmap.org/applications/utils/import/ogr2osm/

UVM Version

Download via git

git clone git://github.com/andrewguertin/ogr2osm.git

Usage

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 file as is, however it can be supplied with a Python-based conversion file (known as a 'translation') to convert the source file's 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-file.py]

See the translations/ directory for sample translation 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 if both of them are members 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.

License

Current/Pnorman Version - MIT License

Original/SVN: "Beerware" -

 "THE BEER-WARE LICENSE":                                                   
<ivan@sanchezortega.es> wrote this file. As long as you retain this notice 
 you can do whatever you want with this stuff. If we meet some day, and you 
think this stuff is worth it, you can buy me a beer in return.             


Known issues

Large Datasets

pnorman's ogr2osm will work on very large files, given enough ram

The original version 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.

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.

Dealing with CSV files

Converting CSV files using the GDAL library is not an easy task. csv2osm is a script similar to ogr2osm to convert .csv files to the .osm format. This script allows to specify the columns containing the longitude and latitude information and to translate the columns into proper OpenStreetMap tags using a user-provided Python file, in the same way ogr2osm does.

External links