Tim's Blog

Importing SA 1:50000 CDSM data into PostGIS

Cool bash script for the day:

This one does a batch import of all Chief Directorate of Surveys and Mapping data for South Africa 1:50 000 topo data in shapefile format. It will create a seamless database ready for use in QGIS / GeoServer etc.

dropdb cdsm50k
createdb cdsm50k
createlang plpgsql cdsm50k
psql cdsm50k < /usr/share/postgresql-8.3-postgis/lwpostgis.sql
psql cdsm50k < /usr/share/postgresql-8.3-postgis/spatial_ref_sys.sql

for MYPATH in `find . -name *.shp`
do 
  FILE=`echo $MYPATH | sed 's/.[0-9]*\///g'`
  LAYER=`echo $FILE| sed 's/\_....\_...shp//g'`
  LAYER=`echo $LAYER | sed 's/^....\_//g'`
  LAYER=`echo $LAYER | sed 's/\_//g'`
  LAYER=`echo $LAYER | sed 's/[0-9]//g'`
  LAYER=`echo $LAYER | tr "[:upper:]" "[:lower:]"`
  DIR=`echo $MYPATH | sed 's/....\_.*$//g'`
  echo $MYPATH
  echo "File : $FILE" 
  echo "Layer: $LAYER"
  echo "Dir  : $DIR"
  RESULT=`echo "\d" | psql cdsm50k | grep $LAYER`
  echo "Result : $RESULT"
  if [ "$RESULT" = "" ] 
  then
    echo "Database table does not exist, creating..."
    shp2pgsql -I -s 4326 -W UTF-8 $MYPATH $LAYER \
    | psql -d cdsm50k
  else
    echo "Database table exists, appending..."
    shp2pgsql -a -s 4326 -W UTF-8 $MYPATH $LAYER \
    | psql -d cdsm50k
  fi
done

To Tumblr, Love PixelUnion