[R-sig-Geo] problems extracting raster values from x,y positions
Benjamin Mack
bmack at uni-bonn.de
Fri Nov 18 18:19:53 CET 2011
Hello all,
I'm getting crazy with a really basic problem but can't figure out what
I am doing wrong. I found a way (see below) to solve the problem but it
is everything but not elegant.
I have an raster img with 72 layers which I read from an envi file.
> img <- stack(fname)
Now I want to extract some pixel values using an SpatialPointsDataFrame
img_vals <- extract(img, ref)
The problem is, that the output tells my that the values in the 72 bands
are all equal - which is not true!
> img_vals
[1,] 0.034341563 0.034341563 ...
[2,] 0.014481238 0.014481238 ...
[...,]
To show you the strange behaviour look at the following.
Trying to do the following (which I suppose to be somehow equal than
using extract but with the pixel row/col -locations) I get the same
problem (equal values in all bands):
>img[1,1]
[1,] 0.008778242 0.008778242 ...
BUT when I do it in a different way I get the truth (controlled in ENVI)
with differnet values in each layer.
> getValues(img at layers[[1]])[1]
[1] 0.008778242
> getValues(img at layers[[2]])[1]
[1] 0.006187854
> getValues(img at layers[[3]])[1]
[1] 0.01333527
Has anybody an idea what is wrong here and how I have to use the extract
command correctly? Any suggestions are highly appreciated!
Thanks in advance!
---------------------------------------------------
My (ugly) workaround:
img <- stack(fname)
ci <- rasterize(ref,img,'ID')
ind_sampling <- which(is.na(getValues(ci))==F)
vals <- c()
for (ii in 1:dim(img)[3]) {
vals <- rbind(vals, getValues(img at layers[[ii]])[ind_sampling]) }
More information about the R-sig-Geo
mailing list