rsbivand / rgrass

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

'ERROR: missing value where TRUE/FALSE needed' when converting larger raster to "SpatialGridDataFrame" #26

Closed MxNl closed 3 years ago

MxNl commented 3 years ago

Hello,

I am using rgrass7_0.2-5 and standalone winGRASS 7.8.5-2 all installed in default directories with

R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server x64 (build 17763)

I start GRASS with

    initGRASS(gisBase = "C:/Program Files/GRASS GIS 7.8",
              addon_base = "C:/username/App Data/GRASS7/addons",
              home = tempdir(),
              gisDbase = tempdir(), 
              location = "test", 
              mapset = "PERMANENT", 
              override = TRUE)

Then I write sf LINESTRINGS and a raster into the GRASS GIS database.

use_sf()
sf_lines %>%
    writeVECT("river_network", 
                     v.in.ogr_flags = c("overwrite"))

use_sp()
reference_raster %>% 
      as("SpatialGridDataFrame") %>% 
      writeRAST("reference_raster",
                overwrite = TRUE)

This all works fine except for writing really large objects to GRASS. Currently, the object size of the raster seems to cause the following error as this is the only difference between two runs where one errors out and the other doesn't:

ERROR: missing value where TRUE/FALSE needed. 

When I write a raster in 1km resolution with an extent of Europe it doesn't crash. When I write it in 100m resolution the error occurs.

Do have any clue what could cause this or how I can prevent that? Thanks a lot!

rsbivand commented 3 years ago

Please never use pipes in examples (the first argument is not available for examination) , and at least provide the verbatim error message, and preferably the output of traceback() and sessionInfo() as well as the OS, GRASS version, etc. This should also be posted on https://lists.osgeo.org/mailman/listinfo/grass-stats, because a user may have seen this before. You do not show the size of reference_raster either, so I have absolutely no idea. Reprex required.

I strongly suspect that reference_raster is not held in memory, and that coercion to "SpatialGridDataFrame" fails if it is too big, because it is then in memory, not external. If all *'raster** is being used for is to open a file, read the file directly into the newly created temporary location (but do set the GRASS region carefully). If the failure is before writeRAST() is reached, the pipeline is a distraction.

MxNl commented 3 years ago

Thanks for that quick reply. Finally, I could look into it today. So the error occurs at the conversion to "SpatialGridDataFrame" as you suspected correctly

as(reference_raster, "SpatialGridDataFrame")

Error:

Fehler in if (.NumberOfCells(object@grid) != nrow(object@data)) return("unequal number of objects in full grid and data slot") : 
  Fehlender Wert, wo TRUE/FALSE nötig ist
Zusätzlich: Warnmeldung:
In attributes(.Data) <- c(attributes(.Data), attrib) :
  NAs introduced by coercion to integer range

subsequent traceback() gives me

11: validityMethod(object)
10: isTRUE(x)
9: anyStrings(validityMethod(object))
8: validObject(.Object)
7: initialize(value, ...)
6: initialize(value, ...)
5: new("SpatialGridDataFrame", grid, data = data)
4: SpatialGridDataFrame(grd, proj4string = .getCRS(r), data = as.data.frame(from))
3: asMethod(object)
2: as(., "SpatialGridDataFrame")
1: reference_raster %>% as("SpatialGridDataFrame")

Should I better write the raster to disk and import it from there?

rsbivand commented 3 years ago

Thanks - please report the size of reference_raster, that is rows and columns, its class, and whether integer or double. The error message is from sp, R/Class-SpatialGridDataFrame.R line 17, and raster R/coerce.R. Please report as(reference_raster, "GridTopology"), and dim(reference_raster).