2

I'm working on some gridded temperature data, which I have categorised into a matrix where each cell can be one of two classes - let's say 0 or 1 for simplicity. For each class I want to calculate patch statistics, taking inspiration from FRAGSTATS, which is used in landscape ecology to characterise the shape and size of habitat patches.

For my purposes, a patch is a cluster of adjacent cells of the same class. Here's an example matrix, mat:

mat <- 
matrix(c(0,1,0,
         1,1,1,
         1,0,1), nrow = 3, ncol  = 3, 
       byrow = TRUE)

0    1    0
1    1    1
1    0    1

All the 1s in mat form a single patch (we'll ignore the 0s), and in order to calculate various different shape metrics I need to be able to calculate the perimeter (i.e. number of outside edges).

EDIT Sorry I apparently can't post an image because I don't have enough reputation, but you can see in the black lines of G5W's answer below that the outside borders of 1's represent the outside edges I'm referring to.

Manually I can count that the patch of 1s has 14 outside edges and I know the area (i.e. number of cells) is 6. Based on a paper by He et al. and this other question I've figured out how to calculate the number of inside edges (5 in this example), but I'm really struggling to do the same for the outside edges! I think it's something to do with how the patch shape compares to the largest integer square that has a smaller area (in this case, a 2 x 2 square), but so far my research and pondering have been to no avail.

N.B. I am aware of the package SDMTools, which can calculate various FRAGSTATS metrics. Unfortunately the metrics returned are too processed e.g. instead of just Aggregation Index, I need to know the actual numbers used to calculate it (number of observed shared edges / maximum number of shared edges).

This is my first post on here so I hope it's detailed enough! Thanks in advance :)

rasenior
  • 81
  • 11
  • How do you get the number 14? or even 5? – Onyambu May 17 '18 at 17:52
  • The number 14 is the one I'm trying to calculate, which I had previously just counted manually (count the black lines in G5W's answer). I calculate the number 5 (the red lines in G5W's answer) by counting the number of instances where adjacent cells have the same value (based on [this post](https://stackoverflow.com/questions/29105175/find-neighbouring-elements-of-a-matrix-in-r)) and then dividing by two. – rasenior May 17 '18 at 19:51

1 Answers1

0

If you know the area and the number of inside edges, it is simple to calculate the number of outside edges. Every patch has four edges so in some way, the total number of edges is 4 * area. But that is not quite right because every inside edge is shared between two patches. So the right number of total edges is

4*area - inside

The number of outside edges is the total edges minus the inside edges, so

outside = total - inside = (4*area- inside) - inside = 4*area - 2*inside.

Patch

You can see that the area is made up of 6 squares each of which has 4 sides. The inside edges (the red ones) are shared by two adjacent squares.

G5W
  • 36,531
  • 10
  • 47
  • 80