Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script for processing NED 1/3 arc second DEMs into contour lines
#!/bin/bash
FEET=true
DELETE_CONVERTED=true
DROP_INTERMEDIATE_TABLE=true
CLIP=true
DB=gis
USER=jesse
HOST=localhost
if $FEET ; then
TABLE=contours_feet
else
TABLE=contours
fi
POSTGRES_OPTIONS="-d $DB -U $USER -h $HOST"
if [ ! -n "$1" ]
then
echo "Usage: `basename $0` foo.tiff"
exit -1
fi
if [ ! -e $1 ]
then
echo file not found: $1
exit
fi
SOURCE=$1
echo processing $SOURCE
LAYERNAME=`basename $SOURCE .img`
LAYERNAME=`basename $LAYERNAME .tiff`
if $FEET ; then
CONVERTED="`dirname $SOURCE`/${LAYERNAME}_FEET.tiff"
if [ ! -e $CONVERTED ] ; then
echo converting $SOURCE to feet $CONVERTED
gdal_translate -scale 0 10000 0 32808.399 $SOURCE $CONVERTED
else
echo Converted file $CONVERTED already exists
fi
SOURCE=$CONVERTED
LAYERNAME=${LAYERNAME}_feet
fi
if $CLIP ; then
CLIPPED=$SOURCE.vrt
BOUNDS=`~/bin/gdal_rounded_extent.py $SOURCE`
echo clipping to $BOUNDS $CLIPPED
gdalbuildvrt -te $BOUNDS $CLIPPED $SOURCE
else
CLIPPED=$SOURCE
fi
echo contouring $CLIPPED into table $LAYERNAME
time gdal_contour -f PostgreSQL -i 40 -snodata 32767 -a height -nln $LAYERNAME -lco OVERWRITE=YES $CLIPPED "PG:host=$HOST user=$USER dbname=$DB"
echo Copying from table $LAYERNAME into table $TABLE
echo " ALTER TABLE $LAYERNAME ALTER COLUMN height type integer;
ALTER TABLE $LAYERNAME ADD COLUMN idx integer;
update $LAYERNAME set idx = 1;
update $LAYERNAME set idx = 2 where height % 80 = 0;
update $LAYERNAME set idx = 5 where height % 200 = 0;
update $LAYERNAME set idx = 10 where height % 400 = 0;
insert into $TABLE (geom, height, idx) (select st_transform(wkb_geometry, 3785), height, idx from $LAYERNAME );
" | time psql $POSTGRES_OPTIONS
if $DROP_INTERMEDIATE_TABLE ; then
psql -c "DROP TABLE $LAYERNAME " $POSTGRES_OPTIONS
fi
if $DELETE_CONVERTED ; then
if $CLIP ; then
echo removing $CLIPPED
rm $CLIPPED
fi
if $FEET; then
echo removing $SOURCE
rm $SOURCE
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.