| 24 | |
| 25 | *Note if you get an import error from postgis similiar to "ERROR: new row for relation "layer1" violates check constraint" you should try substituting "-nlt GEOMETRY" for your GEOMETRY TYPE (in the above example it is POLYGON). This will avoid errors if your shapefile includes both Polygon and Multipolygon geometries, for example. See this thread http://postgis.refractions.net/pipermail/postgis-users/2006-June/012495.html |
| 26 | |
| 27 | These Unix shell steps illustrate how to batch merge multiple shapefiles into a single shapefile using OGR. This assumes you have downloaded a group of shapefiles in .zip format which are all the same type of data but just need to be combined. This works on a Mac but has not been test on other versions of UNIX: |
| 28 | |
| 29 | {{{ |
| 30 | #Make a new directory called "tmp" and a sub directory called "merged" |
| 31 | mkdir tmp |
| 32 | mkdir tmp/merged |
| 33 | |
| 34 | #copy all zipped files to the "tmp" directory and then "cd" into it |
| 35 | cp *.zip tmp |
| 36 | cd tmp |
| 37 | |
| 38 | #unzip all the .zip archives |
| 39 | find . -name "*.zip" -exec unzip '{}' \; |
| 40 | |
| 41 | #delete all .zip archives |
| 42 | rm *.zip |
| 43 | |
| 44 | #move a single shapefile (and the cooresponded .shx, .dbf, etc files) to the "merged" directory (exchange 'myshape*' for the name of one of your shapefiles keeping the '*' at the end of the name) |
| 45 | find . -name 'myshape*' -exec mv '{}' merged \; |
| 46 | |
| 47 | #Batch merge all the remaining shapefiles from the tmp dir into the copied file in the merge dir (exchange 'myshape' for the name of the copied shapefile) |
| 48 | for i in $(ls *.shp); do ogr2ogr -f 'ESRI Shapefile' -update -append merged $i -nln myshape |
| 49 | done |
| 50 | |
| 51 | }}} |
| 52 | |