| 1 |
read.asciigrid <- function(fname, as.image = FALSE, plot.image = FALSE, |
| 2 |
colname=fname, proj4string = CRS(as.character(NA))) { |
| 3 |
t = file(fname, "r") |
| 4 |
l5 = readLines(t, n = 6) |
| 5 |
# instead of: |
| 6 |
# l5s = strsplit(l5, " ") |
| 7 |
# Michael Summer, [email protected] suggested: |
| 8 |
l5s = strsplit(l5, "\\s+", perl = T) |
| 9 |
xllcenter = yllcenter = xllcorner = yllcorner = as.numeric(NA) |
| 10 |
for (i in 1:6) { |
| 11 |
fieldname = casefold(l5s[[i]][1]) |
| 12 |
if (length(grep("ncols", fieldname))) |
| 13 |
ncols = as.numeric(l5s[[i]][2]) |
| 14 |
if (length(grep("nrows", fieldname))) |
| 15 |
nrows = as.numeric(l5s[[i]][2]) |
| 16 |
if (length(grep("xllcorner", fieldname))) |
| 17 |
xllcorner = as.numeric(l5s[[i]][2]) |
| 18 |
if (length(grep("yllcorner", fieldname))) |
| 19 |
yllcorner = as.numeric(l5s[[i]][2]) |
| 20 |
if (length(grep("xllcenter", fieldname))) |
| 21 |
xllcenter = as.numeric(l5s[[i]][2]) |
| 22 |
if (length(grep("yllcenter", fieldname))) |
| 23 |
yllcenter = as.numeric(l5s[[i]][2]) |
| 24 |
if (length(grep("cellsize", fieldname))) |
| 25 |
cellsize = as.numeric(l5s[[i]][2]) |
| 26 |
if (length(grep("nodata_value", fieldname))) |
| 27 |
nodata.value = as.numeric(l5s[[i]][2]) |
| 28 |
} |
| 29 |
if (is.na(xllcorner) && !is.na(xllcenter)) |
| 30 |
xllcorner = xllcenter - 0.5 * cellsize |
| 31 |
else |
| 32 |
xllcenter = xllcorner + 0.5 * cellsize |
| 33 |
if (is.na(yllcorner) && !is.na(yllcenter)) |
| 34 |
yllcorner = yllcenter - 0.5 * cellsize |
| 35 |
else |
| 36 |
yllcenter = yllcorner + 0.5 * cellsize |
| 37 |
map = scan(t, as.numeric(0), quiet = TRUE) |
| 38 |
close(t) |
| 39 |
if (length(as.vector(map)) != nrows * ncols) |
| 40 |
stop("dimensions of map do not match that of header") |
| 41 |
map[map == nodata.value] = NA |
| 42 |
if (as.image) { |
| 43 |
img = matrix(map, ncols, nrows)[,nrows:1] |
| 44 |
img = list(z = img, x = xllcorner + cellsize * ((1:ncols) - 0.5), |
| 45 |
y = yllcorner + cellsize * ((1:nrows) - 0.5)) |
| 46 |
if (plot.image) { |
| 47 |
image(img, asp = 1) |
| 48 |
return(invisible(img)) |
| 49 |
} else |
| 50 |
return(img) |
| 51 |
} |
| 52 |
df = data.frame(map) |
| 53 |
names(df) = colname |
| 54 |
grid = GridTopology(c(xllcenter,yllcenter), rep(cellsize,2), c(ncols,nrows)) |
| 55 |
SpatialGridDataFrame(grid, data = df, proj4string=proj4string) |
| 56 |
} |
| 57 |
|
| 58 |
write.asciigrid <- function(x, fname, attr = 1, na.value = -9999, ...) { |
| 59 |
|
| 60 |
# R> gridparameters(meuse.grid) |
| 61 |
# cellcentre.offset cellsize cells.dim |
| 62 |
# x 178460 40 78 |
| 63 |
# y 329620 40 104 |
| 64 |
|
| 65 |
#NCOLS 80 |
| 66 |
#NROWS 115 |
| 67 |
#XLLCORNER 178400.000000 |
| 68 |
#YLLCORNER 329400.000000 |
| 69 |
#CELLSIZE 40.000000 |
| 70 |
#NODATA_VALUE 1e31 |
| 71 |
|
| 72 |
if (!gridded(x)) |
| 73 |
stop("can only write SpatialGridDataFrame objects to asciigrid") |
| 74 |
x = as(x, "SpatialGridDataFrame") |
| 75 |
gp = gridparameters(x) |
| 76 |
if (length(gp$cells.dim) != 2) |
| 77 |
stop("asciigrid only supports 2D grids") |
| 78 |
if (gp$cellsize[1] != gp$cellsize[2]) |
| 79 |
stop("Asciigrid does not support grids with non-square cells") |
| 80 |
f = file(fname, open = "w") |
| 81 |
writeLines(c( |
| 82 |
paste("NCOLS", gp$cells.dim[1]), |
| 83 |
paste("NROWS", gp$cells.dim[2]), |
| 84 |
paste("XLLCORNER", gp$cellcentre.offset[1] - 0.5 * gp$cellsize[1]), |
| 85 |
paste("YLLCORNER", gp$cellcentre.offset[2] - 0.5 * gp$cellsize[2]), |
| 86 |
paste("CELLSIZE", gp$cellsize[1]), |
| 87 |
paste("NODATA_VALUE", na.value) |
| 88 |
), f) |
| 89 |
z = x@data[[attr]] |
| 90 |
z[is.na(z)] = na.value |
| 91 |
write.table(t(matrix(z, gp$cells.dim[1], gp$cells.dim[2])), file = f, |
| 92 |
row.names = FALSE, col.names = FALSE, ...) |
| 93 |
close(f) |
| 94 |
} |