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

# S4 method for Raster,numeric
xFromCol(object, col)
# S4 method for Raster,numeric
yFromRow(object, row)
# S4 method for Raster,numeric
xFromCell(object, cell)
# S4 method for Raster,numeric
yFromCell(object, cell)
# S4 method for BasicRaster,ANY
xyFromCell(object, cell, spatial=FALSE, ...)
# S4 method for Raster
coordinates(obj, ...)
# S4 method for 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))
#> [1] -179.5  -60.5   -0.5
yFromRow(r, 90)
#> [1] 0.5
xyFromCell(r, 10000)
#>         x    y
#> [1,] 99.5 62.5
xyFromCell(r, c(0, 1, 32581, ncell(r), ncell(r)+1))
#>           x     y
#> [1,]  179.5  90.5
#> [2,] -179.5  89.5
#> [3,]    0.5  -0.5
#> [4,]  179.5 -89.5
#> [5,] -179.5 -90.5

#using a file from disk
r <- raster(system.file("external/test.grd", package="raster"))
r
#> class      : RasterLayer 
#> dimensions : 115, 80, 9200  (nrow, ncol, ncell)
#> resolution : 40, 40  (x, y)
#> extent     : 178400, 181600, 329400, 334000  (xmin, xmax, ymin, ymax)
#> crs        : +proj=sterea +lat_0=52.1561605555556 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +datum=WGS84 +units=m +no_defs 
#> source     : test.grd 
#> names      : test 
#> values     : 138.7071, 1736.058  (min, max)
#> 
cellFromXY(r, c(180000, 330000))
#> [1] 8041
#xy for corners of a raster:
xyFromCell(r, c(1, ncol(r), ncell(r)-ncol(r)+1, ncell(r)))
#>           x      y
#> [1,] 178420 333980
#> [2,] 181580 333980
#> [3,] 178420 329420
#> [4,] 181580 329420