I am looping through a gdb of feature classes and matching them up with rasters from another gdb, in order to perform tabulate area. When I go to run the script, it clims the zone fc doesn't exist, even though it does and I previously printed the list containing it. I had this issue before, and I had to add everything to the map table of contents to get it to work. However, all of my zone fcs have aliases that do not match the feature class name, so I think this might be causing it to continue to throw the same error, that it doesn't exist. Stumped!! Please see script.
# Repeat for Cropscape
crop_out_gdb = r'E:\FATHOM\2020FATHOM\Analysis\MR_cropscape_2016_FPstats_02062021.gdb'
cropscape_gdb = r'E:\FATHOM\2020FATHOM\Analysis\MR_Cropscape_clip_HUC02.gdb'
arcpy.env.workspace = cropscape_gdb
crop_rasters = arcpy.ListRasters()
print (crop_rasters)
catch_fgdb = r'E:\FATHOM\2020FATHOM\Analysis\MR_Catchments_prjAlbers.gdb'
arcpy.env.workspace = catch_fgdb
catch_fcs = arcpy.ListFeatureClasses()
print (catch_fcs)
arcpy.env.workspace = cropscape_gdb
for crop_raster in crop_rasters:
for catch_fc in catch_fcs:
if crop_raster.split("_")[-1] == catch_fc.split("T")[-1]:
env.snapRaster = crop_raster
inZoneData = catch_fc
zoneField = "FtrIDstr"
inClassData = crop_raster
classField = "Value"
out_table_name = "Cropscape_MR_floodplain_stats" + catch_fc.split("_")[-1]
out_table_path = os.path.join(crop_out_gdb, out_table_name)
processingCellSize = 30
TabulateArea(inZoneData, zoneField, inClassData, classField, out_table_path, processingCellSize)
Error: Traceback (most recent call last):
File "<string>", line 12, in <module>
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\sa\Functions.py", line 8645, in TabulateArea
classes_as_rows)
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\sa\Utils.py", line 53, in swapper
result = wrapper(*args, **kwargs)
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\sa\Functions.py", line 8636, in Wrapper
classes_as_rows)
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 511, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000865: Input raster or feature zone data: Prj_Conic_Albers_T002 does not exist.
WARNING 001000: Zone field: Field FtrIDstr does not exist
Failed to execute (TabulateArea).
Solved! Go to Solution.
for crop_raster in crop_rasters:
for catch_fc in catch_fcs:
if crop_raster.split("_")[-1] == catch_fc.split("T")[-1]:
print("{} - {}".format(crop_raster, catch_fc)
else:
print print("No match\n {} - {}".format(crop_raster, catch_fc)
For now quit trying to do something and replace your code with this. Then see what pattern is emerging that is causing the mismatch.
I can't tell without seeing the input filenames and what problems they may cause.
did you import env separately somewhere? If I type or dir 'env' in my ide, I get a whole list of system environment variables and not arcpy ones.
env.snapRaster
Traceback (most recent call last):
File "<ipython-input-146-432a0c4551d8>", line 1, in <module>
env.snapRaster
NameError: name 'env' is not defined
print(arcpy.env.snapRaster) # --- I don't have one set so it is None
None
dir(arcpy.env)[:5] # ---- first 5 env variables
['MDomain',
'MResolution',
'MTolerance',
'S100FeatureCatalogueFile',
'XYDomain']
As for alias's, do you mean the fields have aliases? You are going to have to match them either wholely or partially
Thank you! Ok, I thought I had imported env, but I guess I hadn't. That began processing, but my output tables only have ObjectID and the zoneField as fields, and no actual data. All are in the same projection, so I'm not sure why I would be getting blank outputs? I just converted the catch_fc to rasters, and tested the
crop_raster.split("_")[-1] == catch_raster.split("T")[-1]:
is mismatched. When I print the first half, I get 002, but when I print the other half, I get 018. It's getting the correct part of the string, but they should match, as there is a raster_fc ending in 002. When I run tabulate area outside of this script on one dataset, it works. I wonder why it would be out of sequence?
for crop_raster in crop_rasters:
for catch_fc in catch_fcs:
if crop_raster.split("_")[-1] == catch_fc.split("T")[-1]:
print("{} - {}".format(crop_raster, catch_fc)
else:
print print("No match\n {} - {}".format(crop_raster, catch_fc)
For now quit trying to do something and replace your code with this. Then see what pattern is emerging that is causing the mismatch.
I can't tell without seeing the input filenames and what problems they may cause.
Thank you! I didn't realize that a mismatched or extra dataset with no match would cause a problem, I thought it would simply just not perform the task on that dataset since it had no match.....