-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgrid-preprocess-tool.py
More file actions
432 lines (386 loc) · 15.3 KB
/
grid-preprocess-tool.py
File metadata and controls
432 lines (386 loc) · 15.3 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# grid-preprocess.py
# execute intermediate gridding steps to calculate fields for gridding
# Kytt MacManus
# February 2, 2013
# import libraries
import arcpy, os, sys
import datetime
# set counter
startTime = datetime.datetime.now()
# define inputs
##inFC = r'\\Dataserver0\gpw\GPW4\Gridding\country\inputs\gmb.gdb\gmb_admin2_boundaries_2010'
inFC = arcpy.GetParameterAsText(0)
waterExist = arcpy.GetParameterAsText(1)
# binary flag to indicate whether growth rate has been calculated
grCalc = arcpy.GetParameterAsText(2)
workspace = arcpy.GetParameterAsText(3)
# define workspace environment
arcpy.env.workspace = workspace
# define gridding resolution
# Lines per degree, determines the output resolution 120 = 30 arc-seconds resolution
# 1 degree divided into 120 parts is 30 seconds
linespd = 120
##linespd = arcpy.GetParameterAsText(1)
# parse inFC to determine rootName
rootName = os.path.basename(inFC)[:3]
# define input fishnet
inFish = rootName + "_fishnet"
# check to see that fishnet exists, if it doesn't kill the script
if not arcpy.Exists(inFish):
sys.exit("The input fishnet does not exist, check the geodatabase")
# define estimatesTable
estimatesTable = rootName + "_estimates"
# first check that the UBID field exists in both the estimates and boundaries, if not exit
if not len(arcpy.ListFields(inFC,"UBID"))==1:
sys.exit("The boundaries are missing UBID")
elif not len(arcpy.ListFields(estimatesTable,"UBID"))==1:
sys.exit("The census data is missing UBID")
else:
pass
# check to see that estimates exists, if it doesn't kill the script
if not arcpy.Exists(estimatesTable):
sys.exit("The input census estimates do not exist, check the geodatabase")
# define spatial reference
prjFile = r'\\Dataserver0\gpw\GPW4\Gridding\country\custom_projections' + os.path.sep + rootName + "_mollweide.prj"
# check to see that estimates exists, if it doesn't kill the script
if not arcpy.Exists(prjFile):
sys.exit("The input prj file does not exist, check the network")
else:
spatialRef = open(prjFile,"r").read()
#######################################################################################
# make a copy of inFC
inFCG = inFC + "_gridding"
try:
arcpy.Copy_management(inFC,inFCG)
except:
arcpy.GetMessages()
# add a tmpid field and calculate it equal to the OBJECTID
try:
tmpid = "TEMPID"
arcpy.AddField_management(inFCG,tmpid,'LONG','12')
arcpy.CalculateField_management(inFCG,tmpid,'!OBJECTID!','PYTHON')
arcpy.AddMessage("calculated " + tmpid)
except:
arcpy.GetMessages()
# project inFCG to mollweide
try:
projectFC = inFC + "_mollweide"
arcpy.Project_management(inFCG, projectFC, spatialRef)
arcpy.AddMessage("created " + projectFC)
except:
arcpy.GetMessages()
# add ADMINAREAKM and calculate
try:
adminArea = "ADMINAREAKM"
arcpy.AddField_management(projectFC,adminArea,'DOUBLE')
arcpy.CalculateField_management(projectFC,adminArea,'!shape.area@SQUAREKILOMETERS!','PYTHON')
arcpy.AddMessage("calculated " + adminArea)
except:
arcpy.GetMessages()
# join ADMINAREAKM to inFCG
try:
joinTime = datetime.datetime.now()
arcpy.JoinField_management(inFCG,tmpid,projectFC,tmpid,adminArea)
arcpy.AddMessage("joined " + adminArea + " to " + inFCG)
arcpy.AddMessage(datetime.datetime.now() - joinTime)
except:
arcpy.GetMessages()
if waterExist == "true":
# define input waterMask
waterMask = rootName + "_water_mask"
# check to see that waterMask exists, if it doesn't kill the script
if not arcpy.Exists(waterMask):
sys.exit("The input water mask does not exist, but the parameter states that it should. Verify that it should or should not")
# clip inFC to waterMask
waterFC = rootName + "_water_areas"
try:
arcpy.Clip_analysis(inFCG,waterMask,waterFC)
arcpy.AddMessage("Created " + waterFC)
except:
arcpy.GetMessages
# project waterFC to mollweide
try:
waterProjectFC = waterFC + "_mollweide"
arcpy.Project_management(waterFC, waterProjectFC, spatialRef)
arcpy.AddMessage("created " + waterProjectFC)
except:
arcpy.GetMessages()
# add ADMINWATERAREAKM and calculate
try:
adminWaterArea = "ADMINWATERAREAKM"
arcpy.AddField_management(waterProjectFC,adminWaterArea,'DOUBLE')
arcpy.CalculateField_management(waterProjectFC,adminWaterArea,'!shape.area@SQUAREKILOMETERS!','PYTHON')
arcpy.AddMessage("calculated " + adminWaterArea)
except:
arcpy.GetMessages()
# join ADMINWATERAREAKM to inFCG
try:
joinTime = datetime.datetime.now()
arcpy.JoinField_management(inFCG,tmpid,waterProjectFC,tmpid,adminWaterArea)
arcpy.AddMessage("joined " + adminWaterArea + " to " + inFCG)
arcpy.AddMessage(datetime.datetime.now() - joinTime)
except:
arcpy.GetMessages()
# Need to convert Nulls to Zeros
try:
adminWaterLYR = "adminwaterlyr"
arcpy.MakeFeatureLayer_management(inFCG,adminWaterLYR,adminWaterArea + " IS NULL")
arcpy.CalculateField_management(adminWaterLYR,adminWaterArea,0, "PYTHON")
arcpy.AddMessage("Recoded Nulls")
except:
arcpy.GetMessages()
else:
# define input waterMask
waterMask = rootName + "_water_mask"
# check to see that waterMask exists, if it doesn't kill the script
if arcpy.Exists(waterMask):
sys.exit("The input water mask exists, but the parameter states that it shouldn't. Verify that it should or should not")
# add ADMINWATERAREAKM and calculate
try:
adminWaterArea = "ADMINWATERAREAKM"
arcpy.AddField_management(inFCG,adminWaterArea,'DOUBLE')
arcpy.CalculateField_management(inFCG,adminWaterArea,0,'PYTHON')
arcpy.AddMessage("calculated " + adminWaterArea)
except:
arcpy.GetMessages()
## add ADMINAREAKMMASKED to inFCG and calculate
try:
maskedArea = "ADMINAREAKMMASKED"
arcpy.AddField_management(inFCG,maskedArea,'DOUBLE')
arcpy.CalculateField_management(inFCG,maskedArea,'!' + adminArea + '! - !' + adminWaterArea + "!",'PYTHON')
arcpy.AddMessage("calculated " + maskedArea)
except:
arcpy.GetMessages()
# Need to convert Negatives to Zeros
try:
adminMaskedLYR = "adminmaskedlyr"
arcpy.MakeFeatureLayer_management(inFCG,adminMaskedLYR,maskedArea + " < 0")
arcpy.CalculateField_management(adminMaskedLYR,maskedArea,0, "PYTHON")
arcpy.AddMessage("Recoded Negatives")
except:
arcpy.GetMessages()
# join fields from estimates table to inFCG
# must first create a list of fields and append their names to fieldList
# determine which fields to list based on whether Growth rate has been calculated
if grCalc == "true":
fieldList = []
flds = arcpy.ListFields(estimatesTable,"*E_A*")
for fld in flds:
fieldList.append(fld.name)
else:
fieldList = []
flds = arcpy.ListFields(estimatesTable,"A*2010*")
for fld in flds:
fieldList.append(fld.name)
flds2 = arcpy.ListFields(estimatesTable,"UNE_A*")
for fld2 in flds2:
fieldList.append(fld2.name)
# join estimates fields to inFCG
try:
joinTime = datetime.datetime.now()
arcpy.JoinField_management(inFCG,"UBID",estimatesTable,"UBID",fieldList)
arcpy.AddMessage("joined estimates fields to " + inFCG)
arcpy.AddMessage(datetime.datetime.now() - joinTime)
except:
arcpy.GetMessages()
### This section is adapted from calcdensities.py as written
### by Greg Yetman circa 2010
# iterate estimates fields and calculate densities
# Create a table view to avoid division by zero
tblSel = 'tblSel'
whereCls = adminArea + ' > 0'
tblSel = arcpy.MakeTableView_management(inFCG,tblSel,whereCls)
for field in fieldList:
# define density field
dsField = field + "_DS"
# define masked density field
maskedDSField = field + "_DSM"
# add density field and masked density field
try:
arcpy.AddField_management(inFCG,dsField,'DOUBLE')
arcpy.AddField_management(inFCG,maskedDSField,'DOUBLE')
except:
arcpy.GetMessages()
# do the division
exp = '!' + field + '! / !' + adminArea + '!'
exp2 = '!' + field + '! / !' + maskedArea + '!'
try:
arcpy.CalculateField_management(tblSel,dsField,exp,'PYTHON')
## print "Calculated " + dsField
arcpy.CalculateField_management(tblSel,maskedDSField,exp2,'PYTHON')
## print "Calculated " + maskedDSField
except:
arcpy.GetMessages()
# clip the fishnet to the input fc extent
clipnet = inFC + "_fishnet_clipped"
try:
arcpy.Clip_analysis(inFish,inFCG,clipnet)
arcpy.AddMessage('Clipped fishnet to ' + inFCG)
except:
arcpy.GetMessages()
# intersect fishnet and inFCG
clipnetInt = clipnet + "_intersect"
inFeatures = [inFCG,clipnet]
try:
arcpy.Intersect_analysis(inFeatures, clipnetInt)
arcpy.AddMessage('Intersected clipped fishnet and input features.')
except:
arcpy.GetMessages()
# add and calculate another unique id field called INTRSCTID
INTRSCTID = "INTRSCTID"
try:
arcpy.AddField_management(clipnetInt,INTRSCTID,'LONG')
arcpy.CalculateField_management(clipnetInt,INTRSCTID,'!OBJECTID!','PYTHON')
indexTest1 = arcpy.ListIndexes(clipnetInt,INTRSCTID + "_index")
if len(indexTest1) == 1:
pass
else:
arcpy.AddIndex_management(clipnetInt,INTRSCTID,INTRSCTID + "_index","UNIQUE")
arcpy.AddMessage("Calculated " + INTRSCTID)
except:
arcpy.GetMessages()
# project clipnetInt to mollweideCustom
try:
clipnetIntProjected = clipnetInt + '_projected'
arcpy.Project_management(clipnetInt,clipnetIntProjected,spatialRef)
indexTest2 = arcpy.ListIndexes(clipnetIntProjected,INTRSCTID + "_index")
if len(indexTest2) == 1:
pass
else:
arcpy.AddIndex_management(clipnetIntProjected,INTRSCTID,INTRSCTID + "_index","UNIQUE")
arcpy.AddMessage("Projected " + clipnetInt)
except:
arcpy.GetMessages()
# add an area field to clipnetIntProjected
featureArea = "AREAKM"
try:
arcpy.AddField_management(clipnetIntProjected,featureArea,'DOUBLE')
arcpy.CalculateField_management(clipnetIntProjected,featureArea,'!shape.area@SQUAREKILOMETERS!','PYTHON')
arcpy.AddMessage("Calculated " + featureArea)
except:
arcpy.GetMessages()
# join featureArea to clipnetInt
try:
joinTime = datetime.datetime.now()
arcpy.JoinField_management(clipnetInt,INTRSCTID,clipnetIntProjected,INTRSCTID,featureArea)
arcpy.AddMessage("Joined " + featureArea + " to " + clipnetInt)
arcpy.AddMessage(datetime.datetime.now() - joinTime)
except:
arcpy.GetMessages()
if waterExist =="true":
# clip clipnetInt to the waterMask extent
clipwatInt = inFC + "_water_mask_clipped_intersect"
try:
arcpy.Clip_analysis(clipnetInt,waterMask,clipwatInt)
arcpy.AddMessage('Clipped fishnet to ' + waterMask)
except:
arcpy.GetMessages()
# project clipwatInt to mollweideCustom
try:
clipwatIntProjected = clipwatInt + '_projected'
arcpy.Project_management(clipwatInt,clipwatIntProjected,spatialRef)
arcpy.AddMessage("Projected " + clipwatInt)
except:
arcpy.GetMessages()
# add an area field to clipnetIntProjected
waterArea = "WATERAREAKM"
try:
arcpy.AddField_management(clipwatIntProjected,waterArea,'DOUBLE')
arcpy.CalculateField_management(clipwatIntProjected,waterArea,'!shape.area@SQUAREKILOMETERS!','PYTHON')
arcpy.AddMessage("Calculated " + waterArea)
except:
arcpy.GetMessages()
# join waterArea to clipnetInt
try:
joinTime = datetime.datetime.now()
arcpy.JoinField_management(clipnetInt,INTRSCTID,clipwatIntProjected,INTRSCTID,waterArea)
arcpy.AddMessage("Joined " + waterArea + " to " + clipnetInt)
arcpy.AddMessage(datetime.datetime.now() - joinTime)
except:
arcpy.GetMessages()
# Need to convert Nulls to Zeros
try:
waterLYR = "fishnetwaterlyr"
arcpy.MakeFeatureLayer_management(clipnetInt,waterLYR,waterArea + " IS NULL")
arcpy.CalculateField_management(waterLYR,waterArea,0, "PYTHON")
arcpy.AddMessage("Recoded Nulls")
except:
arcpy.GetMessages()
else:
# add an area field to clipnetIntProjected
waterArea = "WATERAREAKM"
try:
arcpy.AddField_management(clipnetInt,waterArea,'DOUBLE')
arcpy.CalculateField_management(clipnetInt,waterArea,0,'PYTHON')
arcpy.AddMessage("Calculated " + waterArea)
except:
arcpy.GetMessages()
# add and calculate the areakmmasked field
try:
maskedFeatureArea = "AREAKMMASKED"
arcpy.AddField_management(clipnetInt,maskedFeatureArea,'DOUBLE')
arcpy.CalculateField_management(clipnetInt,maskedFeatureArea,'!' + featureArea + '! - !' + waterArea + "!",'PYTHON')
arcpy.AddMessage("calculated " + maskedFeatureArea)
except:
arcpy.GetMessages()
# Need to convert Negatives to Zeros
try:
maskedLYR = "maskedlyr"
arcpy.MakeFeatureLayer_management(clipnetInt,maskedLYR,maskedFeatureArea + " < 0")
arcpy.CalculateField_management(maskedLYR,maskedFeatureArea,0, "PYTHON")
arcpy.AddMessage("Recoded Negatives")
except:
arcpy.GetMessages()
# define cntFields list. this is a list of fields to be aggregated by adminID
cntFields = [[featureArea,'SUM'],[waterArea,'SUM'],[maskedFeatureArea,'SUM']]
joinCNTFields = ["SUM_" + featureArea, "SUM_" + waterArea, "SUM_" + maskedFeatureArea]
# iterate the fields list to calculate counts
for field in fieldList:
# define density field
dsField = field + "_DS"
# define masked density field
maskedDSField = field + "_DSM"
# define count field
cntField = field + "_CNT"
# define masked count field
maskedCNTField = field + "_CNTM"
# append to cntFields
cntFields.append([cntField,'SUM'])
cntFields.append([maskedCNTField,'SUM'])
joinCNTFields.append("SUM_" + cntField)
joinCNTFields.append("SUM_" + maskedCNTField)
# add count field and calculate
try:
arcpy.AddField_management(clipnetInt,cntField,'Double')
arcpy.AddField_management(clipnetInt,maskedCNTField,'Double')
arcpy.CalculateField_management(clipnetInt,cntField,"!" + featureArea + "! * !"
+ dsField + "!","PYTHON")
arcpy.CalculateField_management(clipnetInt,maskedCNTField,"!" + maskedFeatureArea + "! * !"
+ maskedDSField + "!","PYTHON")
arcpy.AddMessage("Calculated Population in " + cntField + " and " + maskedCNTField)
except:
arcpy.GetMessages()
# Sum proportional allocation count fields
sumTbl = inFC + "_aggregated_estimates"
pixelID = "PIXELID"
try:
arcpy.Statistics_analysis(clipnetInt,sumTbl,cntFields,pixelID)
arcpy.AddIndex_management(sumTbl,pixelID,pixelID + "_index","UNIQUE")
arcpy.AddMessage("Calculated Statistics")
except:
arcpy.GetMessages()
# join results to the original fishnet
# first check that the fishnet has an index, if not build one
if not len(arcpy.ListIndexes(inFish,"PIXELID_index"))==1:
arcpy.AddIndex_management(inFish,pixelID,pixelID + "_index","UNIQUE")
else:
pass
# next perform the join
try:
joinTime = datetime.datetime.now()
arcpy.JoinField_management(inFish,"PIXELID",sumTbl,"PIXELID",joinCNTFields)
arcpy.AddMessage("Joined Statistic Fields to " + inFish)
arcpy.AddMessage(datetime.datetime.now() - joinTime)
except:
arcpy.GetMessages()
arcpy.AddMessage(datetime.datetime.now() - startTime)