-
Notifications
You must be signed in to change notification settings - Fork 2
/
NG911_PSAP_build.py
699 lines (577 loc) · 28.3 KB
/
NG911_PSAP_build.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
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 28 14:48:42 2020
@author: eneemann
Script to build NG911 PSAP boundaries from SGID data
"""
import arcpy
import os
import time
from datetime import datetime
import pandas as pd
# Start timer and print start time in UTC
start_time = time.time()
readable_start = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
print("The script start time is {}".format(readable_start))
######################
# Set up variables #
######################
# Set up databases (SGID must be changed based on user's path)
SGID = r"C:\Users\eneemann\AppData\Roaming\ESRI\ArcGISPro\Favorites\internal@SGID@internal.agrc.utah.gov.sde"
ng911_db = r"C:\NG911\NG911_data_updates.gdb"
arcpy.env.workspace = ng911_db
arcpy.env.overwriteOutput = True
arcpy.env.qualifiedFieldNames = False
today = time.strftime("%Y%m%d")
SGID_counties = os.path.join(SGID, 'SGID.BOUNDARIES.Counties')
counties = os.path.join(ng911_db, f'SGID_counties_{today}')
SGID_munis = os.path.join(SGID, 'SGID.BOUNDARIES.Municipalities')
munis = os.path.join(ng911_db, f'SGID_munis_{today}')
unique = os.path.join(ng911_db, 'NG911_PSAP_unique_UTM')
psap_schema = os.path.join(ng911_db, 'NG911_PSAP_Schema')
psap_working = os.path.join(ng911_db, f'NG911_PSAP_bound_working_{today}')
# List of PSAPs with known "nested holes" that need to be fixed - use DsplayName
nested_list = ['Salt Lake Valley Emergency Communications Center', 'Central Utah 911']
# Read in CSV of PSAP info into pandas dataframe, use df to build dictionaries
print("Reading in CSV to get PSAP info ...")
textfile_dir = r'C:\Users\eneemann\Desktop\Python Code\NG911'
work_dir =r'C:\NG911'
csv = os.path.join(textfile_dir, 'PSAP_info.csv')
psap_info = pd.read_csv(csv)
# Create dictionary for single county PSAPs
single_county = psap_info[psap_info['Type'] == 'single county']
single_county.drop(['Key', 'Type', 'Munis'], axis=1, inplace=True)
single_county_dict = single_county.set_index('PSAP').to_dict()['Counties']
# Create dictionary for multi county PSAPs
multi_county = psap_info[psap_info['Type'] == 'multi county']
multi_county.drop(['Key', 'Type', 'Munis'], axis=1, inplace=True)
multi_county_dict = multi_county.set_index('PSAP').to_dict()['Counties']
# Create dictionary for single muni PSAPs
single_muni = psap_info[psap_info['Type'] == 'single muni']
single_muni.drop(['Key', 'Type', 'Counties'], axis=1, inplace=True)
single_muni_dict = single_muni.set_index('PSAP').to_dict()['Munis']
# Create dictionary for multi muni PSAPs
multi_muni = psap_info[psap_info['Type'] == 'multi muni']
multi_muni.drop(['Key', 'Type', 'Counties'], axis=1, inplace=True)
multi_muni_dict = multi_muni.set_index('PSAP').to_dict()['Munis']
# Create dictionary for mixed PSAPs (county & muni boundaries)
mixed = psap_info[psap_info['Type'] == 'mixed']
mixed.drop(['Key', 'Type'], axis=1, inplace=True)
mixed_county_dict = mixed.set_index('PSAP').to_dict()['Counties']
mixed_muni_dict = mixed.set_index('PSAP').to_dict()['Munis']
# Create dictionary for unique PSAPs (county & muni boundaries)
unique = psap_info[psap_info['Type'] == 'unique']
unique.drop(['Key', 'Type'], axis=1, inplace=True)
unique_county_df = unique.dropna(subset=['Counties'])
unique_muni_df = unique.dropna(subset=['Munis'])
unique_county_dict = unique_county_df.set_index('PSAP').to_dict()['Counties']
unique_muni_dict = unique_muni_df.set_index('PSAP').to_dict()['Munis']
# Read in CSV of NGUIDs for PSAPs
print("Reading in CSV to get NGUIDs ...")
# textfile_dir = r'C:\Users\eneemann\Desktop\Neemann\NG911\NG911_project'
psap_csv = os.path.join(textfile_dir, 'psap_nguid.csv')
psap_df = pd.read_csv(psap_csv)
# Create dictionary for PSAP NGUIDs
nguid_dict = psap_df.set_index('DsplayName').to_dict()['ES_NGUID']
uri_dict = psap_df.set_index('DsplayName').to_dict()['URI']
county_dict = psap_df.set_index('DsplayName').to_dict()['County']
# Set up variables for static table and feature class
sgid_data_table = os.path.join(ng911_db, 'SGID_PSAP_data_to_join_20210616')
unique = os.path.join(ng911_db, 'NG911_PSAP_unique_UTM') # Hill AFB and NN
# Set up more variables for intermediate and final feature classes
single_county_temp = os.path.join(ng911_db, 'NG911_psap_bound_sc_temp')
multi_county_temp = os.path.join(ng911_db, 'NG911_psap_bound_mc_temp')
all_county_temp = os.path.join(ng911_db, 'NG911_psap_bound_allc_temp')
mc_diss = os.path.join(ng911_db, 'NG911_psap_bound_mc_diss')
mixed_temp = os.path.join(ng911_db, 'NG911_psap_bound_mixed_temp')
mixed_diss = os.path.join(ng911_db, 'NG911_psap_bound_mixed_diss')
all_mixed_temp = os.path.join(ng911_db, 'NG911_psap_bound_allmixed_temp')
single_muni_temp = os.path.join(ng911_db, 'NG911_psap_bound_sm_temp')
multi_muni_temp = os.path.join(ng911_db, 'NG911_psap_bound_mm_temp')
mm_diss = os.path.join(ng911_db, 'NG911_psap_bound_mm_diss')
county_single_muni_temp = os.path.join(ng911_db, 'NG911_psap_bound_allc_sm_temp')
all_county_muni_temp = os.path.join(ng911_db, 'NG911_psap_bound_allcm_temp')
fixed_edges = os.path.join(ng911_db, 'NG911_PSAP_fixed_edges')
poly_fixes = os.path.join(ng911_db, 'NG911_PSAP_poly_fixes')
unique_muni_temp = os.path.join(ng911_db, 'NG911_PSAP_um_temp')
unique_muni_erased = os.path.join(ng911_db, 'NG911_PSAP_um_erased')
unique_county_temp = os.path.join(ng911_db, 'NG911_PSAP_uc_temp')
unique_county_erased = os.path.join(ng911_db, 'NG911_PSAP_uc_erased')
unique_county_muni_temp = os.path.join(ng911_db, 'NG911_psap_bound_uniquecm_temp')
unique_diss = os.path.join(ng911_db, 'NG911_PSAP_unique_diss') # Hill AFB and NN
all_unique_temp = os.path.join(ng911_db, 'NG911_PSAP_allu_temp') # add in others before cutting in
nested_temp = os.path.join(ng911_db, 'NG911_PSAP_nested_temp') # features with nested holes
nested_fixes = os.path.join(ng911_db, 'NG911_PSAP_nested_fixes') # nested holes are fixed
all_fixed_temp = os.path.join(ng911_db, 'NG911_PSAP_allfixed_temp') # combined fixed features
sgid_final = os.path.join(ng911_db, 'NG911_psap_bound_final_sgid_' + today) # create version for SGID
sgid_final_no_uris = os.path.join(ng911_db, 'NG911_psap_final_sgid_NoURIs_' + today) # create version for SGID without public URIs
psap_wgs84 = os.path.join(ng911_db, 'NG911_psap_bound_final_WGS84_' + today) # create version for NG911
fc_list = [counties, munis, single_county_temp, multi_county_temp, all_county_temp,
mc_diss, mixed_temp, mixed_diss, all_mixed_temp, single_muni_temp, multi_muni_temp,
mm_diss, county_single_muni_temp, all_county_muni_temp, unique_muni_temp,
unique_muni_erased, unique_county_temp, unique_county_muni_temp, unique_diss, all_unique_temp,
nested_temp, nested_fixes, all_fixed_temp]
for fc in fc_list:
if arcpy.Exists(fc):
print(f'Deleting {fc} ...')
arcpy.management.Delete(fc)
# Copy SGID counties and munis to local fc
arcpy.management.CopyFeatures(SGID_counties, counties)
arcpy.management.CopyFeatures(SGID_munis, munis)
# Upper case the names of the munis
# Populate fields with correct information
update_count = 0
fields = ['NAME']
with arcpy.da.UpdateCursor(munis, fields) as update_cursor:
for row in update_cursor:
row[0] = row[0].upper()
update_count += 1
update_cursor.updateRow(row)
print(f"Total count of upper case muni name updates is: {update_count}")
###############
# Functions #
###############
def add_single_county():
# Build single county PSAP boundaries from county boundaries
print("Building single county PSAPs from county boundaries ...")
arcpy.management.CopyFeatures(psap_schema, single_county_temp)
if arcpy.Exists("county_lyr"):
arcpy.management.Delete("county_lyr")
arcpy.management.MakeFeatureLayer(counties, "county_lyr")
# Field Map county name into psap schema fields
fms = arcpy.FieldMappings()
# NAME to DsplayName
fm_name = arcpy.FieldMap()
fm_name.addInputField("county_lyr", "NAME")
output = fm_name.outputField
output.name = "DsplayName"
fm_name.outputField = output
fms.addFieldMap(fm_name)
# Complete the append with field mapping and query
sc_list = list(single_county_dict.values())
print(sc_list)
sc_query = f"NAME IN ({sc_list})".replace('[', '').replace(']', '')
print(sc_query)
arcpy.management.Append("county_lyr", single_county_temp, "NO_TEST", field_mapping=fms, expression=sc_query)
# Populate fields with correct information
update_count = 0
fields = ['DsplayName']
with arcpy.da.UpdateCursor(single_county_temp, fields) as update_cursor:
print("Looping through rows in FC ...")
for row in update_cursor:
for k,v in single_county_dict.items():
if v == row[0]:
row[0] = k
update_count += 1
update_cursor.updateRow(row)
print(f"Total count of single county updates is: {update_count}")
def add_multi_county():
# Build multi county PSAP boundaries from county boundaries
print("Building multi county PSAPs from county boundaries ...")
arcpy.management.CopyFeatures(psap_schema, multi_county_temp)
if arcpy.Exists("county_lyr"):
arcpy.management.Delete("county_lyr")
arcpy.management.MakeFeatureLayer(counties, "county_lyr")
# Field Map county name into psap schema fields
fms = arcpy.FieldMappings()
# NAME to DsplayName
fm_name = arcpy.FieldMap()
fm_name.addInputField("county_lyr", "NAME")
output = fm_name.outputField
output.name = "DsplayName"
fm_name.outputField = output
fms.addFieldMap(fm_name)
# Build query to select multi county
mc_list = [ item.split(',') for item in list(multi_county_dict.values())]
mc_list = [y.strip() for x in mc_list for y in x]
print(mc_list)
mc_query = f"NAME IN ({mc_list})".replace('[', '').replace(']', '')
print(mc_query)
# Complete the append with field mapping and query to get all counties in group
arcpy.management.Append("county_lyr", multi_county_temp, "NO_TEST", field_mapping=fms, expression=mc_query)
# Loop through and populate fields with appropriate information and rename to multi county psaps
update_count = 0
fields = ['DsplayName']
with arcpy.da.UpdateCursor(multi_county_temp, fields) as update_cursor:
print("Looping through rows in FC ...")
for row in update_cursor:
for k,v in multi_county_dict.items():
# print(f'key: {k} value: {v}')
if row[0] in v:
# print(f'Found {row[0]} in {v} ...')
row[0] = k
update_count += 1
update_cursor.updateRow(row)
print(f"Total count of multi county updates is: {update_count}")
print("Dissolving multi county PSAPs ...")
arcpy.management.Dissolve(multi_county_temp, mc_diss, "DsplayName")
# Append multi county psaps into single county psaps fc
arcpy.management.CopyFeatures(single_county_temp, all_county_temp)
print("Appending multi county PSAPs with single county PSAPs ...")
arcpy.management.Append(mc_diss, all_county_temp, "NO_TEST")
def add_mixed_psaps():
# Build mixed PSAP boundaries from county and muni boundaries
print("Building mixed PSAPs from county and muni boundaries ...")
arcpy.management.CopyFeatures(psap_schema, mixed_temp)
if arcpy.Exists("county_lyr"):
arcpy.management.Delete("county_lyr")
if arcpy.Exists("muni_lyr"):
arcpy.management.Delete("muni_lyr")
arcpy.management.MakeFeatureLayer(counties, "county_lyr")
arcpy.management.MakeFeatureLayer(munis, "muni_lyr")
# Assemble counties
# Field Map county name into psap schema fields
fms = arcpy.FieldMappings()
# NAME to DsplayName
fm_name = arcpy.FieldMap()
fm_name.addInputField("county_lyr", "NAME")
output = fm_name.outputField
output.name = "DsplayName"
fm_name.outputField = output
fms.addFieldMap(fm_name)
# Build query to select multi county
mixc_list = [ item.split(',') for item in list(mixed_county_dict.values())]
mixc_list = [y.strip() for x in mixc_list for y in x]
print(mixc_list)
mixc_query = f"NAME IN ({mixc_list})".replace('[', '').replace(']', '')
print(mixc_query)
# Complete the append with field mapping and query to get all counties in group
arcpy.management.Append("county_lyr", mixed_temp, "NO_TEST", field_mapping=fms, expression=mixc_query)
# Assemble munis and append
# Field Map muni name into psap schema fields
fms = arcpy.FieldMappings()
# NAME to DsplayName
fm_name = arcpy.FieldMap()
fm_name.addInputField("muni_lyr", "NAME")
output = fm_name.outputField
output.name = "DsplayName"
fm_name.outputField = output
fms.addFieldMap(fm_name)
# Build query to select multi muni
mixm_list = [ item.split(',') for item in list(mixed_muni_dict.values())]
mixm_list = [y.strip() for x in mixm_list for y in x]
print(mixm_list)
mixm_query = f"NAME IN ({mixm_list})".replace('[', '').replace(']', '')
print(mixm_query)
# Complete the append with field mapping and query to get all counties in group
arcpy.management.Append("muni_lyr", mixed_temp, "NO_TEST", field_mapping=fms, expression=mixm_query)
# Loop through and populate fields with appropriate information and rename to mixed psaps
update_count = 0
fields = ['DsplayName']
with arcpy.da.UpdateCursor(mixed_temp, fields) as update_cursor:
print("Looping through rows in FC ...")
for row in update_cursor:
for k,v in mixed_county_dict.items():
# print(f'key: {k} value: {v}')
if row[0] in v:
# print(f'Found {row[0]} in {v} ...')
row[0] = k
for k,v in mixed_muni_dict.items():
# print(f'key: {k} value: {v}')
if row[0] in v:
# print(f'Found {row[0]} in {v} ...')
row[0] = k
update_count += 1
update_cursor.updateRow(row)
print(f"Total count of mixed PSAP updates is: {update_count}")
print("Dissolving mixed PSAPs ...")
arcpy.management.Dissolve(mixed_temp, mixed_diss, "DsplayName")
# Drop in mixed psaps via erase/append
print("Adding mixed PSAPs into working psaps layer ...")
# Erase
arcpy.analysis.Erase(all_county_temp, mixed_diss, all_mixed_temp)
# Append
arcpy.management.Append(mixed_diss, all_mixed_temp, "NO_TEST")
def add_single_muni():
# Build single muni PSAP boundaries from muni boundaries
print("Building single muni PSAPs from muni boundaries ...")
arcpy.management.CopyFeatures(psap_schema, single_muni_temp)
if arcpy.Exists("muni_lyr"):
arcpy.management.Delete("muni_lyr")
arcpy.management.MakeFeatureLayer(munis, "muni_lyr")
# Field Map muni name into psap schema fields
fms = arcpy.FieldMappings()
# NAME to DsplayName
fm_name = arcpy.FieldMap()
fm_name.addInputField("muni_lyr", "NAME")
output = fm_name.outputField
output.name = "DsplayName"
fm_name.outputField = output
fms.addFieldMap(fm_name)
# Complete the append with field mapping and query
sm_list = list(single_muni_dict.values())
print(sm_list)
sm_query = f"NAME IN ({sm_list})".replace('[', '').replace(']', '')
print(sm_query)
arcpy.management.Append("muni_lyr", single_muni_temp, "NO_TEST", field_mapping=fms, expression=sm_query)
# Populate fields with correct information
update_count = 0
fields = ['DsplayName']
with arcpy.da.UpdateCursor(single_muni_temp, fields) as update_cursor:
print("Looping through rows in FC ...")
for row in update_cursor:
for k,v in single_muni_dict.items():
if v == row[0]:
row[0] = k
update_count += 1
update_cursor.updateRow(row)
print(f"Total count of single muni updates is: {update_count}")
# Drop in single muni psaps via erase/append
print("Adding single muni PSAPs into working psaps layer ...")
# Erase
arcpy.analysis.Erase(all_mixed_temp, single_muni_temp, county_single_muni_temp)
# Append
arcpy.management.Append(single_muni_temp, county_single_muni_temp, "NO_TEST")
def add_multi_muni():
# Build multi muni PSAP boundaries from muni boundaries
print("Building multi muni PSAPs from muni boundaries ...")
arcpy.management.CopyFeatures(psap_schema, multi_muni_temp)
if arcpy.Exists("muni_lyr"):
arcpy.management.Delete("muni_lyr")
arcpy.management.MakeFeatureLayer(munis, "muni_lyr")
# Field Map muni name into psap schema fields
fms = arcpy.FieldMappings()
# NAME to DsplayName
fm_name = arcpy.FieldMap()
fm_name.addInputField("muni_lyr", "NAME")
output = fm_name.outputField
output.name = "DsplayName"
fm_name.outputField = output
fms.addFieldMap(fm_name)
# Build query to select multi muni
mm_list = [ item.split(',') for item in list(multi_muni_dict.values())]
mm_list = [y.strip() for x in mm_list for y in x]
print(mm_list)
mm_query = f"NAME IN ({mm_list})".replace('[', '').replace(']', '')
print(mm_query)
# Complete the append with field mapping and query to get all munis in group
arcpy.management.Append("muni_lyr", multi_muni_temp, "NO_TEST", field_mapping=fms, expression=mm_query)
# Loop through and populate fields with appropriate information and rename to multi muni psaps
update_count = 0
fields = ['DsplayName']
with arcpy.da.UpdateCursor(multi_muni_temp, fields) as update_cursor:
print("Looping through rows in FC ...")
for row in update_cursor:
for k,v in multi_muni_dict.items():
# print(f'key: {k} value: {v}')
if row[0] in v:
# print(f'Found {row[0]} in {v} ...')
row[0] = k
update_count += 1
update_cursor.updateRow(row)
print(f"Total count of multi muni updates is: {update_count}")
print("Dissolving multi muni PSAPs ...")
arcpy.management.Dissolve(multi_muni_temp, mm_diss, "DsplayName")
# Drop in multi muni psaps via erase/append
print("Adding multi muni PSAPs into working psaps layer ...")
# Erase
arcpy.analysis.Erase(county_single_muni_temp, mm_diss, all_county_muni_temp)
# Append
arcpy.management.Append(mm_diss, all_county_muni_temp, "NO_TEST")
def add_unique_psaps():
# Assemble VECC, Metro EC based on mixed boundaries and append static boundaries (Hill AFB -- Navajo Nation not longer used)
print("Building unique PSAPs from county, muni, and fixed boundaries ...")
arcpy.management.CopyFeatures(psap_schema, unique_muni_temp)
arcpy.management.CopyFeatures(psap_schema, unique_county_temp)
if arcpy.Exists("county_lyr"):
arcpy.management.Delete("county_lyr")
if arcpy.Exists("muni_lyr"):
arcpy.management.Delete("muni_lyr")
arcpy.management.MakeFeatureLayer(counties, "county_lyr")
arcpy.management.MakeFeatureLayer(munis, "muni_lyr")
# Assemble munis
# Field Map muni name into psap schema fields
fms = arcpy.FieldMappings()
# NAME to DsplayName
fm_name = arcpy.FieldMap()
fm_name.addInputField("muni_lyr", "NAME")
output = fm_name.outputField
output.name = "DsplayName"
fm_name.outputField = output
fms.addFieldMap(fm_name)
# Build query to select unique muni
unim_list = [ item.split(',') for item in list(unique_muni_dict.values())]
unim_list = [y.strip() for x in unim_list for y in x]
print(unim_list)
unim_query = f"NAME IN ({unim_list})".replace('[', '').replace(']', '')
print(unim_query)
# Complete the append with field mapping and query to get all munis in group
arcpy.management.Append("muni_lyr", unique_muni_temp, "NO_TEST", field_mapping=fms, expression=unim_query)
# Buffer fixed edge layer
print("Buffering fixed edges and erasing muni layer ...")
arcpy.analysis.Buffer(fixed_edges, 'in_memory\\edge_buffer', '100 Meters')
# Erase current muni layer with buffer
arcpy.analysis.Erase(unique_muni_temp, 'in_memory\\edge_buffer', unique_muni_erased)
# Append polygon fixes into erased muni layer
# polygon fixes with NULL names are ignored from the solution
no_nulls = "DsplayName IS NOT NULL AND Type = 'muni'"
print("Appending polygon fixes to erased muni layer ...")
arcpy.management.Append(poly_fixes, unique_muni_erased, "NO_TEST", expression=no_nulls)
# Assemble counties and cut in poly-fixed muni layer
# Field Map county name into psap schema fields
fms = arcpy.FieldMappings()
# NAME to DsplayName
fm_name = arcpy.FieldMap()
fm_name.addInputField("county_lyr", "NAME")
output = fm_name.outputField
output.name = "DsplayName"
fm_name.outputField = output
fms.addFieldMap(fm_name)
# Build query to select unique county
unic_list = [ item.split(',') for item in list(unique_county_dict.values())]
unic_list = [y.strip() for x in unic_list for y in x]
print(unic_list)
unic_query = f"NAME IN ({unic_list})".replace('[', '').replace(']', '')
print(unic_query)
# Complete the append with field mapping and query to create county layer
arcpy.management.Append("county_lyr", unique_county_temp, "NO_TEST", field_mapping=fms, expression=unic_query)
# Append polygon fixes into the county layer
# polygon fixes with NULL names are ignored from the solution
no_nulls = "DsplayName IS NOT NULL AND Type = 'county'"
print("Appending polygon fixes to erased county layer ...")
arcpy.management.Append(poly_fixes, unique_muni_erased, "NO_TEST", expression=no_nulls)
# Erase county layer with poly-fixed muni layer
arcpy.analysis.Erase(unique_county_temp, unique_muni_erased, unique_county_muni_temp)
# Append poly-fixed muni layer into county layer with holes
print("Appending poly-fixed muni layer to erased county layer ...")
arcpy.management.Append(unique_muni_erased, unique_county_muni_temp, "NO_TEST")
# Loop through and populate fields with appropriate information and rename polygons
update_count = 0
fields = ['DsplayName']
with arcpy.da.UpdateCursor(unique_county_muni_temp, fields) as update_cursor:
print("Looping through rows in FC ...")
for row in update_cursor:
for k,v in unique_county_dict.items():
# print(f'key: {k} value: {v}')
if row[0] in v:
# print(f'Found {row[0]} in {v} ...')
row[0] = k
for k,v in unique_muni_dict.items():
# print(f'key: {k} value: {v}')
if row[0] in v:
# print(f'Found {row[0]} in {v} ...')
row[0] = k
update_count += 1
update_cursor.updateRow(row)
print(f"Total count of unique PSAP updates is: {update_count}")
# Now append the unique/static PSAPS (Hill AFB, NN) into the working layer
print("Appending unique/static PSAPs ...")
arcpy.management.Append(unique, unique_county_muni_temp, "NO_TEST")
print("Dissolving unique PSAPs ...")
arcpy.management.Dissolve(unique_county_muni_temp, unique_diss, "DsplayName")
# Drop in unique psaps via erase/append
print("Adding unique PSAPs into working psaps layer ...")
# Erase
arcpy.analysis.Erase(all_county_muni_temp, unique_diss, all_unique_temp)
# Append
arcpy.management.Append(unique_diss, all_unique_temp, "NO_TEST")
def calc_fields():
# Loop through and populate fields with appropriate information
update_count = 0
# 0 1 2 3 4 5 6 7 8 9
fields = ['DsplayName', 'Source', 'DateUpdate', 'State', 'ServiceNum', 'ES_NGUID', 'OBJECTID', 'ServiceURI', 'County', 'DiscrpAgID']
with arcpy.da.UpdateCursor(all_unique_temp, fields) as update_cursor:
print("Looping through rows in FC ...")
for row in update_cursor:
row[1] = 'UGRC'
row[2] = datetime.now()
row[3] = 'UT'
if 'Colorado' in row[0]: row[3] = 'AZ'
row[4] = '911'
# row[5] = f'PSAP{row[6]}@gis.utah.gov'
row[5] = nguid_dict[f'{row[0]}']
row[7] = uri_dict[f'{row[0]}']
row[8] = county_dict[f'{row[0]}']
row[9] = 'gis.utah.gov/data/911'
update_count += 1
update_cursor.updateRow(row)
print(f"Total count of attribute updates is: {update_count}")
def build_sgid():
if arcpy.Exists(sgid_final):
arcpy.management.Delete(sgid_final)
joined_data = arcpy.management.JoinField(all_unique_temp, "DsplayName", sgid_data_table, "DsplayName")
# Remove extra field from join
arcpy.management.DeleteField(all_unique_temp, "DsplayName_1")
# Copy sgid data table to all_unique_temp feature class
# This is a copy of the PSAP Boundaries feature class with all fields for SGID
arcpy.management.CopyFeatures(joined_data, sgid_final)
# Make another version without URIs populated
arcpy.management.CopyFeatures(sgid_final, sgid_final_no_uris)
# Delete the URI field - can still truncate/load into SGID
arcpy.management.DeleteField(sgid_final_no_uris, "ServiceURI")
def remove_nested_holes():
# Converted features with nested holes to singlepart polygons
print("Fixing PSAPs with nested holes ...")
if arcpy.Exists("nested_lyr"):
arcpy.management.Delete("nested_lyr")
nested_query = f"DsplayName IN ({nested_list})".replace('[', '').replace(']', '')
arcpy.management.MakeFeatureLayer(all_unique_temp, "nested_lyr", nested_query)
arcpy.management.CopyFeatures("nested_lyr", nested_temp)
# Explode nested features into singlepart features
arcpy.management.MultipartToSinglepart(nested_temp, nested_fixes)
# Drop nested fixes back in via erase/append
print("Adding nested PSAPs back into working psaps layer ...")
# Erase
arcpy.analysis.Erase(all_unique_temp, nested_fixes, all_fixed_temp)
# Append
arcpy.management.Append(nested_fixes, all_fixed_temp, "NO_TEST")
def renumber(val, record, base):
Start = 1
Interval = 1
if (record == 0):
record = Start
else:
record += Interval
output = val.replace(base, base + str(record).zfill(3))
return output
def recalc_nguids():
# Loop through to recalculate NGUIDS on exploded features
nguid_count = 0
rec = 0
nested_query = f"DsplayName IN ({nested_list})".replace('[', '').replace(']', '')
# 0 1 2
fields = ['DsplayName', 'ES_NGUID', 'OBJECTID']
with arcpy.da.UpdateCursor(all_fixed_temp, fields, nested_query) as update_cursor:
print("Recalculating NGUIDS on exploded features ...")
for row in update_cursor:
base_guid = row[1].split('@', 1)[0]
updated = renumber(row[1], rec, base_guid)
row[1] = updated
rec += 1
nguid_count += 1
update_cursor.updateRow(row)
print(f"Total count of NGUID updates is: {nguid_count}")
def project_to_WGS84():
# Project final data to WGS84
print("Projecting final psap boundaries into WGS84 ...")
sr = arcpy.SpatialReference("WGS 1984")
arcpy.management.Project(all_fixed_temp, psap_wgs84, sr, "WGS_1984_(ITRF00)_To_NAD_1983")
def export_to_shapefile():
shape_folder = "0 NG911_PSAP_Shapefile_" + today
out_shape_dir = os.path.join(work_dir, shape_folder)
if os.path.isdir(out_shape_dir) == False:
os.mkdir(out_shape_dir)
shape_name = "UT_PSAPs_WGS84_" + today
arcpy.conversion.FeatureClassToFeatureClass(psap_wgs84, out_shape_dir, shape_name)
##########################
# Call Functions Below #
##########################
function_time = time.time()
add_single_county()
add_multi_county()
add_mixed_psaps()
add_single_muni()
add_multi_muni()
add_unique_psaps()
calc_fields()
build_sgid()
remove_nested_holes()
recalc_nguids()
project_to_WGS84()
export_to_shapefile()
print("Time elapsed in functions: {:.2f}s".format(time.time() - function_time))
print("Script shutting down ...")
# Stop timer and print end time in UTC
readable_end = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
print("The script end time is {}".format(readable_end))
print("Time elapsed: {:.2f}s".format(time.time() - start_time))