#!/bin/bash
#
# addXYtoSHP dbname shpname1 shpname2

db=$1 
shp1=$2
shp2=$3
srid=$4
if [ -z $shp2 ]; then
  echo "Usage: addXYtoSHP dbname orig_shpfile final_shpfile [SRID]"
  exit
fi

if [ -z $srid ]; then
  shp2pgsql -D $shp1 addxytoshp | psql -d $db
else
  shp2pgsql -D -s $srid $shp1 addxytoshp | psql -d $db
fi

psql -d $db -c 'ALTER TABLE addxytoshp ADD COLUMN x REAL, ADD COLUMN y REAL'

psql -d $db -c 'UPDATE addxytoshp SET x = X(the_geom), y = Y(the_geom)'

pgsql2shp -f $shp2 $db addxytoshp

psql -d $db -c 'DROP TABLE addxytoshp'
