[R-sig-Geo] R: extract matrix values using circular window

Lorenzo Cattarino l.cattarino at uq.edu.au
Fri Mar 16 12:09:21 CET 2012


this is a very detailed answer. It helped a lot.
thank you.
Lorenzo
________________________________________
Inizio: Rolf Turner [r.turner at auckland.ac.nz]
Inviato: venerdì 16 marzo 2012 18.16
Fine: Lorenzo Cattarino
Cc: r-sig-geo at r-project.org
Oggetto: Re: [R-sig-Geo] extract matrix values using circular window

On 16/03/12 19:48, Lorenzo Cattarino wrote:
> I wanna know how many cells with values of one (1) are within a circular neighbourhood with radius r and centre in x0 y0.

This might get you going:

require(spatstat)
# Build an example image of 0's and 1's:
M <- im(matrix(sample(0:1,500*500,TRUE),nrow=500),xcol=seq(0,10,length=500),
               yrow=seq(0,10,length=500))

# Take a disc which lies inside this image.
d <- disc(centre=c(3,5),radius=1)

# Plot the image and the disc.
plot(M)
plot(d,add=TRUE)

# Find the part of the image inside the disc:
Md <- M[d,drop=FALSE]
plot(Md)

# Find the number of 1's in Md:
summary(eval.im(Md==1))

# Or:
table(Md$v)

# Another approach:
i <- inside.owin(rasterx.im(M),rastery.im(M),d)
table(M[i])

HTH

     cheers,

         Rolf Turner





More information about the R-sig-Geo mailing list