From 4a492f2d6399a09b1069d963506f3d008f4ad070 Mon Sep 17 00:00:00 2001 From: Geoffrey Hing Date: Thu, 7 Nov 2013 20:46:23 -0600 Subject: [PATCH 1/2] Create database loading script for SQLite Update the load_psql.sql script to use SQLite. Unlike load_psql.sql, make the ``$DATABASE`` variable a parameter. Use a variable for the CSV filename to make commands more DRY. --- tools/sql/ire_export/load_sqlite.sh | 35 +++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 tools/sql/ire_export/load_sqlite.sh diff --git a/tools/sql/ire_export/load_sqlite.sh b/tools/sql/ire_export/load_sqlite.sh new file mode 100644 index 0000000..b6f9c87 --- /dev/null +++ b/tools/sql/ire_export/load_sqlite.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash +# usage: ./load_sqlite.sh [TABLE CODE] [STATE FIPS CODE] [SUMMARY LEVEL] [DATABASE] +# supports all 331 Census 2010 tables +# only the five summary levels supported by the IRE Census app: +# 040 State +# 050 County +# 060 County Subdivision +# 140 Census Tract +# 160 Place +# see also http://census.ire.org/data/bulkdata.html +# list of FIPS state codes at http://en.wikipedia.org/wiki/Federal_Information_Processing_Standard_state_code#FIPS_state_codes + +TABLE=$1 +STATE=$2 +SUMLEV=$3 +DATABASE=$4 +if [[ X"" = X"$DATABASE" ]]; then + DATABASE='nicar.db' +fi +LOWER_TABLE=`echo $TABLE | tr '[A-Z]' '[a-z]'` +CSV_FILENAME=all_${SUMLEV}_in_${STATE}.${TABLE}.csv + +if command -v gzcat >/dev/null; then + GZCAT=gzcat +elif command -v zcat >/dev/null; then + GZCAT=zcat +else + echo "gzcat or zcat not found" + exit 1 +fi + +echo "Loading table $TABLE for state $STATE and summary level $SUMLEV into $DATABASE" +curl https://raw.github.com/ireapps/census/master/tools/sql/ire_export/ire_${TABLE}.sql | sqlite3 $DATABASE +curl http://censusdata.ire.org/${STATE}/all_${SUMLEV}_in_${STATE}.${TABLE}.csv | $GZCAT > ${CSV_FILENAME} +echo -e ".mode csv\n.import ${CSV_FILENAME} ire_${LOWER_TABLE}\n" | sqlite3 ${DATABASE} From 2b5debf5d13fb57022c9739d1229e3dea5ba24d2 Mon Sep 17 00:00:00 2001 From: Geoffrey Hing Date: Fri, 8 Nov 2013 08:56:47 -0600 Subject: [PATCH 2/2] Use spatialite if available, strip header row from downloaded CSV --- tools/sql/ire_export/load_sqlite.sh | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/tools/sql/ire_export/load_sqlite.sh b/tools/sql/ire_export/load_sqlite.sh index b6f9c87..b832f66 100644 --- a/tools/sql/ire_export/load_sqlite.sh +++ b/tools/sql/ire_export/load_sqlite.sh @@ -21,15 +21,25 @@ LOWER_TABLE=`echo $TABLE | tr '[A-Z]' '[a-z]'` CSV_FILENAME=all_${SUMLEV}_in_${STATE}.${TABLE}.csv if command -v gzcat >/dev/null; then - GZCAT=gzcat + GZCAT_CMD=gzcat elif command -v zcat >/dev/null; then - GZCAT=zcat + GZCAT_CMD=zcat else echo "gzcat or zcat not found" exit 1 fi +if command -v spatialite >/dev/null; then + SQLITE_CMD=spatialite +elif command -v spatialite >/dev/null; then + SQLITE_CMD=sqlite3 +else + echo "spatialite or sqlite3 command not found" + exit 1 +fi + echo "Loading table $TABLE for state $STATE and summary level $SUMLEV into $DATABASE" curl https://raw.github.com/ireapps/census/master/tools/sql/ire_export/ire_${TABLE}.sql | sqlite3 $DATABASE -curl http://censusdata.ire.org/${STATE}/all_${SUMLEV}_in_${STATE}.${TABLE}.csv | $GZCAT > ${CSV_FILENAME} -echo -e ".mode csv\n.import ${CSV_FILENAME} ire_${LOWER_TABLE}\n" | sqlite3 ${DATABASE} +# Use tail to strip the header row from the file so it doesn't get imported +curl http://censusdata.ire.org/${STATE}/all_${SUMLEV}_in_${STATE}.${TABLE}.csv | $GZCAT_CMD | tail -n +2 > ${CSV_FILENAME} +echo -e ".mode csv\n.import ${CSV_FILENAME} ire_${LOWER_TABLE}\n" | ${SQLITE_CMD} ${DATABASE}