-
Notifications
You must be signed in to change notification settings - Fork 0
/
gridding_1k_andy.py
229 lines (163 loc) · 5.85 KB
/
gridding_1k_andy.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
# changed to no actual intersection
import os, sys, time
import arcpy
import multiprocessing
WORKER = multiprocessing.cpu_count() - 2
# WORKER = 8
# run 1 ===============
INPUTFC = r"C:\Data\wdpa_desig\scratch\national\output_fcs\merged_fcs.gdb\merge_uniq_join_0dp"
ID = "countSm"
INPUTFC2 = r"C:\Data\wdpa_desig\raw\wdpa_desig_inputs.gdb\Fishnet_1km_clip_to_Land_Moll"
ID2 = "oid@"
OUTPUT_RESULT = r"C:\Data\wdpa_desig\scratch\result2.csv"
OUTLOG = r"C:\Data\wdpa_desig\scratch\log2.log"
# run 4 ============
INPUTFC = r"C:\Data\wdpa_desig\scratch\national\output_fcs\merged_fcs.gdb\merge_uniq_join_0dp"
ID = "oid@"
INPUTFC2 = r"C:\Data\wdpa_desig\raw\wdpa_desig_inputs.gdb\grid50km_intersecting_wdpa"
ID2 = "oid@"
OUTPUT_RESULT = r"E:\Yichuan\Andy\result1.csv"
OUTLOG = r"C:\Data\wdpa_desig\scratch\log1.log"
def worker(q_input, q_output, q_log):
inputFL = 'temp_layer'
arcpy.MakeFeatureLayer_management(INPUTFC2, inputFL)
# multiprocessing worker
while True:
# wait until get an input
in_row = q_input.get()
if in_row == 'STOP':
break
# run species richness
try:
# out_rows = proto_intersect(in_row)
# out_rows = proto_intersect_mk2(in_row, q_log, inputFL)
# print result
out_rows = []
# select only those intersects. Last item geometry
arcpy.SelectLayerByLocation_management(inputFL, "INTERSECT", in_row[-1])
with arcpy.da.SearchCursor(inputFL, [ID2]) as cursor:
for row in cursor:
out_row = (in_row[0], row[0])
out_rows.append(out_row)
# put only non empty result
if out_rows:
q_output.put(out_rows)
except Exception as e:
msg = "[ERROR];Failed running analysis for IDS: {0}; {1}".format(in_row[0], e)
q_log.put(msg)
# delete layer once all done
arcpy.Delete_management(inputFL)
def worker_writer_mk2(q_output, q_log):
total_done = 0
f = open(OUTPUT_RESULT, 'w')
f.write('{0}, {1}\n'.format(ID, ID2))
f.close()
while True:
try:
out_rows = q_output.get()
# if len(out_rows) ==1 and out_rows == 'STOP':
# break
# eval directly triggers an exception
if out_rows == 'STOP':
break
# write to target
f = open(OUTPUT_RESULT, 'a')
for out_row in out_rows:
f.write('{0}, {1}\n'.format(out_row[0], out_row[1]))
f.close()
except Exception as e:
msg = '[ERROR];failed to get rows from queue: {0}'.format(out_rows)
q_log.put(msg)
pass
total_done +=1
# log
if total_done%100 == 0:
msg = '[INFO];Total groups of intersected features written: {0}'.format(total_done)
q_log.put(msg)
def worker_logger(q_log, outLog=OUTLOG):
while True:
msg = q_log.get()
# print result
if msg == 'STOP':
break
print(time.strftime("%c") + ';' + msg)
f = open(outLog, 'a')
f.write(time.strftime("%c") + ';' + msg + '\n')
f.close()
def main():
print('Total number of workers: {0}'.format(WORKER))
# pipeline for input
q_in = multiprocessing.Queue()
# pipeline for output
q_out = multiprocessing.Queue()
# log for errors
q_log = multiprocessing.Queue()
# setup and run worker processes
p_workers = list()
for i in range(WORKER):
print('Starting worker process: {0}'.format(i))
p = multiprocessing.Process(target=worker, args=(q_in, q_out, q_log))
p_workers.append(p)
# start worker process
for p in p_workers:
p.start()
# run writer and logger processes
p_w = multiprocessing.Process(target=worker_writer_mk2, args=(q_out, q_log))
p_w.start()
p_log = multiprocessing.Process(target=worker_logger, args=(q_log,))
p_log.start()
# debug
counter = 0
# solve memory issue...
with arcpy.da.SearchCursor(INPUTFC, [ID, 'shape@']) as cur:
while True:
try:
row = cur.next()
# if counter < 37000:
# if counter%10000 == 0:
# print('skipped {}'.format(counter))
# continue
if counter%100 == 0:
q_log.put('[INFO];Current features processed {}'.format(counter))
# debug
counter += 1
while row:
if q_in.qsize()<40:
q_in.put(row)
break
else:
pass
except StopIteration:
break
# except Exception as e:
# msg = '[ERROR];failed to put input into queue. '
# q_log.put(msg)
# msg = '[ROWID];{0}'.format(row[0])
# q_log.put(msg)
except RuntimeError as e:
msg = '[ERROR];failed to put input into queue. '
q_log.put(msg)
msg = '[ROWID];{0}'.format(row[0])
q_log.put(msg)
except:
msg = "Unexpected error:", sys.exc_info()[0]
print(msg)
raise
# # full read version
# with arcpy.da.SearchCursor(INPUTFC, ['oid@', 'shape@']) as cur:
# for row in cur:
# q_in.put(row)
# add stop signals to the queue: poison pill
for p in p_workers:
q_in.put('STOP')
# wait for workers to terminate
for p in p_workers:
p.join()
# add stop signal for processing result
q_out.put('STOP')
q_log.put('STOP')
# wait for the writer to finish
p_w.join()
p_log.join()
if __name__ == '__main__':
main()