How to have Script Tool Output Data to Contents Pane as Tool Runs

650
2
05-14-2021 01:15 PM
ArthurCallan
New Contributor II

TLDR - Using ArcPro 2.4, how do I make script tools add data to the Contents Pane as the tool runs? The python script runs fine when ran in the python window as it adds the data  to the Contents Pane as it's created. Running as script tool does not work since it does not add data to Contents Pane as it runs. 

 

I have a Python Script that creates a fishnet, buffers fishnet cells, and then clips  imagery stored on our server to these fishnet cells. 

The script works perfectly fine when copied and pasted into the Python window and ran in ArcPro 2.4. Doing this causes the fishnet and all the rasters to be added to the map as the script runs.

I have created a script tool to make it easier for some of my coworkers that are not as tech savvy.

Problem is, the script tool does not output the fishnet data to the map as it runs, and the clipping of rasters fail due to not being able to reference the fishnet from the Contents Pane.

I have confirmed that my "Add output datasets to an open map" is enabled in my Geoprocesing Options.

Capture.PNG

How do I make it so the script tool adds data to the map as it is created? Any direction or links to help pages would be appreciated.

 

Script Below. I cut the second half of the script since I know it all works and just contains all the clipping of the Rasters. I just need to know how to get these buffered fishnet cells to output to the Contents Pane after they're created.

You can also see where I had to add a parameter at the beginning of the script to add a Buffer since the tool I run before this doesn't add data as well (unless ran in ArcPro's Python Window).

Thanks!

 

 

import arcpy

#USER INPUTS
QuadFolder = arcpy.GetParameterAsText(0)
quad_GDB = arcpy.GetParameterAsText(1)
RasterFolder = arcpy.GetParameterAsText(2)

#Add Buffer layer as previous tool does not add layers to Contents Pane as it runs

AddBuffer = arcpy.GetParameterAsText(3)
aprx = arcpy.mp.ArcGISProject("CURRENT")
aprxMap = aprx.listMaps("Map")[0]
aprxMap.addDataFromPath(AddBuffer)


#****************************  Static Names for Layers  *************************************************

#Rasters
BLM100K = r"\\path\to\data\BLM100K.tif"
Hillshade = r"\\path\to\data\Support Files\Specifications and Templates\ArcMap\SPR\SPR Layers\Hillshade.lyr"
NAIP = r"\\path\to\data\NAIP2017.gdb\Statewide_NAIP2017_1meter_NaturalColor"
USGS100K = r"\\path\to\data\USGS_Topo_Maps.gdb\USGS_TOPO_100K"
USGS24K = r"\\path\to\data\USGS_Topo_Maps.gdb\USGS_TOPO_24K"


#Direct workspace to QuadFolder
workspace = arcpy.env.workspace = QuadFolder

#Redirect workspace to GDB
workspace = arcpy.env.workspace = quad_GDB


#************************************** Raster *******************************************************

#Keep workspace in quad geodatabase to create fishnet to clip maps

#Describe the buffers extent and define bounding box variables

#1.Get buffer boundary minimum and maximum values
desc = arcpy.Describe("Buffer")
origin_coord =  str(desc.extent.lowerLeft)
y_axis_coord = str(desc.extent.upperLeft)
corner_coord = str(desc.extent.upperRight)
out_feature_class = "Other/Fishnet"
cell_width = ""
cell_height = ""
number_rows = "4"
number_columns = "3"
labels = "NO_LABELS"
template = "Buffer"
geometry_type = "POLYGON"
arcpy.CreateFishnet_management (out_feature_class, origin_coord, y_axis_coord, cell_width, cell_height, number_rows, number_columns, corner_coord, labels, template, geometry_type)


#Use select features to export each fishnet polygon to its own feature class
arcpy.Select_analysis("Fishnet", "D1", "OID = 1")
arcpy.Select_analysis("Fishnet", "D2", "OID = 2")
arcpy.Select_analysis("Fishnet", "D3", "OID = 3")


arcpy.Select_analysis("Fishnet", "C1", "OID = 4")
arcpy.Select_analysis("Fishnet", "C2", "OID = 5")
arcpy.Select_analysis("Fishnet", "C3", "OID = 6")


arcpy.Select_analysis("Fishnet", "B1", "OID = 7")
arcpy.Select_analysis("Fishnet", "B2", "OID = 8")
arcpy.Select_analysis("Fishnet", "B3", "OID = 9")


arcpy.Select_analysis("Fishnet", "A1", "OID = 10")
arcpy.Select_analysis("Fishnet", "A2", "OID = 11")
arcpy.Select_analysis("Fishnet", "A3", "OID = 12")


#Create a list of fishnet feature classes
fishnet_fcs = arcpy.ListFeatureClasses("*")

#Run a buffer on each fishnet in the list
#in_features = fishnet_fc
#out_feature_class = fishnet_fc +"buffer"
#buffer_distance_or_field = "250"
#arcpy.Buffer_analysis (in_features, out_feature_class, buffer_distance_or_field

for fishnet_fc in fishnet_fcs:
         arcpy.Buffer_analysis (fishnet_fc, fishnet_fc +"_buffer", 250)

#Create a list of fishnet buffered features classes
fishnet_buffers = arcpy.ListFeatureClasses("*buffer")

arcpy.Delete_management ("Fishnet")
arcpy.Delete_management ("A1")
arcpy.Delete_management ("A2")
arcpy.Delete_management ("A3")

arcpy.Delete_management ("B1")
arcpy.Delete_management ("B2")
arcpy.Delete_management ("B3")

arcpy.Delete_management ("C1")
arcpy.Delete_management ("C2")
arcpy.Delete_management ("C3")

arcpy.Delete_management ("D1")
arcpy.Delete_management ("D2")
arcpy.Delete_management ("D3")

#Redirect workspace to to Raster Folder
workspace = arcpy.env.workspace = RasterFolder

#Create an empty raster list
raster_convert_list = []


#1.Get buffer boundary minimum and maximum values
desc = arcpy.Describe("Buffer")
XMin =  str(desc.extent.XMin)
YMin =  str(desc.extent.YMin)
XMax =  str(desc.extent.XMax)
YMax =  str(desc.extent.YMax)

#********************Hillshade*************************

#Set variables for Imagery Clip

in_raster = Hillshade
rectangle = str(XMin)+ ' ' + str(YMin) + ' ' + str(XMax) + ' ' + str(YMax)
out_raster = "HS.tif"
in_template_dataset = "Buffer"
nodata_value = "255"
clipping_geometry = "ClippingGeometry"
maintain_clipping_extent = "MAINTAIN_EXTENT"

arcpy.Clip_management(in_raster, rectangle, out_raster, in_template_dataset, nodata_value, clipping_geometry, maintain_clipping_extent)

 

0 Kudos
2 Replies
JoeBorgione
MVP Emeritus

You'll probably want to post your code (and refer to this thread before you do); that way someone might be able to tell what is or isn't going on.

Also, ArcGIS Pro 2.8 was released yesterday (5/13/21) you should really consider upgrading; you are missing a at least a metric ton of functionality.

That should just about do it....
ArthurCallan
New Contributor II

Originally had it there, but removed it since it is a long script and wasn't entirely sure it was needed as this seems to be more under the hood with ArcPro than the script. Updated original post with script included. Thankfully it's this one that is broken and not my other one that is like 6 times longer 😀

As for updating ArcPro, that is all up to our IT department since they deal with the contract with ESRI. Not up to me unfortunately.

0 Kudos