Skip to main content
Tweeted twitter.com/#!/StackGIS/status/615048740160847872
Edited to be more clear about where I'm having trouble with the code.
Source Link
KevinB
  • 115
  • 2
  • 7

I have a raster where each pixel is a particular vegetation type. I'm interested in the diversity of vegetation types around each pixel.

I'd like to assign a Shannon's diversity index value (see this link for equation) to each pixel based on a moving window analysis of surrounding pixels.

I've come up with some code to count the number of unique values in the moving window using functions in the raster package (at least that is what I hope it is doing!), but I am having trouble with the rest of the needed code. Here is what I have so far:

fw<-focalWeight(veg_map,5000, "circle") # creates circular filter with a radius of 5000m
test_fcl<-focal(veg_map,w=fw, fun=function(x,
          ...){length(unique(na.omit(x))) }) #counts unique values in moving window #  (e.g. species richness)

The part I'm having the most trouble with is finding the proportion of each unique type within the moving window. From that I could calculate several metrics of diversity, including Shannon's.

I have a raster where each pixel is a particular vegetation type. I'm interested in the diversity of vegetation types around each pixel.

I'd like to assign a Shannon's diversity index value (see this link for equation) to each pixel based on a moving window analysis of surrounding pixels.

I've come up with some code to count the number of unique values in the moving window using functions in the raster package (at least that is what I hope it is doing!), but I am having trouble with the rest of the needed code. Here is what I have so far:

fw<-focalWeight(veg_map,5000, "circle") # creates circular filter with a radius of 5000m
test_fcl<-focal(veg_map,w=fw, fun=function(x,
          ...){length(unique(na.omit(x))) }) #counts unique values in moving window #  (e.g. species richness)

I have a raster where each pixel is a particular vegetation type. I'm interested in the diversity of vegetation types around each pixel.

I'd like to assign a Shannon's diversity index value (see this link for equation) to each pixel based on a moving window analysis of surrounding pixels.

I've come up with some code to count the number of unique values in the moving window using functions in the raster package (at least that is what I hope it is doing!), but I am having trouble with the rest of the needed code. Here is what I have so far:

fw<-focalWeight(veg_map,5000, "circle") # creates circular filter with a radius of 5000m
test_fcl<-focal(veg_map,w=fw, fun=function(x,
          ...){length(unique(na.omit(x))) }) #counts unique values in moving window #  (e.g. species richness)

The part I'm having the most trouble with is finding the proportion of each unique type within the moving window. From that I could calculate several metrics of diversity, including Shannon's.

readability
Source Link
Andre Silva
  • 10.3k
  • 12
  • 55
  • 109

I have a raster where each pixel is a particular vegetation type. I'mI'm interested in the diversity of vegetation types around each pixel. I'd

I'd like to assign a Shannon's diversity index value (see this link for equation: https://en.wikipedia.org/wiki/Diversity_index#Shannon_indexlink for equation) to each pixel based on a moving window analysis of surrounding pixels. I've

I've come up with some code to count the number of unique values in the moving window using functions in the 'raster'raster package (at least that is what I hope it is doing!), but I am having trouble with the rest of the needed code. Here is what I have so far:

fw<-focalWeight(veg_map,5000, "circle") # creates circular filter with a radius of 5000m
test_fcl<-focal(veg_map,w=fw, fun=function(x,
          ...){length(unique(na.omit(x))) }) #counts unique values in moving window #  (e.g. species richness)

Thanks for any help in advanced!

I have a raster where each pixel is a particular vegetation type. I'm interested in the diversity of vegetation types around each pixel. I'd like to assign a Shannon's diversity index value (see this link for equation: https://en.wikipedia.org/wiki/Diversity_index#Shannon_index) to each pixel based on a moving window analysis of surrounding pixels. I've come up with some code to count the number of unique values in the moving window using functions in the 'raster' package (at least that is what I hope it is doing!) but am having trouble with the rest of the needed code. Here is what I have so far:

fw<-focalWeight(veg_map,5000, "circle") # creates circular filter with a radius of 5000m
test_fcl<-focal(veg_map,w=fw, fun=function(x,
          ...){length(unique(na.omit(x))) }) #counts unique values in moving window #  (e.g. species richness)

Thanks for any help in advanced!

I have a raster where each pixel is a particular vegetation type. I'm interested in the diversity of vegetation types around each pixel.

I'd like to assign a Shannon's diversity index value (see this link for equation) to each pixel based on a moving window analysis of surrounding pixels.

I've come up with some code to count the number of unique values in the moving window using functions in the raster package (at least that is what I hope it is doing!), but I am having trouble with the rest of the needed code. Here is what I have so far:

fw<-focalWeight(veg_map,5000, "circle") # creates circular filter with a radius of 5000m
test_fcl<-focal(veg_map,w=fw, fun=function(x,
          ...){length(unique(na.omit(x))) }) #counts unique values in moving window #  (e.g. species richness)
Source Link
KevinB
  • 115
  • 2
  • 7

Calculating Shannon's diversity using moving window in R

I have a raster where each pixel is a particular vegetation type. I'm interested in the diversity of vegetation types around each pixel. I'd like to assign a Shannon's diversity index value (see this link for equation: https://en.wikipedia.org/wiki/Diversity_index#Shannon_index) to each pixel based on a moving window analysis of surrounding pixels. I've come up with some code to count the number of unique values in the moving window using functions in the 'raster' package (at least that is what I hope it is doing!) but am having trouble with the rest of the needed code. Here is what I have so far:

fw<-focalWeight(veg_map,5000, "circle") # creates circular filter with a radius of 5000m
test_fcl<-focal(veg_map,w=fw, fun=function(x,
          ...){length(unique(na.omit(x))) }) #counts unique values in moving window #  (e.g. species richness)

Thanks for any help in advanced!