Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 112 lines (93 sloc) 3.16 KB
#!/bin/bash
if [[ -z $TOPOSM_ENV_SET ]]; then
echo "Error: TopOSM environment not set."
exit 1
fi
SETUPCMD="shp2pgsql -p -I -g way -W latin1"
IMPORTCMD="shp2pgsql -a -g way -W latin1"
DBCMD="psql -q -p $DB_PORT $DB_NAME $DB_USER"
TEMPDIR="temp"
SRS900913="+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs +over"
TEMPFILE="$TEMPDIR/temp.shp"
# NOTE: The data type for fcode and ftype is changed because, for some
# reason, mapnik (silently) fails to filter on bigint columns.
ExtractMajorFeatures() {
TABLESUFFIX=$1
CONDITION="$2"
SRCTABLE="${NHD_TABLE_PREFIX}_${TABLESUFFIX}"
DSTTABLE="${SRCTABLE}_major"
echo "Extracting major features in $SRCTABLE..."
echo "INSERT INTO $DSTTABLE SELECT * FROM $SRCTABLE WHERE $CONDITION" | $DBCMD
}
UpdateColumnDatatypes() {
TABLESUFFIX=$1
TABLE="${NHD_TABLE_PREFIX}_${TABLESUFFIX}"
echo "Updating column datatypes in $TABLE..."
echo "ALTER TABLE $TABLE ALTER COLUMN fcode TYPE INT, ALTER COLUMN ftype TYPE INT" | $DBCMD
}
CreateFcodeIndex() {
TABLESUFFIX=$1
TABLE="${NHD_TABLE_PREFIX}_${TABLESUFFIX}"
INDEXNAME="${TABLE}_fcode"
echo "Creating index $INDEXNAME..."
echo "CREATE INDEX $INDEXNAME ON $TABLE (fcode)" | $DBCMD
}
ImportShapefiles() {
SHAPEFILEPREFIX=$1
TABLESUFFIX=$2
CREATE_MAJOR=$3
echo "Importing ${SHAPEFILEPREFIX} shapefiles..."
PREP_TABLE=1
for SRCFILE in $NHD_DIR/${SHAPEFILEPREFIX}*.shp; do
echo "Reprojecting $SRCFILE and applying bbox..."
ogr2ogr -t_srs "$SRS900913" \
-spat $XMINLL $YMINLL $XMAXLL $YMAXLL \
"$TEMPFILE" "$SRCFILE"
# set up table(s)
if [ "$PREP_TABLE" ]; then
echo "Creating ${NHD_TABLE_PREFIX}_area tables..."
$SETUPCMD "$TEMPFILE" ${NHD_TABLE_PREFIX}_${TABLESUFFIX} | $DBCMD
if [ ! -z $CREATE_MAJOR ]; then
$SETUPCMD "$TEMPFILE" ${NHD_TABLE_PREFIX}_${TABLESUFFIX}_major | $DBCMD
fi
unset PREP_TABLE
fi
# import data
echo "Importing $SRCFILE..."
$IMPORTCMD "$TEMPFILE" ${NHD_TABLE_PREFIX}_${TABLESUFFIX} | $DBCMD
rm "$TEMPFILE"
done
}
# Ensure that our temp directory exists
mkdir -pv "$TEMPDIR"
rm -f $TEMPDIR/temp.{dbf,prj,shp,shx}
echo;echo "##### Area ####"
ImportShapefiles "nhdarhi" "area" 1
ExtractMajorFeatures "area" "areasqkm > 1"
UpdateColumnDatatypes "area"
UpdateColumnDatatypes "area_major"
CreateFcodeIndex "area"
CreateFcodeIndex "area_major"
echo;echo "##### Flowline ####"
ImportShapefiles "nhdflh" "flowline" 1
ExtractMajorFeatures "flowline" "lengthkm > 2"
UpdateColumnDatatypes "flowline"
UpdateColumnDatatypes "flowline_major"
CreateFcodeIndex "flowline"
CreateFcodeIndex "flowline_major"
echo;echo "##### Point ####"
ImportShapefiles "nhdpthi" "point"
UpdateColumnDatatypes "point"
CreateFcodeIndex "point"
echo;echo "##### Waterbody ####"
ImportShapefiles "nhdwbhi" "waterbody" 1
ExtractMajorFeatures "waterbody" "areasqkm > 1"
UpdateColumnDatatypes "waterbody"
UpdateColumnDatatypes "waterbody_major"
CreateFcodeIndex "waterbody"
CreateFcodeIndex "waterbody_major"
echo;echo "##### Line ####"
ImportShapefiles "nhdlihi" "line"
UpdateColumnDatatypes "line"
CreateFcodeIndex "line"
echo "Done."