Skip to content
Prev 4963 / 29559 Next

Clip a raster

On Tue, 3 Feb 2009, Etienne Bellemare Racine wrote:

            
For an interactive method for a SpatialGrid* object, try:

library(sp)
data(meuse.grid)
coordinates(meuse.grid) <- c("x", "y")
gridded(meuse.grid) <- TRUE
fullgrid(meuse.grid) <- TRUE
bbox(meuse.grid)
image(meuse.grid, "dist", breaks=seq(0,1,0.1), 
col=heat.colors(10))
# interactive choice of rectangle of interest, ll and ur
pts <- locator(2)
pts
points(pts)
grd <- getGridTopology(meuse.grid)
gi <- getGridIndex(coordinates(pts), grd)
cd <- slot(grd, "cells.dim")
ys <- sort(ceiling(gi / cd[1]))
xs <- sort(gi %% cd[1])
meuse_sub <- meuse.grid[ys[1]:ys[2], xs[1]:xs[2]]
bbox(meuse_sub)
image(meuse_sub, "dist", breaks=seq(0,1,0.1), 
col=grey.colors(10),
   add=TRUE)

What you call clipping is the "[" operator here.

Hope this helps,

Roger