For ages, people have been asking me to help them with shapefile to OSM conversion, because I wrote one of the scripts that got used a lot for different conversion projects. Since I get a fair amount of email on this, I figured it was worth blogging that I’ve actually put together a newer script from scratch that does something similar, though for Polygons instead of lines.

One of the benefits of this script is that it was written ahead of time with the intention of sharing it — which was never meant for the other script that I wrote. This means that it is slightly more readable; at least, the unreadable parts are better separated. (I will admit that there are several aspects of it that are terribly un-Pythonic.)

You can find the code in OSM’s shp2osm directory.

Some aspects of this code:

  • It is designed to help you create .osm files you can read/merge in JOSM, so it has the ability to do vertical striping across a dataset in order to create geographically ordered smaller datasets.
  • It has an option to limit the number of objects per .osm file; this defaults to 50,000, which in some cases was about JOSM’s limit. (In others, it seemed lower; it’s adjustable via a command line switch.)
  • Uses optparse, which means –help does help you (once you read the initial docstring and get yourself started.)
  • Supports both direct tag mappings (shapefile-attr -> OSM-attr) as well as custom functions to add more tags based on multiple attributes of a feature. It was built for the MassGis OpenSpace Layer upload (which is now in progress), so it needed more advanced tagging possibilities.
  • Supports saving the original shapefile data ‘automatically’, to create the possibility of recreating the original shapefile attribtes. These attributes are namespaced so as to minimize collisions.

The script was also used to convert the MassGIS buildings layer, which means that there are now building outlines for metro Boston slowly appearing on the map.

If you have polygon data, this tool may be helpful to you. If you don’t have polygon data, this tool also may be helpful to you, as a better demonstration of how to map shapefiles into OSM data without writing all the code yourself.

I’m not likely to be doing a lot of support for this, but I wanted to let people know, because I personally think the code is much much more readable than the last shapefile conversion tool I wrote. (Also, it’s not every day I get to collaborate on OSM with Tim Berners-Lee.)

2 Responses to “polyshp2osm”

  1. joe Says:

    thank you very much for this Christopher!

  2. Can Kavaklıoğlu Says:

    Works nicely in my case as well, thanks Christopher. Custom tag editing is really cool.