Select to view content in your preferred language

How to count the adjacent cells that have a different value in a raster dataset?

9520
42
08-08-2018 07:18 PM
MarkLimb
Occasional Contributor

Hi,

I'm using a raster dataset and I'm trying to determine a value for land use adjacency as used by Stathakis & Tsilimigkas (2014) (Measuring the compactness of European medium-sized cities by spatial metrics based on fused data sets. International Journal of Image and Data Fusion, 6(1), 42-64. doi:10.1080/19479832.2014.941018)

This involves counting the number of adjacent cells that have a value different to the centre cell. The attached image illustrates this concept.

It needs to return a new raster dataset where the value of each cell equals this count.

Can anyone suggest a method for doing this in ArcMap 10?

Thank you!

EDIT:

Dan, you're an absolute legend! Amazing dedication and perseverance to assist a total stranger on the other side of the world. Thank you so much for all your effort on this!

Just to update, the main issues experienced had much to do with a combination of differences in arcmap 10.3's and ArcGIS Pro's arcpy, and some major changes in numpy that is used by python 2.7 and python 3.6. Dan worked through them and ended up producing the Toolbox/Script I've attached to this edited post. As a final tip, make sure you run the script in a blank dataframe. You will need to manually set the projection of the final output.

0 Kudos
42 Replies
DanPatterson_Retired
MVP Emeritus

Mark... that is an issue... can you zip the tif and its bits and email it to me at Dan_Patterson@carleton.ca 

I was afraid of that last change causing an issue.  

I just want to make sure it works in my version before I devolve to an older state.

0 Kudos
RaeganYoung1
New Contributor

What a great saga to find! Thanks for updateing with the finished tool. I am working in ArcMap 10.7 but I would like to try and execute this tool as well. Could you provide some guidance on how to use the three tools included? Order or operations, etc. Thanks. 

0 Kudos
by Anonymous User
Not applicable

Hi All,

Thank you for the question and discussion on this topic. In ArcGIS Pro 2.5, this problem can be solved easily using Raster Cell Iterator (RCI). With RCI, visit each cell location in a Raster object, all in a Python environment. The iterator makes it easy to query and modify cell values at each cell location and its neighboring cell locations.


Here are some useful resources to explore:

1) Check out this blog post, https://community.esri.com/community/gis/analysis/spatial-analyst/blog/2020/02/06/introducing-the-ra... to get familiar with this new functionality.

2) The specific problem posed in this question about 'counting adjacent cells with a different value' is addressed in Unleash the power of RasterCellIterator to perform custom raster analysis blog post.

Regards,

Neeraj Rajasekar | Spatial Analyst Product Engineer

0 Kudos