Thanks Robert for your comments,

Below the adjusted code which I tested for different extents of the study area.
The problem seems to be with the dataframe2asc part.
The ncols, nrows and cell size of the .asc files created should be identical, but they are not.

Can anyone try the code and comment on the observed differences?

Many thanks for your help!

André



rm(list = ls())
setwd ("D:/")
getwd()
library(raster)
library(SDMTools)

#  Create a raster and save it to disk
r <- raster(extent(-15, 15, -15, 15))
res(r) <- 1/12
r[] <- 1:ncell(r)
str(r)
plot(r)
writeRaster(r, filename = "D:/testdata", format = "ascii", NAflag = -9999, overwrite = T)

#  Import the raster using asc2dataframe
testdata.df <- asc2dataframe("D:/testdata.asc", varnames = "layer")
str(testdata.df)
head(testdata.df)

#  Export the dataframe as using dataframe2asc
dataframe2asc(testdata.df, outdir = "D:/")

### In Notepad check the headers of the .asc files

Ir. A.S.J. van Proosdij
PhD Student
e  andre.vanproosdij@wur.nl<mailto:andre.vanproosdij@wur.nl>

From: Robert J. Hijmans [mailto:r.hijmans@gmail.com]
Sent: woensdag 14 november 2012 19:13
To: Proosdij, Andre van
Cc: r-sig-geo@r-project.org
Subject: Re: [R-sig-Geo] asc2dataframe and dataframe2asc problem with ncols nrows and cell size


When asking questions on this list, please provide, whenever possible, an example that is simple, small, reproducible, and _self contained_. That is, the example should not depend on a file that needs to be separately downloaded. In stead create the data in your example; or use data that comes with the R package you are asking a question about (see the help files for ample inspiration). For example:

library(raster)
r <- raster(extent(-18, 43, -19, 15))
res(r) <- 1/12
r[] <- 1:ncell(r)
x <- rasterToPoints(r)
write.csv(x, file='data.csv', row.names=FALSE)
v <- read.csv('data.csv')
cells <- cellFromXY(r, v[,1:2])
s <- raster(r)
s[cells] <- v[,3]


Robert

On Wed, Nov 14, 2012 at 3:37 AM, Proosdij, Andre van <andre.vanproosdij@wur.nl<mailto:andre.vanproosdij@wur.nl>> wrote:
Dear all,

When importing a raster using asc2dataframe and exporting it again using dataframe2asc the number of cols, rows and the cell size changes. In the example I used I get:
# original raster has ncols = 7320 and nrows = 4080 and cell size 0.00833333333333333
# produced raster has ncols = 7321 and nrows = 4081 and cell size 0.00833333333332575

How can I adjust this correctly?

Below is the code I used. You have to get the .bil and .hrd files from the Harmonised World Soil Database from:
http://webarchive.iiasa.ac.at/Research/LUC/External-World-soil-database/HTML/HWSD_Data.html?sb=4

Many thanks in advance!

André

Ir. A.S.J. van Proosdij
PhD Student
t   +31 317 48 1198<tel:%2B31%20317%2048%201198>
e  andre.vanproosdij@wur.nl<mailto:andre.vanproosdij@wur.nl><mailto:andre.vanproosdij@wur.nl<mailto:andre.vanproosdij@wur.nl>>

Naturalis Biodiversity Center, section NHN
Biosystematics Group, Wageningen University
the Netherlands



###  BEGIN of Code  ###
###  Set the directory and load libraries
setwd ("D:/R")
library(raster)
library(rgdal)
library(SDMTools)
library(sp)

###  Download the .bil file and the .hdr file from the HWSD site:
# http://webarchive.iiasa.ac.at/Research/LUC/External-World-soil-database/HTML/HWSD_Data.html?sb=4
hwsd.files <- list.files("D:/R", pattern="[.]bil", full.names = T) # List the file
hwsd.files

###  Define the extent of study area and crop the object
ext.CAfr <- extent(-18, 43, -19, 15) # Extent of study area: lon 18W 43E; lat 15N 19S
hwsd.CAfr <- crop((raster(hwsd.files)), ext.CAfr)
plot(hwsd.CAfr) # Check the results
setwd ("D:/R") # Set the directory, otherwise writeRaster will not work
writeRaster(hwsd.CAfr, filename = paste(c("D:/R/", "hwsd_CAfr"), collapse = ""), format = "ascii", NAflag = -9999, overwrite = T) # Export the object as .asc file

###  Create a dataframe from the ascii file
# The df contains only x and y values and values for MU_GLOBAL
HWSD.CAfr.df <- asc2dataframe("D:/R/hwsd_CAfr.asc", varnames = "MU_GLOBAL")
head(HWSD.CAfr.df) # y, x, MU_GLOBAL

###  Export the dataframe as .asc file
dataframe2asc(HWSD.CAfr.df, outdir = "D:/R") # The df is used to generate separate ascii files for each column, in this case only MU_GLOBAL. Note that the df has y, x coordinates (OR lat,lon) and columns for the data to be output (MUST be in that order)

### In Notepad check the headers of the .asc files

###  END of code  ###

        [[alternative HTML version deleted]]


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


	[[alternative HTML version deleted]]

