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
e  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]]

