Skip to content

subsetting terra::raster

4 messages · Herr, Alexander (L&W, Black Mountain), Michael Sumner

#
Hi,

I am moving from raster to terra. I get an error when I adapt the following raster code:
require(raster)
set.seed(666)
 reps=10
 r <- raster( matrix(nrow=reps,ncol=reps, round(rnorm(reps^2),1)))
 extent(r)<- c(1,101,1,101)
plot(r) 
xy<-xyFromCell(r,r[r>0])
z<-r[r>0]
cbind(xy,z)

Under terra code:
require(terra)
r <- rast( matrix(nrow=reps,ncol=reps, round(rnorm(reps^2),1)))
plot(r)
xy<-xyFromCell(r,r[r>0])
Error in r[r > 0] : object of type 'S4' is not subsettable

What am I doing wrong?

Thanks
herry


 in terra
#
The second argument to xyFromCell is a *cell number*, but in `r[r>0]` you
have a *cell value*, which may not be what you want.

It's the r[r > 0] part that's not working in terra, so you could do this as
a workaround ...

xy <- xyFromCell(r, values(r)[values(r) >0])

(but are your cell values really intended for use as cell numbers? It's
certainly useful for that but probably uncommon).

Your code also did not set the extent in the terra object, or set.seed so
there's several steps that lack compatibility.

Cheers, Mike



On Tue, Jul 14, 2020 at 2:18 PM Herr, Alexander (L&W, Black Mountain)
<Alexander.Herr at csiro.au> wrote:

            

  
    
#
You are right,
What I really want is extract cell coordinates and their values for cells with values >0

So I could use in raster
xyz <- rasterToPoints(r, fun=function(x){x>0}, xy=T)
or
Which(r>0,cells=T)->p
cbind(xyFromCell(r,p),extract(r,p))

what?s the terra alternative?




From: Michael Sumner <mdsumner at gmail.com>
Sent: Tuesday, 14 July 2020 2:41 PM
To: Herr, Alexander (L&W, Black Mountain) <Alexander.Herr at csiro.au>
Cc: r-sig-geo at r-project.org
Subject: Re: [R-sig-Geo] subsetting terra::raster

The second argument to xyFromCell is a *cell number*, but in `r[r>0]` you have a *cell value*, which may not be what you want.

It's the r[r > 0] part that's not working in terra, so you could do this as a workaround ...

xy <- xyFromCell(r, values(r)[values(r) >0])

(but are your cell values really intended for use as cell numbers? It's certainly useful for that but probably uncommon).

Your code also did not set the extent in the terra object, or set.seed so there's several steps that lack compatibility.

Cheers, Mike
On Tue, Jul 14, 2020 at 2:18 PM Herr, Alexander (L&W, Black Mountain) <Alexander.Herr at csiro.au<mailto:Alexander.Herr at csiro.au>> wrote:
Hi,

I am moving from raster to terra. I get an error when I adapt the following raster code:
require(raster)
set.seed(666)
 reps=10
 r <- raster( matrix(nrow=reps,ncol=reps, round(rnorm(reps^2),1)))
 extent(r)<- c(1,101,1,101)
plot(r)
xy<-xyFromCell(r,r[r>0])
z<-r[r>0]
cbind(xy,z)

Under terra code:
require(terra)
r <- rast( matrix(nrow=reps,ncol=reps, round(rnorm(reps^2),1)))
plot(r)
xy<-xyFromCell(r,r[r>0])
Error in r[r > 0] : object of type 'S4' is not subsettable

What am I doing wrong?

Thanks
herry


 in terra


_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at r-project.org<mailto:R-sig-Geo at r-project.org>
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


--
Michael Sumner
Software and Database Engineer
Australian Antarctic Division
Hobart, Australia
e-mail: mdsumner at gmail.com<mailto:mdsumner at gmail.com>
#
you could do this for a single layer object:

p <- which(values(r) > 0)    #  Possibly faster to use values(r > 0) > 0
cbind(xyFromCell(r,p),extract(r,p))

as long as getting all values is memory-safe (I'm not sure if there's a
Which analog in terra).

HTH



On Tue, Jul 14, 2020 at 4:02 PM Herr, Alexander (L&W, Black Mountain)
<Alexander.Herr at csiro.au> wrote: