]> gitweb.michael.orlitzky.com - dead/census-tools.git/blob - makefile
649eb5929a555162e43f9fa8ebb0f8965a21f45c
[dead/census-tools.git] / makefile
1 DB_NAME=census
2 DB_USER=postgres
3 TIGER_SRID=4269
4 SHAPELY_URL=http://pypi.python.org/packages/source/S/Shapely/Shapely-1.0.14.tar.gz
5
6
7 # Starting with PostGIS 1.4.0, these paths are calculated at install
8 # time using the pg_config utility. Rather than try to guess where
9 # PostGIS will wind up installed, we can just check the output of
10 # pg_config ourselves.
11 PG_BINDIR=`pg_config --bindir`
12 PG_SHAREDIR=`pg_config --sharedir`
13
14 # Necessary to run test/data without prerequisites.
15 #
16 .PHONY : test data lib
17
18
19 # The default task, since it comes first in the list.
20 #
21 all: clean lib test
22
23
24 test:
25 ./bin/run_tests
26
27
28 # Download or check out any third-party libraries.
29 lib:
30 if [ ! -d lib/Shapely ]; then \
31 wget -O shapely.tar.gz $(SHAPELY_URL); \
32 tar -xvzf shapely.tar.gz -C lib/ ; \
33 rm shapely.tar.gz; \
34 mv lib/Shapely* lib/Shapely; \
35 fi;
36
37
38 # Remove byte-compiled python code.
39 #
40 clean:
41 find ./ -name '*.pyc' -print0 | xargs -0 rm -f
42
43
44 data:
45 bin/download_data
46
47
48 # This imports the Tiger data using shp2pgsql. The shapefiles
49 # should exist, since this task depends on the "data" task, which
50 # downloads said shapefiles.
51 #
52 # After the TIGER import is done, we use the sf1blocks2sql script to
53 # parse and import the geographic header record information.
54 #
55 db: data newdb tiger_blocks_table tiger_lines_table sf1_blocks_table
56 # All Blocks
57 #
58 # The table already exists, so we can append to it, and we don't have
59 # to create the GiST index.
60 for state in data/census2000/*; do \
61 $(PG_BINDIR)/shp2pgsql \
62 -a \
63 -s $(TIGER_SRID) \
64 -D \
65 $$state/blocks/*.shp \
66 tiger_blocks \
67 | psql -U $(DB_USER) -d $(DB_NAME); \
68 done;
69
70 # All Lines
71 #
72 # Since the table and index already exist, we can utilize -a,
73 # and leave -I out.
74 for state in data/census2000/*; do \
75 for shapefile in $$state/lines/*.shp; do \
76 echo "Importing $$shapefile."; \
77 $(PG_BINDIR)/shp2pgsql \
78 -a \
79 -s $(TIGER_SRID) \
80 $$shapefile \
81 tiger_lines \
82 | bin/filter-transactions \
83 | psql -U $(DB_USER) -d $(DB_NAME) \
84 > /dev/null; \
85 done; \
86 done;
87
88 # Summary File 1
89 #
90 # Run all of the geo (uf1) files through the import script. This has
91 # to happen after the blocks import since we impose a foreign key
92 # restriction.
93 for state in data/census2000/*; do \
94 bin/sf1blocks2sql $$state/sf1/*.uf1 sf1_blocks \
95 | psql -U postgres -d $(DB_NAME) \
96 > /dev/null; \
97 done;
98
99
100
101 # First, we drop and re-create the DB_NAME database (or schema,
102 # whatever). Then, we add PL/pgSQL support to the database.
103 #
104 # At that point, we import the two PostGIS files, postgis.sql and
105 # spatial_ref_sys.sql. The postgis.sql file contains the geometry
106 # functions, while spatial_ref_sys.sql contains a table of SRIDs, and
107 # their associated properties. PostGIS requires both.
108 #
109 newdb:
110 # Ignore the result of dropdb when it fails.
111 dropdb -U $(DB_USER) $(DB_NAME) || true
112 createdb -U $(DB_USER) $(DB_NAME)
113 createlang -U $(DB_USER) plpgsql $(DB_NAME)
114
115 psql -d $(DB_NAME) \
116 -U $(DB_USER) \
117 -f $(PG_SHAREDIR)/contrib/postgis.sql \
118 > /dev/null
119
120 psql -d $(DB_NAME) \
121 -U $(DB_USER) \
122 -f $(PG_SHAREDIR)/contrib/spatial_ref_sys.sql \
123 > /dev/null
124
125
126 # This just runs the SQL script to create the sf1_blocks table.
127 sf1_blocks_table:
128 psql -d $(DB_NAME) \
129 -U $(DB_USER) \
130 -f sql/create-sf1_blocks-table.sql \
131 > /dev/null
132
133
134 # Create the tiger_blocks table, and create its GiST index. Having the
135 # table already exist makes importing via shp2pgsql much easier.
136 # Any blocks file will work as an argument.
137 tiger_blocks_table:
138 $(PG_BINDIR)/shp2pgsql \
139 -p \
140 -I \
141 -s $(TIGER_SRID) \
142 data/census2000/maryland/blocks/tl_2009_24_tabblock00.shp \
143 tiger_blocks \
144 | psql -U postgres -d $(DB_NAME) \
145 > /dev/null
146
147
148 # Prepare the tiger_lines table, and create the GiST index on its
149 # geometry column. Any lines shapefile will do here.
150 tiger_lines_table:
151 $(PG_BINDIR)/shp2pgsql \
152 -p \
153 -I \
154 -s $(TIGER_SRID) \
155 data/census2000/maryland/lines/tl_2009_24510_edges.shp \
156 tiger_lines \
157 | psql -U postgres -d $(DB_NAME) \
158 > /dev/null
159
160 # Add a unique index on the "tlid" column.
161 psql -U postgres \
162 -d census \
163 -f sql/create_tlid_unique_index.sql