Select to view content in your preferred language

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

9105
42
08-08-2018 07:18 PM
MarkLimb
New Contributor III

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
MarkLimb
New Contributor III

That's right: for each cell, I need a count of the number of surrounding cells that have a different value to the core (excluding cells with no data).


For the sample you require... do you a raster that includes the input data and another that includes the solved data for that area? If so, what grid size do you require?

0 Kudos
DanPatterson_Retired
MVP Emeritus

Mark, I don't need any specific cell size.  The sample you sent is sufficient, I can make my array from it.

Tonight...

0 Kudos
DanPatterson_Retired
MVP Emeritus

something to amuse you in the interim.... based on your example

input array
[[1 1 1 2 2]
 [1 2 2 2 2]
 [1 2 2 2 2]
 [1 2 2 2 2]]
strided array... 
[[[[1 1 1]
   [1 2 2]
   [1 2 2]]

  [[1 1 2]
   [2 2 2]
   [2 2 2]]

  [[1 2 2]
   [2 2 2]
   [2 2 2]]]


 [[[1 2 2]
   [1 2 2]
   [1 2 2]]

  [[2 2 2]
   [2 2 2]
   [2 2 2]]

  [[2 2 2]
   [2 2 2]
   [2 2 2]]]]
sub array
[[1 1 1]
 [1 2 2]
 [1 2 2]] ... number different than 2 ... 5
sub array
[[1 1 2]
 [2 2 2]
 [2 2 2]] ... number different than 2 ... 2
sub array
[[1 2 2]
 [2 2 2]
 [2 2 2]] ... number different than 2 ... 1
sub array
[[1 2 2]
 [1 2 2]
 [1 2 2]] ... number different than 2 ... 3
sub array
[[2 2 2]
 [2 2 2]
 [2 2 2]] ... number different than 2 ... 0
sub array
[[2 2 2]
 [2 2 2]
 [2 2 2]] ... number different than 2 ... 0

MarkLimb
New Contributor III

Yep, that's exactly what I'm looking for.

0 Kudos
DanPatterson_Retired
MVP Emeritus

I had to trim off a lot of nodata area to get to the crux of the data… a couple of visuals.

I trimmed, ran the 'algorithm', made a fishnet so you could see cell outlines, and zoomed into a random area for you to have a look at.  I then converted the 'result' to a point file so that I could use labelling as well.  So if the colors aren't great, you dan check a cell value against the number.

The first image is the original values. 

The second is the number of cells around the original cell that are different.

Check the logic.

EDIT See images in a subsequent thread

MarkLimb
New Contributor III

Thank you very much for this Dan, very clearly explained!

Unless I'm missing something, I'm not sure if this is quite doing it though?  Two issues I can see:

  1. Values are being calculated for cells with no data (I'm assuming that's what the black is in the first image?). For example cell 7,B (x,y). It's a null cell but appears to be calculating the two adjacent red cells. The no data cells should be skipped
  2. Others don't seem to be working as expected. For example, cell 8,B is shown as 5 when 0 was expected. Or cell 8,F which is 3, when 0 would be expected. Cell 2,H is 5 when 3 was expected

Mark

0 Kudos
DanPatterson_Retired
MVP Emeritus

Mark... forget the other images.

Sorry for the delay (holidays.. sort of)

So, nulls have been removed as being a different value around the focal cell... wasn't sure about that one.

That affects the results.  

A newly clipped sample and result... have a gander

Now the result... notice that most of the areas are classes as 0 difference which is to be expected, now that null values are removed from consideration.

MarkLimb
New Contributor III

That's the ticket, perfect!

Many thanks Dan! The question now of course is... how did you do it?

0 Kudos
DanPatterson_Retired
MVP Emeritus

Just got back, so I am putting it into a useable script form in a toolbox, but I only use PRO, so you will be the tester of whether saving a toolbox to an ArcMap version actually works... so ArcMap 10.????? after the dot would help

0 Kudos
MarkLimb
New Contributor III

Hi Dan,

I'm not in the office today so not certain... I think it is 10.3 but will need to confirm.

Many thanks again!

0 Kudos