-
Notifications
You must be signed in to change notification settings - Fork 16
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
Comments
Thanks for your report! Yes, we surely should and likely will support Best, |
Ok! I forked the package and tried starting with a function - I tried adapting all of them to terra - mostly through pretty simple adaptations, and defining a new 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 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 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'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. |
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 forterra
(which is incredible faster for some things), I still need to come back toraster
functions to allow compatibility with other packages (such aslandscapetools
). I guess several other people in the R community might be in this movement (at least from my personal contacts and colleagues).The text was updated successfully, but these errors were encountered: