-
Notifications
You must be signed in to change notification settings - Fork 12
/
import_nhd
executable file
·111 lines (93 loc) · 3.16 KB
/
import_nhd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/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."