[R-sig-Geo] writeRaster to ascii file (.asc)

Mauricio Zambrano-Bigiarini mauricio.zambrano at jrc.ec.europa.eu
Thu Jul 11 09:02:53 CEST 2013


On 10/07/13 18:38, Manuel Spínola wrote:
> Dear list members,
>
> I am trying to export a raster file to a ascii file (.asc) but the min and
> max values are not the same. Any idea why?

Hi Manuel,

Could you provide your sessionInfo() ?
>
>> bio1res
> class       : RasterLayer
> dimensions  : 382, 407, 155474  (nrow, ncol, ncell)
> resolution  : 0.008333333, 0.008333333  (x, y)
> extent      : -85.95, -82.55833, 8.041667, 11.225  (xmin, xmax, ymin, ymax)
> coord. ref. : NA
> data source : in memory
> names       : bio1_23
> values      : 47, 272  (min, max)
>
>> writeRaster(bio1res, filename="temp.asc", format="ascii",overwrite=TRUE)
> class       : RasterLayer
> dimensions  : 382, 407, 155474  (nrow, ncol, ncell)
> resolution  : 0.008333333, 0.008333333  (x, y)
> extent      : -85.95, -82.55833, 8.041667, 11.225  (xmin, xmax, ymin, ymax)
> coord. ref. : NA
> data source : /Users/manuelspinola/Dropbox/Distribución de
> especies/World_Clim_Costa_Rica/temp.asc
> names       : temp
> values      : -2147483648, 2147483647  (min, max)

Unfortunately, your example is not reproducible, so I tried with a 
different example and the values in the original and the written ascii 
file were the same (rgdal_0.8-10, raster_2.1-48, sp_1.0-11):

----------  START --------------
library(raster)
Loading required package: sp

# Creation of a raster file for testing
r <- raster(system.file("external/test.grd", package="raster"))

r
#class       : RasterLayer
#dimensions  : 115, 80, 9200  (nrow, ncol, ncell)
#resolution  : 40, 40  (x, y)
#extent      : 178400, 181600, 329400, 334000  (xmin, xmax, ymin, ymax)
#coord. ref. : +init=epsg:28992 
+towgs84=565.237,50.0087,465.658,-0.406857,0.350733,-1.87035,4.0812 
+proj=sterea +lat_0=52.15616055555555 +lon_0=5.38763888888889 
+k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +units=m +no_defs
#data source : /usr/lib64/R/library/raster/external/test.grd
#names       : test
#values      : 128.434, 1805.78  (min, max)

writeRaster(r, filename="temp.asc", format="ascii",overwrite=TRUE)
#rgdal: version: 0.8-10, (SVN revision 478)
#Geospatial Data Abstraction Library extensions to R successfully loaded
#Loaded GDAL runtime: GDAL 1.9.2, released 2012/10/08
#Path to GDAL shared files: /usr/share/gdal
#Loaded PROJ.4 runtime: Rel. 4.8.0, 6 March 2012, [PJ_VERSION: 480]
#Path to PROJ.4 shared files: (autodetected)
#class       : RasterLayer
#dimensions  : 115, 80, 9200  (nrow, ncol, ncell)
#resolution  : 40, 40  (x, y)
#extent      : 178400, 181600, 329400, 334000  (xmin, xmax, ymin, ymax)
#coord. ref. : NA
#data source : /home/zambrhe/temp.asc
#names       : temp

# reading the .asc file form disk
r2 <- raster("temp.asc")

r2
#class       : RasterLayer
#dimensions  : 115, 80, 9200  (nrow, ncol, ncell)
#resolution  : 40, 40  (x, y)
#extent      : 178400, 181600, 329400, 334000  (xmin, xmax, ymin, ymax)
#coord. ref. : NA
#data source : /home/zambrhe/temp.asc
#names       : temp

# Summary of the ascii file in the hard disk
#summary(r2)
#             temp
#Min.     128.4340
#1st Qu.  293.2325
#Median   371.4120
#3rd Qu.  499.8195
#Max.    1805.7800
#NA's    6097.0000


# Summary of the original raster file
summary(r)
#             test
#Min.     128.4340
#1st Qu.  293.2325
#Median   371.4120
#3rd Qu.  499.8195
#Max.    1805.7800
#NA's    6097.0000
----------  END --------------

Kind regards,

Mauricio Zambrano-Bigiarini, Ph.D

-- 
=================================================
Water Resources Unit
Institute for Environment and Sustainability (IES)
Joint Research Centre (JRC), European Commission
TP 261, Via Enrico Fermi 2749, 21027 Ispra (VA), IT
Work Phone : +39 0332 789588
webinfo    : http://floods.jrc.ec.europa.eu/
=================================================
DISCLAIMER:
"The views expressed are purely those of the writer
and may not in any circumstances be regarded as sta-
ting an official position of the European Commission"
=================================================
"The journey is the reward"  (Steve Jobs)



More information about the R-sig-Geo mailing list