forked from degauss-org/geocoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiger_import
executable file
·66 lines (61 loc) · 2.06 KB
/
tiger_import
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
#!/bin/bash
TMP="/tmp/tiger-import.$$"
SHPS="edges"
DBFS="featnames addr"
BASE=$(dirname $0)
PATH=$PATH:$BASE
SQL="$BASE/sql"
HELPER_LIB="$BASE/../lib/geocoder/us/sqlite3.so"
SHP2SQLITE=src/shp2sqlite/shp2sqlite
DATABASE=$1
SOURCE=$2
shift
shift
mkdir -p $TMP || exit 1
# Initialize the database if it doesn't exist.
# Added places back in after adding the "drop if exists" directive to the SQL file. theduckylittle, 2011/12/15
[ ! -r $DATABASE ] && cat ${SQL}/{create,place}.sql | sqlite3 $DATABASE
#[ ! -r $DATABASE ] && cat ${SQL}/create.sql | sqlite3 $DATABASE
# Marshal the county directories to import.
#
# If no directory was given on the command-line, read a list of county IDs from STDIN.
if [ x"$1" != x"" ]; then
cat
else
# Otherwise, find all of the IDs from the contents of the directory structure.
ls $SOURCE/tl_*_edges.zip | while read file; do
file=$(basename $file)
code=${file##tl_????_}
echo ${code%%_edges.zip}
done
fi | sort | while read code; do
echo "--- $code"
# Unpack the county files into the temp directory.
for file in $SHPS $DBFS; do
ZIP=$(ls $SOURCE/*_${code}_${file}.zip 2>/dev/null)
SHP=$(ls $SOURCE/*_${code}_${file}.* 2>/dev/null)
if [ x"$ZIP" != x"" ]; then
unzip -q $ZIP -d $TMP
elif [ x"$SHP" != x"" ]; then
ln -s $SHP $TMP
fi
done
# Generate an SQL stream to feed into the sqlite3 binary.
# Start by loading the helper libs and initializing the temporary tables
# that will hold the TIGER data before ETL.
(echo ".load $HELPER_LIB" && \
cat ${SQL}/setup.sql && \
for file in $SHPS; do
# Convert each Shapefile into SQL statements.
${SHP2SQLITE} -aS ${TMP}/*_${file}.shp tiger_${file}
done && \
for file in $DBFS; do
# Convert each DBF into SQL statements likewise.
shp2sqlite -an ${TMP}/*_${file}.dbf tiger_${file}
done && \
cat ${SQL}/convert.sql) | sqlite3 $DATABASE
# Finally, do the transform/load phase (convert.sql)
# and clean up the temporary files.
rm -f $TMP/*
done 2>&1 | tee import-$$.log
rm -rf $TMP