wiki:FAQVector

Version 5 (modified by warmerdam, 13 years ago) (diff)

add splitting geometry types into separate shapefiles example

FAQ - Vector

  1. How can I merge hundreds of shapefiles?
  2. How do I translate a mixed geometry file to shapefile format?

How can I merge hundreds of shapefiles?

Here's a bash script to bulk load a directory of shapefiles that have the same schema to postgis. It could obviously be made smarter, but it seems to do the trick.

#!/bin/bash

# let OGR create a table from one of the files
ogr2ogr -f Postgresql PG:"host=smoke.hobu.net" -a_srs "EPSG:26915" -nln outputlayer first_input_shape.shp -overwrite -nlt POLYGON

# delete all the data in the table we just created (but don't delete the table)
ogrinfo PG:"host=smoke.hobu.net" -sql "delete  from outputlayer"

# loop through all of the shapefiles in the directory and load them
for i in $(ls *.shp); do
  ogr2ogr -f Postgresql PG:"host=smoke.hobu.net" -a_srs " EPSG:26915" -nln outputlayer $i -update -append -skipfailures
done 

This Windows cmd shell example merges multiple *wetlands*shapefiles in the current directory to a single merged\wetlands.shp (double % to put in a script, %f --> %%f):

mkdir merged
for %f in (*wetland*.shp) do (
  if not exist merged\wetlands.shp (
      ogr2ogr -f "esri shapefile" merged\wetlands.shp %f) else (
      ogr2ogr -f "esri shapefile" -update -append merged\wetlands.shp %f -nln Wetlands )
)

The trick is to use the first input to create a new shapefile, and thereafter only update and append. See the end of http://www.gdal.org/ogr/drv_shapefile.html

If you don't need to specify a human friendly name via -nln, using -append by itself is simpler:

 for %f in (dir1\*.shp dir2\*.shp) do (ogr2ogr -f "esri shapefile" -append merged %f)

How do I translate a mixed geometry file to shapefile format?

Some formats (such as ESRI Shapefiles) only allow one type of geometry in a layer, while other formats (such as DGN, MapInfo?, GML) allow a mixture of geometry types within a single layer. Direct attempts to translate result in errors like this:

% ogr2ogr out.shp mixed.dgn 
ERROR 1: Attempt to write non-linestring (POLYGON) geometry to ARC type shapefile.
ERROR 1: Terminating translation prematurely after failed
translation of layer elements

The first step in dealing with such a problem is to discover what geometry types exist in the source file. For a DGN file called mixed.dgn, with a layer called elements this can be accomplished using the following OGR SQL command (see OGR SQL tutorial for details):

% ogrinfo -ro mixed.dgn -sql 'select distinct ogr_geometry from elements'
INFO: Open of `mixed.dgn'
      using driver `DGN' successful.

Layer name: elements
Geometry: Unknown (any)
Feature Count: 1
Layer SRS WKT:
(unknown)
ogr_geometry: String (0.0)
OGRFeature(elements):0
  ogr_geometry (String) = LINESTRING

OGRFeature(elements):1
  ogr_geometry (String) = POLYGON

OGRFeature(elements):2
  ogr_geometry (String) = POINT

This file has point, line and polygon geometries. Each will need to be translated to a separate output file.

% ogr2ogr out_point.shp mixed.dgn -where 'ogr_geometry = "POINT"'
% ogr2ogr out_line.shp mixed.dgn -where 'ogr_geometry = "LINESTRING"'
% ogr2ogr out_poly.shp mixed.dgn -where 'ogr_geometry = "POLYGON"'