Select to view content in your preferred language

How do I make a calculation on a raster file within a specific neighbourhood?

1282
5
01-11-2011 08:15 AM
TaylerHamilton
Emerging Contributor
I was hoping to run a calculation on all of the pixels within my DEM.  How can I apply a formula to be calculated on each pixel within a certain neighbourhood?
I was thinking focal statistics, but you can only calculate statistics, not a different formula.

Thank you!
0 Kudos
5 Replies
TaylerHamilton
Emerging Contributor
Thanks for you quick response, whuber!

Well, I was hoping to calculate Simpson's diversity index:

I = 1 - �??P2i   ("P squared subscript i") 

Where I would need to calculate the observed proportions (Pi) of the different types (i) within a specific neighbourhood.

So if I'm following you correctly, should I break this formula up with focal statistics by calculating the observed proportions and the different types first?
0 Kudos
TaylerHamilton
Emerging Contributor
Thanks for your help, Bill.

How do I make an indicator grid?

The data I am working with is in raster format, so each type (I) is listed within a column in the attribute table. I was thinking to use the raster calculator for the indicator grids, but I don't know how (if it's even possible) to separate single rows and columns from the attribute table of the raster file.

Thanks in advance,

Tayler
0 Kudos
TaylerHamilton
Emerging Contributor
Hi Bill,

So I made indicator grids (I have made them before, just didn't know that is what they were called). I have used focal statistics to calculate the sum of the number of 1s in each neighbourhood. I can calculate the proportions according to focal stats results for each different indicator grid, but now my problem is adding the proportions in a meaningful way.

Because the focal statistics just gives the value from 0-317 (max # of cells in a circular neighbourhood of 10 radius), the proportions are based on this. When I try to add the proportions from each indicator grid, they are all the same proportions because the values are all 0-317 for each grid, not taking into account the "count" of each neighbourhood with that many 1s.

I really am not sure if I am explaining this so it makes sense, but any help is appreciated.
0 Kudos
TaylerHamilton
Emerging Contributor
I used the focal sum to determine how many pixels (1s) of each landcover type occurred within the focal stats neighbourhood, not the focal mean.

I was then able to calculate the proportions by creating a new column in the attribute table, where I divided the focal sum by the total number of pixels in the neighbourhood. I added a column to compute the squared proportions as well.

I came across a new problem trying to calculate the sum of the squared proportions. I did not know you could single out a specific column in the attribute table by using the following syntax in the raster calculator:

[Output] = [Layer1].FIELD_NAME_HERE + [Layer2].FIELD_NAME_HERE + ...


I do have another question for you, Bill, regarding an old post that is now closed for replies. It was titled "Grid Correlation", from January 2006. You explained methodology for using spatial statistics and the raster calculator to determine correlation between 2 raster layers, specifically:

"Spatial Analyst can be persuaded to compute a moving window correlation grid.

To obtain consistent results around the edges, you will need an indicator grid to identify (and later count) the cells where neither of your grids and is empty. Start by computing the product grid [XY] as *. One way to produce this indicator (let's call it ) is to compute

[XY] - [XY] + 1

This will have 1's in all cells where [XY] is not null and will otherwise be null.

At this point, replace the original grid with * and replace with *.

Having done these preliminaries, compute the squares of the grids: the square [XX] equals *, and the square [YY] equals *.

The moving-window correlation grid is computed from focal means. Choose a window size and shape. Using this, compute the focal means of , , [XX], [YY], and [XY]. Let's call the resulting grids [Xm], [Ym], [XXm], [YYm], and [XYm], respectively. The correlation grid, by definition, is

([XYm] - [Xm]*[Ym]) / Sqrt(([XXm] - [Xm]*[Xm])*([YYm] - [Ym]*[Ym]))

It will have non-null values at all cells having neighborhoods where two more more cells both have non-null values of and and not all values of and in those neighborhoods are constant."


I understand everything here, I am just curious as to what type of correlation results? Is it Pearson's Correlation? I am interested in calculating correlations but there seems to be no straightforward way to do it.

Thanks!!:D
0 Kudos
TaylerHamilton
Emerging Contributor
Thanks Bill!

I didn't even realize what I was doing would give the same result as the focal mean!! I will have to do the next few with the focal mean to see if it speeds things up...

I have run correlations in excel and SPSS etc. but I want the visual aspect of it, so I will try out the Pearson correlation methods you have outlined to try and get a visual raster representation.

Thank you for all of your replies, they really did help!!

Tayler
0 Kudos