Skip to content
This repository has been archived by the owner on Apr 20, 2020. It is now read-only.

NA-Bug? #93

Closed
raff-k opened this issue Dec 19, 2017 · 4 comments
Closed

NA-Bug? #93

raff-k opened this issue Dec 19, 2017 · 4 comments

Comments

@raff-k
Copy link
Contributor

raff-k commented Dec 19, 2017

Dear RQGIS team,
since the QGIS processing toolbox is working on my machine, I could further invesitage the issue with NA-values. I discovered, that it seems to be a more general RQGIS (to QGIS) issue. I tested different ways of writing out a raster containing NA values and then computed slope manually in QGIS (I tried SAGA and GRASS GIS version). I discovered that generally tiff-files written out using rgdal::writeGDAL(…) worked in QGIS. Tiffs written with raster::writeRaster(…) only worked in QGIS if I have set a NA-flag. Perhaps QGIS cannot deal with or pass a correct no data value to SAGA or GRASS when its no data value is -3.39e^38?

Can you repdoruce the issue? – If yes, maybe an alternative can be to set a default NA-value (i.e. -99999) or switching the write function to rgdal.

I used QGIS 2.18.15, and RQGIS_1.0.2.

# set environment and variables
setwd("d:/Users/Desktop/NA-Test")

library("RQGIS", "raster", "rgdal")

dem <- RQGIS::dem

RQGIS:::set_env(new = TRUE, dev = TRUE)
RQGIS::open_app()
RQGIS::qgis_session_info()

# create dem with NA values
dem.NA <- raster::calc(dem, fun = function(x){(ifelse(x > 500, NA, x))})
raster::plot(dem.NA)



# write data with different functions and with/without NAflag
raster::writeRaster(dem.NA, "dem_writRast_NAFlag.tif", NAflag = -99999, overwrite = TRUE) # 1
raster::writeRaster(dem.NA, "dem_writRast.tif", overwrite = TRUE) # 2

rgdal::writeGDAL(as(dem.NA, "SpatialGridDataFrame"), "dem_writGDAL_NAFlag.tif", mvFlag = -99999) # 3
rgdal::writeGDAL(as(dem.NA, "SpatialGridDataFrame"), "dem_writGDAL.tif") # 4

# ... then I tested manually in QGIS if the alogithm
# r.slope.aspect can deal with the NA value

# Result:
# 1. worked in QGIS
# 2. DID NOT WORK, NoData values are somehow included in output
# 3. worked in QGIS
# 4. worked in QGIS


# try: process slope in RQGIS that worked in QGIS
dem.WritRast.NAFlag <- raster::raster("dem_writRast_NAFlag.tif") # one with flag
dem.WritGDAL <- raster::raster(rgdal::readGDAL("dem_writRast_NAFlag.tif")) # one without flag



# calculate slope based on the different files
slope.WritRast.NAFlag <- RQGIS::run_qgis("grass:r.slope.aspect", elevation = dem.WritRast.NAFlag, 
                         slope = file.path(getwd(), "slope_WRNA.tif"), load_output = TRUE)
summary(raster::values(slope.WritRast.NAFlag)) # max = 90?!


slope.WritGDAL <- RQGIS::run_qgis("grass:r.slope.aspect", elevation = dem.WritGDAL, 
                                              slope = file.path(getwd(), "slope_WGDAL.tif"), 
                                               load_output = TRUE)
summary(raster::values(slope.WritGDAL)) # max = 90?!
@raff-k raff-k changed the title NA-Bug NA-Bug? Dec 19, 2017
@jannes-m
Copy link
Collaborator

Thanks @raff-k, will have a look at it the next days. If it the solution is as simple as specifying NA values, give it a try yourself, here is where we write out rasters:
https://github.com/jannes-m/RQGIS/blob/f413ee940c14062f9e3d0a55ddef61f2773467ad/R/helper_funs.R#L481-L494

Then you can issue a PR. In any case thanks for your valuable input!

@raff-k
Copy link
Contributor Author

raff-k commented Dec 19, 2017

The NA issue is solved on my fork when I added NAflag = NaN as function parameter to save_spatial_objects <- function(params, type_name, NAflag = NaN) and of course to the writeRast-functions.

try(writeRaster(
            params[[i]], filename = fname, format = "GTiff",
            prj = TRUE, overwrite = TRUE, NAflag = NAflag
          ), silent = TRUE)
      )
      if (inherits(test, "try-error")) {
        while (tolower(basename(fname)) %in% tolower(dir(tempdir()))) {
          fname <- paste0(gsub(".tif", "", fname), 1, ".tif")
        }
        writeRaster(
          params[[i]], filename = fname, format = "GTiff",
          prj = TRUE, overwrite = TRUE, NAflag = NAflag
        )

Maybe you could think about to add NAflag as "flag"-parameter to RQGIS::run_qgis so that the user can specify themselves an appropriate no data value. I am not sure if NaN here is the best option - maybe -99999 is a better default value. When I open this NaN-written raster in ArcGIS, NoData values are -1,#IND - also a bit awful :-/

@jannes-m
Copy link
Collaborator

Commit 9e74a4a should address your issue, pls check and if solved, close the issue.

@raff-k
Copy link
Contributor Author

raff-k commented Jan 18, 2018

thank you for implementation, @jannes-m. Issue is solved :-)

@raff-k raff-k closed this as completed Jan 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants