On Thu, Mar 14, 2013 at 1:44 AM, Robert Buitenwerf <
buitenwerf@em.uni-frankfurt.de> wrote:

> Dear all,
>
> The select function in raster seems really neat and works well for
> rectangles i.e. use="rec".
>
> I can't get good results for selecting polygons though i.e. use="pol".
>
> I do:
>
> r <- raster(nrow=10, ncol=10)
> r[] <- 1:ncell(r)
> plot(r)
> s <- select(r, use="pol")
>
> After clicking on the image multiple times in the shape that I want I
> press escape (running R on 64bit Windows 7).


you should use "right-click" / "stop", but apparently escape works too
(which is surprising).



> A polygon of the wanted shape is drawn onto the raster, but the new object
> s is not created. I get this error:
>
> Error in as.vector(y) : no method for coercing this S4 class to a vector
>
>
And that is a bug that I will fix for the next release. The fixed function
is at the bottom of this message, you can copy and paste it into R after
loading raster.


>
> Perhaps I shouldn't use escape to tell select I'm done, but I can't figure
> out what else to do...
>
> It would also be nice if the polygon lines were drawn while clicking the
> image (not only afterwards) so that you can see what you're doing?
>
>

But they are, aren't they? At least I can see them. Perhaps you are using
an older version?



>
> Thanks,
> Robert
>
> ______________________________**_________________
> R-sig-Geo mailing list
> R-sig-Geo@r-project.org
> https://stat.ethz.ch/mailman/**listinfo/r-sig-geo<https://stat.ethz.ch/mailman/listinfo/r-sig-geo>
>



setMethod('select', signature(x='Raster'),
function(x, use='rec', ...) {
use <- substr(tolower(use), 1, 3)
stopifnot(use %in% c('rec', 'pol'))
if (use == 'rec') {
e <- drawExtent()
int <- intersect(e, extent(x))
if (is.null(int)) {
x <- NULL
} else {
x <- crop(x, e)
}
} else {
e <- drawPoly()
int <- intersect(extent(x), e)
if (is.null(int)) {
x <- NULL
} else {
x <- crop(x, e)
x <- mask(x, e)
}
}
x
}
)

	[[alternative HTML version deleted]]

