Comments (9)
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.
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.
I'd say I close the PR, you pull the terra branch + add your changes and create a new PR?
from landscapetools.
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.
@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.
I'm creating a branch with a working version, just a sec
from landscapetools.
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.
Can we close this?
from landscapetools.
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)
- util_facetplot color scales HOT 1
- landscapetools 0.5 HOT 16
- Generate neutral landscapes that match exactly the extent (non-NA cells) of another raster HOT 10
- Share plots HOT 9
- Issues with `show_landscape()` for categorical rasters HOT 4
- long - wide option for raster2tibble HOT 1
- Build failure HOT 4
- Wrong result while using weighting in the util_classify function HOT 7
- show_landscape coloring bug with ratified raster
- CRAN HOT 2
- show_shareplot function cannot be found HOT 2
- How to define the order of the rasters shown with `show_landscape()`? HOT 3
- `util_extract_multibuffer` as a separate function HOT 10
- Re-add continuous integration workflows? HOT 5
- Fix pkgdown configuration for reference HOT 2
- random % with corresponding % from real landscape HOT 8
- Discrepancy in the `util_classify()` docs HOT 1
- white space
- util_facetplot + raster names
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from landscapetools.