[R-sig-Geo] decimals in NA value using raster::writeRaster with .ascii format

Javier Garcia-Pintado j.garcia-pintado at reading.ac.uk
Fri Apr 11 09:09:05 CEST 2014


Note you also have a 'PCRaster' driver in GDAL.

Javier
---


________________________________________
From: r-sig-geo-bounces at r-project.org [r-sig-geo-bounces at r-project.org] on behalf of Zack Holden [zaholdenfs at gmail.com]
Sent: 10 April 2014 21:06
To: r-sig-geo at r-project.org
Subject: [R-sig-Geo] decimals in NA value using raster::writeRaster with        .ascii format

Dear list,

I am creating ascii files as inputs to the PCraster software (no other
gdal format is allowed). When I write my ascii file using
raster::writeRaster, the first cell of every raster that I write has a
large number of decimal values appended to it. This causes errors
later in the model workflow.

This also happens to the first NA value in the file, so that all NA
values in the file are e.g. -9999 except the first cell. which appears
as: -9999.00000000000

Can anyone suggest a method for ensuring that all of my NA values are
identical and with no trailing decimals?

A small reproducible example follows. Any help or suggestions is appreciated.

Thanks,
Zack

##################
# start code
require(raster)
r <- raster(nrows=2, ncols=2, xmn=0, xmx=2, ymn=0, ymx=2)
r <- setValues(r, seq(1:ncell(r)))
rexpand <- extend(r, c(2,2))
NAvalue(rexpand) <- -9999

temp.dir <- "/home/zholden/"
writeRaster(rexpand, file=paste(temp.dir, "testraster.asc",sep=""),
NAflag=-9999, dataType="INT2U", overwrite=T)
# end code
##################

# opening in a text editor reveals trailing decimals in the first NA value.
system(paste("gedit ", temp.dir, "testraster.asc", sep=""))

################################
 sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-redhat-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
LC_MONETARY=en_US.UTF-8
 [6] LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C
               LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

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

other attached packages:
[1] rgdal_0.8-16  raster_2.2-12 sp_1.0-14

loaded via a namespace (and not attached):
[1] grid_3.0.2      lattice_0.20-23 tools_3.0.2

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list