OSM2SHP Tutorial: Step-by-Step Export of OSM Data to SHP

Convert OSM2SHP: Quick Guide to Turning OpenStreetMap into ShapefilesOpenStreetMap (OSM) is a rich, community-driven source of geospatial data — roads, buildings, land use, points of interest, and more. Shapefiles (SHP) remain one of the most widely used vector formats in desktop GIS and many geospatial workflows. This guide shows how to use OSM2SHP to convert OSM data into shapefiles, covering installation, workflow options, practical tips, and troubleshooting. Examples assume a basic familiarity with command-line tools and GIS concepts.


What is OSM2SHP?

OSM2SHP is a converter designed to transform OpenStreetMap data (typically in .osm or .pbf formats) into ESRI Shapefile format (.shp, .shx, .dbf, .prj). It extracts OSM primitives — nodes, ways, and relations — and maps them to point, line, and polygon shapefiles according to tags and geometry. Different implementations exist (standalone scripts, utilities bundled in larger toolkits, or plugins), so behavior can vary; this guide focuses on common patterns and practical steps applicable to many implementations.


When to use OSM2SHP

  • You need to import OSM data into desktop GIS (QGIS, ArcGIS) that prefers shapefiles.
  • You’re building legacy workflows or tools that require Shapefile format.
  • You want a straightforward export of specific feature types (roads, buildings, landuse) for mapping or analysis.
  • You need offline access to OSM-derived vector layers.

Alternatives to OSM2SHP (brief)

If you need more flexible or modern workflows, consider:

  • ogr2ogr (GDAL/OGR) — reads OSM via the OSM driver and writes many formats including Shapefile.
  • Osmosis — powerful OSM processing, can export to shapefiles with plugins or intermediate formats.
  • QGIS — directly loads OSM data and exports layers to Shapefile.
  • osmconvert + osmfilter — for preprocessing before conversion.

Installation and prerequisites

Depending on the specific OSM2SHP tool you use, prerequisites vary. Common requirements:

  • Python 3 (for Python-based scripts)
  • Java (for some tools)
  • GDAL/OGR (useful for writing shapefiles or reprojection)
  • unzip/wget/curl for downloading OSM extracts
  • Enough disk space and memory for your target area (planet extracts are large)

On Linux/macOS you can install GDAL via package managers:

  • Debian/Ubuntu: sudo apt install gdal-bin python3-gdal
  • macOS (Homebrew): brew install gdal

If using a Python-based OSM2SHP, create a virtual environment and pip install required packages (check the tool’s README).


Workflow overview

  1. Obtain OSM data (extract).
  2. Optionally filter/preprocess (by bounding box, tags, or type).
  3. Convert to shapefile(s) with OSM2SHP.
  4. Reproject and clean attribute schema as needed.
  5. Load into GIS or use in analysis.

Step 1 — Getting OSM data

You can obtain OSM data in several ways:

  • Download a regional extract in .pbf or .osm from providers (Geofabrik, BBBike).
  • Use the Overpass API for smaller, tag-specific queries.
  • Use osmium-tool or osmconvert to clip or convert larger extracts.

Example: download a city extract (PBF) from Geofabrik using curl or your browser.


Large datasets benefit from preprocessing:

  • Clip to a smaller bounding box with osmium or osmconvert.
  • Filter by tags with osmfilter or Overpass queries to reduce size (e.g., only highways and buildings).
  • Simplify geometry if high detail isn’t needed.

Example osmium clip:

osmium extract -b left,lower,right,upper input.osm.pbf -o clipped.osm.pbf 

Step 3 — Converting with OSM2SHP

Usage varies by implementation, but typical options include:

  • Specify input (.osm or .pbf).
  • Choose output directory for shapefiles.
  • Select which feature types/tags to export (points, lines, polygons).
  • Optionally supply a translation or mapping file that maps OSM tags to shapefile attribute fields.

Basic command (example syntax):

osm2shp -i input.osm.pbf -o output_folder --layers points,lines,polygons 

If your OSM2SHP supports a mapping file, use it to control attributes and classification, for example mapping OSM tag “highway” to a shapefile field “road_type”.

If OSM2SHP cannot write a .prj, create one using GDAL or manually set the projection (most OSM data is in WGS84 / EPSG:4326).


Step 4 — Reprojection and attribute cleanup

Most OSM files use EPSG:4326. Many GIS projects use a projected CRS (e.g., Web Mercator EPSG:3857 or a local UTM). Reproject shapefiles with ogr2ogr:

ogr2ogr -f "ESRI Shapefile" -t_srs EPSG:3857 output_reprojected.shp input.shp 

Check attribute types in the DBF. Shapefiles have limitations (field name length, data types). Use ogr2ogr or your GIS to rename fields, convert types, and split or join attributes.


Handling common OSM features

  • Roads: usually exported as lines. Keep name, highway, surface, lanes tags. Consider splitting motorways vs residential via mapping rules.
  • Buildings: exported as polygons. Tag cleaning helps remove multipolygons without building attributes.
  • Points of interest (POIs): nodes converted to point shapefiles. Tags like amenity, shop, tourism are important.
  • Relations (multipolygons): ensure your converter supports relations to build proper polygons.

Performance tips

  • Work in PBF format when possible — smaller and faster than XML .osm.
  • Filter early to avoid processing unnecessary data.
  • Use tools like osmium and osmconvert for efficient preprocessing.
  • For large areas, process by tiles/regions and merge outputs if needed.

Troubleshooting

  • Missing polygons: check whether relations were processed; some converters require multipolygon support enabled.
  • Attribute truncation: shapefile DBF limits field name length to 10 characters; use a mapping file to control names.
  • Encoding issues: ensure your DBF uses UTF-8 or convert using ogr2ogr with proper encoding options.
  • Large files failing to open: split into smaller tiles or use GeoPackage (GPKG) instead of Shapefile for large datasets.

Example end-to-end (concise)

  1. Download city.pbf from Geofabrik.
  2. Clip area (if needed): osmium extract -b … city.pbf -o city_clip.pbf
  3. Convert: osm2shp -i city_clip.pbf -o city_shp –layers lines,polygons,points
  4. Reproject: ogr2ogr -f “ESRI Shapefile” -t_srs EPSG:3857 city_shp_3857.shp city_shp.shp
  5. Load into QGIS, inspect attributes, and fix field names as needed.

When to prefer other formats

Shapefile has age-related limitations (field name length, size limits, multi-file complexity). For modern workflows prefer GeoPackage (.gpkg) or GeoJSON when possible. OSM2SHP is appropriate when you need legacy compatibility.


Resources and next steps

  • Check your specific OSM2SHP implementation’s README for exact CLI flags and mapping file syntax.
  • Learn osmium/osmconvert for efficient preprocessing.
  • Use ogr2ogr for reprojection and format conversion to overcome shapefile limitations.

Converting OSM to shapefiles is straightforward once you have a repeatable workflow: get the right extract, filter what you don’t need, run the converter with a mapping file for clean attributes, then reproject and tidy in your GIS.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *