Coder Social home page Coder Social logo

Support for terra about landscapetools HOT 9 OPEN

ropensci avatar ropensci commented on May 30, 2024
Support for terra

from landscapetools.

Comments (9)

bitbacchus avatar bitbacchus commented on May 30, 2024 2

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

from landscapetools.

bitbacchus avatar bitbacchus commented on May 30, 2024 1

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

from landscapetools.

bitbacchus avatar bitbacchus commented on May 30, 2024 1

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

from landscapetools.

bniebuhr avatar bniebuhr commented on May 30, 2024

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?

from landscapetools.

Nowosad avatar Nowosad commented on May 30, 2024

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

from landscapetools.

bitbacchus avatar bitbacchus commented on May 30, 2024

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

from landscapetools.

bniebuhr avatar bniebuhr commented on May 30, 2024

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

from landscapetools.

marcosci avatar marcosci commented on May 30, 2024

Can we close this?

from landscapetools.

bniebuhr avatar bniebuhr commented on May 30, 2024

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.

from landscapetools.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.