# raster area calculation using python script

Hi I am a beginner to python programming in GIS and am trying to calculate the area of a raster. When I ran the program, I got error message below. I am also providing my code. Any help would be greatly appreciated!

import arcpy
from arcpy import env

#To overwrite output
arcpy.env.overwriteOutput = True

#Set environment settings

#set local variables-STI extraction
inRaster ="sti"

# Create a search cursor for raster attribute

count = row.getValue("Count")

print count


Error Message:

"Traceback (most recent call last):
File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\__init__.py", line 1167, in SearchCursor
return gp.searchCursor(dataset, where_clause, spatial_reference, fields, sort_fields)
File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\geoprocessing\_base.py", line 359, in searchCursor
self._gp.SearchCursor(*gp_fixargs(args, True)))
IOError: "inRaster" does not exist"

-
You variable is inRaster but you are passing it to your search cursor as a string "inRaster". Also, you should look into the data access module for an updated cursor object that's faster and more reliable. –  Paul Jul 11 at 14:23
Try throwing a print statement after you define inRaster...if it fails, perhaps you need a / after raster-read –  Dan Patterson Jul 11 at 14:24
like @Paul said remove the quotes from inRaster in your search cursor also use arcpy.da.SearchCursor() instead. –  Chris R Jul 11 at 15:01

A very simple way to calculate the area of a raster is:

raster = <path to raster>
ext = arcpy.Describe(raster).extent
area = ext.width * ext.height

-
This method includes the area of cells with NoData values. –  Luke Jul 11 at 22:10

The following script uses a SearchCursor to extract the "Count" field rows. You can see that I am using the da module, which is available with ArcGIS 10.1+ as this method is much more efficient.

import arcpy

raster = r'C:\path\to\raster'

# Create a search cursor for raster attribute
with arcpy.da.SearchCursor(raster, ["OBJECTID", "COUNT"]) as cursor:
for row in cursor:
# You may need to do a conversion if pixels are in units other than m
print "OID %s has an area of %s m^2" % (row[0], row[1])


-
Thank you very much Paul, dan, Chris,and Aaron for your time and help!. I removed the quotes from inRaster and used arcpy.da.SearchCursor() instead of arcpy.SearchCursor(). I did get an error below: Error: Traceback (most recent call last): File "C:/Subhasis/Test/raster-read/read-raster.py", line 15, in <module> read = arcpy.da.SearchCursor(inRaster,"","","Value;Count","") RuntimeError: cannot open 'sti' –  Inception Jul 11 at 17:40
Thank you Roman! I used your code and got some number. But little bit confused regarding the units as it doesn't provide the units. Anyways, again thanks a lot guys for your help! This is pretty helpful for beginner like me in scripting in GIS. I was wondering "Could you please suggest me (beginner to Python scripting) some books or any website that would help me learning Python script for GIS. Have a good weekend guys! –  Inception Jul 11 at 18:18
@Subhasis Regarding the units, lets say you have a pixel count of 10 and your pixels are at 1m spatial resolution. This means that the total area is 10 m^2. You can convert this to whatever you want (e.g. acres, hectares, ft etc). –  Aaron Jul 11 at 18:36
Thank you Aaron! –  Inception Jul 11 at 18:38

Instead of using cursors to find the raster area I would use Describe and use the raster band properties to find the area of my raster. Once I know the cell width, cell height, raster width, and raster height of my raster I can calcuate its area. However, this method does not give the units of the raster, you'll need to know them ahead of its calculation.

For example:

arcpy.env.workspace = r"C:\Example_Workspace"
rast = "ExampleRaster"
descRast = arcpy.Describe(rast)
x_cell = descRast.meanCellWidth
y_cell = descRast.meanCellHeight
x_rast = descRast.width
y_rast = descRast.height

rasterHeight = y_cell * y_rast
rasterWidth = x_cell * x_rast
rasterArea = rasterHeight * rasterWidth

print "The area of {0} is {1}".format(rast, rasterArea)

-
I think you mean descRast = arcpy.Raster(rast)? Describe objects don't have all of those properties. –  Paul Jul 11 at 16:41
Nope, Raster Band Properties are included in arcpy.Describe(). Describe(arcpy), Geoprocessor Program Model –  Roman Jul 11 at 17:00
Ahh, but if you look closely, it says that it only works on " Raster Bands". If you try that with a 3 band image, it fails. Add only one of the bands, and it works. –  Paul Jul 11 at 17:10

There's another "Ah..." here as well. If you make a raster object from the raster reference like :

PathtoMyRaster = "c:/Temp/MyRaster.tif"
ras = arcpy.Raster(PathtoMyRaster)


Then the raster object has properties like : . bandCount, extent, mean, min, max, meanCellHeight, meanCellWidth, height, width. So you can access them that way

-