Learn R Programming

raster (version 1.8-3)

raster-package: An overview of the functions in this package

Description

The raster package provides classes and functions to manipulate geographic (spatial) data in 'raster' format. Raster data divides space into cells (rectangles; pixels) of equal size (in units of the coordinate reference system). Such data are also referred to as 'grid' data. The package should be particularly useful when using very large datasets that can not be loaded into the computer's memory. Functions will work correctly, because they they process large files in chunks, i.e., they read, compute, and write blocks of data, without loading all values into memory at once. Below is a list of functions grouped by theme. See the vignette for more information and some examples (you can open it by running this command: vignette('Raster'))

Arguments

I. Creating Raster* objects

Raster* objects can be created, from scracth, files, or from objects of other classes, with the following functions: ll{ raster To create a RasterLayer stack To create a RasterStack (multiple layers) brick To create a RasterBrick (multiple layers) addLayer Add a layer to a Raster* object dropLayer Remove a layer from a RasterStack or RasterBrick unstack Create a list of RasterLayer objects from a RasterStack } RasterLayer, RasterStack, and RasterBrick objects are, as a group, referred to as Raster* objects

II. Changing the spatial extent and/or resolution of Raster* objects

ll{ merge Combine Raster* objects with different extents (but same origin and resolution) mosaic Combine RasterLayer objects with different extents; using a function to set values for overlapping areas crop Select a geographic subset of a Raster* object expand Enlarge a Raster* object trim Trim a Raster* object by removing exterior rows and/or columns that only have NA values aggregate Combine cells of a Raster* object to create larger cells disaggregate Subdivide cells resample Warp values to a Raster* object with a different origin or resolution projectRaster Warp values to a Raster* object with a different coordinate reference system shift Move the location of Raster flip Flip values horizontally or vertically rotate Rotate values around the date-line (for lon/lat data) t Transpose a Raster* object }

III. Raster algebra

ll{ Arith-methods Arith functions (+, -, *, ^, %%, %/%, /) Math-methods Math functions (abs, sqrt, trunc, log, log10, exp, cos, sin, gamma, round among others) Logic-methods Logic functions (!, &, |) Summary-methods Summary functions (mean, Median, max, min, range, prod, sum, any, all) Compare-methods Compare functions (==, !=, >, <, <=",">=) }

IV. Cell based computation

ll{ calc Computations on a single Raster* object overlay Computations on multiple RasterLayer objects cover First layer covers second layer except where the first layer is NA mask Use values from first Raster except where cells of the mask Raster are NA (these become NA) cut Reclassify values using ranges subs Reclassify values using a 'is-becomes' matrix reclass Reclassify using a 'from-to-becomes' matrix init Initialize cells with new values stackApply Computations on groups of layers in Raster* object stackSelect Select cell values from different layers using an index RasterLayer }

V. Spatial contextual computation

ll{ distance Shortest distance to a cell that is not NA gridDistance Distance when traversing grid cells that are not NA distanceFromPoints Shortest distance to any point in a set of points direction Direction (azimuth) to or from cells that are not NA focal Focal (neighborhood) functions focalFilter Focal (neighborhood) filters edge Edge detection clump Find clumps (patches) adjacency Identify cells that are adjacent to a set of cells on a raster area Compute area of cells (for longitude/latitude data) }

VI. Model predictions

ll{ predict Predict a non-spatial model to a RasterLayer interpolate Predict a spatial model to a RasterLayer }

VII. Data type conversion

ll{ rasterize Rasterizing points, lines or polygons rasterToPoints Create points from a RasterLayer rasterToPolygons Create polygons from a RasterLayer rasterToContour Contour lines from a RasterLayer rasterFromXYZ RasterLayer from regularly spaces points rasterFromCells RasterLayer from a Raster object and cell numbers raster RasterLayer from SpatialGrid*, image, or matrix objects } You can coerce Raster* objects to Spatial* objects using as, as in as(object, 'SpatialGridDataFrame')

VIII. Summarizing

ll{ cellStats Summarize a Raster cell values with a function summary Summary of the values of a Raster* object (quartiles and mean) freq Frequency table of Raster cell values count Count the frequency of a single (range of) value(s) crosstab Cross-tabulate two RasterLayer objects unique Get the unique values in a Raster* object zonal Summarize a Raster* object by zones in a RasterLayer }

IX. Accessing values of Raster* object cells

ll{ getValues Get all cell values (will fail with very large rasters and limited computer memory), or a row of values (safer) getValuesBlock Get values for a block (a rectangular area defined by start and end row and column as.matrix Get cell values as a matrix as.array Get cell values as an array extract Use cell numbers, points, lines, polygons, or an extent, to extract values from a Raster* object, or extract focal values sampleRandom Random sample sampleRegular Regular sample minValue Get the minimum value of the cells of a Raster* object (not always known) maxValue Get the maximum value of the cells of a Raster* object (not always known) setMinMax Compute the minimum and maximum value of a Raster* object if these are not known } You can also use indexing with [ for cell numbers, and [[ for row / column number combinations

X. Plotting

Maps ll{ plot Plot a Raster* object. The main method to create a map plotRGB Combine three layers (red, green, blue channels) into a single 'real color' image plot3D 3-dimensional plot of a RasterLayer image Plot a Raster* using the image function persp Perspective plot of a RasterLayer contour Contour plot of a RasterLayer filledContour Filled contour plot of a RasterLayer text Plot the values of a RasterLayer on top of a map } Interacting with a map (plot of a RasterLayer) ll{ zoom Zoom in on a plot of a RasterLayer click Draw points and/or query values of RasterLayer by clicking on a map drawPoly Create a SpatialPolygons object by drawing it drawLine Create a SpatialLines object by drawing it drawExtent Create an Extent object by drawing it } Other plots ll{ plot x-y scatter plot of values of two RasterLayer objects hist Histogram of Raster* object values density Density plot of Raster* object values pairs Pairs plot for layers in a RasterStack or RasterBrick boxplot Box plot of the values of one or multiple layers }

XI. Getting and setting Raster* dimensions

Basic parameters of existing Raster* objects can be obtained, and in most cases changed, with: ll{ ncol The number of columns nrow The number of rows ncell The number of cells (can not be set directly, only via ncol or nrow) res The resolution (x and y) xres The x resolution (can be set with res) yres The y resolution (can be set with res) xmin The minimum x coordinate (or longitude) xmax The maximum x coordinate (or longitude) ymin The minimum y coordinate (or latitute) ymax The maximum y coordinate (or latitute) extent The extent (minimum and maximum x and y coordinates) origin The origin of a Raster* object projection The coordinate reference system (map projection) isLonLat Test if an object has a longitude/latitude coordinate reference system filename Filename to which a RasterLayer or RasterBrick is linked band Band (layer) of a multi-band file that this RasterLayer is linked to nbands How many bands (layers) does the file have? compare Compare for equality the basic parameters of two Raster* objects NAvalue Get or set the NA value (for reading from file; see writeRaster for setting it when writing a file) } If there are values associated with a RasterLayer object (either in memory or via a link to a file) these are lost when you change the number of columns or rows or the resolution. This is not the case when the extent is changed (as the number of columns and rows will not be affected). Simlarly, with projection you can set the projection, but this does not transform the data (see projectRaster for that).

XII. Computing row, column, cell numbers and coordinates

Cell numbers start at 1 in the upper-left corner. They increase within rows, from left to right, and then row by row from top to bottom. Likewise, row numbers start at 1 at the top of the raster, and column numbrers start at 1 at the left side of the raster. ll{ xFromCol x-coordinates from column numbers yFromRow y-coordinates from row numbers xFromCell x-coordinates from row numbers yFromCell y-coordinates from cell numbers xyFromCell x and y coordinates from cell numbers colFromX Column numbers from x-coordinates (or longitute) rowFromY Row numbers from y-coordinates (or latitute) cellFromXY Cell numbers from x and y coordinates cellFromRowCol Cell numbers from row and column numbers cellsFromExtent Cell numbers from extent object validCell Is this a valid cell number? validCol Is this a valid column number? validRow Is this a valid row number? }

XIII. Writing files

Basic writing ll{ setValues Put new values in a Raster* object writeRaster Write all values of Raster* object to disk KML Save raster as KML file } Advanced writing of entire files or parts thereof ll{ blockSize Get suggested block size for reading and writing writeStart Open a file for writing writeValues Write some values writeStop Close the file after writing update Change the values of an existing file }

XIV. Extent objects

ll{ extent Create an extent object intersectExtent Intersect two extent objects unionExtent Combine two extent objects alignExtent Align an extent with a Raster* object roundExtent Round the coordinates of an Extent object nudgeExtent Floor/ceiling of the coordinates of an Extent object drawExtent Create an Extent object by drawing it on top of a map (see plot) polygonFromExtent Create a SpatialPolygons object from an Extent object } It is also possible to use Arithmetic functions with Extent objects (but these work perhaps unexpectedly!)

XV. Options

ll{ setOptions Set session options showOptions Show session options saveOptions Save session options to make them persistent clearOptions Set session options to default values }

XVI. Miscellaneous

ll{ getData Download and geographic data pointDistance Distance between points readIniFile Read a (windows) 'ini' file hdr Write header file for a number of raster formats trim Remove leading and trainling blanks from a character string ext Get or set the extentsion of a filename cv Coefficient of variation modal Modal value Median Median value sampleInt Random sample of (possibly very large) range of integer values showTmpFiles Show temporary files removeTmpFiles Remove temporary files }

XVII. For programmers

ll{ canProcessInMemory Test whether a file can be created in memory pbCreate Initialize a progress bar pbStep Take a progress bar step pbClose Close a progress bar openConnection Open a file connection closeConnection Close a file connection rasterTmpFile Get a name for a temporary file inMemory Are the cell values in memory? fromDisk Are the cell values read from a file? }

Acknowledgements

Extensive suggestions were made by Jonathan Greenberg, Matteo Mattiuzzi, and Steven Mosher. Contributions were also made by Neil Best, Roger Bivand, Isabelle Boulangeat, Herry Herry, Paul Hiemstra, Mayeul Kauffmann, Rainer Krug, Alice Laborte, John Lewis, Lennon Li, Agustin Lobo, Edzer Pebesma, David Ramsey, Stefan Schlaffer, Jon Olav Skoien, Kevin Ummel, Shaun Walbridge, and others.

Details

The package implements classes for Raster data (see Raster-class) and supports
  • Creation of Raster* objects from scratch or from file
  • Handling extremely large raster files
  • Raster algebra and overlay functions
  • Distance, neighborhood (focal) and patch functions
  • Polygon, line and point to raster conversion
  • Model predictions
  • Summarizing raster values
  • Easy access to raster cell-values
  • Plotting (making maps)
  • Manipulation of raster extent, resolution and origin
  • Computation of row, col and cell numbers to coordinates and vice versa
  • Reading and writing various raster file types