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 "", line 20, in
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.
Answer
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.
No comments:
Post a Comment