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

xyFromCell

Coordinates from a row, column or cell number


Description

These functions get coordinates of the center of raster cells for a row, column, or cell number of a Raster* object.

Usage

## S4 method for signature 'Raster,numeric'
xFromCol(object, col)
## S4 method for signature 'Raster,numeric'
yFromRow(object, row)
## S4 method for signature 'Raster,numeric'
xFromCell(object, cell)
## S4 method for signature 'Raster,numeric'
yFromCell(object, cell)
## S4 method for signature 'BasicRaster,ANY'
xyFromCell(object, cell, spatial=FALSE, ...)
## S4 method for signature 'Raster'
coordinates(obj, ...)
## S4 method for signature 'Extent'
coordinates(obj, ...)

Arguments

object

Raster* object (or a SpatialPixels* or SpatialGrid* object)

col

column number; or vector of column numbers. If missing, the x coordinates for all columns are returned

row

row number; or vector of row numbers. If missing, the y coordinates for all rows are returned

cell

cell number(s)

spatial

If spatial=TRUE, xyFromCell returns a SpatialPoints object instead of a matrix

...

additional arguments. None implemented

obj

Raster object

Details

Cell numbers start at 1 in the upper left corner, and increase from left to right, and then from top to bottom. The last cell number equals the number of cells of the Raster* object.

Value

xFromCol, yFromCol, xFromCell, yFromCell: vector of x or y coordinates

xyFromCell: matrix(x,y) with coordinate pairs

coordinates: xy coordinates for all cells

See Also

Examples

#using a new default raster (1 degree global)
r <- raster()
xFromCol(r, c(1, 120, 180))
yFromRow(r, 90)
xyFromCell(r, 10000)
xyFromCell(r, c(0, 1, 32581, ncell(r), ncell(r)+1))

#using a file from disk
r <- raster(system.file("external/test.grd", package="raster"))
r
cellFromXY(r, c(180000, 330000))
#xy for corners of a raster:
xyFromCell(r, c(1, ncol(r), ncell(r)-ncol(r)+1, ncell(r)))

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.