-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnmt_manager.py
726 lines (607 loc) · 26.6 KB
/
nmt_manager.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
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
import arcpy
import math
import os
# exceptionally, in this script, the comments are in Polish
# warstwy ASCII TBD
# http://www.gugik.gov.pl/pzgik/zamow-dane/dane-pomiarowe
# 1. p - punkty siatki
# 2. j - obszary planarne
# 3. c - cieki
# 4. k - punkty (koty) wysokościowe
# 5. o - obiekty inżynieryjne
# 6. pz - punkty na obszarach wydzielonych
# 7. s - linie nieciągłości
# 8. sz - linie nieciągłości w obszarach wydzieleń
# 9. z - obszary wydzielone (o obniżonej dokładności np. lasy)
POINT_SIGNS = ['p', 't', 'pz', 'k']
LINE_SIGNS = ['w', 's', 'c', 'sz', 'j', 'z', 'o']
# in-memory fc names
MEM_WORKSPACE = "in_memory" # new "memory" workspace error
MEM_NMT_POINTS_TEMP ="nmtPointsTemp"
MEM_NMT_LINES_TEMP = "nmtLinesTemp"
MEM_NMT_ENVELOPES_TEMP = "nmtEnvelopesTemp"
MEM_NMT_LINES_TEMP_CLIP = "nmtLinesTempClip"
MEM_NMT_POINTS = "nmtPoints"
MEM_NMT_LINES = "nmtLines"
MEM_NMT_ENVELOPES = "nmtEnvelopes"
MEM_NMT_INDEX_AREA = "nmtIndexArea"
MEM_CLIP_AREA = "clipArea"
# layer names
LYR_NMT_POINTS_TEMP = "nmtPointsTempLyr"
LYR_INDEX_AREA = "nmtIndexAreaLyr"
LYR_NMT_POINTS = "nmtPointsLyr"
LYR_NMT_LINES = "nmtLineslinesLyr"
# fields
FLD_INDEX_UNIFIED = "GODLO_UNIFIED"
FLD_INDEX = "GODLO"
FLD_LAYER = "WARSTWA"
FLD_WARNINGS = "UWAGI"
FLD_SHAPE_XY = "SHAPE@XY"
FLD_SHAPE_Z = "SHAPE@Z"
FLD_SHAPE = "SHAPE@"
# PUWG92
EPSG_102173 = 102173
EPSG_2180 = 2180
ENVELOPE_EDGE_BUFFER = "1 Meters"
EPSILON = 0.005
SEPARATOR_LENGTH = 80
SEPARATOR_SIGN = "="
# message types
MSG_MESSAGE = "MESSAGE"
MSG_WARNING = "WARNING"
MSG_ERROR = "ERROR"
OUT_POINTS = "points"
OUT_LINES = "lines"
OUT_ENVELOPES = "envelopes"
OUT_BRIDGES = "bridges"
OUT_RAW_POINTS = "raw_points"
OUT_RAW_LINES = "raw_lines"
def add_arcpy_message(message, separator=False, type=MSG_MESSAGE):
if separator:
add_message_separator(SEPARATOR_SIGN)
type_up = type.upper()
if type_up in (MSG_MESSAGE, "MSG", "M"):
arcpy.AddMessage(message)
elif type_up in (MSG_WARNING, "WRN", "W"):
arcpy.AddWarning(message)
elif type_up in (MSG_ERROR, "ERR", "E"):
arcpy.AddError(message)
def generate_separator(separator):
return separator * SEPARATOR_LENGTH
def add_message_separator(separator):
arcpy.AddMessage(generate_separator(separator))
def get_asc_files_dict(tbd_folder):
# old version: ascFiles = [f for f in os.listdir(tbd_folder) if f.lower().endswith('.asc')]
asc_files_dict = {}
for root, _, files in os.walk(tbd_folder):
for file in files:
if(file.lower().endswith(".asc")):
file_no_extension = file.split(".")[0]
asc_files_dict[file_no_extension] = os.path.join(root, file)
return asc_files_dict
class Extent:
'''A class represent an object extent'''
def __init__(self):
self.reset()
def is_empty(self):
return math.isnan(self.x_min) or math.isnan(self.y_min)
def reset(self):
self.x_min = float('NaN')
self.y_min = float('NaN')
self.x_max = float('NaN')
self.y_max = float('NaN')
def get_line_type(self):
if abs(self.x_max - self.x_min) < EPSILON:
return "all_vertical"
elif abs(self.y_max - self.y_min) < EPSILON:
return "all_horizontal"
else:
return ""
def update(self, x, y):
if math.isnan(self.x_min) or math.isnan(self.y_min):
self.x_min = self.x_max = x
self.y_min = self.y_max = y
else:
if x < self.x_min:
self.x_min = x
if x > self.x_max:
self.x_max = x
if y < self.y_min:
self.y_min = y
if y > self.y_max:
self.y_max = y
def parse_xyz_line(line):
floats = [float(x) for x in line.split()]
# x/y w plikach ascii maja odwrocona kolejnosc
#? wyprowadzic jako parametr odwrocenie XY
x = floats[1]
y = floats[0]
z = floats[2]
return x, y, z
def set_arcpy_environment(workspace, spatial_ref):
arcpy.env.overwriteOutput = True
arcpy.env.workspace = workspace
arcpy.env.cartographicCoordinateSystem = spatial_ref
def get_out_fc_names(workspace, prefix):
workspace_desc = arcpy.Describe(workspace)
if workspace_desc.dataType == "Folder":
ext = ".shp"
else:
ext = ""
out_fc_names = {}
out_fc_names[OUT_POINTS] = f"{prefix}_NMT_punkty{ext}"
out_fc_names[OUT_LINES] = f"{prefix}_NMT_linie{ext}"
out_fc_names[OUT_ENVELOPES] = f"{prefix}_NMT_obrysy{ext}"
out_fc_names[OUT_BRIDGES] = f"{prefix}_NMT_obiekty{ext}"
out_fc_names[OUT_RAW_POINTS] = f"{prefix}_punkty_surowe{ext}"
out_fc_names[OUT_RAW_LINES] = f"{prefix}_linie_surowe{ext}"
return out_fc_names
def delete_existing_fc(out_fc_names):
add_arcpy_message(f"Usuwanie istniejących plików...", True)
for fc in out_fc_names.values():
if arcpy.Exists(fc):
add_arcpy_message(f"usuwanie: {fc}")
arcpy.Delete_management(fc)
def get_indexes_set(asc_files_dict):
"""Gets set of unique indexes"""
indexes = set([asc_file_name.split('_')[0] for asc_file_name in asc_files_dict.keys()])
message = "Odnalezione godla:\n"
message += "\n".join(indexes)
add_arcpy_message(message, separator=True)
return indexes
def unify_index(index):
"""
Upraszczanie i unifikowanie godla:
"N-33-132-A-c-2-2" do postaci "n33132ac22"
Dodatkowo przycinanie prefixu "NMT-"
"""
index_unified = (("").join(index.split("-"))).lower()
if index_unified.startswith("nmt"):
index_unified = index_unified.split("nmt")[1]
return index_unified
def get_index_key (index, sign):
return f"{index}_{sign}"
def get_line_type(array):
extent = Extent()
[extent.update(point.X, point.Y) for point in array]
return extent.get_line_type()
def delete_lines_on_envelopes(nmt_lines_fc, nmt_envelopes_fc, spatial_ref_92):
"""
Usuwanie lini wzdluz krawedzi obrysow:
* self-intersect na skorowidzach, aby otrzymac linie podzialu ("MEM_NMT_ENELOPES_SELF_INTERSECT")
* agregacja linii ("nmtEnvelopesSelfInterDis") ?? nieporzebne
* bufor z tych linii o wielkosci 1 m ("MEM_NMT_ENELOPES_SELF_INTERSECT_BUF_DIS")
* selekcja przestrzenna typu INTERSECT linii przecinajacych sie z buforem
* zapis selekcji do nowego zbioru ("MEM_NMT_LINES_ON_ENVELOPES")
* usuniecie selekcji z oryginalu
* rozbicie wyodrebnionych polilinii na linie (MEM_NMT_LINES_ON_ENVELOPES_SPLIT)
* selekcja linii spoza bufora i zwrocenie ich do zbioru glownego linii ("splitted")
"""
MEM_NMT_ENELOPES_SELF_INTERSECT = "nmtEnvelopesSelfIntersect"
MEM_NMT_ENELOPES_SELF_INTERSECT_BUF_DIS = "nmtEnvelopesSelfIntersectBufDis"
MEM_NMT_LINES_ON_ENVELOPES = "nmtLinesOnEnvelopes"
MEM_NMT_LINES_ON_ENVELOPES_SPLIT = "nmtLinesOnEnvelopesSplit"
LYR_NMT_LINES_ON_ENVELOPES_SPLIT = "nmtLinesOnEnvelopesSplitLyr"
nmt_lines_on_envelopes_fc = f"{MEM_WORKSPACE}\\{MEM_NMT_LINES_ON_ENVELOPES}"
nmt_lines_on_envelopes_split_fc = f"{MEM_WORKSPACE}\\{MEM_NMT_LINES_ON_ENVELOPES_SPLIT}"
add_arcpy_message("Usuwanie linii wzdłuż krawędzi skorowidzów...", True)
arcpy.Intersect_analysis(
nmt_envelopes_fc,
f"{MEM_WORKSPACE}\\{MEM_NMT_ENELOPES_SELF_INTERSECT}",
"ONLY_FID", "#", "LINE")
arcpy.Buffer_analysis(
f"{MEM_WORKSPACE}\\{MEM_NMT_ENELOPES_SELF_INTERSECT}",
f"{MEM_WORKSPACE}\\{MEM_NMT_ENELOPES_SELF_INTERSECT_BUF_DIS}",
ENVELOPE_EDGE_BUFFER,
"FULL", "ROUND", "ALL", "#")
arcpy.SelectLayerByLocation_management(
LYR_NMT_LINES,
"INTERSECT",
f"{MEM_WORKSPACE}\\{MEM_NMT_ENELOPES_SELF_INTERSECT_BUF_DIS}")
arcpy.Select_analysis(LYR_NMT_LINES, nmt_lines_on_envelopes_fc)
# usun zaznaczenie
arcpy.DeleteFeatures_management(LYR_NMT_LINES)
arcpy.CreateFeatureclass_management(
MEM_WORKSPACE,
MEM_NMT_LINES_ON_ENVELOPES_SPLIT,
"POLYLINE",
nmt_lines_on_envelopes_fc,
"DISABLED",
"ENABLED",
spatial_ref_92)
insert_cursor = arcpy.da.InsertCursor(
nmt_lines_on_envelopes_split_fc,
[FLD_SHAPE, FLD_LAYER, FLD_INDEX, FLD_WARNINGS])
with arcpy.da.SearchCursor(
nmt_lines_on_envelopes_fc,
[FLD_SHAPE, FLD_LAYER, FLD_INDEX, FLD_WARNINGS]) as search_cursor:
for row in search_cursor:
sign = row[1]
index = row[2]
partnum = 0
# Step through each part of the feature
for part in row[0]:
# Step through each vertex in the feature
prevX = None
prevY = None
prevZ = None
for pnt in part:
if pnt:
if prevX:
line_array = arcpy.Array(
[arcpy.Point(prevX, prevY, prevZ), arcpy.Point(pnt.X, pnt.Y, pnt.Z)])
line = arcpy.Polyline(line_array, spatial_ref_92, True)
insert_cursor.insertRow((line, sign, index, 'splitted'))
prevX = pnt.X
prevY = pnt.Y
prevZ = pnt.Z
else:
pass
partnum += 1
del insert_cursor
# --------------------------------------------------------------------------------
arcpy.MakeFeatureLayer_management(
nmt_lines_on_envelopes_split_fc,
LYR_NMT_LINES_ON_ENVELOPES_SPLIT)
arcpy.SelectLayerByLocation_management(
LYR_NMT_LINES_ON_ENVELOPES_SPLIT,
'WITHIN',
f'{MEM_WORKSPACE}\\{MEM_NMT_ENELOPES_SELF_INTERSECT_BUF_DIS}')
arcpy.SelectLayerByLocation_management(
LYR_NMT_LINES_ON_ENVELOPES_SPLIT,
'INTERSECT',
f'{MEM_WORKSPACE}\\{MEM_NMT_ENELOPES_SELF_INTERSECT_BUF_DIS}',
'#', 'SWITCH_SELECTION')
arcpy.Append_management([LYR_NMT_LINES_ON_ENVELOPES_SPLIT], nmt_lines_fc, "NO_TEST")
return
def extract_shp_from_tbd(
tbd_folder_in,
workspace_out,
prefix_out,
spatial_ref_out,
clip_area_in,
nmt_index_area_in,
import_points,
import_lines,
export_raw_data):
# initialization
set_arcpy_environment(workspace_out, spatial_ref_out)
out_fc_names = get_out_fc_names(workspace_out, prefix_out)
delete_existing_fc(out_fc_names)
#? get from index area fc
spatial_ref_92 = arcpy.SpatialReference(EPSG_2180)
# paths to in-memory feature classes
nmt_points_temp_fc = f"{MEM_WORKSPACE}\\{MEM_NMT_POINTS_TEMP}"
nmt_lines_temp_fc = f"{MEM_WORKSPACE}\\{MEM_NMT_LINES_TEMP}"
nmt_envelopes_temp_fc = f"{MEM_WORKSPACE}\\{MEM_NMT_ENVELOPES_TEMP}"
nmt_lines_temp_clip_fc = f"{MEM_WORKSPACE}\\{MEM_NMT_LINES_TEMP_CLIP}"
# nmtBridgesTempFC = r'in_memory\nmtBridgesTemp'
nmt_points_fc = f"{MEM_WORKSPACE}\\{MEM_NMT_POINTS}"
nmt_lines_fc = f"{MEM_WORKSPACE}\\{MEM_NMT_LINES}"
nmt_envelopes_fc = f"{MEM_WORKSPACE}\\{MEM_NMT_ENVELOPES}"
nmt_index_area_fc = f"{MEM_WORKSPACE}\\{MEM_NMT_INDEX_AREA}"
clip_area_fc = f"{MEM_WORKSPACE}\\{MEM_CLIP_AREA}"
# nmtPolylinesIntersectFC = r'in_memory\nmtLinesIntersect'
# Przygotowywanie zbiorów w pamieci oraz odpowiadajacych im warstw
# CLIP AREA
arcpy.CopyFeatures_management(clip_area_in, clip_area_fc)
# NMT INDEX AREA
arcpy.CopyFeatures_management(nmt_index_area_in, nmt_index_area_fc)
# aby wyszkuiwac po godle nalezy stworzyc ujednolicony jego format:
# * bez myslnikow
# * male litery
# * tylko indels godla, bez przedrostka "nmt"
arcpy.AddField_management(nmt_index_area_fc, FLD_INDEX_UNIFIED, 'TEXT', '#', '#', 15)
code = """def unify_symbol(symbol):
symbol_unified = (("").join(symbol.split("-"))).lower()
if symbol_unified.startswith("nmt"):
symbol_unified = symbol_unified.split("nmt")
return symbol_unified"""
arcpy.CalculateField_management(
nmt_index_area_fc,
FLD_INDEX_UNIFIED,
f"unify_symbol(!{FLD_INDEX}!)",
"PYTHON3",
code)
# Tworzenie tymczasowych zbiorow w pamieci do przechowywania danych z aktualnie przetwarzanego godla
# NMT POINTS TEMP
arcpy.CreateFeatureclass_management(
MEM_WORKSPACE,
MEM_NMT_POINTS_TEMP,
'POINT',
'#',
'DISABLED',
'ENABLED',
spatial_ref_92)
arcpy.AddField_management(nmt_points_temp_fc, FLD_LAYER, 'TEXT', '#', '#', 2)
arcpy.AddField_management(nmt_points_temp_fc, FLD_INDEX, 'TEXT', '#', '#', 15)
# NMT LINES TEMP
arcpy.CreateFeatureclass_management(
MEM_WORKSPACE,
MEM_NMT_LINES_TEMP,
'POLYLINE',
'#',
'DISABLED',
'ENABLED',
spatial_ref_92)
arcpy.AddField_management(nmt_lines_temp_fc, FLD_LAYER, 'TEXT', '#', '#', 2)
arcpy.AddField_management(nmt_lines_temp_fc, FLD_INDEX, 'TEXT', '#', '#', 15)
arcpy.AddField_management(nmt_lines_temp_fc, FLD_WARNINGS, 'TEXT', '#', '#', 15)
# NMT ENVELOPES TEMP
arcpy.CreateFeatureclass_management(
MEM_WORKSPACE,
MEM_NMT_ENVELOPES_TEMP,
'POLYGON',
'#',
'DISABLED',
'ENABLED',
spatial_ref_92)
arcpy.AddField_management(nmt_envelopes_temp_fc, FLD_INDEX, 'TEXT', '#', '#', 15)
arcpy.AddField_management(nmt_envelopes_temp_fc, FLD_INDEX_UNIFIED, 'TEXT', '#', '#', 15)
# Tworzenie zbiorow w pamieci do przwchowywania wszystkich danych
# szablon z tymczasowych TEMP
# NMT POINTS
arcpy.CreateFeatureclass_management(
MEM_WORKSPACE,
MEM_NMT_POINTS,
'POINT',
nmt_points_temp_fc,
'DISABLED',
'ENABLED',
spatial_ref_92)
# NMT LINES
arcpy.CreateFeatureclass_management(
MEM_WORKSPACE,
MEM_NMT_LINES,
'POLYLINE',
nmt_lines_temp_fc,
'DISABLED',
'ENABLED',
spatial_ref_92)
# NMT ENVELOPES
arcpy.CreateFeatureclass_management(
MEM_WORKSPACE,
MEM_NMT_ENVELOPES,
'POLYGON',
nmt_envelopes_temp_fc,
'DISABLED',
'ENABLED',
spatial_ref_92)
# RAW DATA
if export_raw_data:
arcpy.CreateFeatureclass_management(
workspace_out,
out_fc_names[OUT_RAW_POINTS],
'POINT',
nmt_points_temp_fc,
'DISABLED',
'ENABLED',
spatial_ref_92)
arcpy.CreateFeatureclass_management(
workspace_out,
out_fc_names[OUT_RAW_LINES],
'POLYLINE',
nmt_lines_temp_fc,
'DISABLED',
'ENABLED',
spatial_ref_92)
# Tworzenie warstw
arcpy.MakeFeatureLayer_management(nmt_index_area_fc, LYR_INDEX_AREA)
arcpy.MakeFeatureLayer_management(nmt_points_fc, LYR_NMT_POINTS)
arcpy.MakeFeatureLayer_management(nmt_lines_fc, LYR_NMT_LINES)
# Tworzenie kursorow do zbiorow tymczasowych
points_temp_cursor = arcpy.da.InsertCursor(
nmt_points_temp_fc,
[FLD_SHAPE_XY, FLD_SHAPE_Z, FLD_LAYER, FLD_INDEX])
lines_temp_cursor = arcpy.da.InsertCursor(
nmt_lines_temp_fc,
[FLD_SHAPE, FLD_LAYER, FLD_INDEX, FLD_WARNINGS])
# envelopeCursor = arcpy.da.InsertCursor(nmtEnvelopesTempFC, [FIELD_SHAPE, FIELD_INDEX])
# słownik nazw plikow oraz ich sciezek
asc_files_dict = get_asc_files_dict(tbd_folder_in)
# ze wszystkich plikow .ASC wybieramy tylko unikalne godla
indexes = get_indexes_set(asc_files_dict)
extent = Extent()
arcpy_array = arcpy.Array()
index_count = len(indexes)
for i, index in enumerate(indexes):
add_arcpy_message(f"Przetwarzanie godla {i+1} z {index_count}: {index}...", True)
index_unified = unify_index(index)
extent.reset()
# ---------------------------------------------------------------------
# PUNKTY
# ---------------------------------------------------------------------
if import_points:
add_arcpy_message("Przetwarzanie punktów...", True)
for point_sign in POINT_SIGNS:
index_key = get_index_key(index, point_sign)
if index_key not in asc_files_dict:
continue
point_asc_file_path = asc_files_dict[index_key]
if os.path.exists(point_asc_file_path):
point_asc_file = open(point_asc_file_path, "r")
for asc_line in point_asc_file.readlines():
try: # try nie pogarsza wydajnosci
x, y, z = parse_xyz_line(asc_line)
points_temp_cursor.insertRow(((x, y), z, point_sign, index_unified))
# okreslanie min/max do obrysu
extent.update(x, y)
except:
exc_message = f"{point_asc_file_path} - napotkano na blad konwersji linii: {asc_line}"
add_arcpy_message(exc_message, message=MSG_ERROR)
add_arcpy_message(point_asc_file_path + ' - przekonwertowano')
else:
add_arcpy_message(point_asc_file_path + ' - nie odnaleziono', message=MSG_WARNING)
# ---------------------------------------------------------------------
# POLILINIE i POLIGONY
# ---------------------------------------------------------------------
# poligony importowane jako polilinie ze wzgledu na poziome i pionowe linie oddzielajace arkusze
# linie poziome i pionowe oddzielane w celu poniejszego odfiltrowania
if import_lines:
add_arcpy_message("Przetwarzanie polilini...", separator=True)
for line_sign in LINE_SIGNS:
index_key = get_index_key(index, line_sign)
if index_key not in asc_files_dict:
continue
line_asc_file_path = asc_files_dict[index_key]
if os.path.exists(line_asc_file_path):
line_asc_file = open(line_asc_file_path, "r")
for iLine, asc_line in enumerate(line_asc_file.readlines()):
try:
if "Start" in asc_line:
# nowy obiekt liniowy
arcpy_array.removeAll()
# extent.reset()
elif "End" in asc_line:
# koniec obiektu
# aby utworzyc polilinie potrzeba wiecej niz 1 wierzcholek
if arcpy_array.count < 2:
continue
# check if extent is horizontal, vertical or none
line_type = get_line_type(arcpy_array)
if line_type:
add_arcpy_message(
f"{line_asc_file_path} - odnaleziona horyzontalna lub wertykalna linia (linia nr {iLine+1})",
type=MSG_WARNING
)
lines_temp_cursor.insertRow(
(arcpy.Polyline(arcpy_array, spatial_ref_92, True),
line_sign,
index_unified,
line_type))
elif '\n' == asc_line:
continue
else:
x, y, z = parse_xyz_line(asc_line)
arcpy_array.add(arcpy.Point(x, y, z))
extent.update(x, y)
except:
exc_message = f"{line_asc_file_path} - napotkano na blad w linii {iLine+1}: {asc_line}"
add_arcpy_message(exc_message, type=MSG_ERROR)
add_arcpy_message(f"{line_asc_file_path} - przekonwertowano")
else:
add_arcpy_message(f"{line_asc_file_path} - nie odnaleziono", message=MSG_WARNING)
# sprawdzamy czy dla danego godla/pliku sa dane
if extent.is_empty():
continue
# ------------------------------------------------------------------------------
add_arcpy_message("Dodawanie danych z aktualnego godla do zbioru danych...", separator=True)
# przerzucanie danych do zbiorow wlasciwych
select_query = f""""{FLD_INDEX_UNIFIED}" = '{index_unified}'"""
arcpy.SelectLayerByAttribute_management(LYR_INDEX_AREA, "NEW_SELECTION", select_query)
arcpy.Append_management([LYR_INDEX_AREA], nmt_envelopes_temp_fc, 'NO_TEST')
# usuwanie duplikatow ze zbioru
# wybieranie danych wewnatrz obrysu skorowidzu
if import_points:
add_arcpy_message('# punkty...', separator=False)
arcpy.MakeFeatureLayer_management(nmt_points_temp_fc, LYR_NMT_POINTS_TEMP)
arcpy.SelectLayerByLocation_management( LYR_NMT_POINTS_TEMP, 'INTERSECT', LYR_INDEX_AREA)
arcpy.Append_management([LYR_NMT_POINTS_TEMP], nmt_points_fc)
if import_lines:
add_arcpy_message('# linie...', separator=False)
# usuwanie linii-duplikatow ze zbioru
arcpy.Clip_analysis(nmt_lines_temp_fc, LYR_INDEX_AREA, nmt_lines_temp_clip_fc)
arcpy.Append_management([nmt_lines_temp_clip_fc], nmt_lines_fc)
if export_raw_data:
if import_points: arcpy.Append_management([nmt_points_temp_fc], out_fc_names[OUT_RAW_POINTS])
if import_lines: arcpy.Append_management([nmt_lines_temp_fc], out_fc_names[OUT_RAW_LINES])
# wyczyszczenie zbiorow tymczasowych do nastepnej kolejki
arcpy.DeleteFeatures_management(nmt_points_temp_fc)
arcpy.DeleteFeatures_management(nmt_lines_temp_fc)
arcpy.Append_management([nmt_envelopes_temp_fc], nmt_envelopes_fc)
# ------------------------------------------------------------------------------
# zwalnianie zasobów
add_arcpy_message("Zwalnianie tymczasowych zasobów...", True)
del points_temp_cursor
del lines_temp_cursor
arcpy.Delete_management(nmt_points_temp_fc)
arcpy.Delete_management(nmt_lines_temp_fc)
arcpy.Delete_management(nmt_lines_temp_clip_fc)
arcpy.Delete_management(nmt_envelopes_temp_fc)
if import_lines:
delete_lines_on_envelopes(nmt_lines_fc, nmt_envelopes_fc, spatial_ref_92)
# ostatki
add_arcpy_message("Wybieranie danych wewnatrz obszaru zaineresowania...", separator=True)
# CLIP AREA
# PUNKTY
if import_points:
add_arcpy_message('# punkty...', separator=False)
arcpy.SelectLayerByLocation_management(LYR_NMT_POINTS, "INTERSECT", clip_area_fc)
# POLILINIE
if import_lines:
add_arcpy_message('# linie...', separator=False)
arcpy.SelectLayerByLocation_management(LYR_NMT_LINES, "INTERSECT", clip_area_fc)
# nie wybieraj obiektow
arcpy.SelectLayerByAttribute_management(LYR_NMT_LINES, "SUBSET_SELECTION", f""""{FLD_LAYER}" <> 'o'""")
# If in/out CS is the same, only copy data
if spatial_ref_out.PCSCode == EPSG_2180 or spatial_ref_out.PCSCode == EPSG_102173:
add_arcpy_message('Zapisywanie do katalogu docelowego...', separator=True)
# kopiowanie do katalogu wyjsciowego
arcpy.CopyFeatures_management(nmt_envelopes_fc, out_fc_names[OUT_ENVELOPES])
# PUNKTY
if import_points:
add_arcpy_message('# punkty...', separator=False)
arcpy.CopyFeatures_management(LYR_NMT_POINTS, out_fc_names[OUT_POINTS])
# POLILINIE
if import_lines:
add_arcpy_message('# linie...', separator=False)
arcpy.CopyFeatures_management(LYR_NMT_LINES, out_fc_names[OUT_LINES])
# OBIEKTY
add_arcpy_message('Wyodrebnianie obiektow inzynieryjnych...', separator=True)
arcpy.SelectLayerByLocation_management(LYR_NMT_LINES, "INTERSECT", clip_area_fc)
arcpy.SelectLayerByAttribute_management(LYR_NMT_LINES, "SUBSET_SELECTION", f""""{FLD_LAYER}" = 'o'""")
arcpy.CopyFeatures_management(LYR_NMT_LINES, out_fc_names[OUT_BRIDGES])
# otherwise, project data
else:
add_arcpy_message('Zapisywanie do katalogu docelowego + reprojekcja...', separator=True)
arcpy.Project_management(nmt_envelopes_fc, out_fc_names[OUT_ENVELOPES], spatial_ref_out)
# PUNKTY
if import_points:
add_arcpy_message('# punkty...', separator=False)
arcpy.Project_management(LYR_NMT_POINTS, out_fc_names[OUT_POINTS], spatial_ref_out)
# POLILINIE
if import_lines:
add_arcpy_message('# linie...', separator=False)
arcpy.Project_management(LYR_NMT_LINES, out_fc_names[OUT_LINES], spatial_ref_out)
# OBIEKTY
add_arcpy_message('Wyodrebnianie obiektow inzynieryjnych...', separator=True)
arcpy.SelectLayerByLocation_management(LYR_NMT_LINES, 'INTERSECT', clip_area_fc)
arcpy.SelectLayerByAttribute_management(LYR_NMT_LINES, 'SUBSET_SELECTION', '"TYP" = \'o\'')
arcpy.Project_management(LYR_NMT_LINES, out_fc_names[OUT_BRIDGES], spatial_ref_out)
add_arcpy_message("Zakończono powodzeniem", True)
return
# This is used to execute code if the file was run but not imported
if __name__ == '__main__':
development = (arcpy.GetArgumentCount() == 0) and (arcpy.GetParameter(0) is None)
if development:
add_arcpy_message('Skrypt uruchomiony w trybie "DEVELOPMENT"', True)
tbd_folder_in = r"<TBD folder>"
workspace_out = r"<output workspace>"
prefix_out = "xEVRF_2km_pionowe_poziome"
spatial_ref_out = arcpy.SpatialReference(2180)
clip_area_in = r"<shapefile with area of interest>"
nmt_index_area_in = r"<shapefile with NMT indexes>"
import_points = True
import_lines = True
export_raw_data = True
else:
# Tool parameter accessed with GetParameter or GetParameterAsText
tbd_folder_in = arcpy.GetParameterAsText(0)
workspace_out = arcpy.GetParameterAsText(1)
prefix_out = arcpy.GetParameterAsText(2)
spatial_ref_out = arcpy.GetParameter(3)
clip_area_in = arcpy.GetParameterAsText(4)
nmt_index_area_in = arcpy.GetParameterAsText(5)
import_points = arcpy.GetParameter(6)
import_lines = arcpy.GetParameter(7)
export_raw_data = arcpy.GetParameter(8)
extract_shp_from_tbd(
tbd_folder_in,
workspace_out,
prefix_out,
spatial_ref_out,
clip_area_in,
nmt_index_area_in,
import_points,
import_lines,
export_raw_data)