Skip to content

create ascii grid from grid.list object (fields)?

2 messages · Zack Holden, Roger Bivand

#
On Sat, 6 Dec 2008, zack holden wrote:

            
Another time, please use a cluefull text-only email client - the code was 
completely mixed up.
setwd("C:/climate_data/pnw_data")
data <- readShapePoints("climate_PNW_PCLOAD.shp")

If possible use the rgdal package, it gives many more drivers for vector 
and raster data - see readOGR().

# read in shapefile with X,Y and PC loadings for climate 
stationsproj4string=CRS(as.character(NA)), verbose=FALSE)

No idea what this was supposed to be

# data <- as.data.frame(data)
# long <- x[,1]
# longitude column
# lat <- x[,2]
# latitude column
# minlat <- min(lat)
# maxlat <- max(lat)
# minlong <- min(long)
# maxlong <- max(long)
# grid.l<- list( X1= seq(minlong, maxlong,,100), 
# X3=seq(minlat,maxlat,,100))

PredGrid <- Sobj_SpatialGrid(data)$SG
grd <- slot(PredGrid, "grid")
o <- rep(NA, prod(slot(grd, "cells.dim")))
PredGridDF <- SpatialGridDataFrame(slot(PredGrid, "grid"),
  data=data.frame(o=o))

This makes an empty SpatialGridDataFrame, use the maxDim= argument to 
Sobj_SpatialGrid() to control resolution.

# create grid.list object using ranges of Long. and Lat.
# make.surface.grid(grid.l)
# y <- data$pc1
# Princ. Comp. loading I want to interpolate
# LL <- cbind(long,lat)
# combine Lat and Long
# fit = Krig(LL,y, theta = 10)

fit <- Krig(coordinates(data), data$pc1, theta=10)

Use the data object directly

# fit Princ. comp. 1 to Lat long.
# surface( fit, grid.list=grid.l)

PredGridDF$pc1 <- predict(fit, coordinates(PredGridDF))
spplot(PredGridDF, "pc1", col.regions=tim.colors())

Use the predict() method on the prediction grid coordinates, assigning to 
a variable in the object (treat it as you would treat a data frame).

writeGDAL(PredGridDF["pc1"], "pc1.tif", drivername="GTiff")

to write a geotiff - see other drivers with gdalDrivers().

Hope this helps,

Roger

#  create interpolated surface based on Krig object