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")