In a geodatabase, I have several rasters with different suffixes such as (_a, _b, _c). Some gdbs contain all three rasters and others are missing one or more. I'm trying to use if-else statement along with arcpy.Exists
to check if true then use it as an input, if false ignore it. Much of the script has map algebra equations and other raster tools. The issue is that with my current method, I'm unable to append the remaining strings from elif
statements to the list so it can be used as the input for the mosaic to new raster tool. I'm using 10.4 and yes this is a crude method of doing of if-else statements and any suggestions are welcomed. This is my sample code block:
import arcpy,os
# Local variables:
rasA = os.path.join("C:/Test_Folder/","county.gdb/" + "raster_a")
rasB = os.path.join("C:/Test_Folder/","county.gdb/" + "raster_b")
rasC = os.path.join("C:/Test_Folder/","county.gdb/" + "raster_c")
print ("rasA", arcpy.Exists(rasA))
print ("rasB", arcpy.Exists(rasB))
print ("rasC", arcpy.Exists(rasC))
('rasA', True)
('rasB', True)
('rasC', True)
inrasList = [ ];
if arcpy.Exists(rasA):
inrasList.append('rasA')
if arcpy.Exists(rasB):
inrasList.append('rasB')
if arcpy.Exists(rasC):
inrasList.append('rasC')
print (inrasList)
['Raster(rasA)','Raster(rasB)','Raster(rasC)']
# Step 1 - Mosaic to New Raster
rasLocation = "C:/Test_Folder/output/"
arcpy.MosaicToNewRaster_management(inrasList,rasLocation,"new_mos_ras","", "4_BIT", "1", "1", "LAST", "FIRST")
The append tool works using Brian's correction, but the list is not recognized as a valid input for mosaic to new raster tool.
semiC = ";"
inrasList1 = semiC.join(inrasList)
rasLocation = "C:/output/"
arcpy.MosaicToNewRaster_management(inrasList1,rasLocation,"new_mos_ras","", "4_BIT", "1", "1", "LAST", "FIRST")
This successfully executes the tool, but creates a blank raster.
The code has been updated. I was using Raster(rasA)
whereas the input should be simply rasA
.