cellnumbers: Extract cell numbers from a Raster object.

View source: R/cellnumbers.R

cellnumbersR Documentation

Extract cell numbers from a Raster object.

Description

Provide the 'cellnumbers' capability of raster::extract and friends directly, returning a data frame of query-object identifiers 'object_' and the cell number.

Usage

cellnumbers(x, query, ...)

## Default S3 method:
cellnumbers(x, query, ...)

## S3 method for class 'SpatialLines'
cellnumbers(x, query, ...)

## S3 method for class 'sfc'
cellnumbers(x, query, ...)

## S3 method for class 'sf'
cellnumbers(x, query, ...)

Arguments

x

Raster object

query

Spatial object or matrix of coordinates

...

unused

Details

Raster data is inherently 2-dimensional, with a time or 'level' dimension treated like a layers of these 2D forms. The 'raster' package cell number is counted from 1 at the top-left, across the rows and down. This corresponds the the standard "raster graphics" convention used by 'GDAL' and the 'sp' package, and many other implementations. Note that this is different to the convention used by the graphics::image function.

Currently this function only operates as if the input is a single layer objects, it's not clear if adding an extra level of grouping for layers would be sensible.

Value

a data frame (tibble) with columns

  • object_ - the object ID (what row is it from the spatial object)

  • cell_ - the cell number of the raster

Examples

library(raster)
library(dplyr)
r <- raster(volcano) %>% aggregate(fact = 4)
cellnumbers(r, rasterToContour(r, level = 120))
library(dplyr)

cr <- cut(r,  pretty(values(r)))

suppressWarnings(tt <- cellnumbers(cr, polycano))
library(dplyr)
tt %>% mutate(v = extract(r, cell_)) %>% 
group_by(object_) %>% 
summarize(mean(v)) 
head(pretty(values(r)), -1)

tabularaster documentation built on Nov. 2, 2023, 5:14 p.m.