0

I need to run my code through a lot of rasters that i've downloaded with "Download Raster" function, but some rasters have no information, so my script fails. I wonder if there's any way to use an if/else statement to solve my problem, in other words, if there's statsitics in the image, run the code, else, ignore the raster and select the next one.

I tried this, but it's not working...

for tiff in rasters:

    NodataResult = arcpy.GetRasterProperties_management (tiff, "ANYNODATA", "")
    Nodata = NodataResult.getOutput


    if Nodata = 1

        CalculateStatistics_management (tiff)

    else

        arcpy.MakeRasterLayer_management(tiff, "Z:\\04 - Temp\\Charles\\NDVI_tempo\\NAL\\temp_rast1.tif", "", "", "3")
        arcpy.MakeRasterLayer_management(tiff, "Z:\\04 - Temp\\Charles\\NDVI_tempo\\NAL\\temp_rast2.tif", "", "", "4") 
5
  • 1
    Yes you can use if/else logic with some python scripting. – Hornbydd Sep 27 '17 at 11:43
  • Do you have any ideia about how can i write that? I'm new in python scripting – Charlera Sep 27 '17 at 11:45
  • 2
    There are plenty of examples in the help file, look here, they show you code or similar approaches in modelbuilder. – Hornbydd Sep 27 '17 at 11:51
  • 1
    You're not using Python if syntax (missing colons) and you're using the assignment operator instead of equivalence. – Vince Sep 27 '17 at 12:37
  • 1
    What you are looking for is try: and except: though if and else may work if you correct your syntax errors as @Vince has pointed out. – jbchurchill Sep 27 '17 at 13:00
3

A simple if statement by your example (note the double "==" for comparison), goes like:

if NoData == 1:
   #do something here
else:
   #do something different here

You can find more detailed examples on the web. Here you can learn abit more about comparing values to build your statement.

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.