Select to view content in your preferred language

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

9104
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
DanPatterson_Retired
MVP Emeritus

an oldie but goodie

I will keep you posted, it may have to be a script with instructions if I can't go back that far

MarkLimb
New Contributor III

Thanks very much for all your help Dan!

Just confirming, the version is 10.3.1

0 Kudos
DanPatterson_Retired
MVP Emeritus

Mark... should work in 10.3 or above... 

Test it, I embedded the script inside, but if it all works, I will post a version so that you can see how it works and include other raster tools.

Anything else you can think of … let me know.

I have tools in my version that includes stats (like Cell statistics) for raster stacks (ie statistics for an area for variable(s) over time) etc

And some conversion tools etc.

MarkLimb
New Contributor III

Thanks Dan,

Probably something silly I'm missing...

Opening the toolbox with notepad I can see something is there, but when adding the toolbox to ArcToolbox I can't export or run the script - the toolbox appears empty?

0 Kudos
DanPatterson_Retired
MVP Emeritus

Ok... gimme a sec I will zip a new one with the scripts separated... just have to clean out a lot of fluff from the scripts folder.

Unzip in a folder, don't move the toolbox from the script

MarkLimb
New Contributor III

Hi Dan,

Thanks for that, I can see the script now.  I'm going to need some more guidance on using it though I'm afraid...

I added the toolbox to ArcToolbox. I then right clicked on the toolbox, add, and added a new script. In the wizard I point to your python script.  I then added some parameters, with a Raster Layer Input and Raster Layer output. 

Using the sample raster layer I attached as the input and new raster layer as the output, I received the following error:

Executing: Script1 GSV_Data_Updated_EntClassic_2016 "C:\Research\GIS\Activity Centres.gdb\Adjacency_GSV_EntClassic_2016"
Start Time: Tue Aug 14 12:46:56 2018
Running script Script1...
Failed script Script1...

Traceback (most recent call last):
File "C:\Research\GIS\ProximityScript\Scripts\focal_stats.py", line 37, in <module>
import arraytools as art
ImportError: No module named arraytools

Failed to execute (Script1).
Failed at Tue Aug 14 12:46:59 2018 (Elapsed Time: 2.82 seconds)

0 Kudos
DanPatterson_Retired
MVP Emeritus

Mark... unzip the zip file in a folder... don't move the *.tbx from the 'script' folder.

Add the toolbox to arctoolbox (if I remember ArcMap....)

There should be a tool inside that toolbox

download the zip again, I updated it

That's the problem with backporting.... are you sure you don't just want to switch to Pro????

MarkLimb
New Contributor III

Hi Dan,

Nope, no tool, just an empty toolbox. I extraced the files to a folder. There's the tbx file and the script sub-folder. Adding using this structure adds an empty toolbox. I tried moving the tbx into the script folder and re-added with the same result.

0 Kudos
DanPatterson_Retired
MVP Emeritus

again … named zip file differently to avoid confusion   PRO is the way to go

MarkLimb
New Contributor III

Thanks Dan, I can see the scripts now without having to manually add anything.

I see three tools:

  • Focal Statistics
  • Numpy Array to Raster
  • Raster Stats

I ran the first with the statistic as focal distance, and received this error:

Executing: FocalStats GSV_Data_Updated_EntClassic_2016 "C:\Research\GIS\Activity Centres.gdb\EucDist_GSV_EntClassic_2016_FocalStats" "focal difference"
Start Time: Tue Aug 14 13:15:00 2018
Running script FocalStats...
Failed script FocalStats...
SyntaxError: invalid syntax (focal_stats.py, line 138)
Failed to execute (FocalStats).
Failed at Tue Aug 14 13:15:03 2018 (Elapsed Time: 2.85 seconds)

0 Kudos