1

Last week I asked Looping through two raster folders to perform raster calculation? about a script to loop through two geodatabases and perform a raster calculation by matching the two rasters up based on their coordinates. This is my post for clarification:

"I have two geodatabases with rasters containing information about "treecover" and "loss". The file names are slightly different but can be matched based on the last 8 characters (the coordinates of each raster granule).See image

I can perform this simple conditional statement using either CON or RASTER CALCULATOR to retrieve all "Loss" data situated on pixels which have a value for "treecover" greater than or equal to 50, with 9999 being assigned as the output value if the statement is FALSE. The output raster is exactly what I want. I just need to do it for 137 pairs of rasters

RASTER CALC STATEMENT: con("treecover raster" >= 50, "Loss raster", 9999)"

The script so far looks like this:

import arcpy, os, math
arcpy.CheckOutExtension("Spatial")
from arcpy.sa import *
CVR = r"F:\Work\Aquifers_recalc\Final_recalc\Continent_Data\TEST\Treecover_TEST.gdb"
LSS = r"F:\Work\Aquifers_recalc\Final_recalc\Continent_Data\TEST\Lossyear_TEST.gdb"
# get first set of rasters
arcpy.env.workspace = CVR
cvr_rasters = arcpy.ListRasters()

# get second set of rasters
arcpy.env.workspace = LSS
lss_rasters = arcpy.ListRasters()
arcpy.env.workspace = r"F:\Work\Aquifers_recalc\Final_recalc\Continent_Data\TEST"
for cvr_ras, lss_ras in zip(cvr_rasters, lss_rasters):
    r1 = arcpy.sa.Raster(cvr_ras)
    r2 = arcpy.sa.Raster(lss_ras)
    result = Con(r1 >=50, r2, 999)
    out_name = "loss_{}.tif".format(cvr_ras[-8:])
    result.save(out_name)

I keep getting an error saying:

ERROR 000732: Input Raster: Dataset Hansen_GFC2015_treecover2000_00N_010E does not exist or is not supported

I think something with my paths is wrong?

UPDATE: THIS ERROR WAS FIXED BUT WHEN APPLYING SCRIPT TO LARGER FOLDER I GET THIS ERROR:

Runtime error 
Traceback (most recent call last):
  File "<string>", line 20, in <module>
  File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy\sa\Functions.py", line 269, in Con
    where_clause)
  File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy\sa\Utils.py", line 53, in swapper
    result = wrapper(*args, **kwargs)
  File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy\sa\Functions.py", line 256, in Wrapper
    ["IfThenElse", in_conditional_raster, in_true_raster_or_constant, in_false_raster_or_constant])
RuntimeError: ERROR 999998: Unexpected Error.
2

cvr_rasters will be a list of only raster names with no path/workspace. When you change workspace after it is created the rasters will not be found. Try adding path to each raster:

...
arcpy.env.workspace = CVR
cvr_rasters = arcpy.ListRasters()
cvr_rasters = [os.path.join(CVR, r) for r in cvr_rasters]
...

Same goes for lss_rasters since you change workspace again after this is created.

  • Hmm for some reason when I try running this with new folders containing more rasters it fails on the 6th raster (out of 9) with an error that I included in the original post – seak23 Aug 26 at 18:49
  • That is another error and question you should post as a new one. Add a print statement to see which raster it is. – BERA Aug 26 at 18:59

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.