subsetting terra::raster

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

subsetting terra::raster

Herry
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
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: subsetting terra::raster

Michael Sumner-2
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)
<[hidden email]> 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
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>


--
Michael Sumner
Software and Database Engineer
Australian Antarctic Division
Hobart, Australia
e-mail: [hidden email]

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: subsetting terra::raster

Herry
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 <[hidden email]>
Sent: Tuesday, 14 July 2020 2:41 PM
To: Herr, Alexander (L&W, Black Mountain) <[hidden email]>
Cc: [hidden email]
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) <[hidden email]<mailto:[hidden email]>> 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
[hidden email]<mailto:[hidden email]>
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


--
Michael Sumner
Software and Database Engineer
Australian Antarctic Division
Hobart, Australia
e-mail: [hidden email]<mailto:[hidden email]>

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: subsetting terra::raster

Michael Sumner-2
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)
<[hidden email]> 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 <[hidden email]>
> *Sent:* Tuesday, 14 July 2020 2:41 PM
> *To:* Herr, Alexander (L&W, Black Mountain) <[hidden email]>
> *Cc:* [hidden email]
> *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) <
> [hidden email]> 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
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>
>
>
> --
>
> Michael Sumner
> Software and Database Engineer
> Australian Antarctic Division
> Hobart, Australia
> e-mail: [hidden email]
>


--
Michael Sumner
Software and Database Engineer
Australian Antarctic Division
Hobart, Australia
e-mail: [hidden email]

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo