[R-sig-Geo] Extract values from RasterBrick using lat/lon

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Sat Jul 7 17:29:10 CEST 2012


On Fri, Jul 6, 2012 at 9:41 PM, tiffanysmith <smithtiffanyt at gmail.com> wrote:
> Hello,
>
> I have imported my data as a RasterBrick and am trying to extract a
> timeseries from a location within the larger extent. I have the lat/lon of
> the location, so is there a way for me to use the 'extract' command (or
> something else?) to get the time series from my RasterBrick at the gridcell
> closest to my lat/lon point?

 Have you read the help for extract? It says for extract(x,y)

      x: Raster* object

       y: A vector (representing cell numbers); or a SpatialPoints
          object or a two-column data.frame or matrix (representing
          points); or a ‘SpatialPolygons’, ‘SpatialLines’, or ‘Extent’
          object

And then it says if y is a SpatialPoints object then it extracts the
value at that location. For a brick this gets the values of all the
layers of the brick, which is your time series. Make a SpatialPoints
from your lat-long with SpatialPoints(longitude, latitude) and you're
nearly there:

 > extract(b,SpatialPoints(cbind(0.1,0.2)))
[1] 0.2296313 0.8351380 0.7611164 0.9320771 0.7940506

 You'll have to make sure this is in the right order, maybe you need
to call rev(that) to reverse it.

You can sample multiple points at once to get a matrix:

 > extract(b,SpatialPoints(cbind(c(0.1,.2),c(0.3,.8))))
          [,1]      [,2]      [,3]      [,4]      [,5]
[1,] 0.2296313 0.8351380 0.7611164 0.9320771 0.7940506
[2,] 0.8162665 0.6251411 0.6882939 0.4350877 0.6635630

Barry



More information about the R-sig-Geo mailing list