# NOT RUN {
library(sf)
library(stars)
# read in habitat suitability data of wolf in Europe
hsi.file = system.file("extdata","wolf3_int.tif",package="habCluster")
wolf = read_stars(hsi.file)
# rescale raster value to 0 - 1
wolf = wolf / 100
# find habitat cluster using Fast Greedy Algorithm.
# Raster will be resampled to 40 km, to cluser at the scale of 40 km and reduce calculation amount.
clst = cluster(wolf, method = cluster_fast_greedy, cellsize = 40000)
# plot the results
image(wolf,col=terrain.colors(100,rev = TRUE),asp = 1)
boundary = clst$boundary
plot( boundary$geometry, add=TRUE, asp=1, border = "lightseagreen")
# discard patches smaller than 1600 sqkm
boundary$area = as.numeric(st_area(boundary))
boundary = boundary[boundary$area > 40000*40000,]
image(wolf,col=terrain.colors(100,rev = TRUE),asp = 1)
plot( boundary$geometry, add=TRUE, asp=1, border = "lightseagreen")
# can also use RasterLayer object#
library(raster)
wolf = read_stars(hsi.file)
wolf = wolf / 100
clst = cluster(wolf, method = cluster_leiden, cellsize = 40000, resolution_parameter = 0.0002)
# }
Run the code above in your browser using DataLab