Issues with saving 'terra' asc files - "Couldn't find data values in ASCII Grid file"?

59 Views Asked by At

Reproducible example from my dataset;

library(terra)
ext1_boreal <- ext(-154.529, -104.855, 47.663, 70.4789)
x <- rast(ext1_boreal, res=.1, crs="epsg:4326")
values(x) <- 1:ncell(x)

ext_EcoZone1 <- ext(-140.999, -122.812, 56.408, 64.700)
EcoZone2 <- vect(ext_EcoZone1, crs="epsg:4326")
x <- mask(x, EcoZone2)
f <- "D:/GitHub/MyProject/Outputs/Rasters/Explanatory/Testx.asc"
writeRaster(x, filename=f, overwrite=TRUE)

The problem:

Warning: D:/GitHub/MyProject/Outputs/Rasters/Explanatory/Testx.asc: Couldn't find data values in ASCII Grid file. (GDAL error 1)Error: [rast] cannot open this file as a SpatRaster: D:/GitHub/MyProject/Outputs/Rasters/Explanatory/Testx.asc

Why is this warning coming up? Using: Package terra version 1.7-71, R version 4.3.3.

I have been unable to export an acs (ESRI ASCII) Spatraster for my data. I can export tif without issues. I need the acs format to run another piece of software that runs off acs format. I am able to take Spatraster tif files into QGIS and individual save them as acs files that can then be imported back into R.

1

There are 1 best solutions below

6
Robert Hijmans On BEST ANSWER

I get the same error with the CRAN version of "terra" (1.7-71). The reason is that the NODATA value written to file is "nan"

f <- paste0(tempfile(), ".asc")
writeRaster(x, filename=f, overwrite=TRUE)
#Error: [rast] cannot open this file as a SpatRaster: c:\temp\RtmpmYSDeo\file24704e227b7.asc
#In addition: Warning message:
#file24704e227b7.asc: Couldn't find data values in ASCII Grid file. (GDAL error 1) 

readLines(f, 7)
#[1] "ncols        497"              
#[2] "nrows        228"              
#[3] "xllcorner    -154.529000000000"
#[4] "yllcorner    47.663000000000"  
#[5] "cellsize     0.100000000000"   
#[6] "NODATA_value nan"                                                 
#[7] "nan nan nan nan nan nan nan nan ...

I do not see that problem in the development version of "terra"

library(terra)
#terra 1.7.74
> readLines(f, 6)
[1] "ncols        497"              
[2] "nrows        228"              
[3] "xllcorner    -154.529000000000"
[4] "yllcorner    47.663000000000"  
[5] "cellsize     0.100000000000"   
[6] "NODATA_value  4294967295"  
[7] " 4294967295.0 4294967295 4294967295 4294967295 4294967295  ...

You can install the development version with

install.packages('terra', repos='https://rspatial.r-universe.dev')

Note that this also works without the separate call to writeRaster

x <- mask(x, EcoZone2, filename=f, overwrite=TRUE)

I see that this works for integer rasters, but not for rasters with real (floating point) values. The odd thing is that it does work with the example (if you use x/2). This may be because it is not a very large dataset and GDAL detects the real numbers in the first part of the file, whereas it does not do that correctly with your data).

This should be fixed but here is a work-around. You can set the NAflag manually so a value outside of the range of your actual values. For example

writeRaster(x, filename=f, overwrite=TRUE, NAflag=-9999)

readLines(f, 6)
#[1] "ncols        497"              
#[2] "nrows        228"              
#[3] "xllcorner    -154.529000000000"
#[4] "yllcorner    47.663000000000"  
#[5] "cellsize     0.100000000000"   
#[6] "NODATA_value  -9999" 
#" -9999.0 -9999 -9999 -9999 -9999