]> gitweb.michael.orlitzky.com - dead/census-tools.git/blob - makefile
Added a new script, download_data, to aid in the retrieval of the census data.
[dead/census-tools.git] / makefile
1 DB_NAME=census
2 DB_USER=postgres
3 TIGER_SRID=4269
4
5 # Root folder for the shapefiles.
6 TIGER_ROOT=http://www2.census.gov/geo/tiger/TIGER2009
7
8 # State-specific folders.
9 DC_ROOT=$(TIGER_ROOT)/11_DISTRICT_OF_COLUMBIA
10 MD_ROOT=$(TIGER_ROOT)/24_MARYLAND
11 VA_ROOT=$(TIGER_ROOT)/51_VIRGINIA
12 PA_ROOT=$(TIGER_ROOT)/42_PENNSYLVANIA
13 NY_ROOT=$(TIGER_ROOT)/36_NEW_YORK
14
15 # URLs for the TIGER/Line block-level shapefiles.
16 DC_BLOCKS_URL=$(DC_ROOT)/tl_2009_11_tabblock00.zip
17 MD_BLOCKS_URL=$(MD_ROOT)/tl_2009_24_tabblock00.zip
18 VA_BLOCKS_URL=$(VA_ROOT)/tl_2009_51_tabblock00.zip
19 PA_BLOCKS_URL=$(PA_ROOT)/tl_2009_42_tabblock00.zip
20 NY_BLOCKS_URL=$(NY_ROOT)/tl_2009_36_tabblock00.zip
21
22 # Starting with PostGIS 1.4.0, these paths are calculated at install
23 # time using the pg_config utility. Rather than try to guess where
24 # PostGIS will wind up installed, we can just check the output of
25 # pg_config ourselves.
26 PG_BINDIR=`pg_config --bindir`
27 PG_SHAREDIR=`pg_config --sharedir`
28
29 # Necessary to run test/data without prerequisites.
30 #
31 .PHONY : test data
32
33
34 # The default task, since it comes first in the list.
35 #
36 all: clean test
37
38
39 test:
40 ./bin/run_tests
41
42
43 # Remove byte-compiled python code.
44 #
45 clean:
46 find ./ -name '*.pyc' -print0 | xargs -0 rm -f
47
48
49 # Download the shapefiles from Tiger if they don't already exist.
50 # For now, we're only dealing with the Census 2000 Maryland Block
51 # data, so the filenames are hard-coded. Easy enough to change.
52 #
53 data: tiger_blocks tiger_lines
54
55 tiger_blocks: dc_blocks md_blocks va_blocks pa_blocks ny_blocks
56
57 dc_blocks:
58 mkdir -p data/census2000/dc/block
59 if [ ! -f data/census2000/dc/block/tl_2009_11_tabblock00.shp ]; \
60 then \
61 wget -O dcblocks.zip $(DC_BLOCKS_URL); \
62 unzip dcblocks.zip -d ./data/census2000/dc/block; \
63 rm dcblocks.zip; \
64 fi;
65
66 md_blocks:
67 mkdir -p data/census2000/maryland/block
68 if [ ! -f data/census2000/maryland/block/tl_2009_24_tabblock00.shp ]; \
69 then \
70 wget -O mdblocks.zip $(MD_BLOCKS_URL); \
71 unzip mdblocks.zip -d ./data/census2000/maryland/block; \
72 rm mdblocks.zip; \
73 fi;
74
75 va_blocks:
76 mkdir -p data/census2000/virginia/block
77 if [ ! -f data/census2000/virginia/block/tl_2009_51_tabblock00.shp ]; \
78 then \
79 wget -O vablocks.zip $(VA_BLOCKS_URL); \
80 unzip vablocks.zip -d ./data/census2000/virginia/block; \
81 rm vablocks.zip; \
82 fi;
83
84 pa_blocks:
85 mkdir -p data/census2000/pennsylvania/block
86 if [ ! -f data/census2000/pennsylvania/block/tl_2009_42_tabblock00.shp ]; \
87 then \
88 wget -O pablocks.zip $(PA_BLOCKS_URL); \
89 unzip pablocks.zip -d ./data/census2000/pennsylvania/block; \
90 rm pablocks.zip; \
91 fi;
92
93 ny_blocks:
94 mkdir -p data/census2000/new_york/block
95 if [ ! -f data/census2000/new_york/block/tl_2009_36_tabblock00.shp ]; \
96 then \
97 wget -O nyblocks.zip $(NY_BLOCKS_URL); \
98 unzip nyblocks.zip -d ./data/census2000/new_york/block; \
99 rm nyblocks.zip; \
100 fi;
101
102
103 tiger_lines:
104 bin/download_data
105
106
107 # This imports the Tiger data using shp2pgsql. The shapefiles
108 # should exist, since this task depends on the "data" task, which
109 # downloads said shapefiles.
110 #
111 # After the TIGER import is done, we use the sf1blocks2sql script to
112 # parse and import the geographic header record information.
113 #
114 db: data newdb tiger_blocks_table tiger_lines_table sf1_blocks_table
115 # All Blocks
116 #
117 # The table already exists, so we can append to it, and we don't have
118 # to create the GiST index.
119 for state in data/census2000/*; do \
120 $(PG_BINDIR)/shp2pgsql \
121 -a \
122 -s $(TIGER_SRID) \
123 -D \
124 $$state/block/*.shp \
125 tiger_blocks \
126 | psql -U $(DB_USER) -d $(DB_NAME); \
127 done;
128
129 # MD Lines
130 #
131 # Since the table and index already exist, we can utilize -a,
132 # and leave -I out.
133 for x in data/census2000/maryland/lines/*.shp; do \
134 $(PG_BINDIR)/shp2pgsql \
135 -a \
136 -s $(TIGER_SRID) \
137 -D \
138 $$x \
139 tiger_lines \
140 | psql -U $(DB_USER) -d $(DB_NAME); \
141 done;
142
143 bin/sf1blocks2sql src/Tests/Fixtures/SummaryFile1/mdgeo.uf1 sf1_blocks \
144 | psql -U postgres -d $(DB_NAME) \
145 > /dev/null
146
147
148
149 # First, we drop and re-create the DB_NAME database (or schema,
150 # whatever). Then, we add PL/pgSQL support to the database.
151 #
152 # At that point, we import the two PostGIS files, postgis.sql and
153 # spatial_ref_sys.sql. The postgis.sql file contains the geometry
154 # functions, while spatial_ref_sys.sql contains a table of SRIDs, and
155 # their associated properties. PostGIS requires both.
156 #
157 newdb:
158 # Ignore the result of dropdb when it fails.
159 dropdb -U $(DB_USER) $(DB_NAME) || true
160 createdb -U $(DB_USER) $(DB_NAME)
161 createlang -U $(DB_USER) plpgsql $(DB_NAME)
162
163 psql -d $(DB_NAME) \
164 -U $(DB_USER) \
165 -f $(PG_SHAREDIR)/contrib/postgis.sql \
166 > /dev/null
167
168 psql -d $(DB_NAME) \
169 -U $(DB_USER) \
170 -f $(PG_SHAREDIR)/contrib/spatial_ref_sys.sql \
171 > /dev/null
172
173
174 # This just runs the SQL script to create the sf1_blocks table.
175 sf1_blocks_table:
176 psql -d $(DB_NAME) \
177 -U $(DB_USER) \
178 -f sql/create-sf1_blocks-table.sql \
179 > /dev/null
180
181
182 # Create the tiger_blocks table, and create its GiST index. Having the
183 # table already exist makes importing via shp2pgsql much easier.
184 # Any blocks file will work as an argument.
185 tiger_blocks_table:
186 $(PG_BINDIR)/shp2pgsql \
187 -p \
188 -I \
189 -s $(TIGER_SRID) \
190 data/census2000/maryland/block/tl_2009_24_tabblock00.shp \
191 tiger_blocks \
192 | psql -U postgres -d $(DB_NAME) \
193 > /dev/null
194
195
196 # Prepare the tiger_lines table, and create the GiST index on its
197 # geometry column. Any lines shapefile will do here.
198 tiger_lines_table:
199 $(PG_BINDIR)/shp2pgsql \
200 -p \
201 -I \
202 -s $(TIGER_SRID) \
203 data/census2000/maryland/lines/tl_2009_24510_edges.shp \
204 tiger_lines \
205 | psql -U postgres -d $(DB_NAME) \
206 > /dev/null