Get sum of value from raster file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import arcpy
from arcpy.sa import *
arcpy.CheckOutExtension("Spatial")

def getValueSum(rasterPath):
raster=arcpy.Raster(rasterPath)
raster2 = SetNull(raster == 0,raster)
sum=0
rstArray = arcpy.RasterToNumPyArray(raster2)
rows, cols = rstArray.shape
for rowNum in xrange(rows):
for colNum in xrange(cols):
s=rstArray.item(rowNum, colNum)
sum=sum+s
return sum