[R-sig-Geo] raster::zonal with more than 1 zonal layer

Dominik Schneider dosc3612 at colorado.edu
Wed Mar 2 05:31:18 CET 2016


How do you separate the zone values from the zonal output into the original
elv and WS values?
On Mar 1, 2016 7:58 PM, <Alexander.Herr at csiro.au> wrote:

> Is this what you are after?
>
>
> require(raster)
> require(spatial)
> require(sp)
> dat=raster(matrix(runif(64),nrow=8))
> z1=raster(matrix( sample(1:4, 64, replace=T),nrow=8))#represents elevation
> bands
> z2=raster(matrix(sample(1401:1408,64,replace=T),nrow=8))#represents
> watersheds
>
>
> #assign unique id for each elevation x watershed
> z1[]->elv
> z2[]->ws
> elv*1000000+ws->nd
> z1->z3
> z3[]<-nd
> sort(unique(nd))
>
> # do zonal
> zonal(dat,z3, 'mean')
>
>
> Cheers
> Herry
>
> -----Original Message-----
> From: R-sig-Geo [mailto:r-sig-geo-bounces at r-project.org] On Behalf Of
> Dominik Schneider
> Sent: Wednesday, 2 March 2016 11:48 AM
> To: Help R-Sig_Geo
> Subject: [R-sig-Geo] raster::zonal with more than 1 zonal layer
>
> I'd like to summarise a raster using elevation and watershed. I was
> originally using extract() with a shape file and then each elevation band
> within each polygon but it's very slow.   zonal() is much faster and I can
> rasterize my polygons to use it.  But how do I robustly combine multiple
> rasterized shapefiles?
> e.g.
> dat=raster(matrix(runif(64),nrow=8))
> z1=raster(matrix( sample(1:4, 64, replace=T),nrow=8))#represents elevation
> bands z2=raster(matrix(sample(1401:1408,64,replace=T),nrow=8))#represents
> watersheds
> zonal(dat,z1*z2,'mean')
>
> this works well if you are certain that each combination of the values in
> z1 and z2 are unique and each combination is present. otherwise it gets
> messy.  are there any suggestions for this use case?
> ideally one could do: zonal(dat,stack(z1,z2),'mean')  and all the
> bookkeeping would be taken care of. my other thought is to extract all the
> values into data frames and use dplyr but I was wondering if there was a
> raster way to do this.
> Thanks
> Dominik
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list