0

I am learning Python. I have a script that when copied and ran in ArcGIS Pro's Python Window, it works flawlessly as it adds the feature classes to the Contents pane. However, when ran as a script tool from a toolbox, the script fails because data is not added to the Contents pane. Since data is not added to the Contents pane as it is created, the script is unable to clip the raster files.

Is there something I am missing in the script or an option in the script tool properties?

Is there a way I can use something such as aprxMap.addLayer in the script to add the buffered fishnet cells to the Contents pane?

I tried but was not able to get it to work, possibly improper usage of combining the quad_GDB variable and the rest of the directory to the fishnet cells. I can't remember what I tried, but I think it was something like aprxMap.addLayer(quad_GDB + "\A1_buffer).

I have been able to stumble through most of my scripts through trial and error, but this one has me stumped.

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 automatically

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\BLM\BLM100K.tif"
Hillshade = r"\\path\to\017CartoStaff\GenMapping\Archives\Mapping\Support Files\Specifications and Templates\ArcMap\SPR\SPR Layers\Hillshade.lyr"
NAIP = r"\\path\to\NAIP2017\NAIP2017.gdb\Statewide_NAIP2017_1meter_NaturalColor"
USGS100K = r"\\path\to\TopoMaps\USGS_Topo_Maps.gdb\USGS_TOPO_100K"
USGS24K = r"\\path\to\TopoMaps\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)


#********************USGS 100k Topo *************************

#Set variables for USGS 100k Clip

in_raster = USGS100K
rectangle = str(XMin)+ ' ' + str(YMin) + ' ' + str(XMax) + ' ' + str(YMax)
out_raster = "USGS100k.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)


#********************BLM 100k Topo *************************

#Set variables for BLM 100k Clip

in_raster = BLM100K
rectangle = str(XMin)+ ' ' + str(YMin) + ' ' + str(XMax) + ' ' + str(YMax)
out_raster = "BLM100k.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)


#******************** 24k Topo ********************************

in_raster = USGS24K
rectangle = str(XMin)+ ' ' + str(YMin) + ' ' + str(XMax) + ' ' + str(YMax)
out_raster = "USGS24k.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)


#Divide the newly clipped image by the 6 buffered polygon feature classes
for buffer in fishnet_buffers:
    desc = arcpy.Describe(buffer)
    coords=str(desc.extent.XMin) + " " + str(desc.extent.YMin) + " " + str(desc.extent.XMax) + " " + str(desc.extent.YMax)
    outraster = buffer.replace("buffer","24k.tif")
    arcpy.Clip_management("USGS24k.tif", coords, outraster, buffer, "255")
    raster_convert_list.append(outraster)


#******************** NAIP ********************************

in_raster = NAIP
rectangle = str(XMin)+ ' ' + str(YMin) + ' ' + str(XMax) + ' ' + str(YMax)
out_raster = "NAIP.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)

#Divide the newly clipped image by the 6 buffered polygon feature classes
for buffer in fishnet_buffers:
    desc = arcpy.Describe(buffer)
    coords=str(desc.extent.XMin) + " " + str(desc.extent.YMin) + " " + str(desc.extent.XMax) + " " + str(desc.extent.YMax)
    outraster = buffer.replace("buffer","NAIP.tif")
    arcpy.Clip_management("NAIP.tif", coords, outraster, buffer, "255")
    raster_convert_list.append(outraster)


#************************************* Organization and Cleanup****************************

#Buffer

arcpy.Delete_management ("A1_Buffer")
arcpy.Delete_management ("A2_Buffer")
arcpy.Delete_management ("A3_Buffer")

arcpy.Delete_management ("B1_Buffer")
arcpy.Delete_management ("B2_Buffer")
arcpy.Delete_management ("B3_Buffer")

arcpy.Delete_management ("C1_Buffer")
arcpy.Delete_management ("C2_Buffer")
arcpy.Delete_management ("C3_Buffer")

arcpy.Delete_management ("D1_Buffer")
arcpy.Delete_management ("D2_Buffer")
arcpy.Delete_management ("D3_Buffer")
0

1 Answer 1

2

I think @KHibma is correct, the expectation is that a Geoprocessing tool creates an output. To do that you need to set the output as a derived parameter and the system handles it correctly, it also makes your tool more compatible with connecting it to other tools if you want others to use it correctly in modelbuilder

As by way of an example I created a script that simply buffers the input FeatureClass and sets the output to be a FeatureLayer.

Setting parameters

The code would be:

import arcpy
arcpy.env.overwriteOutput=True
outfc = r"c:\scratch\fGDB_Scratch.gdb\testbuffer"

# Get input FeatureClass and buffer
fc = arcpy.GetParameterAsText(0)
arcpy.Buffer_analysis(fc,outfc,"20 METERS")

# Your output featureclass is converted to a FeatureLayer as that is what the parameter is
arcpy.SetParameterAsText(1,outfc)

But if you want to ignore this approach and add it manually then in this example, drop the derived output parameter from the script interface and use the code:

import arcpy
arcpy.env.overwriteOutput=True
outfc = r"c:\scratch\fGDB_Scratch.gdb\testbuffer"

# Get input FeatureClass and buffer
fc = arcpy.GetParameterAsText(0)
arcpy.Buffer_analysis(fc,outfc,"20 METERS")

# Get Map
aprx = arcpy.mp.ArcGISProject("CURRENT")
aprxMap = aprx.listMaps("Map")[0]

# Create a layer and add it to the map
res = arcpy.MakeFeatureLayer_management(outfc,"my new layer")
aprxMap.addLayer(res.getOutput(0))

I think why your attempt to use addLayer() failed is because you were adding a full path string and not a layer object.

3
  • Thanks! Your answer gave me more insight than Esri's site did! I am stumped again. Your parameter example showed how to setup an input parameter for a feature class, which then outputs a buffered feature layer with an output parameter. In my script, the only input parameters are for folder and GDB workspaces. How would I go about using feature classes that are made by this script? I tried creating the variable A1buff = quad_GDB + "\A1_buffer" followed by 'arcpy.SetParameterAsText(4,A1_buffer) to no avail. Also confirmed I have added the derived parameter to the script tool.
    – Duff
    May 18, 2021 at 22:50
  • Think of Set parameter as an end result. Looking at your code it seems like you want to create, use then destroy your buffers? You are also resetting the workspace at various stages so it becomes difficult to understand what workspace a tool is using. I tend to avoid that coding style for that very reason. Refer to your buffers and fishnets by full path strings this will simplify your code.
    – Hornbydd
    May 19, 2021 at 8:08
  • 1
    I just wanted to thank you for taking the time to explain this to me! I was able to get this solved due to your input!
    – Duff
    May 24, 2021 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.