Software comparison/Import a shapefile
This article is about importing ESRI Shapefiles into OSM. A shapefile is a popular geospatial vector data format for geographic information systems software. Shapefiles spatially describe geometries: points, polylines, and polygons.
Shapefile data should not be uploaded to OSM until consideration has been given to making the data topologically correct. If data are uploaded without reviewing for compatibility they may need to be removed to avoid other contributors having to spend many hours making them topologically correct. The mere fact that data are in shapefile format does not mean that they are of high quality (e.g. those describing the PGS coastline lack the fine resolution required by OSM). When adding shapefiles to OSM, specific care should be taken to avoid duplicate node issues.
See Import/Guidelines and Automated Edits code of conduct for more information. Imports/automated edits which do not follow these guidelines might be reverted!
Basic: Opening shapefiles in OSM editors
Basic shapefile support has been added to the main OSM editors. When opening shapefiles the attributes are converted into tags. It is highly likely that these will not be suitable for OSM, so care should be taken to remove unwanted tags and apply new tags following the tagging guidelines.
JOSM
JOSM is able to open shapefiles using the OpenData plugin. The individual files that make up a shapefile must use a common file name and be in the same directory or contained within a zip file. Simply open the .shp or .zip file using File->Open...
To add a shapefile feature to the OSM data, copy from the shapefile layer and paste into the OSM data layer. To switch between active layers use the "Layers" side pane (clicking just to the left of the visibility eye makes the layer active - green tick).
- Note: Only attempt this with shapefiles with a small file size.
Potlatch 2
It is possible to add shapefiles as a vector background layer in Potlatch 2. To pull a feature through from the background layer to the main map simply Alt-Click (Shift-Ctrl-Click in Linux). Please note the shapefile must use the WGS-84 (EPSG:4326), NAD83, or Ordnance Survey GB projection.
For step by step instructions, see Richard's guide (remember to select "shapefile" rather than "OSM").
Merkaartor
Merkaartor has support for reading and converting shapefiles.
iD
iD does not currently support shapefiles
Intermediate: Manipulating shapefiles
To make a shapefile more suitable for adding to OSM, you may first have to manipulate the file.
Filtering shapefiles
Shapefiles can be very large (over 100 MB) and include thousands of individual features. Files of this size are not suitable for the regular OSM editors and a subset should, therefore, be extracted before attempting to open in JOSM or Potlatch.
Using QGIS:
Open the shapefile in QGIS using Layer->Add Vector Layer... (the projection should be automatically detected, if not it can be set via Layer->Set CRS of Layer). Select just the features you wish to keep. This can be done in several ways:
- Select single features or features within a area using View->Select.
- Select features by their attributes (tags) using Layer->Open Attribute Table, where each row represents a individual feature.
Export the selected features using Layer->Save Selection as Vector File... (also allow export to other vector formats and different projections).
Dealing with polygons sharing the same boundary
Polygons (closed ways) that share part of their boundary with a neighbouring polygon can be broken into a net of polylines (ways). This helps when working with administrative boundaries, for example, as it enables us to import polylines between net nodes instead of polygons with overlapping boundaries.
In QGIS (with the GRASS plugin):
- Convert to GRASS Vector Map
Plugins/GRASS/New mapset. Then Plugins/GRASS/Open GRASS Tools. In a tab "Modules Tree/File management/Import vector into GRASS/" by v.in.ogr.qgis for example. In a module tab click "Show advanced options" and tick "Import area boundaries as lines". - Break Polygons at the nodes where they first diverge
In a tab "Modules Tree/Vector/Toolset for cleaning topology of vector map/v.clean.bpol" - Export new layer
Layer->Save As...
Generalization
Depending on the origin of the shape file, it may be necessary to not only convert but also simplify the shapes from the shapefile (a.k.a. "generalization"), because it has many more nodes than we can actually use.
Convert Coordinate Reference Systems
We received Shapefiles with Gauss-Krüger coordinates from our municipality which couldn't be handled by the JOSM OpenData plugin (JOSM either displayed nothing or crashed when opening). I could fix it by using QGIS to convert them to GPX files with WGS84:
- Start QGIS (I used QGIS 2.8.2 under OpenSUSE Linux 13.2)
- Go to Settings > Project Properties > CRS, enable on the fly CRS transformation
- Add the shape via Layer > Add Layer > Add Vector Layer
- Select the used coordinate reference system (in my case "DHDN / Gauss-Kruger zone 4" or "EPSG:31468" for Bavaria)
- You should see WGS84 coordinates displayed in the status line when moving around in the data
- select the layer in the layer list, choose "Save as" in context menu
- Select "Project CRS" as CRS, assure that it is WGS 84
Now you should be able to open the created files in JOSM (w/o any special plugin, btw).
Advanced: Conversion tools
There are multiple tools for converting shapefiles into .osm format suitable for importing. These include:
- Merkaartor — simply import the shp, export the osm.xml.
- shp2osm (and polyshp2osm) — require some programming skill, but very flexible
- shp-to-osm.jar — less flexible, but requires only a simple configuration file
- shape2osm — a Python script that will convert a POINT shapefile to OSM XML
- PostGIS
- convert shp to osm using grass and gpsbabel
- Load the shapefile into PostGIS first, using shp2pgsql (feature of PostGIS) May be helpful as first conversion step
- ogr2osm — a script to convert any OGR supported vector format into .osm format, with emphasis in converting polygon boundaries into relations
- Using QGIS & GPX Editor to convert to GPX
- 1) Menu "Plugins/ Manage Plugins..." enable ,,GPS Tools".
- 2) Menu "Plugins/GPS Tools/Create new GPS layer"
- 3) Select feature(s) in the default layer. Copy, paste into a GPS tracks layer, save the changed layer.
- 4) In GPX Editor open the gpx file. Menu "Edit/ OpenStreetMap Tools/ Anonymize time".
- MapCreator-Toolset: ShapeToOsmTool — This C#-OpenSource tool converts a set of shape files to an osm file, applying specific conversion rules defined in a dedicated XML conversion rule file.
- The tool is simple and flexible.
- The tool can convert many layers (represented by many shape files) to one OSM output file.
- The tool supports the following geometries: Polygon, LineString and Point. A Polygon will be mapped to a Closed OSM Way, a LineString to a OSM Way and a Point to a OSM Node.
- With certain limits the tool can convert geometry types (Polygon to Point, LineString to Point), if so wanted.
- The tool supports a flexible Tag generation. The Tag values can be constant or read out of the shape file fields.
- SHPtoOSMBoundaries python script. If the SHP contains Administrative Boundaries and you want to transform them into OSM in order to autogenerate and join the multiple boundary relations before importing.
See also
- John Reiser's Export_to_OSM.py script is an ArcToolbox script that allows ArcGIS users to save feature classes (and shapefiles) as .OSM files.
- Andy Ayre's blog describes using GPS TrackMaker to create a .osm file
- WikiProject Corine Land Cover/Corine Data Import