Do ArcGIS Pro tasks support Python Toolbox tools which operate on Map Frames?
The reason I ask is that I am stuck when trying to do the following:
- Start ArcGIS Pro 1.1.1
- Create a new project - I called mine TestProject and placed in in C:\Temp
- Use the Project pane to Add Folder Connection to where I have a shapefile of the countries of the world from Natural Earth ()
- Drag and drop ne_10m_admin_0_countries.shp into the Map to create layer called ne_10m_admin_0_countries
- Insert a new Layout - I used A3 Landscape
- Insert a new Map Frame on the Layout
- In the Project pane create a New Python Toolbox in the TestProject folder - I called mine TestPYT
- Right-click on TestPYT in the Project pane to Edit it
- Replace the code with that below to give the Python Toolbox two tools called Chile and Switzerland
- Save the script and Refresh the Python Toolbox to see the two new tools
- Run the Chile tool to see the map frame on the layout zoom to Chile
- Run the Switzerland tool to see the map frame on the layout zoom to Switzerland
- Insert a New Task Item
- In the Task Item 1 insert a New Task and call it Chile
- In the Chile task insert a New Step and call it Zoom to Chile
- For Step Behavior make it Automatic and Hidden
- On the Actions tab I try to set Command/Geoprocessing as a Geoprocessing Tool choosing the Chile tool
- It seems to stick when I choose OK
- It seems to "lose" the tool when I click Done
Specifically, what I am trying to create is a workflow with two tasks that I can click on to Zoom to Chile or Zoom to Switzerland but I am stuck at step 19 above.
What I am trying to do overall is to find an ArcPy (for ArcGIS Pro) equivalent to a Python AddIn toolbar in ArcPy (for ArcGIS 10.x architecture) with two buttons (Chile and Switzerland) to zoom to those countries.
I've run through this procedure a few times, and on one occasion I was able to get the Chile and Switzerland tools to stick as tasks, but even then they did not seem to interact with the Map Frame (no error, just no change to what was displayed in the Map Frame when they ran), even though the tools when run from the Python Toolbox continued to work without a problem.
This is the code to copy/paste into the Python Toolbox (TestPYT).
import arcpy
class Toolbox(object):
def __init__(self):
"""Define the toolbox (the name of the toolbox is the name of the
.pyt file)."""
self.label = "Toolbox"
self.alias = ""
# List of tool classes associated with this toolbox
self.tools = [Slide1,Slide2]
class Slide1(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "Chile"
self.description = ""
self.canRunInBackground = False
def getParameterInfo(self):
"""Define parameter definitions"""
params = None
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."""
aprx = arcpy.mp.ArcGISProject("CURRENT")
mapx = aprx.listMaps()[0]
lyt = aprx.listLayouts()[0]
lyr = mapx.listLayers("ne_10m_admin_0_countries")[0]
lyr.definitionQuery = '"ADMIN" = ' + "'Chile'"
mFrame = lyt.listElements("MAPFRAME_ELEMENT")[0]
mFrame.camera.setExtent(mFrame.getLayerExtent(lyr, False, True))
lyr.definitionQuery = ""
return
class Slide2(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "Switzerland"
self.description = ""
self.canRunInBackground = False
def getParameterInfo(self):
"""Define parameter definitions"""
params = None
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."""
aprx = arcpy.mp.ArcGISProject("CURRENT")
mapx = aprx.listMaps()[0]
lyt = aprx.listLayouts()[0]
lyr = mapx.listLayers("ne_10m_admin_0_countries")[0]
lyr.definitionQuery = '"ADMIN" = ' + "'Switzerland'"
mFrame = lyt.listElements("MAPFRAME_ELEMENT")[0]
mFrame.camera.setExtent(mFrame.getLayerExtent(lyr, False, True))
lyr.definitionQuery = ""
return
Answer
*.PYT toolboxes are not supported for tasks in ArcGIS Pro 1.0 and 1.1.
However, they have been supported since ArcGIS Pro 1.2.
As a workaround try to insert your tool into a geoprocessing model, and then use a task step to call the model.
No comments:
Post a Comment