Shp2osm

From OpenStreetMap Wiki
Jump to navigation Jump to search
This article is about the shp2osm.pl and polyshp2osm.py family of conversion scripts which convert shapefiles to OSM files. For the general process regarding importing shapefiles along with alternative scripts see Import/Shapefile

exclamation mark

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. Imports/automated edits which do not follow these guidelines might be reverted!

Versions of Shp2osm and Polyshp2osm

The shp2osm and polyshp2osm scripts may be found in the OSM SVN repository. These versions are not compatible with API 0.6.

The script polyshp2osm was written by Christopher Schmidt. He describes it in a blog posting and links to the copy in the repository described above. He intended his script as an example which should be hacked for each import. There are numerous versions floating around, adapted to the requirements of specific import projects.

Dale Puch uploaded four modified versions of polyshp2osm with a readme to Filebeam http://filebeam.com/179cc73920afa0a187e76839e6ec9308. They are based off a script by Christopher Schmidt and a derivative by Ian Dees (Shp-to-osm.jar). They are not compatible with API 0.6 either.

There is a version of shp2osm which was used for the "Florida import". It does not appear to be compatible with API 0.6.

There is a script called shp_to_osm.pl. It is based on a "script by Christopher Schmidt". It is not compatible with API 0.6.

Several additional versions of polyshp2osm were created for the NHD import. They appear to have been patched to work with API 0.6.

Emilie Laffray posted her own version to the imports mailing list. It looks like it will work with API 0.6. She notes that it will break huge polygons into pieces.

Scaling the Data

Neither Shp2osm nor Polyshp2osm are able to convert coordinate systems. This must be done in a separate step before running them. Use ogr2ogr to convert the point coordinates to WGS84 (see Converting to WGS84).

You can download ogr2ogr from FWTools2.1.0 http://fwtools.maptools.org. There is a nice package of tools especially designed for newbies called "Gis on a stick" that you may want to to download from http://www.archaeogeek.com/blog/portable-gis. It is about 450 megs. If you prefer a graphical interface, you can check out ogr2gui. It works good for doing the conversion.

Most shapefile downloads will have a prj file included, which is usually needed for converting. If the specific shapefile doesn't have one included, you can try using another one from the same source with the same settings.

To run enter either of the following in the command line prompt:

ogr2ogr -t_srs WGS84 -s_srs "ESRI::in_shp.prj" "out_shp.shp" "in_shp.shp" 

or

ogr2ogr -t_srs EPSG:4326 new_file.shp old_file.shp

As long as you have the ogr2ogr package installed, you don't need to add a directory in front of ogr2ogr. You do need the relative directory for the shapefile locations. I.e. c:/maps/shapefiles/zone.shp.

To automate the process to allow mass conversion on the entire directory at once try this command from where the command was run from:

FOR /R .\ %G IN (*.prj) DON:\usbgis\apps\FWTools2.1.0\bin\ogr2ogr -t_srs WGS84 -s_srs "ESRI::%~dpnG.prj" "%~dpnG_out.shp" "%~dpnG.shp"

This outputs with the same file name but with "_out" added at the end. If you use this in a batch file, you will need to change the single '%' to '%%' - a quirk of MS batch files. You can also remove "/R .\" to only process files in the current directory.

Running shp2osm

Once the shapefiles have been converted to the WGS84 coordinate sytem, they can be converted to OSM format. Here is an MS-Windows command to run multiple files through shp2osm:

FOR /R .\ %G IN (*_out.shp) DO shp2osm.pl "%~dpnG" > "%~dpnG.osm"

It converts points, lines, and areas to OSM node/way XML, then applies the shapefile's data as tag/value pairs. Once you have that OSM data, you can use JOSM's search feature to do more work on applying OSM-centered tags.

Advice and Examples

Other Scripts

There is a new script called ogr2osm. Unlike shp2osm and polyshp2osm, it can do reprojection internally. It is compatible with API 0.6.