ropensci/landscapetools

Support for terra

Opened this issue ยท 9 comments

Hi!

Is there any intended support for terra? If not, I would like to suggest this as an improvement.

The landscapetools package is very useful, but now that I am trying to migrate for terra (which is incredible faster for some things), I still need to come back to raster functions to allow compatibility with other packages (such as landscapetools). I guess several other people in the R community might be in this movement (at least from my personal contacts and colleagues).

Thanks for your report!

Yes, we surely should and likely will support terra in future. However none of us has any concrete plans to implement it at the moment, so any help would be appreciated.

Best,
Sebastian

Ok! I forked the package and tried starting with a function - show_landscape(), which I am using quite often and is easy to test.
In a way, however, there are other functions called by it (for instance, when the input is a list, it uses util_rescale() and util_raster2tibble()).

I tried adapting all of them to terra - mostly through pretty simple adaptations, and defining a new
show_landscape.SpatRaster function with the corresponding terra functions, but it did not work. It seems my knowledge on how to use methods here is limiting...

Here is the function:

#' @name show_landscape
#' @export
show_landscape.SpatRaster <- function(x,
                                      xlab = "Easting",
                                      ylab = "Northing",
                                      discrete = FALSE,
                                      ...) {
  # derive ratio for plot, cells should be a square and axis equal in length
  if (terra::ncol(x) == terra::nrow(x)) {
    ratio <- 1
  } else {
    ratio <- terra::nrow(x) / terra::ncol(x)
  }
  if (isTRUE(discrete)) {
    # get rasterlabels
    legend_labels <- tryCatch({
      terra::levels(x)[!is.na(terra::levels(x))]
    },
    error = function(e) {
      x <- raster::as.factor(x) # error
      levels <- terra::unique(x)
      levels(x) <- levels # error
    })

    xyz  <- terra::as.data.frame(x, xy = TRUE)

    ggplot2::ggplot(xyz) +
      ggplot2::geom_tile(ggplot2::aes(x, y, fill = factor(xyz[, 3]))) +
      ggplot2::labs(x = xlab,
                    y = ylab)  +
      theme_nlm_discrete(..., legend_labels = legend_labels, ratio = ratio)

  } else {
    xyz  <- raster::as.data.frame(x, xy = TRUE)

    ggplot2::ggplot(xyz) +
      ggplot2::geom_tile(ggplot2::aes(x, y, fill = xyz[, 3])) +
      ggplot2::labs(x = xlab,
                    y = ylab) +
      theme_nlm(..., ratio = ratio)
  }
}

The trick for discrete map plotting is still lacking but that is not the point. In fact the function was not exported when I built the package, and when I try it with a terra object, I get the following:

library(landscapetools)
library(terra)
library(dplyr)

> y <- gradient_landscape %>% 
+     terra::rast()
> show_landscape(y)
Error in UseMethod("show_landscape") : 
  no applicable method for 'show_landscape' applied to an object of class "SpatRaster"

Any hints or even recommendation of basic reading about methods in R so that I can try it out?

@bniebuhr could you create a draft pull request? This way I will be able to test your implementation and see if I can help.

I'm creating a branch with a working version, just a sec

Have a look at the new branch terra. I added your code (replaced some raster funs by terra equivalents, added terra to the dependencies in the DESCRIPTION file, and ran roxygen to update NAMESPACE.

Seems to work just fine, thanks for tackling this @bniebuhr :)

Uh, haven't seen your reply earlier, @bitbacchus , and just submitted a PR. But maybe it is not necessary.
I'll test it.

I'd say I close the PR, you pull the terra branch + add your changes and create a new PR?

Can we close this?

I am currently working on that, but at a quite slow pace. If you prefer you can close it, and I reopen it when there are updates. Or I open new, more specific issues then.