-
Notifications
You must be signed in to change notification settings - Fork 2
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
Migrating to terra from raster #4
Comments
I have read up on this topic and it seems that we can stay on raster, and probably should, so long as we can lose our dependency on |
I have made a list of imported/suggested packages with problematic dependencies.
So we just need to tick up the version of |
I don't think we need a replacement The dev version of leastcostpath seems to do something similar but uses terra instead of raster which means we would have to add a function to do the conversion, which possibly (?) brings up the issue of parallel processing with terra objects that I mentioned above. They also don't interally do the conversion if a leastcostpath::create_cs(x = as.raster(r), neighbours = 16, dem = NULL, max_slope = NULL)
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘values’ for signature ‘"raster"’ The output of The Adding a check to here to make sure the CRS is projected if a region raster is provided: https://github.com/GlobalEcologyLab/poems/blob/b0e18f52b2877593e6f750548f3bdc63d750607f/R/Region.R#L178C1-L178C1 region_raster = function(value) {
if (missing(value)) {
if (is.null(private$.region_raster) && !is.null(private$.coordinates) && self$use_raster) {
## if coordinates are provided assume EPSG:4087. Issue warning to use raster object instead
## https://epsg.io/4087
warning("CRS for cooordinates assumed to be EPSG:4087 (https://epsg.io/4087).\nSuggest providing a raster instead of coordinates.",
immediate. = TRUE, call. = FALSE)
raster::rasterFromXYZ(cbind(private$.coordinates, cell = 1:nrow(private$.coordinates)),
crs = "EPSG:4087")
} else {
private$.region_raster
}
} else {
if (raster::isLonLat(value)) {
stop("Region raster must use a projected CRS", call. = FALSE)
}
if (!is.null(value) && !("RasterLayer" %in% class(value))) {
stop("Region raster should be a raster::RasterLayer (or inherited class) object", call. = FALSE)
}
if (!is.null(value) && !is.null(private$.coordinates)) {
stop("Region is already associated with a set of coordinates", call. = FALSE)
}
if (raster::fromDisk(value)) { # move to memory
raster::values(value) <- raster::values(value)
}
private$.region_raster <- value
}
} We would need an additional check if coordinates instead of a raster is passed to the region. Something to stop if the x coordinates are within -365 and 365 and the y coordinates are within -90.1 and 90.1. |
So in summary, it seems the problem will resolve on its own if we just wait a bit for |
The new plan is to keep |
Like I mentioned in my earlier comment, the There is also no need to migrate away from From a quick look, |
Sounds like the right way forward Stu.
From: Stuart Brown ***@***.***>
Date: Sunday, 24 September 2023 at 10:19 am
To: GlobalEcologyLab/poems ***@***.***>
Cc: Subscribed ***@***.***>
Subject: Re: [GlobalEcologyLab/poems] Migrating to terra from raster (Issue #4)
CAUTION: External email. Only click on links or open attachments from trusted senders.
…________________________________
Like I mentioned in my earlier comment, the geosphere dependency can be resolved by forcing people/groups to use projected coordinates. It is only used for calculating distances when lat/long rasters or coordinates are given. It's a really simple fix, and in my opinion, a conditon which probably should've been enforced from the beginning of package development.
There is also no need to migrate away from gdistance as sp isn't being removed from CRAN or even retired. Internally, sp has been migrated to use sf instead of rgdal, rgeos, and maptools. Those three packages are being retired and removed very soon, any packages which don't require a sp version after those functions have been transitioned to use sf will fail. gdistance only uses a single sp function which will be affected by the transition - sp::CRS. See here - https://r-spatial.org/r/2023/05/15/evolution4.html<https://r-spatial.org/r/2023/05/15/evolution4.html>. As long as the latest version of sf is used, sp will use the sf package to extract the CRS and it won't be an issue. The easist fix is to force a version dependency for sp and sf in poems DESCRIPTION file - probably under suggests as we don't actually use the packages in any functions, but it will force any downstream packages to use the latest versions.
From a quick look, gdistance only uses sp::CRS when coordinates are given<https://github.com/AgrDataSci/gdistance/blob/cef36d7c27ec2ff5ab0a3e072afe21328c06de29/R/internal-functions.R#L27> or for showing the least cost path between two points<https://github.com/AgrDataSci/gdistance/blob/cef36d7c27ec2ff5ab0a3e072afe21328c06de29/R/shortestPath.R#L140>. The transition layers, barriers, cost distance etc. are all created from raster inputs. As poems passes a raster<https://github.com/GlobalEcologyLab/poems/blob/b1964bd16979f9aafaea73fda1baf13fd918f227/R/DispersalFriction.R#L118C9-L118C9> through to all the gdistance functions, poems is not affected by any of the changes.
—
Reply to this email directly, view it on GitHub<#4 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AEDXBYCB6CBVKKZ54K5S75LX357QTANCNFSM6AAAAAA3MJXKCI>.
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
I've started this process, but I'm not 100% sure it's necessary in our case. You can see what I've done here https://github.com/scbrown86/poems/tree/terra_dev. The problem is that now my terra-dev branch is a number of commits behind the poems-main branch and I don't think it can be easily merged (if at all).
I'm just dumping some thoughts below, but happy to discuss.
rgeos
andrgdal
packages which are no longer under development and will be removed from CRAN. As of the 18th September 2022 these dependencies have been removed andraster
now usesterra
for the same functionality. This process started in 2021. See the raster news here.terra
doesn't readily support parallel processing without wrapping anyspatRaster
objects. This is easily done, but in my testing we need to keep a wrapped copy of theregion$region_raster
object that then needs to be unwrapped on each parallel session to make sure the data is consistent with the region. AnyspatRaster
objects that would be passed on to any parallel functions need to be wrapped. This adds an overhead in wrapping and unwrapping particularly with large regionsterra
and still relies onraster
for it's calculations. If we swap toterra
we would need to convert back toraster
before doing those calculations adding additional overhead. At the moment in my terra-dev branch of poems I do it like thisno_friction_rast <- raster::raster(terra::unwrap(raster_region$region_raster_packed))
## <simpleError in h(simpleError(msg, call)): error in evaluating the argument 'i' in selecting a method for function '[<-': NULL value passed as symbol address>
. I'm absolutely sure this is due to the way the conductance raster is stored in the region, but I need to keep working on this when I have time. See here and heregdistance
issues with swapping toterra
I'm not overly concerned with raster becoming deprecated particuarly as it's been heavily rewritten to use functionality from
terra
in place of the oldrgeos
andrgdal
functionality, so maybe it's best to just stick withraster
? Particularly given that outside of defining the region, the friction, and plotting, we don't do much else withraster
objects?The text was updated successfully, but these errors were encountered: