2

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.

2

If I'm understanding you correctly, you can use 'if' for all of them. 'elif' is only needed if you want to test another condition if it fails the first condition. In this case you want to add anyone that does exist.

if arcpy.Exists(rasA):
    inrasList.append('Raster(rasA)')
if arcpy.Exists(rasB):
    inrasList.append('Raster(rasB)')
if arcpy.Exists(rasC):
    inrasList.append('Raster(rasC)')
  • But of course! You're correct, it does correctly append but I'm running into another problem the appended list is not recognized as a valid input for the mosaic to new raster. – cptpython Sep 19 '16 at 20:03
  • 1
    It's expecting a semicolon delimited string. Try this ";".join(inrastList) – Brian Sep 19 '16 at 20:17
  • Take a look at the examples at the bottom of the page: MosaicToNewRaster – Brian Sep 19 '16 at 20:18
  • using ";".join(inrasList) the tool successfully executes but creates blank rasters. I've checked the input rasters are valid and their bit-depths are consistent. – cptpython Sep 20 '16 at 18:15
  • @cptpython you should probably post this question as a new post as it's moved away from the original question. – Brian Sep 21 '16 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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