Skip to content

Commit 9edff8a

Browse files
committed
Added Class 13
1 parent b762e16 commit 9edff8a

File tree

8 files changed

+232
-2
lines changed

8 files changed

+232
-2
lines changed

Classes/13_Toolboxes/Step_1.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
#####
3+
# Step 1 - "Traditional" ArcToolboxes
4+
#####
5+
6+
# In this process, we will create a "Script Tool" which will create a toolbox that we can interact with using the
7+
# script below. This is a Walk Through Session, so follow as I go along.
8+
9+
# First you should add the Toolbox that I provided in Step_1_Data.zip, and we will look at the script and
10+
# code used to initialize it.
11+
12+
## Add Toolbox provided to ArcToolbox
13+
14+
# Now let's create our own. The script is provided below, and a brief description of the workflow is provided.
15+
16+
# 1. Open ArcToolbox, rc Add Toolbox, click new Toolbox icon in window that pops up, call it "Testing Toolbox.tbx"
17+
# 2. Inside this new Toolbox, rc Add "Script", then add the following params:
18+
##### Name = TestTool1 - This is the machine name of the script
19+
##### Label = Test Tool 1 - This shows up in ArcToolbox as the name of the tool.
20+
##### Description = "Testing a tool, does nothing really".
21+
##### Leave the rest as it is.
22+
# 3. Click Next, now browse to select the "Script File", you will note that I have provided one for you in
23+
# Step_1_Data.zip, called Testing_Tool_Script.py.
24+
# 4. Click Next, here you add the individual parameters, we are going to add a display name of "Shapefile" in the
25+
# top most box, on the left, and a datatype of "Shapefile", leave the parameter settings as their defaults.
26+
# 5. Click finish! Test it on the Shapefile in the "Test Shapefile" folder.
27+
28+
29+
30+
# Task - Taking the more complex code below, I want you to turn this into a toolbox. Note that it accepts
31+
# three arguments, one input line file and one input polygon file, and an output file. You do not need to
32+
# change the code below, just put it in it's own Python file, and then create the appropriate parameters
33+
# inside the toolbox settings as we just walked through.
34+
35+
36+
# import arcpy
37+
# import sys
38+
#
39+
# input_line = sys.argv[1]
40+
# input_polygon = sys.argv[2]
41+
# output = sys.argv[3]
42+
#
43+
# arcpy.Clip_analysis(in_features=input_line,
44+
# clip_features=input_polygon,
45+
# out_feature_class=output,
46+
# cluster_tolerance="")
45.4 KB
Binary file not shown.

Classes/13_Toolboxes/Step_1_Data.zip

40.4 KB
Binary file not shown.

Classes/13_Toolboxes/Step_2.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
#####
3+
# Step 2 - Python Toolboxes
4+
#####
5+
6+
# The process to create a "Traditional" toolbox takes you out of Python, and somewhat can limit what you can
7+
# do. The solution is an all Python Toolbox, a hidden gem of python and arcpy. There is a lot of information
8+
# about them here; http://desktop.arcgis.com/en/arcmap/10.3/analyze/creating-tools/a-quick-tour-of-python-toolboxes.htm
9+
10+
# The template for a Python toolbox uses classes and functions. See a class as a higher level function that allows
11+
# us to keep code separate. Below is an example template, I shall walk you through the different parts:
12+
13+
# Note:
14+
### class Toolbox defines the toolbox, you only have this once, but you can have multiple tool classes, i.e.
15+
### Tool, Tool1, Tool2 etc, just extend the self.tools = [Tool, Tool1, Tool2] and add the corresponding
16+
### classes for the new tools.
17+
18+
19+
import arcpy
20+
21+
22+
class Toolbox(object):
23+
def __init__(self):
24+
"""Define the toolbox (the name of the toolbox is the name of the
25+
.pyt file)."""
26+
self.label = "Toolbox"
27+
self.alias = ""
28+
29+
# List of tool classes associated with this toolbox
30+
self.tools = [Tool]
31+
32+
33+
class Tool(object):
34+
def __init__(self):
35+
"""Define the tool (tool name is the name of the class)."""
36+
self.label = "Tool"
37+
self.description = ""
38+
self.canRunInBackground = False
39+
40+
def getParameterInfo(self):
41+
"""Define parameter definitions"""
42+
params = None
43+
return params
44+
45+
def isLicensed(self):
46+
"""Set whether tool is licensed to execute."""
47+
return True
48+
49+
def updateParameters(self, parameters):
50+
"""Modify the values and properties of parameters before internal
51+
validation is performed. This method is called whenever a parameter
52+
has been changed."""
53+
return
54+
55+
def updateMessages(self, parameters):
56+
"""Modify the messages created by internal validation for each tool
57+
parameter. This method is called after internal validation."""
58+
return
59+
60+
def execute(self, parameters, messages):
61+
"""The source code of the tool."""
62+
return
63+
64+
# I have created a rudimentary Python Toolbox for you in Step_2_Data/Python_Toolbox_Example, add this to ArcToolbox

Classes/13_Toolboxes/Step_2_Data.zip

61.2 KB
Binary file not shown.

Classes/13_Toolboxes/Step_3.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
#####
3+
# Step 3 - Make your own Python Toolbox!
4+
#####
5+
6+
# Task - Using the code I provide below (basically , including the parameters that I have prepared for you (note, you can
7+
# find all the Python Toolbox Parameters here:
8+
# http://desktop.arcgis.com/en/arcmap/10.3/analyze/creating-tools/defining-parameters-in-a-python-toolbox.htm)
9+
10+
# I want you to attempt to construct a working Python Toolbox. Hint the code is the same as we used before for the
11+
# traditional toolbox, however, I have changed how the arguements are provided to the tool.
12+
13+
# Code for parameters function
14+
params = []
15+
input_line = arcpy.Parameter(name="input_line",
16+
displayName="Input Line",
17+
datatype="DEFeatureClass",
18+
parameterType="Required", # Required|Optional|Derived
19+
direction="Input", # Input|Output
20+
)
21+
params.append(input_line)
22+
input_polygon = arcpy.Parameter(name="input_polygon",
23+
displayName="Input Polygon",
24+
datatype="DEFeatureClass",
25+
parameterType="Required", # Required|Optional|Derived
26+
direction="Input", # Input|Output
27+
)
28+
params.append(input_polygon)
29+
output = arcpy.Parameter(name="output",
30+
displayName="Output",
31+
datatype="DEFeatureClass",
32+
parameterType="Required", # Required|Optional|Derived
33+
direction="Output", # Input|Output
34+
)
35+
params.append(output)
36+
return params
37+
38+
# Code for execution function
39+
input_line = parameters[0].valueAsText
40+
input_polygon = parameters[1].valueAsText
41+
output = parameters[2].valueAsText
42+
43+
arcpy.Clip_analysis(in_features=input_line,
44+
clip_features=input_polygon,
45+
out_feature_class=output,
46+
cluster_tolerance="")
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import arcpy
2+
3+
4+
class Toolbox(object):
5+
def __init__(self):
6+
"""Define the toolbox (the name of the toolbox is the name of the
7+
.pyt file)."""
8+
self.label = "Python Toolbox Clippy"
9+
self.alias = ""
10+
11+
# List of tool classes associated with this toolbox
12+
self.tools = [Clippy]
13+
14+
15+
class Clippy(object):
16+
def __init__(self):
17+
"""Define the tool (tool name is the name of the class)."""
18+
self.label = "Clippy Tool"
19+
self.description = ""
20+
self.canRunInBackground = False
21+
22+
def getParameterInfo(self):
23+
"""Define parameter definitions"""
24+
params = []
25+
input_line = arcpy.Parameter(name="input_line",
26+
displayName="Input Line",
27+
datatype="DEFeatureClass",
28+
parameterType="Required", # Required|Optional|Derived
29+
direction="Input", # Input|Output
30+
)
31+
params.append(input_line)
32+
input_polygon = arcpy.Parameter(name="input_polygon",
33+
displayName="Input Polygon",
34+
datatype="DEFeatureClass",
35+
parameterType="Required", # Required|Optional|Derived
36+
direction="Input", # Input|Output
37+
)
38+
params.append(input_polygon)
39+
output = arcpy.Parameter(name="output",
40+
displayName="Output",
41+
datatype="DEFeatureClass",
42+
parameterType="Required", # Required|Optional|Derived
43+
direction="Output", # Input|Output
44+
)
45+
params.append(output)
46+
return params
47+
48+
def isLicensed(self):
49+
"""Set whether tool is licensed to execute."""
50+
return True
51+
52+
def updateParameters(self, parameters):
53+
"""Modify the values and properties of parameters before internal
54+
validation is performed. This method is called whenever a parameter
55+
has been changed."""
56+
return
57+
58+
def updateMessages(self, parameters):
59+
"""Modify the messages created by internal validation for each tool
60+
parameter. This method is called after internal validation."""
61+
return
62+
63+
def execute(self, parameters, messages):
64+
"""The source code of the tool."""
65+
input_line = parameters[0].valueAsText
66+
input_polygon = parameters[1].valueAsText
67+
output = parameters[2].valueAsText
68+
69+
arcpy.Clip_analysis(in_features=input_line,
70+
clip_features=input_polygon,
71+
out_feature_class=output,
72+
cluster_tolerance="")
73+
return
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import arcpy
22

3-
3+
4+
45
arcpy.AddMessage("Hello, this is an add message output")
5-
6+
67
print("This is a print statement")

0 commit comments

Comments
 (0)