azizka / sampbias

Sampbias is a method and tool to 1) visualize the distribution of occurrence records and species in any user-provided dataset, 2) quantify the biasing effect of geographic features related to human accessibility, such as proximity to cities, rivers or roads, and 3) create publication-level graphs of these biasing effects in space.
33 stars 9 forks source link

Error when `restrict_sample` is a `SpatVector` #16

Open adamlilith opened 6 months ago

adamlilith commented 6 months ago

calculate_bias() throws an error when restrict_sample is supplied and is a SpatVector (the help indicates it should be a SpatVector). Adapted from the example for that function:

x <- data.frame(species = rep(sample(x = LETTERS, size = 5), times = 20),
                   decimalLongitude = runif(n = 100, min = 0, max = 20),
                   decimalLatitude = runif(n = 100, min = -4, max = 4))

x_vect <- terra::vect(x, geom = names(x)[2:3], crs = 'EPSG:4326')
restrict_sample <- terra::buffer(x_vect, 20000)

out <- calculate_bias(x, terrestrial = TRUE, buffer = 0, restrict_sample = restrict_sample)
Error: unable to find an inherited method for function ‘vect’ for signature ‘x = "SpatVector"’

Note that restrict_sample should be able to take a SpatVector, as per the help: @param restrict_sample a \code{SpatVector} object.

This line:

restrict_sample <- terra::vect(restrict_sample)

assumes restrict_sample is not a SpatVector, so throws the error.

This only converts restrict_sample to a SpatVector if needed:

if (!inherits(restrict_sample, 'SpatVector')) restrict_sample <- terra::vect(restrict_sample)

This now works: out <- calculate_bias(x, terrestrial = TRUE, buffer = 0, restrict_sample = restrict_sample)