1

I am looping through 2 sets of rasters to do a extract by mask of one raster using the other, with ArcPy.

However, if the mask raster does not exist I want to skip to the next set of rasters.

Both sets of rasters are named the same.

for inras in inras_list[5:]:
    rasx=arcpy.sa.Raster(inras)
    base=(os.path.basename(inras).split(".")[0])
    print base
    trial=os.path.join(base+"_wilderness_reclass.tif") #name of second set of rasters
    print trial
    rasy=arcpy.sa.Raster(os.path.join(workspace2, trial))
    print (os.path.join(workspace2, trial))
    if os.path.isfile(os.path.join(workspace2, trial)):
            inras_extent = rasx.extent
            print inras_extent
            maskras_extent = rasy.extent
            print maskras_extent
            arcpy.env.extent = rasy
            out_extract = arcpy.sa.ExtractByMask(rasx, rasy)
            outname = os.path.join(outdir, os.path.splitext(inras)[0] + "_opporwithoutroad_withoutwilderness.tif")
            arcpy.BuildRasterAttributeTable_management(out_extract)
            out_extract.save(outname)
    else:
            print "Wilderness raster does not exist for the state. Saving original ras x as is"
            out_same= rasx
            outname = os.path.join(outdir, os.path.splitext(inras)[0] + "_opporwithoutroad_withoutwilderness.tif")
            arcpy.BuildRasterAttributeTable_management(out_same)
            out_same.save(outname)

But the loop stops at the 9th file (because yes one set of rasters does not exists) and does not move to the next set of rasters i.e. 10th file.

The error I get-

RuntimeError: ERROR 000732: Input Raster: Dataset E:\Staging\GLCarbonScience\Reforestation_Attribution\Outputs\Anciliary_variables\PAD_US_Outputs\Wildnerness\Wilderness_raster_reclass\09_wilderness_reclass.tif does not exist or is not supported

Any solutions?

  • 1
    Does any portion of the else occur e.g. does the print statement print? – smiller May 22 '19 at 15:24
  • 1
    What is in inras_list? How did you define it? Does it print the message in the else statement? Do you receive any error? – umbe1987 May 22 '19 at 15:24
  • Nothing. And I don't understand why the if() is not being skipped to the else() section. – tg110 May 22 '19 at 15:24
  • @umbe1987- inras_list is just a list of rasters defined as arcpy.ListRasters() to list all the rasters in the folder. I don't get the print statement in the else() section. I get the error that files does not exist, I guess after the if() section, but that is all. – tg110 May 22 '19 at 15:26
  • 1
    You might be getting the error ABOVE the print statement in line 7, where you call arcpy.sa.Raster on a non-existent file. – smiller May 22 '19 at 15:27
3

Try moving line 7

rasy=arcpy.sa.Raster(os.path.join(workspace2, trial))

into the if statement.

It looks like this is being called on a non-existent .tif and will fail, causing the script to error out before reaching the if statement for this particular dataset.

for inras in inras_list[5:]:
    rasx=arcpy.sa.Raster(inras)
    base=(os.path.basename(inras).split(".")[0])
    print base
    trial=os.path.join(base+"_wilderness_reclass.tif") #name of second set of rasters
    print trial

    print (os.path.join(workspace2, trial))
    if os.path.isfile(os.path.join(workspace2, trial)):
            rasy=arcpy.sa.Raster(os.path.join(workspace2, trial))
            inras_extent = rasx.extent
            print inras_extent
            maskras_extent = rasy.extent
            print maskras_extent
            arcpy.env.extent = rasy
            out_extract = arcpy.sa.ExtractByMask(rasx, rasy)
            outname = os.path.join(outdir, os.path.splitext(inras)[0] + "_opporwithoutroad_withoutwilderness.tif")
            arcpy.BuildRasterAttributeTable_management(out_extract)
            out_extract.save(outname)
    else:
            print "Wilderness raster does not exist for the state. Saving original ras x as is"
            out_same= rasx
            outname = os.path.join(outdir, os.path.splitext(inras)[0] + "_opporwithoutroad_withoutwilderness.tif")
            arcpy.BuildRasterAttributeTable_management(out_same)
            out_same.save(outname)

It's also possible that the name of the tif is causing some problems -- see Raster Naming Conventions. http://desktop.arcgis.com/en/arcmap/latest/extensions/spatial-analyst/performing-analysis/output-raster-formats-and-names.htm

1

You can use try/except to continue to the next set when error occured.

for inras in inras_list[5:]:

    try:
        <your code>
    except:
        print ('error')

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.