3

(This question comes from https://stackoverflow.com/questions/40698369/r-language-problems-with-spatialpixelsdataframe?noredirect=1#comment68629240_40698369)

The following two scripts will generate a "SpatialPixelDataFrame" object:

# FIRST
library(rgdal)
elev.grid <- readGDAL("whatever.asc")
elev.grid <- as(elev.grid, "SpatialPixelsDataFrame")

# SECOND
library(raster)
library(SDMTools)
library(adehabitat)
elev.grid <- raster("whatever.asc")
elev.grid.asc <- asc.from.raster(elev.grid) 
elev.grid.SPDF <- asc2spixdf(elev.grid.asc)

However, the first one exceeds the capability of my computing resources when applying it to big (15000 x 16000) grids, and the second one generates an object which I can't use for some of my further analyses. For example, when I use it for krige purposes

x <- krige(V3~var, points, elev.grid) 

I get the following:

Error in model.frame.default(terms(formula), as(data, "data.frame"), na.action = na.fail) : invalid type (closure) for variable 'var'

Whether providing me a trick to bypass the memory/capability issue in the first case (preferably), or fixing the error generated by the second case please recommend.

3
  • How big is .asc file? Are you working in 32-bit or 64-bit version? From R Memory Limit: The address-space limit is system-specific: 32-bit OSes imposes a limit of no more than 4Gb: it is often 3Gb. Running 32-bit executables on a 64-bit OS will have similar limits: 64-bit executables will have an essentially infinite system-specific limit (e.g., 128Tb for Linux on x86_64 cpus). – aldo_tapia Nov 20 '16 at 11:53
  • Gracias @aldo_tapia, I'm running on 64-bit operating system base: Rocks 6.1.1 (Sand Boa), with 128 GB of memory. The .asc is 1550 Mb... – perep1972 Nov 20 '16 at 12:12
  • First workflow seems to me; (1) Build SpatialGridDataFrame by {rgdal}, then (2) Convert SGDF to SPDF directly by raster:: as() . (It is not clear if {raster} is called, though). Is it possible to do (1) Build SGDF by {rgdal}, then (2) Convert SGDF to raster object by raster:: raster() , then (3) Coerce raster object by as() ? (I have not tested it myself, sorry. Just thought raster object would be generally more efficient in storing big data, and having intermediate object may reduce burden on your machine.) – Kazuhito Nov 20 '16 at 12:13
1

A different way to obtain a spatial object useful for gstatpackage (krige() for Krigging) avoiding as(object,"Class") function, is convert it to SpatialPointsDataFrame and grid data.

Try this:

library(raster)
r <- raster("whatever.asc")
xy <- data.frame(xyFromCell(r, 1:ncell(r)))
v <- getValues(r)
xy[,"z"] <- v
coordinates(xy) <- ~x+y
gridded(xy) <- TRUE

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.