-
Notifications
You must be signed in to change notification settings - Fork 0
/
soundplan_prepare_ground.py
347 lines (298 loc) · 11 KB
/
soundplan_prepare_ground.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
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
from os import utime
import arcpy
from soundplan_prepare_buildings import FIELD_HOLES
import utils
INTEGRATE_SIZE = 0.1
TEMP_WORKSPACE = "in_memory"
FIELD_PT_XKOD = "X_KOD"
FIELD_AREA = "Area"
FIELD_LENGTH = "Length"
FIELD_PT_TERYT = "TERYT"
FIELD_G = "G"
FIELD_HOLE_ID = "HoleId"
FIELD_HOLE_EDGE_LEN = "HoleEdgeL"
FIELD_HOLE_LEN_ID = "HoleEdgeId"
FIELD_HOLES = "Holes"
FIELD_COUNTY_CODE = "JPT_KOD_JE"
GROUND_ASSIGNMENT = {
"PTWP01": 0,
"PTWP02": 0,
"PTWP03": 0,
"PTZB01": 0.6,
"PTZB02": 0.6,
"PTZB03": 0.2,
"PTZB04": 0.2,
"PTZB05": 0.2,
"PTLZ01": 1,
"PTLZ02": 1,
"PTLZ03": 1,
"PTRK01": 1,
"PTRK02": 1,
"PTUT01": 1,
"PTUT02": 1,
"PTUT03": 1,
"PTUT04": 1,
"PTUT05": 1,
"PTTR01": 1,
"PTTR02": 1,
"PTKM01": 0.3,
"PTKM02": 0.3,
"PTKM03": 0.3,
"PTKM04": 0.3,
"PTGN01": 0.5,
"PTGN02": 0.3,
"PTGN03": 0.3,
"PTGN04": 0.5,
"PTPL01": 0.2,
"PTSO01": 0.5,
"PTSO02": 0.5,
"PTWZ01": 0.2,
"PTWZ02": 0.2,
"PTNZ01": 0.2,
"PTNZ02": 0.2
}
def soundplan_prepare_ground(
workspace_bdot_in,
clip_area_in,
min_hole_area_in,
ground_out,
check_output_topology
):
# in-memory features
ground_merge_fc = f"{TEMP_WORKSPACE}\\ground_merge"
ground_merge_clip_fc = f"{TEMP_WORKSPACE}\\ground_merge_clip"
ground_merge_clip_disG_fc = f"{TEMP_WORKSPACE}\\ground_merge_clip_disG"
ground_merge_clip_intersectSelf_fc = f"{TEMP_WORKSPACE}\\ground_merge_clip_intersectSelf"
ground_merge_clip_intersectSelf_disXkod_fc = f"{TEMP_WORKSPACE}\\ground_merge_clip_intersectSelf_disXkod"
ground_merge_clip_intersectSelf_disXkod_sortArea_fc = f"{TEMP_WORKSPACE}\\ground_merge_clip_intersectSelf_disXkod_sortArea"
ground_merge_clip_errors_fc = f"{TEMP_WORKSPACE}\\ground_merge_clip_errors"
holes_fc = f"{TEMP_WORKSPACE}\\holes"
holes_single_fc = f"{TEMP_WORKSPACE}\\holes_single"
holes_single_intersect_ground_fc = f"{TEMP_WORKSPACE}\\holes_single_intersect_ground"
holes_single_intersect_ground_dis_fc = f"{TEMP_WORKSPACE}\\holes_single_intersect_ground_dis"
workspace_in_describe = arcpy.Describe(workspace_bdot_in)
if workspace_in_describe.dataType == 'FeatureDataset':
# set workspace for the use of the 'ListFeatureClasses' function
utils.set_arcpy_environment(workspace_in_describe.path, arcpy.SpatialReference(2180))
ground_fc_list = arcpy.ListFeatureClasses("*PT*A*", "Polygon", workspace_in_describe.baseName)
else:
arcpy.AddError("Not implemented yet")
return
if len(ground_fc_list) == 0:
arcpy.AddError("No PT ground files in the workspace!")
return
arcpy.AddMessage("Merging ground layers...")
arcpy.Merge_management(ground_fc_list, ground_merge_fc)
arcpy.AddMessage("Clipping ground areas...")
arcpy.PairwiseClip_analysis(ground_merge_fc, clip_area_in, ground_merge_clip_fc)
arcpy.Delete_management(ground_merge_fc)
arcpy.AddMessage("Integrating ground areas...")
arcpy.PairwiseIntegrate_analysis(ground_merge_clip_fc, INTEGRATE_SIZE)
arcpy.AddMessage("Fixing self intersections...")
arcpy.PairwiseIntersect_analysis(
ground_merge_clip_fc,
ground_merge_clip_intersectSelf_fc
)
arcpy.PairwiseDissolve_analysis(
ground_merge_clip_intersectSelf_fc,
ground_merge_clip_intersectSelf_disXkod_fc,
FIELD_PT_XKOD
)
arcpy.Delete_management(ground_merge_clip_intersectSelf_fc)
# Extract only features surrounding error features to speed-up geoprocessing operations
ground_merge_clip_lyr = utils.create_layer_name(ground_merge_clip_fc)
arcpy.MakeFeatureLayer_management(ground_merge_clip_fc, ground_merge_clip_lyr)
arcpy.SelectLayerByLocation_management(
ground_merge_clip_lyr,
"INTERSECT",
ground_merge_clip_intersectSelf_disXkod_fc
)
# save features with errors to a separate fc
arcpy.Select_analysis(ground_merge_clip_lyr, ground_merge_clip_errors_fc)
# delete them in original dataset
arcpy.DeleteFeatures_management(ground_merge_clip_lyr)
arcpy.CalculateGeometryAttributes_management(
ground_merge_clip_intersectSelf_disXkod_fc,
[[FIELD_AREA, "AREA"]]
)
arcpy.Sort_management(
ground_merge_clip_intersectSelf_disXkod_fc,
ground_merge_clip_intersectSelf_disXkod_sortArea_fc,
FIELD_AREA
)
arcpy.Delete_management(ground_merge_clip_intersectSelf_disXkod_fc)
ground_merge_clip_intersectSelf_disXkod_sortArea_lyr = utils.create_layer_name(ground_merge_clip_intersectSelf_disXkod_sortArea_fc)
arcpy.MakeFeatureLayer_management(
ground_merge_clip_intersectSelf_disXkod_sortArea_fc,
ground_merge_clip_intersectSelf_disXkod_sortArea_lyr
)
field_object_id = arcpy.Describe(ground_merge_clip_intersectSelf_disXkod_sortArea_fc).OIDFieldName
ids = utils.list_field_values(ground_merge_clip_intersectSelf_disXkod_sortArea_fc, field_object_id)
in_fc = ground_merge_clip_errors_fc
out_fc = ""
for id in ids:
select_query = f"{field_object_id} = {id}"
arcpy.SelectLayerByAttribute_management(
ground_merge_clip_intersectSelf_disXkod_sortArea_lyr,
"NEW_SELECTION",
select_query
)
out_fc = f"{ground_merge_clip_errors_fc}_{id}"
utils.update(
in_fc,
ground_merge_clip_intersectSelf_disXkod_sortArea_lyr,
out_fc
)
in_fc = out_fc
out_fc = out_fc if out_fc else ground_merge_clip_errors_fc
arcpy.Append_management(out_fc, ground_merge_clip_fc, "NO_TEST")
arcpy.AddMessage("Assigning G values...")
arcpy.AddField_management(ground_merge_clip_fc, FIELD_G, "FLOAT")
with arcpy.da.UpdateCursor(ground_merge_clip_fc, [FIELD_PT_XKOD, FIELD_G]) as cursor:
for row in cursor:
code = row[0]
if code in GROUND_ASSIGNMENT:
g = GROUND_ASSIGNMENT[code]
else:
arcpy.AddError(f"{code} does not exist in ground assignment dictionary!")
row[1] = g
cursor.updateRow(row)
arcpy.AddMessage("Dissolving polygons...")
arcpy.PairwiseDissolve_analysis(
ground_merge_clip_fc,
ground_merge_clip_disG_fc,
FIELD_G,
multi_part="SINGLE_PART" # must be singlepart to use selection by area
)
arcpy.CalculateGeometryAttributes_management(
ground_merge_clip_disG_fc,
[[FIELD_AREA, "AREA"]]
)
arcpy.AddMessage("Deleting small holes...")
ground_merge_clip_disG_lyr = utils.create_layer_name(ground_merge_clip_disG_fc)
arcpy.MakeFeatureLayer_management(ground_merge_clip_disG_fc, ground_merge_clip_disG_lyr)
arcpy.SelectLayerByAttribute_management(
ground_merge_clip_disG_lyr,
"NEW_SELECTION",
f"{FIELD_AREA} < {min_hole_area_in}"
)
arcpy.DeleteFeatures_management(ground_merge_clip_disG_lyr)
arcpy.AddMessage("Filling holes...")
arcpy.PairwiseErase_analysis(clip_area_in, ground_merge_clip_disG_fc, holes_fc)
arcpy.MultipartToSinglepart_management(holes_fc, holes_single_fc)
field_object_id = arcpy.Describe(holes_single_fc).OIDFieldName
arcpy.CalculateField_management(
holes_single_fc,
FIELD_HOLE_ID,
f"!{field_object_id}!"
)
arcpy.PairwiseIntersect_analysis(
[holes_single_fc, ground_merge_clip_disG_fc],
holes_single_intersect_ground_fc,
output_type="LINE"
)
arcpy.CalculateField_management(
holes_single_intersect_ground_fc,
FIELD_HOLE_EDGE_LEN,
"!Shape.length!*1000",
"PYTHON3",
field_type="LONG"
)
arcpy.PairwiseDissolve_analysis(
holes_single_intersect_ground_fc,
holes_single_intersect_ground_dis_fc,
FIELD_HOLE_ID,
[[FIELD_HOLE_EDGE_LEN, "MAX"]],
"SINGLE_PART"
)
# add HoleId prefix to join field to avoid incorrect assignment
arcpy.CalculateField_management(
holes_single_intersect_ground_fc,
FIELD_HOLE_LEN_ID,
f'str(!{FIELD_HOLE_ID}!) + "_" + str(!{FIELD_HOLE_EDGE_LEN}!)',
"PYTHON3",
field_type="TEXT"
)
arcpy.CalculateField_management(
holes_single_intersect_ground_dis_fc,
FIELD_HOLE_LEN_ID,
f'str(!{FIELD_HOLE_ID}!) + "_" + str(!MAX_{FIELD_HOLE_EDGE_LEN}!)',
"PYTHON3",
field_type="TEXT"
)
arcpy.JoinField_management(
holes_single_intersect_ground_dis_fc,
FIELD_HOLE_LEN_ID,
holes_single_intersect_ground_fc,
FIELD_HOLE_LEN_ID,
FIELD_G
)
arcpy.JoinField_management(
holes_single_fc,
FIELD_HOLE_ID,
holes_single_intersect_ground_dis_fc,
FIELD_HOLE_ID,
FIELD_G
)
arcpy.Append_management(
holes_single_fc,
ground_merge_clip_disG_fc,
"NO_TEST"
)
arcpy.AddMessage(f"Saving ground areas to: {ground_out}...")
arcpy.PairwiseDissolve_analysis(
ground_merge_clip_disG_fc,
ground_out,
FIELD_G,
multi_part="SINGLE_PART"
)
if check_output_topology:
arcpy.AddMessage("Checking output topology...")
# Holes test
ground_out_dis_fc = f"{TEMP_WORKSPACE}\\ground_out_dis"
arcpy.PairwiseDissolve_analysis(
ground_out,
ground_out_dis_fc
)
arcpy.CalculateGeometryAttributes_management(
ground_out_dis_fc,
[[FIELD_HOLES, "HOLE_COUNT"]]
)
holes_count = sum(utils.list_field_values(ground_out_dis_fc, FIELD_HOLES))
if holes_count:
arcpy.AddWarning(f"Output dataset has {holes_count} hole(s).")
# Self-intersection test
ground_out_intersectSelf_fc = f"{TEMP_WORKSPACE}\\ground_out_intersectSelf"
arcpy.PairwiseIntersect_analysis(
ground_out,
ground_out_intersectSelf_fc
)
intersection_count = int(arcpy.GetCount_management(ground_out_intersectSelf_fc)[0])
if intersection_count:
arcpy.AddWarning(f"Output dataset has {intersection_count} self-intersection(s).")
return
# This is used to execute code if the file was run but not imported
if __name__ == '__main__':
# Tool parameter accessed with GetParameter or GetParameterAsText
workspace_bdot_in = arcpy.GetParameterAsText(0)
if not workspace_bdot_in:
workspace_bdot_in = r"<BDOT folder>"
clip_area_in = arcpy.GetParameterAsText(1)
if not clip_area_in:
clip_area_in = r"<shapefile with area of interest>"
min_hole_area_in = arcpy.GetParameter(2)
if not min_hole_area_in:
min_hole_area_in = 500
ground_out = arcpy.GetParameterAsText(3)
if not ground_out:
ground_out = r"output feature class with ground areas>"
check_output_topology = arcpy.GetParameter(4)
if not check_output_topology:
check_output_topology = True
soundplan_prepare_ground(
workspace_bdot_in,
clip_area_in,
min_hole_area_in,
ground_out,
check_output_topology
)