#!/bin/bash
#
# tigerimport.sh - (c) 2005 Schuyler Erle <schuyler@geocoder.us>
#   Last updated Fri Mar  4 08:13:28 PST 2005
#
# tigerimport.sh imports the CompleteChain, Landmarks, AreaLandMarks, and
# Polygon layers from a TIGER/Line .ZIP file into a PostGIS database. You
# must have ogr2ogr from GDAL (http://gdal.org), as well as the GDAL
# Python extensions.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You can download a copy of the GPL from http://www.gnu.org/copyleft/gpl.html,
# or write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA  02111-1307, USA.

ZIP=$1
FILE=$(basename $ZIP)
FILE=${FILE#tgr}
FILE=${FILE%.zip}
TIGER=TGR$FILE.RT1

SRID=4269
SRS="-a_srs '+init=epsg:$SRID'"
POST="$POST -lco DIM=2"

unzip $ZIP 

echo Converting CompleteChain, etc...
ogr2ogr $PRE $OGROPTS $SRS CompleteChain.shp $TIGER \
    CompleteChain ZipCodes EntityNames PolygonEconomic $POST

echo Converting Polygons....
python tigerpoly2.py $TIGER

echo Making indexes...
for layer in *.shp; do shptree $layer; done

echo Cleaning up file system...
rm -f tmp_poly_$$.{shp,shx,dbf,prj} TGR$FILE.{RT*,MET}

mkdir $FILE
mv *.{shp,shx,dbf,prj,qix} $FILE
