Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for terra #33

Open
bniebuhr opened this issue Jan 24, 2022 · 9 comments
Open

Support for terra #33

bniebuhr opened this issue Jan 24, 2022 · 9 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@bniebuhr
Copy link
Contributor

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).

@bitbacchus
Copy link
Collaborator

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

@bitbacchus bitbacchus added enhancement New feature or request help wanted Extra attention is needed labels Jan 24, 2022
@bniebuhr
Copy link
Contributor Author

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?

@Nowosad
Copy link
Collaborator

Nowosad commented Jan 27, 2022

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

@bitbacchus
Copy link
Collaborator

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

bitbacchus added a commit that referenced this issue Jan 27, 2022
@bitbacchus
Copy link
Collaborator

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 :)

@bniebuhr
Copy link
Contributor Author

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

@bitbacchus
Copy link
Collaborator

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

@marcosci
Copy link
Collaborator

Can we close this?

@bniebuhr
Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants