Skip to content
This repository
tree: 1d78285755
Fetching contributors…

Cannot retrieve contributors at this time

executable file 58 lines (54 sloc) 1.765 kb
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
#!/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"
DATABASE=$1
shift

mkdir -p $TMP || exit 1

# Initialize the database if it doesn't exist.
[ ! -r $DATABASE ] && cat ${SQL}/{create,place}.sql | sqlite3 $DATABASE
 
# Marshal the county directories to import.
#
# If no directory was given on the command-line, read a list from STDIN.
if [ x"$1" = x"" ]; then
    cat
else
    # Otherwise, find all of the contents of each state directory.
    ls -d $1/[0-9]* | while read state; do
        ls -d ${state}/[0-9]*
    done
fi | while read county; do
    echo "--- $county"
    # Unpack the county files into the temp directory.
    for file in $SHPS $DBFS; do
ZIP=$(ls ${county}/*_${file}.zip 2>/dev/null)
SHP=$(ls ${county}/*_${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
Something went wrong with that request. Please try again.