mapme-initiative / mapme.biodiversity

Efficient analysis of spatial biodiversity datasets for global portfolios
https://mapme-initiative.github.io/mapme.biodiversity/
GNU General Public License v3.0
24 stars 7 forks source link

Make year range limit easier to adjust in sum_gfw #273

Closed fBedecarrats closed 1 month ago

fBedecarrats commented 1 month ago

Currenly .sum_gfw() is defined as follows in R/calc_treecover_area.R:

.sum_gfw <- function(data, what = "coverage_area") {
  # calculate loss area by year
  df <- data.frame(years = 2000:2022, var = 0)
  names(df)[2] <- what
  my_sum <- by(data[[what]], data[["lossyear"]], sum, na.rm = TRUE)
  sum_years <- as.numeric(names(my_sum))
  sum_years <- sum_years + 2000

  index <- match(sum_years, df[["years"]])
  df[[what]][index] <- as.numeric(my_sum)

  df
}

The line df <- data.frame(years = 2000:2022, var = 0) implies that 2000:2022 must be updated at each get_gfw_lossyear() to include the latest available year for the resource. This is not very intuitive and possibly error prone. I wonder if it would not be convenient to extract the year range directly from the gfw_lossyear resource being used.

goergen95 commented 1 month ago

Thanks! We now detect the maximum year from the layer name of the gfw_lossyear object.