I'd like to read from the attribute table of an intermediate raster generated by arcpy.sa.Combine:
combined = arcpy.sa.Combine(['raster1', 'raster2'])
arcpy.BuildRasterAttributeTable_management(combined)
data = [row for row in arcpy.da.SearchCursor(combined, ["VALUE", "COUNT"])]
The above code raises:
RuntimeError: 'in_table' is not a table or a featureclass
Is there a way to access the RAT without saving the raster?
-
Try to create raster layer from it.FelixIP– FelixIP2018年09月28日 19:38:40 +00:00Commented Sep 28, 2018 at 19:38
1 Answer 1
Try setting arcpy.env.workspace = 'in_memory'
, or the cursor will not know where to find the raster.
It could also be that combined
is a raster object which the cursor cannot read. Try "saving" it to in_memory workspace:
combined.save(r"in_memory\outcombine2")
data = [row for row in arcpy.da.SearchCursor(r"in_memory\outcombine2", ["VALUE", "COUNT"])]
-
The second solution worked for me: I had to call
.save()
on the raster and then I could useSearchCursor
on it like any other table. This is odd, because you can print the variable containing the output of your raster tool (in your example, you could usearcpy.AddMessage(f"Output raster={combined}"
) and it too will be a path to a raster. But you can't read its contents as a table. In my case I was usingarcpy.sa.ExtractByAttributes
, so a lot of raster tools probably do this.Cowirrie– Cowirrie2024年03月08日 12:59:34 +00:00Commented Mar 8, 2024 at 12:59