forked from unorthodox123/RacialDotMap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdotfile.py
303 lines (183 loc) · 10 KB
/
dotfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import sys
from osgeo import ogr
from shapely.wkb import loads
from shapely.geometry import *
from random import uniform
import sqlite3
# Import the module that converts spatial data between formats
sys.path.append("filepath")
from globalmaptiles import GlobalMercator
# Main function that reads the shapefile, obtains the population counts,
# creates a point object for each person by race, and exports to a SQL database.
def main(input_filename, output_filename):
# Create a GlobalMercator object for later conversions
merc = GlobalMercator()
# Open the shapefile
ds = ogr.Open(input_filename)
if ds is None:
print "Open failed.\n"
sys.exit( 1 )
# Obtain the first (and only) layer in the shapefile
lyr = ds.GetLayerByIndex(0)
lyr.ResetReading()
# Obtain the field definitions in the shapefile layer
feat_defn = lyr.GetLayerDefn()
field_defns = [feat_defn.GetFieldDefn(i) for i in range(feat_defn.GetFieldCount())]
# Obtain the index of the field for the count for whites, blacks, Asians,
# Others, and Hispanics.
for i, defn in enumerate(field_defns):
if defn.GetName() == "POP10":
pop_field = i
if defn.GetName() == "nh_white_n":
white_field = i
if defn.GetName() == "nh_black_n":
black_field = i
if defn.GetName() == "nh_asian_n":
asian_field = i
if defn.GetName() == "hispanic_n":
hispanic_field = i
if defn.GetName() == "NH_Other_n":
other_field = i
if defn.GetName() == "STATEFP10":
statefips_field = i
# Set-up the output file
conn = sqlite3.connect( output_filename )
c = conn.cursor()
c.execute( "create table if not exists people_by_race (statefips text, x text, y text, quadkey text, race_type text)" )
# Obtain the number of features (Census Blocks) in the layer
n_features = len(lyr)
# Iterate through every feature (Census Block Ploygon) in the layer,
# obtain the population counts, and create a point for each person within
# that feature.
for j, feat in enumerate( lyr ):
# Print a progress read-out for every 1000 features and export to hard disk
if j % 1000 == 0:
conn.commit()
print "%s/%s (%0.2f%%)"%(j+1,n_features,100*((j+1)/float(n_features)))
# Obtain total population, racial counts, and state fips code of the individual census block
pop = int(feat.GetField(pop_field))
white = int(feat.GetField(white_field))
black = int(feat.GetField(black_field))
asian = int(feat.GetField(asian_field))
hispanic = int(feat.GetField(hispanic_field))
other = int(feat.GetField(other_field))
statefips = feat.GetField(statefips_field)
# Obtain the OGR polygon object from the feature
geom = feat.GetGeometryRef()
if geom is None:
continue
# Convert the OGR Polygon into a Shapely Polygon
poly = loads(geom.ExportToWkb())
if poly is None:
continue
# Obtain the "boundary box" of extreme points of the polygon
bbox = poly.bounds
if not bbox:
continue
leftmost,bottommost,rightmost,topmost = bbox
# Generate a point object within the census block for every person by race
for i in range(white):
# Choose a random longitude and latitude within the boundary box
# and within the orginial ploygon of the census block
while True:
samplepoint = Point(uniform(leftmost, rightmost),uniform(bottommost, topmost))
if samplepoint is None:
break
if poly.contains(samplepoint):
break
# Convert the longitude and latitude coordinates to meters and
# a tile reference
x, y = merc.LatLonToMeters(samplepoint.y,samplepoint.x)
tx,ty = merc.MetersToTile(x, y, 21)
# Create a unique quadkey for each point object
quadkey = merc.QuadTree(tx, ty, 21)
# Create categorical variable for the race category
race_type = 'w'
# Export data to the database file
c.execute( "insert into people_by_race values (?,?,?,?,?)", (statefips, x, y, quadkey,race_type) )
for i in range(black):
# Choose a random longitude and latitude within the boundary box
# points and within the orginial ploygon of the census block
while True:
samplepoint = Point(uniform(leftmost, rightmost),uniform(bottommost, topmost))
if samplepoint is None:
break
if poly.contains(samplepoint):
break
# Convert the longitude and latitude coordinates to meters and
# a tile reference
x, y = merc.LatLonToMeters(samplepoint.y,samplepoint.x)
tx,ty = merc.MetersToTile(x, y, 21)
# Create a unique quadkey for each point object
quadkey = merc.QuadTree(tx, ty, 21)
# Create categorical variable for the race category
race_type = 'b'
# Export data to the database file
c.execute( "insert into people_by_race values (?,?,?,?,?)", (statefips, x, y, quadkey,race_type) )
for i in range(asian):
# Choose a random longitude and latitude within the boundary box
# points and within the orginial ploygon of the census block
while True:
samplepoint = Point(uniform(leftmost, rightmost),uniform(bottommost, topmost))
if samplepoint is None:
break
if poly.contains(samplepoint):
break
# Convert the longitude and latitude coordinates to meters and
# a tile reference
x, y = merc.LatLonToMeters(samplepoint.y,samplepoint.x)
tx,ty = merc.MetersToTile(x, y, 21)
# Create a unique quadkey for each point object
quadkey = merc.QuadTree(tx, ty, 21)
# Create categorical variable for the race category
race_type = 'a'
# Export data to the database file
c.execute( "insert into people_by_race values (?,?,?,?,?)", (statefips, x, y, quadkey,race_type) )
for i in range(hispanic):
# Choose a random longitude and latitude within the boundary box
# points and within the orginial ploygon of the census block
while True:
samplepoint = Point(uniform(leftmost, rightmost),uniform(bottommost, topmost))
if samplepoint is None:
break
if poly.contains(samplepoint):
break
# Convert the longitude and latitude coordinates to meters and
# a tile reference
x, y = merc.LatLonToMeters(samplepoint.y,samplepoint.x)
tx,ty = merc.MetersToTile(x, y, 21)
# Create a unique quadkey for each point object
quadkey = merc.QuadTree(tx, ty, 21)
# Create categorical variable for the race category
race_type = 'h'
# Export data to the database file
c.execute( "insert into people_by_race values (?,?,?,?,?)", (statefips, x, y, quadkey,race_type) )
for i in range(other):
# Choose a random longitude and latitude within the boundary box
# points and within the orginial ploygon of the census block
while True:
samplepoint = Point(uniform(leftmost, rightmost),uniform(bottommost, topmost))
if samplepoint is None:
break
if poly.contains(samplepoint):
break
# Convert the longitude and latitude coordinates to meters and
# a tile reference
x, y = merc.LatLonToMeters(samplepoint.y,samplepoint.x)
tx,ty = merc.MetersToTile(x, y, 21)
# Create a unique quadkey for each point object
quadkey = merc.QuadTree(tx, ty, 21)
# Create categorical variable for the race category
race_type = 'o'
# Export data to the database file
c.execute( "insert into people_by_race values (?,?,?,?,?)", (statefips, x, y, quadkey,race_type) )
conn.commit()
# Execution code...
if __name__=='__main__':
for state in ['01','02','04','05','06','08','09','10','11','12','13','15',
'16','17','18','19','20','21','22','23','24','25','26','27','28','29','30',
'31','32','33','34','35','36','37','38','39','40','41','42','44','45','46',
'47','48','49','50','51','53','54','55','56']:
print "state:%s"%state
main( ".../Census Data/statefile_"+state+".shp",
".../Map Data/people_by_race5.db")