X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=makefile;h=b47b5f20252c06a8264aac783393c76c6bc324ff;hb=f4d0245a7e3e026779ef7baa997b8793737f327e;hp=f3136a6cda47c33afcb4d13b3d9a8cde272ae5ff;hpb=84d512e317cac78d67bec5c64fcf761d3f5e0689;p=dead%2Fcensus-tools.git diff --git a/makefile b/makefile index f3136a6..b47b5f2 100644 --- a/makefile +++ b/makefile @@ -1,9 +1,156 @@ -.PHONY : test +DB_NAME=census +DB_USER=postgres +TIGER_SRID=4269 +SHAPELY_URL=http://pypi.python.org/packages/source/S/Shapely/Shapely-1.0.14.tar.gz + + +# Starting with PostGIS 1.4.0, these paths are calculated at install +# time using the pg_config utility. Rather than try to guess where +# PostGIS will wind up installed, we can just check the output of +# pg_config ourselves. +PG_BINDIR=`pg_config --bindir` +PG_SHAREDIR=`pg_config --sharedir` + +# Necessary to run test/data without prerequisites. +# +.PHONY : test data lib + + +# The default task, since it comes first in the list. +# +all: clean lib test -all: clean test test: ./bin/run_tests + +# Download or check out any third-party libraries. +lib: + if [ ! -d lib/Shapely ]; then \ + wget -O shapely.tar.gz $(SHAPELY_URL); \ + tar -xvzf shapely.tar.gz -C lib/ ; \ + rm shapely.tar.gz; \ + mv lib/Shapely* lib/Shapely; \ + fi; + + +# Remove byte-compiled python code. +# clean: find ./ -name '*.pyc' -print0 | xargs -0 rm -f + + +data: + bin/download_data + + +# This imports the Tiger data using shp2pgsql. The shapefiles +# should exist, since this task depends on the "data" task, which +# downloads said shapefiles. +# +# After the TIGER import is done, we use the sf1blocks2sql script to +# parse and import the geographic header record information. +# +db: data newdb tiger_blocks_table tiger_lines_table sf1_blocks_table +# All Blocks +# +# The table already exists, so we can append to it, and we don't have +# to create the GiST index. + for state in data/census2000/*; do \ + $(PG_BINDIR)/shp2pgsql \ + -a \ + -s $(TIGER_SRID) \ + -D \ + $$state/blocks/*.shp \ + tiger_blocks \ + | psql -U $(DB_USER) -d $(DB_NAME); \ + done; + +# All Lines +# +# Since the table and index already exist, we can utilize -a, +# and leave -I out. + for state in data/census2000/*; do \ + for shapefile in $$state/lines/*.shp; do \ + echo "Importing $$shapefile."; \ + $(PG_BINDIR)/shp2pgsql \ + -a \ + -s $(TIGER_SRID) \ + $$shapefile \ + tiger_lines \ + | bin/filter-transactions \ + | psql -U $(DB_USER) -d $(DB_NAME) \ + > /dev/null; \ + done; \ + done; + + bin/sf1blocks2sql src/Tests/Fixtures/SummaryFile1/mdgeo.uf1 sf1_blocks \ + | psql -U postgres -d $(DB_NAME) \ + > /dev/null + + + +# First, we drop and re-create the DB_NAME database (or schema, +# whatever). Then, we add PL/pgSQL support to the database. +# +# At that point, we import the two PostGIS files, postgis.sql and +# spatial_ref_sys.sql. The postgis.sql file contains the geometry +# functions, while spatial_ref_sys.sql contains a table of SRIDs, and +# their associated properties. PostGIS requires both. +# +newdb: +# Ignore the result of dropdb when it fails. + dropdb -U $(DB_USER) $(DB_NAME) || true + createdb -U $(DB_USER) $(DB_NAME) + createlang -U $(DB_USER) plpgsql $(DB_NAME) + + psql -d $(DB_NAME) \ + -U $(DB_USER) \ + -f $(PG_SHAREDIR)/contrib/postgis.sql \ + > /dev/null + + psql -d $(DB_NAME) \ + -U $(DB_USER) \ + -f $(PG_SHAREDIR)/contrib/spatial_ref_sys.sql \ + > /dev/null + + +# This just runs the SQL script to create the sf1_blocks table. +sf1_blocks_table: + psql -d $(DB_NAME) \ + -U $(DB_USER) \ + -f sql/create-sf1_blocks-table.sql \ + > /dev/null + + +# Create the tiger_blocks table, and create its GiST index. Having the +# table already exist makes importing via shp2pgsql much easier. +# Any blocks file will work as an argument. +tiger_blocks_table: + $(PG_BINDIR)/shp2pgsql \ + -p \ + -I \ + -s $(TIGER_SRID) \ + data/census2000/maryland/blocks/tl_2009_24_tabblock00.shp \ + tiger_blocks \ + | psql -U postgres -d $(DB_NAME) \ + > /dev/null + + +# Prepare the tiger_lines table, and create the GiST index on its +# geometry column. Any lines shapefile will do here. +tiger_lines_table: + $(PG_BINDIR)/shp2pgsql \ + -p \ + -I \ + -s $(TIGER_SRID) \ + data/census2000/maryland/lines/tl_2009_24510_edges.shp \ + tiger_lines \ + | psql -U postgres -d $(DB_NAME) \ + > /dev/null + +# Add a unique index on the "tlid" column. + psql -U postgres \ + -d census \ + -f sql/create_tlid_unique_index.sql