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)
# 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?
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)
Below I use the example data to
SpatialPolygon
# 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)
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.