# How can I calculate the number of raster pixels within polygons?

I have two layers, flood area (flood.img) as a raster and flow direction as polygons. Would you please help me in calculating the number of pixels in each flow direction polygon? I want to get the percentage of flood area in each flow direction polygon. I use ArcGIS 10.

A solution will require several steps (each of them simple), but no scripting, looping or "model building" are required.

As an intermediate result, we're aiming to build a table that counts pixels by flow direction within polygon. It will look something like this:

``````PolygonID  FlowDirection Count
1              1    34
1              2   101
1              4    79
1              8    16
1             16     2
1            128     7
2              1  1099
...
134             64   370
134            128   299
``````

(Typically, flow directions are coded as powers of 2, {1,2,4,8,16,32,64,128}. The polygon identifiers can be anything.)

With this table in hand, it is straightforward to sum the counts by polygon (a "summarize" operation), join the sum to the table (a "table join"), and divide each count by the polygon sum (using the Field Calculator): those are the desired proportions of each flow direction within each polygon.

Let's focus on obtaining this table.

One approach is to combine the flow direction and polygon grids. The new grid contains four fields: as usual, there is a "value" and "count" field; in addition, one field records the flow direction in the cell and the other records the polygon identifier. But that's precisely the table described above (with an additional "value" field, which goes unused).

• Dear Whuber.I used the Combine function, but at last I just got the number of pixel in each flow direction (1,2,4,8,16,32,64,128). This task can be done in Erdas Imagine with Matrix function. But the main point I want to calculate is the area of flood (or the number of pixel) in each polygon. I can not understand the part " join the sum to table , divide each...." that to told me. So would you mind explain for me more clearly! Thanks – quangminh May 4 '12 at 2:03
• If you only have the counts by flow direction after `combine`, you probably have not uniquely identified each polygon. Do that first using `RegionGroup`. The other operations--join, divide, etc--require discussions of their own, so it would be best to ask them as new questions once you have succeeded with `Combine` in creating the table of counts by flow direction and polygon. – whuber May 4 '12 at 13:53

Using Zonal Statistics with "Input raster or feature zone data" is your flow direction polygon and Input value raster is the raster flood area. The output of this tool would be a raster with a field named COUNT, that's the number of pixels you need.

• Thank for your help. I have already done, but finally I got the number of pixel flood of the whole flow direction. In my case, the flow direction shape file has many polygon with the code of different directions (8 direction: North, West, South...). So I want to calculate the number of pixel (flood area) in each polygon. It seems difficult. I tried the Union, Intersect, Zone statistic but I can not get any idea to solve this problem. DO you have any idea? Thank you so much – quangminh May 3 '12 at 7:06
• In your case, i think you may want to do a routine that loop thru each polygon and run zonal statistics with the polygon and flood raster. As you are using ArcGIS 10 and assuming that you have little knowledge on scripting automatically, i think you should try ModelBuilder with Iterators Tool. – VietThanh Le May 3 '12 at 7:24
• I will try Model Builder. Hope that can be done! – quangminh May 4 '12 at 2:05