xyFromCell: Coordinates from a row, column or cell number

Description Usage Arguments Details Value See Also Examples

Description

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

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
## 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

cellFromXY

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#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)))

Example output

Loading required package: sp
[1] -179.5  -60.5   -0.5
[1] 0.5
        x    y
[1,] 99.5 62.5
                 x     y
[1,]  1.844674e+19  89.5
[2,] -1.795000e+02  89.5
[3,]  5.000000e-01  -0.5
[4,]  1.795000e+02 -89.5
[5,] -1.795000e+02 -90.5
class       : RasterLayer 
dimensions  : 115, 80, 9200  (nrow, ncol, ncell)
resolution  : 40, 40  (x, y)
extent      : 178400, 181600, 329400, 334000  (xmin, xmax, ymin, ymax)
coord. ref. : +init=epsg:28992 +towgs84=565.237,50.0087,465.658,-0.406857,0.350733,-1.87035,4.0812 +proj=sterea +lat_0=52.15616055555555 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +units=m +no_defs 
data source : /usr/lib/R/site-library/raster/external/test.grd 
names       : test 
values      : 128.434, 1805.78  (min, max)

[1] 8041
          x      y
[1,] 178420 333980
[2,] 181580 333980
[3,] 178420 329420
[4,] 181580 329420

raster documentation built on Jan. 5, 2021, 3:01 a.m.