Select to view content in your preferred language

Area of a raster in Python

1364
2
Jump to solution
11-08-2012 05:01 PM
NataliaOcampo
New Contributor
Hello,

I have a file full of rasters with a Value of 1, the rest is NoData.
I need to extract the Count for each raster, to then calculate the area, by multiplying by the cellsize.

I was trying to do this with the Get Raster Properties tool, using UNIQUEVALUECOUNT as the property type yin the script but instead of returning the count, it would return a 1.

  count = arcpy.GetRasterProperties_management (fileName, "VALUETYPE")

Is there an automated way to extract the count from a raster, using Python????

I would really appreciate your help.

thanks!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MarcinGasior
Frequent Contributor
There's no simple property of COUNT for raster values.
It can be read from raster attribute table with a search cursor in this way:
import arcpy,  inRaster = r"C:\test.gdb\myRaster"  # Build attribute table if needed #arcpy.BuildRasterAttributeTable_management(inRaster, "OVERWRITE")  # Create a search cursor for desired raster VALUE sCur = arcpy.SearchCursor(inRaster, '"VALUE" = 1') for row in sCur:     cellCount = row.getValue("COUNT")     print cellCount

View solution in original post

0 Kudos
2 Replies
MarcinGasior
Frequent Contributor
There's no simple property of COUNT for raster values.
It can be read from raster attribute table with a search cursor in this way:
import arcpy,  inRaster = r"C:\test.gdb\myRaster"  # Build attribute table if needed #arcpy.BuildRasterAttributeTable_management(inRaster, "OVERWRITE")  # Create a search cursor for desired raster VALUE sCur = arcpy.SearchCursor(inRaster, '"VALUE" = 1') for row in sCur:     cellCount = row.getValue("COUNT")     print cellCount
0 Kudos
NataliaOcampo
New Contributor
Thank you so much!

This was very helpful!
0 Kudos