Skip to content

How to extract the index (i, j) row and column from a raster?

4 messages · Alex, Lionel, Rafael Wüest +1 more

#
The easiest way is to use the function 'values' which return the values 
of a raster row by row:

r<-raster(nrows=10,ncols=10)
r[]<-rnorm(100)
data<-data.frame(X=rep(1:10,each=10),Y=rep(1:10,10),Z=values(r))
head(data)

Cheers,
Lionel
On 19/06/2013 12:42, Alex wrote:
#
Well, if the ultimate aim is to build a polygon from a raster, I suggest you look at

library(raster)
?rasterToPolygons

Rafael
On 19.06.2013, at 13:00, Lionel Hertzog <s6lihert at uni-bonn.de> wrote:

            
--
Rafael W?est
Swiss Federal Research Institute WSL
Z?rcherstrasse 111
8903 Birmensdorf
Switzerland

+41 44 7392126
rafael.wueest at wsl.ch
http://www.wsl.ch/info/mitarbeitende/wueest/index_EN
#
I think the first part of what you're looking for is in the raster
package.  You can load your raster file using the raster() function
(instead of creating one from scratch as in the example below).  Then
use the rasterToPoints() function to convert to a matrix object:

require(raster)
r <- raster(nrow=10, ncol=10, xmn=0, xmx=10, ymn=0, ymx=10, crs=NA)
r[] <- runif(ncell(r))
r[r<0.5] <- NA

xyz <- rasterToPoints(r)
convert to polygons via a variety of paths.  But if it's just a subset
of values you're looking to convert you might look at the
rasterToPolygons() function as well, it may do what you need.

Sincerely,
Forrest
On Wed, Jun 19, 2013 at 6:42 AM, Alex <alxcart at gmail.com> wrote: