简体   繁体   中英

R - raster function NAs values lower than -9999 in ASCII file

I have been having problems importing a ASCII raster that has values that go from Min. :-69826220 Min. :-69826220 to Max. :167780500 Max. :167780500 . The problem I am encountering is that when I use the raster function to import the ASCII file then every value smaller than -9999 is reported as NA and the minimum value is -9458 .

Is this a bug of the function and is there a workaround? When I import the same ASCII file as a data frame everything is fine and I get the whole range of values. Also I am using the same procedure to import other ASCII rasters and don't have any problem.

here is the link to the ASCII file https://dl.dropboxusercontent.com/u/24234831/ps0011yme.asc

Here is the session info, I opened a new session just in case.

sessionInfo() R version 3.0.0 (2013-04-03) Platform: x86_64-w64-mingw32/x64 (64-bit)

locale: [1] LC_COLLATE=English_United States.1252 [2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 [4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] raster_2.1-16 sp_1.0-8

loaded via a namespace (and not attached): [1] grid_3.0.0 lattice_0.20-15

Any help is appreciated

You can try to use setMinMax() on your raster file to try and work out the min and max values and store them in the returned Raster* object. Try it like so:

r <- setMinMax( raster("path/to/myraster.asc") )

I am not sure what is happening because if I downlaod you data and do:

r1 <- raster( "~/Downloads/test.asc")
summary(values(r1))
     Min.   1st Qu.    Median      Mean   3rd Qu.      Max.      NA's 
-69830000  -4789000    737300  16950000  13880000 167800000     71468

Please add the output of sessionInfo() into your question , ie not as a comment.

The errors in this case were being caused by not having rgdal installed, which are bindings to the Geospatial Data Abstraction Library and are very important for importing/exporting raster and shapefile data.

I'm unable to reproduce your error. Here's a hand-built .asc file:

NCOLS 3 
NROWS 3 
XLLCORNER 0 
YLLCORNER 0 
CELLSIZE 0.5 
NODATA_value -9999 
1e-6 0.3 -34567891234
0.2 -1e6 25
3 68492758321934 20

That loaded correctly into a raster object. You'll notice the NODATA_value item there, which explains where your -9999 come from. My bet is that there's something corrupted in your source .asc file. Can you post the header and a small sample of the data?

The internal ascii file driver in 'raster' assumes that there are no valid values lower than the NA flag value if the flag value is < 0 (and I would not recommend using an NA flag in the middle of the values). Clearly, this approach can cause problems like in this case; and I will change that. You can see the difference between the internal driver and the gdal driver if you do

 library(raster)
 library(rgdal)
 a1 <- raster(filename, native=TRUE)
 a2 <- raster(filename, native=FALSE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM