-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount-poly-overlap.pyt
73 lines (58 loc) · 2.34 KB
/
count-poly-overlap.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
import arcpy, os
class Toolbox(object):
def __init__(self):
"""Define the toolbox (the name of the toolbox is the name of the
.pyt file)."""
self.label = "count-poly-overlap"
self.alias = "count-poly-overlap"
# List of tool classes associated with this toolbox
self.tools = [Tool]
class Tool(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "count-poly-overlap"
self.description = ""
self.canRunInBackground = False
def getParameterInfo(self):
"""Define parameter definitions"""
p0 = arcpy.Parameter(
displayName="Input Layer",
name="InLyr",
datatype="GPFeatureLayer",
parameterType="Required",
direction="Input")
p1 = arcpy.Parameter(
displayName="Output Location",
name="OutLoc",
datatype="DEWorkspace",
parameterType="Required",
direction="Input")
return [p0, p1]
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."""
def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
def execute(self, parameters, messages):
"""The source code of the tool."""
inlyr = parameters[0].valueAsText
outloc = parameters[1].valueAsText
base, fc = os.path.split(inlyr)
# Remove '.shp' extension if input is shapefile
fname = fc.split('.')[0]
unionlyr = outloc + '/' + fname + '_Union'
disslyr = outloc + '/' + fname + '_Diss'
# If output location is not a geodatabase, append '.shp' file extension
if len(os.path.basename(outloc).split('.')) == 1:
unionlyr = unionlyr + '.shp'
disslyr = disslyr + '.shp'
arcpy.Union_analysis(inlyr, unionlyr)
arcpy.AddField_management(unionlyr, 'DissField', 'TEXT', 100)
arcpy.CalculateField_management(unionlyr, 'DissField', 'str(!SHAPE_Area!)+str(!SHAPE.CENTROID.X!)+str(!SHAPE.CENTROID.Y!)', 'PYTHON_9.3')
arcpy.Dissolve_management(unionlyr, disslyr, 'DissField', [['DissField', 'COUNT']])
return