<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body text="#333333" bgcolor="#FFFFFF">
    <p>Sorry the exact code is:</p>
    <p><tt>xy.r <- extract(r, xy.sp)</tt><tt><br>
      </tt><tt>xy.r.sample <- lapply(xy.r, sample, 10)</tt><br>
    </p>
    <br>
    <div class="moz-cite-prefix">On 10/24/2017 01:34 PM, Bacou, Melanie
      wrote:<br>
    </div>
    <blockquote type="cite"
      cite="mid:bd4cfccf-274b-80d7-73a9-49edff43ea25@mbacou.com">
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <p>Andy,<br>
        Simple use `extract()` instead of `mask()`, and then randomly
        sample cells in each polygon. Should be much faster, e.g.:</p>
      <p><tt>xy.r <- extract(r, xy.sp)</tt><tt><br>
        </tt><tt>xy.r.sample <- lapply(xy.r, sample.int, n=10)</tt><br>
      </p>
      --Mel.<br>
      <br>
      <div class="moz-cite-prefix">On 10/23/2017 12:57 PM, Andy Bunn
        wrote:<br>
      </div>
      <blockquote type="cite"
        cite="mid:82F6A4C1-CBF3-4AE7-823B-0718C9F2AC9D@wwu.edu">
        <pre wrap="">Hi all, I’ve followed the thread on the updates to raster and would like to acknowledge the wonderful work done by Robert and others in making raster. It is an amazing piece of software. Thanks to all who help people like me do my job using the fruits of your labors!

Anyways, I’m working with some largish rasters and running into a step that is more computationally intensive than I would like. I’m trying to randomly sample a raster within the bounds of a polygon. E.g., in the example below I’d like to apply sampleRandom() on the raster r but constrain the sample to points within the polygons defined by xy.sp.


    require(raster)
    
    xy = cbind(
      x = c(13.4, 13.4, 13.6, 13.6, 13.4),
      y = c(48.9, 49, 49, 48.9, 48.9)
    )
    hole.xy <- cbind(
      x = c(13.5, 13.5, 13.45, 13.45, 13.5),
      y = c(48.98, 48.92, 48.92, 48.98, 48.98)
    )
    
    xy.sp <- SpatialPolygons(list(
      Polygons(list(Polygon(xy),
                    Polygon(hole.xy, hole = TRUE)), "1"),
      Polygons(list(Polygon(xy + 0.2),
                    Polygon(xy + 0.35),
                    Polygon(hole.xy + 0.2, hole = TRUE)), "2")
    ))
    
    r <- raster(nrow=100, ncol=100, ext=extent(xy.sp), resolution=0.01)
    r[] <- runif(ncell(r))
    
    plot(xy.sp,col="grey")
    plot(r,add=T,alpha=0.5)



I can accomplish this with a mask  via: 
    sampleRandom(mask(r, xy.sp),size = 10)
    
However, I have many different polygons to apply over a big raster and the mask() is taking a long time. Is there a better way to go about this?

Thanks in advance, A     


_______________________________________________
R-sig-Geo mailing list
<a class="moz-txt-link-abbreviated" href="mailto:R-sig-Geo@r-project.org" moz-do-not-send="true">R-sig-Geo@r-project.org</a>
<a class="moz-txt-link-freetext" href="https://stat.ethz.ch/mailman/listinfo/r-sig-geo" moz-do-not-send="true">https://stat.ethz.ch/mailman/listinfo/r-sig-geo</a></pre>
      </blockquote>
      <br>
    </blockquote>
    <br>
  </body>
</html>