How can you count pixels in a raster by value?

11978
11
02-06-2013 11:09 AM
EmmaYoung
New Contributor
Hi-

I have a raster file classified into 5 values (1-5, zero being no data), with points overlaid in a shapefile.

What I'm trying to do is, within a 2 km buffer around each of these points, count the number of pixels of each value. I've attached a photo. I'm pretty new to Arc, but what I've done so far is use the Zonal tool in the spatial analyst toolbox to export the zonal statistics to a table, with the "input feature zone data" being the buffer shapefile, and the "input value raster" being the underlying raster file containing the pixel values.

From here, I think I need to just add a column to the resulting stat table and have it calculate the number of pixels of each value, but I don't know how to do that.

Any help would be extremely appreciated! I hope all that made sense.

Cheers,
Emma
0 Kudos
11 Replies
EricRice
Esri Regular Contributor
Hi Emma,

Have a look at the Zonal Histogram tool instead.  Zonal Statistics doesn't give you the breakdown of each value within the zone and their counts.  I typically run the Pivot Table tool after Zonal Histogram to turn rows into columns.

Your buffers overlap each other, so you need to iterate through each record (buffer feature) for the results to be correct.  See Iterate Feature Selection.

Best,
Eric
0 Kudos
EmmaYoung
New Contributor
Eric-

Thanks for your help.

I'm not sure I do need to use iterate feature selection, though, because the overlapping buffers are counted as distinct polygons, and so the pixels they cover should be recounted for each overlapping buffer, should they not? The values in the zonal histogram seem distinct... am I totally off-base on this? Thanks.

Emma
0 Kudos
EricRice
Esri Regular Contributor
Emma,

You must use an iterator for it to be correct.  When features are input, they are converted to rasters behind the scenes.  Because you have two features overlapping, the underlying pixel must choose which feature to represent. Each pixel can only be one value.  In the case of just two overlapping polygons, one would be correct and one would be smaller than it should be because the software has to choose only 1 of them during the conversion.  This is discussed in at least 50 other threads if you feel inclined to read up, and is discussed in the documentation.  See the 9th bullet point under Usage for Zonal Histogram. 

If you want to visually see what I mean, you can convert your buffers into a raster to see that you don't get two values for each pixel where the overlap occurred.  It will pick one for you and you have no control over which one it picks.  These tools originally only allowed raster inputs, but because feature input was requested we allow it and do the conversion for you, but the underlying process to compute the output is ALL raster based analysis.

Best,
Eric
0 Kudos
EmmaYoung
New Contributor
Eric, thanks again.

I think I've got the iterate feature selection all set up, but for some reason I'm getting an error when I try to run it using the iterated buffer. I tried running the whole thing using the regular (un-iterated) buffer, and it worked just fine... is it because the input feature zone field is "value"? When I used the un-iterated buffer, the zone field was "FID". That's the only difference in the entire process... do you know what's going on? I've attached a picture of the workflow. Thanks again,

Emma
0 Kudos
EmmaYoung
New Contributor
Just to add to that a little more, I'm getting this error message:

"Executing (Zonal Histogram): ZonalHistogram I_buffer_FID Value "C:\Documents and Settings\eiyoung\Desktop\Madagascar Research\Forest Disturbance Scenes\All GRIDS\158-76" "C:\Documents and Settings\eiyoung\My Documents\ArcGIS\Default.gdb\ZonalHi_I_buffe1" #
Start Time: Fri Feb 08 13:16:21 2013
ERROR 000865: Input raster or feature zone data: I_buffer_FID does not exist.:

So apparently the iterated buffer output doesn't exist, even though it's part of the workflow... do I have to permanently save it somewhere? How can i do that? the Arc help says "The temporary feature layer can be saved as a layer file using the Save To Layer File tool or saved as a new feature class using the Copy Features tool.", but I don't know how to actually do that. Thanks,

Emma
0 Kudos
EmmaYoung
New Contributor
Eric-

Sorry for all of the posts, I'm just sort of working through this. I've attached a photo of my workflow as it currently is... I have the original buffer, I iterated it to create an iterated buffer, i saved it as a layer, and then i opened that layer and ran it through the zonal histogram tool for my results. I've taken a look at them, and they seem to be identical to my original results with the un-iterated buffer layer... did I do something incorrectly? your insight would be much appreciated! Thanks again.

Emma
0 Kudos
EricRice
Esri Regular Contributor
Emma,

Your model is not yet finished.  Remember that the Zonal Histogram tool outputs a table...so for each iteration you are going to get a table, but in the end you want these associated with the original buffers/points so you must merge each table created into a final output table so that you can join it back to the features using the Zone ID field.  You are in 'advanced' territory here.  You don't want merge to execute for each iteration either, which means you need to nest the iterative model inside another model that only contains Merge/Append.  You must also make use of inline variables.  I would also recommend taking advantage of the in memory workspace within Zonal Histogram for the output table.  You can use Collect Values to collect the individual tables (from the in memory workspace) prior to putting them into Merge.  Don't forget that you can extend the model a bit more to include the 'join' process back to the original features, and don't forget about looking into the Pivot Table tool.

Let me know if you have any questions. 😄
Best,
Eric
0 Kudos
ChiragTernikar
New Contributor

Thankyou Eric Rice,

It's been quite long since this post was in 2013.

I was trying to run through the process you have mentioned here.It's stuck at the 'Pivot Table' part. What would be the value field in this 'Pivot Table' tool ?

Any help would be thankful.

Best,

Chirag

0 Kudos
EricRice
Esri Regular Contributor
What format is your input value raster?  The one named, 158-76.  It appears to be an Esri GRID, but you can't name our format like that and expect things to work.  In fact the only way to violate the rule for GRID naming is to rename it outside of ArcGIS.  Our system won't let you name an Esri GRID with a number in the first position.

See about Esri GRID Format, specifically the section called Storage Limitations.  I believe you also have spaces in the path to the data, which has caused issues in the past...

"C:\Documents and Settings\eiyoung\Desktop\Madagascar Research\Forest Disturbance Scenes\All GRIDS\158-76" "

Best,
Eric
0 Kudos