Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

How to produce "SpatialPixelsDataFrame" from staccked RasterLayer ?

I have a stacked rasterlayer and I want to convert them to SpatialPixelsDataFrame.

RasterLayer information is

> RAD.all[[1]]
class       : RasterLayer 
dimensions  : 180, 240, 43200  (nrow, ncol, ncell)
resolution  : 0.01, 0.01  (x, y)
extent      : -123.305, -120.905, 36.705, 38.505  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : C:\Users\Michael\Documents\1998bil\1998ASC5min_ppt_spas1214_0001_19980202_0810_UTC.asc.bil 
names       : X1998ASC5min_ppt_spas1214_0001_19980202_0810_UTC.asc 
share|improve this question

2 Answers 2

up vote 2 down vote accepted

There are two nice packages in (Macro)Ecology that have a workaround for what you want to do:

library(raster)
library(SDMTools)
library(adehabitat)

r <- raster("yourFile.tif") # Get a link to a raster
rAsc <- asc.from.raster(r) # Function from SDMTools to convert to asc format
b <- asc2spixdf(rAsc) # Function from adehabitat to convert to SpatialPixelDataFrame
class(b) # See if it worked
>[1] "SpatialPixelsDataFrame"
>attr(,"package")
>[1] "sp"

Then just apply this in a loop or via StackApply and a custom function to your stacked object.

share|improve this answer
    
Thanks a lot. It worked. Yes, I can see > class(b) [1] "SpatialPixelsDataFrame" attr(,"package") [1] "sp" > –  Nahm Aug 23 at 8:47
    
Very inefficient approach. mdsummer's suggestion is the correct answer. –  RobertH Sep 3 at 17:32

Coercion methods are included in the raster package:

as(RAD.all, "SpatialPixelsDataFrame")
share|improve this answer
    
It works very well. Thanks. > AA <- as(RAD.all, "SpatialPixelsDataFrame") > class(AA) [1] "SpatialPixelsDataFrame" attr(,"package") [1] "sp" –  Nahm Aug 24 at 1:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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