-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateFromKML.py
163 lines (141 loc) · 6.22 KB
/
updateFromKML.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
# -*- coding: utf-8 -*-
# This script should be ran from ArcGIS Pro's Python window.
# 1. The kmlToFC() and appendTo() methods can be called from
# outside of the ArcGIS Pro application.
# 2. The appendToFL() method is set up to be called from
# an open ArcGIS Pro project with a map and a feature layer.
# The master_FC variable is pointing to a feature class with
# the same name as the feature layer in AGOL.
import arcpy
import os
import re
import config
kmz_path = config.updateFromKML["kmz_path"] # /KMZ/
kmz_file = config.updateFromKML["kmz_file"]
kmz_file_path = kmz_path + kmz_file
kmz_filename = os.path.splitext(kmz_file)[0]
starting_numbers = re.findall(r"^\d+", kmz_filename) # returns list
fc_name_wo_spaces = kmz_filename.replace(" ", "_")
fc_name_replaced_numbers = re.sub(r"^\d+[_]", "", fc_name_wo_spaces)
fc_name_final = fc_name_replaced_numbers + "_" + starting_numbers[0]
staging_folder = config.updateFromKML["staging_folder"]
staging_kmzFGDB_path = staging_folder + kmz_filename + ".gdb/"
staging_kmzfc = staging_kmzFGDB_path + "Placemarks/Points"
projected_FGDB = staging_folder + config.updateFromKML["projected_FGDB"]
projected_fc = projected_FGDB + fc_name_final
spatial_ref = arcpy.SpatialReference(config.updateFromKML["spatial_ref"])
transform_method = config.updateFromKML["transform_method"]
delete_list = [staging_kmzFGDB_path, projected_fc]
master_FGDB = config.appendTo["master_FGDB"] # /<FGDB name>/
master_FC = master_FGDB + config.appendTo["master_FC"]
projected_fc_fields = config.appendTo["projected_fc_fields"]
master_FC_fields = config.appendTo["master_FC_fields"]
map_name = config.appendToFL["map_name"]
def removeExisting():
# Remove stale data
for i in delete_list:
# Remove FC if exists
if arcpy.Exists(projected_fc):
arcpy.management.Delete(projected_fc)
msg_Delete = f"Completed removing existing {i}"
+ arcpy.GetMessages()
arcpy.AddMessage(msg_Delete)
print(arcpy.GetMessages()) # For running in IDE
# Convert KMZ file to Points FC (and layer)
def kmlToFC():
try:
# Prepare the layer from the KML/KMZ file
arcpy.conversion.KMLToLayer(
kmz_file_path, staging_folder
)
msg_KML = f"Completed KML to Layer from {kmz_file_path}" \
f"to {staging_folder}"
arcpy.AddMessage(msg_KML)
print(arcpy.GetMessages())
# Project the layer to FC
arcpy.management.Project(
staging_kmzfc, projected_fc, spatial_ref, transform_method
)
msg_Prj = f"Completed Projecting {staging_kmzfc} to {projected_fc}, " \
f"using {spatial_ref.name} and {transform_method}"
arcpy.AddMessage(msg_Prj)
print(arcpy.GetMessages())
# Return geoprocessing-specific errors
except arcpy.ExecuteError:
print(arcpy.GetMessages())
# Append Projected FC to Master FC and Feature Service
def appendTo():
try:
insert_cursor = arcpy.da.InsertCursor(master_FC, master_FC_fields
+ ["SHAPE@"])
msg_Ins = f"Created Insert Cursor for {master_FC}"
arcpy.AddMessage(msg_Ins)
print(arcpy.GetMessages())
count1 = arcpy.management.GetCount(master_FC)
msg_Start = f"{master_FC} is starting with a feature count of {count1}"
arcpy.AddMessage(msg_Start)
print(arcpy.GetMessages())
new_rows = 0
with arcpy.da.SearchCursor(
projected_fc, projected_fc_fields + ["SHAPE@"]
) as cursor:
for row in cursor:
insert_cursor.insertRow(row)
new_rows += 1
del insert_cursor
count2 = arcpy.management.GetCount(master_FC)
msg_End1 = f"Completed adding {new_rows} total features to {master_FC}"
msg_End2 = f"{master_FC} now has a feature count of {count2}"
arcpy.AddMessage(msg_End1 + "\n" + msg_End2)
print(arcpy.GetMessages())
except arcpy.ExecuteError:
print(arcpy.GetMessages())
# Loop through layers from opened ArcGIS Pro project
# and Append to the layer of interest
def appendToFL():
try:
aprx = arcpy.mp.ArcGISProject("CURRENT")
msg_aprx = f"Accessed the ArcGIS Pro project, {aprx}"
arcpy.AddMessage(msg_aprx)
print(arcpy.GetMessages())
m = aprx.listMaps(map_name)[0]
msg_map = f"Accessed {map_name}map. Now looking through its layers!"
arcpy.AddMessage(msg_map)
print(arcpy.GetMessages())
for lyr in m.listLayers():
msg_lyr = f"The {map_name} map has a layer named {lyr.name}"
arcpy.AddMessage(msg_lyr)
print(arcpy.GetMessages())
if lyr.name == config.appendToFL["layer_name"]:
insert_cursor = arcpy.da.InsertCursor(
lyr, master_FC_fields + ["SHAPE@"]
)
msg_Inslyr = f"Created Insert Cursor for {lyr.name}"
arcpy.AddMessage(msg_Inslyr)
print(arcpy.GetMessages())
count1 = arcpy.management.GetCount(lyr)
msg_count1 = f"{lyr} is starting with a feature count " \
f"of {count1}"
arcpy.AddMessage(msg_count1)
print(arcpy.GetMessages())
new_rows = 0
with arcpy.da.SearchCursor(
projected_fc, projected_fc_fields + ["SHAPE@"]
) as cursor:
for row in cursor:
insert_cursor.insertRow(row)
new_rows += 1
del insert_cursor
count2 = arcpy.management.GetCount(lyr)
msg_End1 = f"Completed adding {new_rows} total features" \
f"to {lyr}"
msg_End2 = f"{lyr} now has a feature count of {count2}"
arcpy.AddMessage(msg_End1 + "\n" + msg_End2)
print(arcpy.GetMessages())
except arcpy.ExecuteError:
print(arcpy.GetMessages())
if __name__ == "__main__":
removeExisting()
kmlToFC()
appendTo()
appendToFL()