-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeConnectingTrails.py
220 lines (189 loc) · 9.23 KB
/
MergeConnectingTrails.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
import os
from typing import Dict, List
import typing
import arcpy
from arcpy import Parameter
from arcpy import ValueTable
class MergeConnectingTrails(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = (
"Merge Connecting Trails (Designed for Rails to Trails OpenTrails Data)"
)
self.description = "Merges trails from the Rails to Trails Conservancy's OpenTrails data that are next to each other but not seen as a single multipart line. This tool currently is hardcoded to work with the Rails to Trails OpenTrails dataset and may need to be expanded to support generic line inputs."
self.canRunInBackground = False
def getParameterInfo(self):
"""Define parameter definitions"""
paramInput = arcpy.Parameter(
displayName="Census Polygons",
name="INPUT",
datatype="GPFeatureLayer",
parameterType="Required",
direction="Input",
)
paramOutput = arcpy.Parameter(
displayName="Census Data",
name="OUTPUT",
datatype="GPFeatureLayer",
multiValue=True,
parameterType="Required",
direction="Output",
)
paramOutput.parameterDependencies = [paramInput.name]
params = [
paramInput,
paramOutput,
]
return params
def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True
def updateParameters(self, parameters: List[Parameter]):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
return
def updateMessages(self, parameters: List[Parameter]):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return
def execute(self, parameters: List[Parameter], messages):
"""The source code of the tool."""
arcpy.env.overwriteOutput = True
# create a dictionary containing key-value pairs of the parameters
# where the key is the param name and the value is the text value
params = {}
for elem in parameters:
if elem.altered:
params[elem.name] = elem.valueAsText
arcpy.analysis.Buffer(
params.get("INPUT"), "SC_T_Buffer", "2 Meters", "FULL", "ROUND"
)
arcpy.management.Dissolve(
"SC_T_Buffer",
"SC_T_Buffer__Dissolve",
"",
"",
"SINGLE_PART",
"DISSOLVE_LINES",
)
arcpy.management.AddField("SC_T_Buffer__Dissolve", "BUFFERID", "LONG")
arcpy.management.CalculateField(
"SC_T_Buffer__Dissolve", "BUFFERID", "!OBJECTID!"
)
# create a new fieldmappings
fieldmappings = arcpy.FieldMappings()
# add the BUFFERID field
BUFFERID_field_map = arcpy.FieldMap()
BUFFERID_field_map.addInputField("SC_T_Buffer__Dissolve", "BUFFERID")
fieldmappings.addFieldMap(BUFFERID_field_map)
# add the trails input feature classes
fieldmappings.addTable(params.get("INPUT"))
# delete
if fieldmappings.findFieldMapIndex("ACCT_ID") >= 0:
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("ACCT_ID"))
if fieldmappings.findFieldMapIndex("ACCT_TYPE") >= 0:
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("ACCT_TYPE"))
if fieldmappings.findFieldMapIndex("ACCT_CLASS") >= 0:
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("ACCT_CLASS"))
if fieldmappings.findFieldMapIndex("ACCT_CAT") >= 0:
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("ACCT_CAT"))
if fieldmappings.findFieldMapIndex("Length_DMS") >= 0:
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("Length_DMS"))
if fieldmappings.findFieldMapIndex("FY") >= 0:
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("FY"))
if fieldmappings.findFieldMapIndex("DateAdded") >= 0:
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("DateAdded"))
if fieldmappings.findFieldMapIndex("DateUpdated") >= 0:
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("DateUpdated"))
if fieldmappings.findFieldMapIndex("STATUS") >= 0:
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("STATUS"))
for _fieldmap in fieldmappings:
if _fieldmap.outputField.type == "String":
index = fieldmappings.findFieldMapIndex(_fieldmap.outputField.name)
fieldmap = fieldmappings.getFieldMap(index)
fieldmap.mergeRule = "Join"
fieldmap.joinDelimiter = ", "
outputField = fieldmap.outputField
outputField.length = 9999
fieldmap.outputField = outputField
fieldmappings.replaceFieldMap(index, fieldmap)
arcpy.analysis.SpatialJoin(
"SC_T_Buffer__Dissolve",
params.get("INPUT"),
"SC_T_B",
"JOIN_ONE_TO_ONE",
"KEEP_ALL",
fieldmappings,
)
arcpy.topographic.PolygonToCenterline(
in_features="SC_T_B",
out_feature_class="SC_T_B__Centerline_Original",
connecting_features=None,
)
arcpy.analysis.SpatialJoin(
target_features="SC_T_B__Centerline_Original",
join_features="SC_T_B",
out_feature_class="SC_T_B__Centerline_BUFFERID",
join_operation="JOIN_ONE_TO_MANY",
join_type="KEEP_ALL",
field_mapping='BUFFERID "BUFFERID" true true false 255 Text 0 0,First,#,SC_T_B,BUFFERID,-1,-1',
match_option="INTERSECT",
search_radius=None,
distance_field_name="",
)
arcpy.analysis.PairwiseDissolve(
in_features="SC_T_B__Centerline_BUFFERID",
out_feature_class="SC_T_B__Centerline_Dissolved",
dissolve_field="BUFFERID",
statistics_fields=None,
multi_part="MULTI_PART",
)
arcpy.analysis.SpatialJoin(
target_features="SC_T_B__Centerline_Dissolved",
join_features="SC_T_B",
out_feature_class=params.get("OUTPUT"),
join_operation="JOIN_ONE_TO_ONE",
join_type="KEEP_ALL",
field_mapping='BUFFERID "BUFFERID" true true false 255 Text 0 0,First,#,SC_T_B__Centerline_Dissolved,BUFFERID,0,255;SHAPE_Length "SHAPE_Length" false true true 8 Double 0 0,First,#,SC_T_B__Centerline_Dissolved,SHAPE_Length,-1,-1;Join_Count "Join_Count" true true false 4 Long 0 0,First,#,SC_T_B,Join_Count,-1,-1;TARGET_FID "TARGET_FID" true true false 4 Long 0 0,First,#,SC_T_B,TARGET_FID,-1,-1;BUFFERID_1 "BUFFERID" true true false 4 Long 0 0,First,#,SC_T_B,BUFFERID,-1,-1;SOURCE "SOURCE" true true false 9999 Text 0 0,First,#,SC_T_B,SOURCE,0,9999;QC "QC" true true false 9999 Text 0 0,First,#,SC_T_B,QC,0,9999;TRAIL_NAME "TRAIL_NAME" true true false 9999 Text 0 0,First,#,SC_T_B,TRAIL_NAME,0,9999;STATE "STATE" true true false 9999 Text 0 0,First,#,SC_T_B,STATE,0,9999;COUNTY "COUNTY" true true false 9999 Text 0 0,First,#,SC_T_B,COUNTY,0,9999;ORIG_FID "ORIG_FID" true true false 4 Long 0 0,First,#,SC_T_B,ORIG_FID,-1,-1;Shape_Length_1 "Shape_Length" false true true 8 Double 0 0,First,#,SC_T_B,Shape_Length,-1,-1;Shape_Area "Shape_Area" false true true 8 Double 0 0,First,#,SC_T_B,Shape_Area,-1,-1',
match_option="INTERSECT",
search_radius=None,
)
arcpy.management.Delete("SC_T_Buffer")
arcpy.management.Delete("SC_T_Dissolve")
arcpy.management.Delete("SC_T_Buffer__Dissolve")
arcpy.management.Delete("SC_T_B")
arcpy.management.Delete("SC_T_B__Centerline_Original")
arcpy.management.Delete("SC_T_B__Centerline_BUFFERID")
arcpy.management.Delete("SC_T_B__Centerline_Dissolved")
arcpy.management.CalculateField(
in_table=params.get("OUTPUT"),
field="SOURCE",
expression='", ".join([*set(!SOURCE!.split(", "))])',
)
arcpy.management.CalculateField(
in_table=params.get("OUTPUT"),
field="QC",
expression='", ".join([*set(!QC!.split(", "))])',
)
arcpy.management.CalculateField(
in_table=params.get("OUTPUT"),
field="TRAIL_NAME",
expression='", ".join([*set(!TRAIL_NAME!.split(", "))])',
)
arcpy.management.CalculateField(
in_table=params.get("OUTPUT"),
field="STATE",
expression='", ".join([*set(!STATE!.split(", "))])',
)
arcpy.management.CalculateField(
in_table=params.get("OUTPUT"),
field="COUNTY",
expression='", ".join([*set(!COUNTY!.split(", "))])',
)
arcpy.AddMessage(" ✅ Done")
return
def postExecute(self, parameters: List[Parameter]):
"""This method takes place after outputs are processed and
added to the display."""
return