Learn R Programming

tabularaster (version 0.5.0)

cellnumbers: 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, ...)

# S3 method for default cellnumbers(x, query, ...)

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

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

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

Arguments

x

Raster object

query

Spatial object or matrix of coordinates

...

unused

Value

tbl_df data frame

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.

Examples

Run this code
# NOT RUN {
library(raster)
library(dplyr)
r <- raster(volcano) %>% aggregate(fact = 4)
cellnumbers(r, rasterToContour(r, level = 120))
#library(dplyr)

#cr <- cut(r,  pretty(values(r)))
#p <- raster::rasterToPolygons(cr, dissolve = TRUE)
#p <- spex::qm_rasterToPolygons_sp(cr)
#tt <- cellnumbers(cr, p)
#library(dplyr)
#tt %>% mutate(v = extract(r, cell_)) %>% 
#group_by(object_) %>% 
#summarize(mean(v)) 
#head(pretty(values(r)), -1)
# }

Run the code above in your browser using DataLab