Averaging raster cells that fall within a polygon

2101
3
04-29-2014 08:59 AM
KeithWeber1
New Contributor III
Hello,

I am about to begin developing a function that has a polygon featureclass and a raster as inputs. The purpose of the function is to calculate the average of the raster cell values that fall within each polygon.

For example

polygon feature class contains 3 polygons

Polygon 1 overlaps/contains 3 cells with values 1,2,3
Polygon 2 overlaps/contains 5 cells with values 1,3,5,7,9
Polygon 3 overlaps/contains 3 cells with values 7,8,9

The output would be
Polygon 1 average = 2
Polygon 2 average = 5
Polygon 3 average = 8


The only way that I know to do this is to loop through each cell of the raster, create a spatial query using the center point of the raster cell as the geometry, querying the polygon featureclass to get the polygon that contains the point, and finally having some data structure that holds the values for each polygon in order to compute the averages after the loop.

These seems like a very inefficient way of implementing this functionality. Does anyone have any suggestions on a better way to implement this?
0 Kudos
3 Replies
HailiangShen
New Contributor
I think there are more efficient ways to do this. This is what i will use:

1 find the extent of a given polygon
2 align this extent to cell side center by comparing with raster extent
3 generate points within this aligned extent based on cell size, these points will be the centers of cells
4 loop the generated cells to read pixel values and average them

Hailiang
0 Kudos
KeithWeber1
New Contributor III
Hailiang,

Thanks for the reply! I am going to implement both and do some performance testing with some different scenarios.

-Keith
0 Kudos
KeithWeber1
New Contributor III
I think I've found an easier way to get the functionality I am looking for. If I convert the raster to a point featureclass, then I can perform a spatial query on the point featureclass for each polygon using the contains spatial relationship. This allows me to perform one spatial query per polygon rather than performing a spatial query for each raster cell.
0 Kudos