rsbivand / rgrass

Interpreted interface between the GRASS geographical information system and R
https://rsbivand.github.io/rgrass/
26 stars 9 forks source link

`initGRASS` may stop with "no generic function found for 'crs'" #95

Closed Kodiologist closed 3 months ago

Kodiologist commented 3 months ago
$ R --no-save -q --vanilla -e 'rgrass::initGRASS("/usr/lib/grass83", home = tempdir(), SG = terra::rast(), override = T)'
[…] # No issue
$ R --no-save -q --vanilla -e 'lidR::readLAS; rgrass::initGRASS("/usr/lib/grass83", home = tempdir(), SG = terra::rast(), override = T)'
> lidR::readLAS; rgrass::initGRASS("/usr/lib/grass83", home = tempdir(), SG = terra::rast(), override = T)
function (files, select = "*", filter = "") 
{
    if (filter == "-h" | filter == "-help") {
        rlas:::lasfilterusage()
        return(invisible())
    }
    UseMethod("readLAS", files)
}
<bytecode: 0x5a7fb97679a8>
<environment: namespace:lidR>
Error in getMethod("crs", "SpatRaster") : 
  no generic function found for 'crs'
Calls: <Anonymous> -> getMethod
Execution halted
$ R --no-save -q --vanilla -e 'lidR::readLAS; library(terra); rgrass::initGRASS("/usr/lib/grass83", home = tempdir(), SG = terra::rast(), override = T)'
[…] # No issue

I'm using:

rsbivand commented 3 months ago

The problem appears to be in lidR, please raise there. Please also provide a regular script, not using Rscript.

Kodiologist commented 3 months ago

I don't know what a "regular script" is, but the above is from using the normal R binary, not Rscript.

rsbivand commented 3 months ago

To be run in interactive R at the R prompt. There are no "rgrass guys", there is one retired academic.

Kodiologist commented 3 months ago

That would need some way to reset the state of all packages (I think) which isn't fully implemented (I think).

rsbivand commented 3 months ago

Why do you need to run the pointless lidR code prior to the rgrass code?

Kodiologist commented 3 months ago

Loading lidR seems to be necessary to reproduce the bug. The original code in which I encountered this bug uses lidR nontrivially. I changed it to a trivial use here to make the example minimal.

rsbivand commented 3 months ago

Provide a minimal example actually doing something. Could you create the GRASS location first, then use lidR?

Kodiologist commented 3 months ago

To be clear, I don't expect you to find a workaround for me. I already have one, which is to say library(terra). I just filed a bug report because I thought it might be useful.

Could you create the GRASS location first, then use lidR?

Maybe? The trick is that I'm creating the location using a raster that's generated by lidR.

Provide a minimal example actually doing something.

Here's a little incomplete script I've been working on. It doesn't do anything useful yet. The idea is to use GRASS's r.sun to map sun exposure at the nursing homes.

library(sf)
library(terra)

grass.path = "/usr/lib/grass83"
las.dir = "/tmp"
las.crs = "epsg:6568"
crs.lonlat = "epsg:4326"
las.buffer.ft = 500.

nursing.homes = list(
    list(name = "Kingston Center for Rehabilitation and Nursing",
        lon = -71.61699, lat = 41.54679,
        las.files = "USGS_LPC_RI_Statewide_D22_295000_167500.laz"),
    list(name = "Mikaela Esau",
        lon = -71.59471, lat = 41.67216,
        las.files = c("USGS_LPC_RI_Statewide_D22_300000_212500.laz", "USGS_LPC_RI_Statewide_D22_302500_212500.laz")))

for (l in nursing.homes)
   {coords = st_coordinates(st_transform(crs = las.crs, st_as_sf(
        data.frame(l$lon, l$lat), coords = c(1, 2), crs = crs.lonlat)))
    dsm = lidR::rasterize_canopy(
        res = 1,
        algorithm = lidR::pitfree(),
        las = lidR::readLAS(
            file.path(las.dir, l$las.files),
            select = "xyzr",
            filter = do.call(sprintf, c(list("-keep_xy %d %d %d %d"), round(
                coords[c(1, 2, 1, 2)] +
                c(-1, -1, 1, 1) * las.buffer.ft)))))
    w = rgrass::initGRASS(grass.path,
        home = tempdir(), SG = dsm, override = T)}
Kodiologist commented 3 months ago

Yeah, this seems to be a lidR issue.