<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
<title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
I am trying to install "raster" package, but it is not available in
this site. <a class="moz-txt-link-freetext" href="http://R-Forge.R-project.org">http://R-Forge.R-project.org</a> . <br>
<br>
Zia<br>
<br>
Robert J. Hijmans wrote:
<blockquote
cite="mid:dc22b2570911231305s7f8a23d8v25f85b02d0e311e1@mail.gmail.com"
type="cite">
<pre wrap="">Hi Ned,
Here is an approach to get values from a RasterStack to all cells in
each polygon:
library(raster)
# a polygon
data(meuse.riv)
pol <- SpatialPolygons(list(Polygons(list(Polygon(meuse.riv)), "x")))
# a raster
r1 <- raster(system.file("external/test.ag", package="sp"))
r2 <- sqrt(r1)
# a stack
s <- stack(r1, r2)
# convert the polygon to a RasterLayer
rr <- polygonsToRaster(pol, s)
par(mfrow=c(1,2))
plot(r1)
plot(pol, add=TRUE)
plot(rr)
# extract points that are not NA
pts <- rasterToPoints(rr) # see additional arguments to select a
subset (useful for very large rasters)
# perhaps subsample your points
sampl <- sample(1:length(pts[,1]), min(100, length(pts[,1])))
pts <- pts[sampl,]
v <- xyValues(s, pts[,1:2])
# if you have mutiple polygons, bind the polygon ID to the raster values:
v <- cbind(pts[,3], v)
# You could also sample points with spsample
pts <- spsample(pol, 100, "random")
# remove duplicate cells
cells <- unique(cellFromXY(s, pts))
v2 <- cellValues(s, cells)
# or sample from a SpGDF if you can create it from the RasterLayer (if
it is not too big)
spdf <- as(rr, 'SpatialGridDataFrame')
pts <- spsample(spdf, 100, "random")
v3 <- xyValues(s, pts)
For extremely large rasters, rasterToPoints could fail. If so, first
create a RasterLayer with random values with calc and "fun=runif" and
make values F for e.g x > 0.01, overlay that with rr, and try
rasterToPoints again...
Hth, Robert
On Mon, Nov 23, 2009 at 12:16 PM, Ned Horning <a class="moz-txt-link-rfc2396E" href="mailto:horning@amnh.org"><horning@amnh.org></a> wrote:
</pre>
<blockquote type="cite">
<pre wrap="">Greetings,
I am looking for a way to use R to extract pixel values (currently in a
large RasterStack object) that fall under polygons (currently a
SpatialPolygonsDataFrame object). I seem to recall a discussion about using
overlay to do this but I can't find a method that would work.
Any insight would be appreciated.
Ned
_______________________________________________
R-sig-Geo mailing list
<a class="moz-txt-link-abbreviated" href="mailto:R-sig-Geo@stat.math.ethz.ch">R-sig-Geo@stat.math.ethz.ch</a>
<a class="moz-txt-link-freetext" href="https://stat.ethz.ch/mailman/listinfo/r-sig-geo">https://stat.ethz.ch/mailman/listinfo/r-sig-geo</a>
</pre>
</blockquote>
<pre wrap=""><!---->
_______________________________________________
R-sig-Geo mailing list
<a class="moz-txt-link-abbreviated" href="mailto:R-sig-Geo@stat.math.ethz.ch">R-sig-Geo@stat.math.ethz.ch</a>
<a class="moz-txt-link-freetext" href="https://stat.ethz.ch/mailman/listinfo/r-sig-geo">https://stat.ethz.ch/mailman/listinfo/r-sig-geo</a>
</pre>
</blockquote>
</body>
</html>