STBrinkmann / GVI

Other
20 stars 2 forks source link

Issue with functions used #1

Closed labiblm closed 2 years ago

labiblm commented 2 years ago

When running the new package, there are issues with the Terra function. It has the following error: "unable to find an inherited method for function ‘res’ for signature ‘"SpatRaster"’

STBrinkmann commented 2 years ago

I have updated some functions the examples of the Readme report no issues. Can you install the latest version of GVI with remotes::install_git("https://github.com/STBrinkmann/GVI") and report which function of the GVI package throws the error?

labiblm commented 2 years ago

Installed the new version, but it is still the same: Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘res’ for signature ‘"SpatRaster"’. Is it anything to do with Terra's version?

STBrinkmann commented 2 years ago

Can you tell me what version of terra you have installed? And does the error occur for every function of the GVI package or only at a specific one?

labiblm commented 2 years ago

The problem is with the raster and terra package combination. Works fine with raster 3.5.8, and terra 1.5.1. There is a discussion on this in Terra https://github.com/rspatial/terra/issues/432 But now there are issues with: task 1 failed - "No current support for different x and y resolution." Not sure, if it is getting the old version for some reason. I will test the package installation again!

STBrinkmann commented 2 years ago

Issue has been solved after updating depending packages.

STBrinkmann commented 2 years ago

I figured out what was might have been the reason. You have used the GreenSpace raster for Manchester as input. I have run some tests on this dataset and found the reason to be its extent:

First download the GS raster from Zenodo

library(terra)    
gs_path <- file.path("Manchester_LULC_green.tif")

# Downlaod GreenSpace mask from Zenodo. For details of see:
# https://doi.org/10.5281/zenodo.5061257
download.file(url = "https://zenodo.org/record/5061257/files/GreaterManchester_GreenSpace_5m.tif",
                destfile = gs_path, mode="wb")

gs <- rast(gs_path)

When testing for equal resolution it returns FALSE!

res(gs)[1] == res(gs)[2] # This returns FALSE!!

From the extent we can see that the x/y min/max values will result in not perfectly equal X and Y resolution

ext(gs)
# SpatExtent : 349999.9968, 409999.9969, 380004.9997, 430004.9998 (xmin, xmax, ymin, ymax)

We can fix this by manually creating a raster

e <- round(ext(gs))
gs2 <- terra::rast(crs = crs(gs),
                   xmin =  e[1],
                   xmax = e[2],
                   ymin = e[3],
                   ymax = e[4],
                   resolution = 5)
gs2[] <- terra::values(gs, mat = FALSE)

Now checking the resolution returns TRUE

res(gs2)[1] == res(gs2)[2] # This returns TRUE
labiblm commented 2 years ago

Thanks, this sounds right! There were definitely some issues with the extent and resolution. I already fixed the resolution issue using resampling and matching with other rasters. We should keep a note of this in the function description.