-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMapBuilderv1d5.pyt
373 lines (296 loc) · 16.5 KB
/
MapBuilderv1d5.pyt
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
import arcpy
from checkAndDelete import checkAndDelete
class Toolbox (object):
def __init__(self):
"""Define the toolbox (the name of the toolbox is the name of the
.pyt file)."""
self.label = "BuildMap"
self.alias = "BuildMap"
# List of tool classes associated with this toolbox
self.tools = [BuildAndSymbolize, BuildAndCheck]
class BuildAndSymbolize(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "BuildAndSymbolize"
self.description = ""
self.canRunInBackground = False
def getParameterInfo(self):
"""Define parameter definitions"""
param0 = arcpy.Parameter(
displayName="Input Lines:",
name="lines",
datatype="GPFeatureLayer",
parameterType="Required",
direction="Input")
param1 = arcpy.Parameter(
displayName="Input Points:",
name="points",
datatype="GPFeatureLayer",
parameterType="Required",
direction="Input")
param2 = arcpy.Parameter(
displayName="Quad Boundary:",
name="quad",
datatype="GPFeatureLayer",
parameterType="Optional",
direction="Input")
param3 = arcpy.Parameter(
displayName="Reference Layer:",
name="layer",
datatype="GPFeatureLayer",
parameterType="Required",
direction="Input")
param4 = arcpy.Parameter(
displayName="Output all polygons:",
name="polys",
datatype="DEFeatureClass",
parameterType="Required",
direction="Output")
param5 = arcpy.Parameter(
displayName="Output polygons in quad:",
name="selectPolys",
datatype="DEFeatureClass",
parameterType="Required",
direction="Output")
param6 = arcpy.Parameter(
displayName="Output Layer:",
name="layerOut",
datatype="GPLayer",
parameterType="Required",
direction="Output")
param7 = arcpy.Parameter(
displayName="Add to mxd?",
name="addToMXD",
datatype="Boolean",
parameterType="Optional",
direction="Input")
params = [param0, param1, param2, param3, param4, param5, param6, param7]
return params
def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True
def updateParameters(self, parameters):
"""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):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return
def execute(self, parameters, messages):
"""The source code of the tool."""
#Inputs
lines = parameters[0].valueAsText
points = parameters[1].valueAsText
quad = parameters[2].valueAsText
layer = parameters[3].valueAsText #reference layer
#Outputs
polys = parameters[4].valueAsText
selectPolys = parameters[5].valueAsText
layerOut = parameters[6].valueAsText
addToMXD = parameters[6].valueAsText
arcpy.env.overwriteOutput = True
if quad:
#Builds polyons
arcpy.FeatureToPolygon_management(in_features=lines+";"+quad,
out_feature_class=polys,
cluster_tolerance="#",
attributes="ATTRIBUTES",
label_features=points)
arcpy.AddMessage("Built")
checkAndDelete("polyLayer")
arcpy.MakeFeatureLayer_management(polys, "polyLayer")
#Selects polygons within quad boundary
arcpy.SelectLayerByLocation_management(in_layer="polyLayer",
overlap_type="WITHIN",
select_features=quad,
search_distance="#",
selection_type="NEW_SELECTION")
arcpy.AddMessage("Selected")
else:
arcpy.FeatureToPolygon_management(in_features=lines,
out_feature_class=polys,
cluster_tolerance="#",
attributes="ATTRIBUTES",
label_features=points)
arcpy.AddMessage("Built")
checkAndDelete("polyLayer")
arcpy.MakeFeatureLayer_management(polys, "polyLayer")
#Export selected polygons
arcpy.CopyFeatures_management(in_features="polyLayer",
out_feature_class=selectPolys)
arcpy.AddMessage("Exported")
checkAndDelete("Symbolized Map")
arcpy.MakeFeatureLayer_management(selectPolys, "Symbolized Map")
#Apply symbology from layer
arcpy.ApplySymbologyFromLayer_management(in_layer="Symbolized Map",
in_symbology_layer=layer)
arcpy.AddMessage("Symbolized")
#Export layer
arcpy.SaveToLayerFile_management(in_layer="Symbolized Map",
out_layer=layerOut,
is_relative_path="#",
version="CURRENT")
arcpy.AddMessage("Exported Layer")
if addToMXD:
arcpy.AddMessage(" Adding / Removing from the MXD")
mxd = arcpy.mapping.MapDocument("CURRENT")
df = mxd.activeDataFrame
#df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
sourceLayer2 = arcpy.mapping.Layer(selectPolys)
arcpy.mapping.RemoveLayer(df, sourceLayer2)
sourceLayer1 = arcpy.mapping.Layer(polys)
arcpy.mapping.RemoveLayer(df, sourceLayer1)
sourceLayer3 = arcpy.mapping.Layer("Symbolized Map")
arcpy.mapping.AddLayer(df, sourceLayer3, "TOP")
arcpy.env.overwriteOutput = False
return
class BuildAndCheck(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "BuildAndCheck"
self.description = ""
self.canRunInBackground = False
def getParameterInfo(self):
"""Define parameter definitions"""
param0 = arcpy.Parameter(
displayName="Input Lines:",
name="lines",
datatype="GPFeatureLayer",
parameterType="Required",
direction="Input")
param1 = arcpy.Parameter(
displayName="Input Points:",
name="points",
datatype="GPFeatureLayer",
parameterType="Required",
direction="Input")
param2 = arcpy.Parameter(
displayName="Quad Boundary:",
name="quad",
datatype="GPFeatureLayer",
parameterType="Optional",
direction="Input")
param3 = arcpy.Parameter(
displayName="Output polygons in quad:",
name="selectPolys",
datatype="DEFeatureClass",
parameterType="Required",
direction="Output")
param4 = arcpy.Parameter(
displayName="Joined polygons in quad:",
name="joinPolys",
datatype="DEFeatureClass",
parameterType="Required",
direction="Output")
param5 = arcpy.Parameter(
displayName="Output Layer:",
name="layerOut",
datatype="GPLayer",
parameterType="Required",
direction="Output")
param6 = arcpy.Parameter(
displayName="Add to mxd?",
name="addToMXD",
datatype="Boolean",
parameterType="Optional",
direction="Input")
params = [param0, param1, param2, param3, param4, param5, param6]
return params
def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True
def updateParameters(self, parameters):
"""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):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return
def execute(self, parameters, messages):
"""The source code of the tool."""
#Inputs
lines = parameters[0].valueAsText
points = parameters[1].valueAsText
quad = parameters[2].valueAsText
layer = r"BuildAndCheckTemplatev2.lyr"
#Outputs
selectPolys = parameters[3].valueAsText
joinPolys = parameters[4].valueAsText
layerOut = parameters[5].valueAsText
addToMXD = parameters[6].valueAsText
polys = arcpy.CreateScratchName(workspace=arcpy.env.scratchGDB)
arcpy.env.overwriteOutput = True
if quad:
arcpy.AddMessage(" Quad Name: " + quad)
# Builds polyons
arcpy.AddMessage(" Building Polygons")
arcpy.FeatureToPolygon_management(in_features=lines + ";" + quad,
out_feature_class=polys,
cluster_tolerance="#",
attributes="ATTRIBUTES",
label_features=points)
arcpy.AddMessage(" Making Layer")
arcpy.MakeFeatureLayer_management(polys, "polyLayer")
# Selects polygons within quad boundary
arcpy.AddMessage(" Selecting Within Quad")
arcpy.SelectLayerByLocation_management(in_layer="polyLayer",
overlap_type="WITHIN",
select_features=quad,
search_distance="#",
selection_type="NEW_SELECTION")
# Export selected polygons
arcpy.AddMessage(" Exporting")
arcpy.CopyFeatures_management(in_features="polyLayer",
out_feature_class=selectPolys)
else:
arcpy.AddMessage(" No Quad Boundary")
# Builds polyons
arcpy.AddMessage(" Building Polygons")
arcpy.FeatureToPolygon_management(in_features=lines,
out_feature_class=polys,
cluster_tolerance="#",
attributes="ATTRIBUTES",
label_features=points)
arcpy.AddMessage(" Making Layer")
arcpy.MakeFeatureLayer_management(polys, "polyLayer")
arcpy.AddMessage(" Exporting")
arcpy.CopyFeatures_management(in_features="polyLayer",
out_feature_class=selectPolys)
arcpy.AddMessage(" Joining")
arcpy.SpatialJoin_analysis(target_features=selectPolys,
join_features=points,
out_feature_class=joinPolys,
join_operation="JOIN_ONE_TO_ONE",
join_type="KEEP_ALL",
# field_mapping="""type "Type" true false false 255 Text 0 0 ,First,#,locogeo.sde.PKHContactsAndFaults,type,-1,-1;isconcealed "IsConcealed" true false false 1 Text 0 0 ,First,#,locogeo.sde.PKHContactsAndFaults,isconcealed,-1,-1;existenceconfidence "ExistenceConfidence" true false false 50 Text 0 0 ,First,#,locogeo.sde.PKHContactsAndFaults,existenceconfidence,-1,-1;identityconfidence "IdentityConfidence" true false false 50 Text 0 0 ,First,#,locogeo.sde.PKHContactsAndFaults,identityconfidence,-1,-1;locationconfidencemeters "LocationConfidenceMeters" true false false 8 Double 8 38 ,First,#,locogeo.sde.PKHContactsAndFaults,locationconfidencemeters,-1,-1;symbol "Symbol" true true false 255 Text 0 0 ,First,#,locogeo.sde.PKHContactsAndFaults,symbol,-1,-1;label "Label" true true false 50 Text 0 0 ,First,#,locogeo.sde.PKHContactsAndFaults,label,-1,-1;datasourceid "DataSourceID" true false false 50 Text 0 0 ,First,#,locogeo.sde.PKHContactsAndFaults,datasourceid,-1,-1;notes "Notes" true true false 255 Text 0 0 ,First,#,locogeo.sde.PKHContactsAndFaults,notes,-1,-1;creator "Creator" false true false 255 Text 0 0 ,First,#,locogeo.sde.PKHContactsAndFaults,creator,-1,-1;createdate "CreateDate" false true false 36 Date 0 0 ,First,#,locogeo.sde.PKHContactsAndFaults,createdate,-1,-1;editor "Editor" false true false 255 Text 0 0 ,First,#,locogeo.sde.PKHContactsAndFaults,editor,-1,-1;editdate "EditDate" false true false 36 Date 0 0 ,First,#,locogeo.sde.PKHContactsAndFaults,editdate,-1,-1;datasourcenotes "datasourcenotes" true true false 150 Text 0 0 ,First,#,locogeo.sde.PKHContactsAndFaults,datasourcenotes,-1,-1;st_length_shape_ "st_length_shape_" false false true 0 Double 0 0 ,First,#,locogeo.sde.PKHContactsAndFaults,st_length(shape),-1,-1;mapunit "MapUnit" true false false 10 Text 0 0 ,First,#,locogeo.sde.PKHMapUnitPoints,mapunit,-1,-1;identityconfidence_1 "IdentityConfidence" true false false 50 Text 0 0 ,First,#,locogeo.sde.PKHMapUnitPoints,identityconfidence,-1,-1;label_1 "Label" true true false 50 Text 0 0 ,First,#,locogeo.sde.PKHMapUnitPoints,label,-1,-1;symbol_1 "Symbol" true true false 255 Text 0 0 ,First,#,locogeo.sde.PKHMapUnitPoints,symbol,-1,-1;notes_1 "Notes" true true false 255 Text 0 0 ,First,#,locogeo.sde.PKHMapUnitPoints,notes,-1,-1;datasourceid_1 "DataSourceID" true false false 50 Text 0 0 ,First,#,locogeo.sde.PKHMapUnitPoints,datasourceid,-1,-1;mapunit2 "MapUnit2" true true false 50 Text 0 0 ,First,#,locogeo.sde.PKHMapUnitPoints,mapunit2,-1,-1;origunit "OrigUnit" true true false 50 Text 0 0 ,First,#,locogeo.sde.PKHMapUnitPoints,origunit,-1,-1;creator_1 "Creator" false true false 255 Text 0 0 ,First,#,locogeo.sde.PKHMapUnitPoints,creator,-1,-1;createdate_1 "CreateDate" false true false 36 Date 0 0 ,First,#,locogeo.sde.PKHMapUnitPoints,createdate,-1,-1;editor_1 "Editor" false true false 255 Text 0 0 ,First,#,locogeo.sde.PKHMapUnitPoints,editor,-1,-1;editdate_1 "EditDate" false true false 36 Date 0 0 ,First,#,locogeo.sde.PKHMapUnitPoints,editdate,-1,-1;datasourcenotes_1 "datasourcenotes_1" true true false 150 Text 0 0 ,First,#,locogeo.sde.PKHMapUnitPoints,datasourcenotes,-1,-1;facies "facies" true true false 50 Text 0 0 ,First,#,locogeo.sde.PKHMapUnitPoints,facies,-1,-1""",
match_option="INTERSECT",
search_radius="",
distance_field_name="")
arcpy.MakeFeatureLayer_management(joinPolys, "Build And Check Map")
# Apply symbology from layer
arcpy.AddMessage(" Symbolizing")
arcpy.ApplySymbologyFromLayer_management(in_layer="Build And Check Map",
in_symbology_layer=layer)
# Export layer
arcpy.SaveToLayerFile_management(in_layer="Build And Check Map",
out_layer=layerOut,
is_relative_path="#",
version="CURRENT")
arcpy.AddMessage(" Exporting")
if addToMXD:
arcpy.AddMessage(" Adding / Removing from the MXD")
mxd = arcpy.mapping.MapDocument("CURRENT")
df = mxd.activeDataFrame
#df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
sourceLayer2 = arcpy.mapping.Layer(selectPolys)
arcpy.mapping.RemoveLayer(df, sourceLayer2)
sourceLayer1 = arcpy.mapping.Layer(polys)
arcpy.mapping.RemoveLayer(df, sourceLayer1)
sourceLayer3 = arcpy.mapping.Layer("Build And Check Map")
arcpy.mapping.AddLayer(df, sourceLayer3, "TOP")
arcpy.env.overwriteOutput = False
return