3

I am asking for your R skills' support. For one of my papers, I am modelling the distribution of tree species in Amazonia using remote sensing variables (LT band 3, 4, 5, 7, DEM and NDVI) and I am planning to mask out all the sites out of the "convex hull" of my predictors. I have reviewed several packages and functions in R (convhull, convhulln) but without success.

I have extracted all the variables' values of more than 6000 presence points. I need to do a convex hull to that set of points and then take it to the geographical space and then mask everything out. So basically I need a raster file with NAs and 1s (NAs areas out of the convex hull and 1s areas within the convex hull)

dbase <-read.csv("dbase.csv")
names(dbase)
# [1] "id"        "pca"       "block"     "strip"     "tree.n"    "plaque"   
# [7] "species"   "diameter"  "height"    "volume"    "x"         "y"        
# [13] "condition" "sector" 
coordinates(dbase)<-~x+y

files <- list.files("C:/Users/...", 
                    pattern="asc", full.names=TRUE )
predictors <-stack(files) # b3, b4, b5, b7, ndvi and dem
presence_var <-extract(predictors,dbase)

The predictor variables

   #      B3  B4 B5 B7 DEM      ndvi
   # [1,] 25  75 57 18 349 0.5000000
   # [2,] 22  79 64 19 332 0.5643564
   # [3,] 24  79 62 20 336 0.5339806
   # [4,] 23  79 62 20 341 0.5490196
   # [5,] 25  80 63 21 307 0.5238096
   # [6,] 24  83 63 20 342 0.5514019
   # ...

conhull <-convHull(presence_var)
pr <- predict(conhull, predictors)
plot(pr) # empty results

Any suggestions?

  • It is nice to see an `SDM` question :) However, if you want to increase your odds of a good answer, please post a minimum reproducible example. This includes some example data and some code of what you have tried so far. This post might help you: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – JanLauGe Jul 24 '17 at 19:37
  • Thanks JanLauGe, I already did. Hopefully someone might be able to help :) – Pablo Perez Jul 25 '17 at 07:00

1 Answers1

2

I have created some example data to illustrate the approach I think would be right for you:

library(raster)
library(tidyverse)

# an empty raster of global extent
r <- raster()
# make up some raster values
values(r) <- runif(length(r))

# make up some random coordinates around north america
coords <- cbind( 
  lon = runif(100, min = -120, max = -60),
  lat = runif(100, min = 30, max = 50))

# let's have a look
plot(r)
points(coords, add = TRUE)

enter image description here


Below I use the example data to

  1. identify the points that make up the convex hull
  2. create a convex hull SpatialPolygon
  3. mask the raster so that only cells intercepting the convex hull remain

# get the convex hull
hull_points <- coords[chull(coords),]

# convert to polygon
hull_polygon <- hull_points %>%
  Polygon() %>%
  list() %>%
  Polygons(1) %>%
  list() %>%
  SpatialPolygons()

# mask the raster
rr <- mask(r, hull_polygon)

# let's have another look
plot(rr)

enter image description here

One side note: If I correctly understand what you are trying to do, I would recommend that you add a buffer around your spatial polygon before doing the masking. This is because there are most likely areas with high habitat suitability right next to your marginal occurrences but outside the convex hull and you should be careful about clipping these away.

JanLauGe
  • 2,297
  • 2
  • 16
  • 40
  • Thanks a lot JanLauGe – Pablo Perez Jul 26 '17 at 10:26
  • Thanks a lot @JanLauGe. I tried that also with the convex hull function of the geometry package. I need to do an environmental convex hull with all the 6 variables that I have (not a "geographic convex hull" with coordinates only). Then try to create a raster file with would contain NAs for pixels that are out of this 6-dimensional convex hull. – Pablo Perez Jul 26 '17 at 10:32
  • I see! Do struggle to compute the 6D convex hull in the first place, or is your problem only related to then cropping these values from the raster? – JanLauGe Jul 26 '17 at 10:43
  • I could also do the environmental hull from each 2 by 2 variables, the thing is I don´t figure out how to make the "mask" raster from the environmental space to the geographical space. I don´t if thats understandable :) – Pablo Perez Jul 26 '17 at 10:51
  • So far I have seen only geographical covex hulls (just using the coordinates) but not within the variables and then taking to the "geographical" space – Pablo Perez Jul 26 '17 at 10:52
  • As you already pointed out, you can calculate the n-dimensional convex hull with `geometry::convhulln(coords)`. To find out whether a point is within the `chull`, have a look at this post: https://stackoverflow.com/questions/4901959/find-if-a-point-is-inside-a-convex-hull-for-a-set-of-points-without-computing-th (but short answer is it might get computationally expensive) – JanLauGe Jul 26 '17 at 11:28
  • Thanks again @JanLauGe! I will check it out – Pablo Perez Jul 26 '17 at 11:40