Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

crop

Crop


Description

crop returns a geographic subset of an object as specified by an Extent object (or object from which an extent object can be extracted/created). If x is a Raster* object, the Extent is aligned to x. Areas included in y but outside the extent of x are ignored (see extend if you want a larger area).

Usage

## S4 method for signature 'Raster'
crop(x, y, filename="", snap='near', datatype=NULL, ...)

## S4 method for signature 'Spatial'
crop(x, y, ...)

Arguments

x

Raster* object or SpatialPolygons*, SpatialLines*, or SpatialPoints* object

y

Extent object, or any object from which an Extent object can be extracted (see Details)

filename

Character, output filename. Optional

snap

Character. One of 'near', 'in', or 'out', for use with alignExtent

datatype

Character. Output dataType (by default it is the same as the input datatype)

...

Additional arguments as for writeRaster

Details

Objects from which an Extent can be extracted/created include RasterLayer, RasterStack, RasterBrick and objects of the Spatial* classes from the sp package. You can check this with the extent function. New Extent objects can be also be created with function extent and drawExtent by clicking twice on a plot.

To crop by row and column numbers you can create an extent like this (for Raster x, row 5 to 10, column 7 to 12) crop(x, extent(x, 5, 10, 7, 15))

Value

RasterLayer or RasterBrick object; or SpatialLines or SpatialPolygons object.

Note

values within the extent of a Raster* object can be set to NA with mask

See Also

Examples

r <- raster(nrow=45, ncol=90)
values(r) <- 1:ncell(r)
e <- extent(-160, 10, 30, 60)
rc <- crop(r, e)	

# use row and column numbers:
rc2 <- crop(r, extent(r, 5, 10, 7, 15))

# crop Raster* with Spatial* object
b <- as(extent(6, 6.4, 49.75, 50), 'SpatialPolygons')
crs(b) <- crs(r)
rb <- crop(r, b)

# crop a SpatialPolygon* object with another one
if (require(rgdal) & require(rgeos)) {
  p <- shapefile(system.file("external/lux.shp", package="raster"))
  pb <- crop(p, b)
}

raster

Geographic Data Analysis and Modeling

v3.4-10
GPL (>= 3)
Authors
Robert J. Hijmans [cre, aut] (<https://orcid.org/0000-0001-5872-2872>), Jacob van Etten [ctb], Michael Sumner [ctb], Joe Cheng [ctb], Dan Baston [ctb], Andrew Bevan [ctb], Roger Bivand [ctb], Lorenzo Busetto [ctb], Mort Canty [ctb], Ben Fasoli [ctb], David Forrest [ctb], Aniruddha Ghosh [ctb], Duncan Golicher [ctb], Josh Gray [ctb], Jonathan A. Greenberg [ctb], Paul Hiemstra [ctb], Kassel Hingee [ctb], Institute for Mathematics Applied Geosciences [cph], Charles Karney [ctb], Matteo Mattiuzzi [ctb], Steven Mosher [ctb], Babak Naimi [ctb], Jakub Nowosad [ctb], Edzer Pebesma [ctb], Oscar Perpinan Lamigueiro [ctb], Etienne B. Racine [ctb], Barry Rowlingson [ctb], Ashton Shortridge [ctb], Bill Venables [ctb], Rafael Wueest [ctb]
Initial release
2021-05-02

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.