Skip to contents

The S2 cell indexing system forms the basis for spatial indexing in the S2 library. On their own, S2 cells can represent points or areas. As a union, a vector of S2 cells can approximate a line or polygon. These functions allow direct access to the S2 cell indexing system and are designed to have minimal overhead such that looping and recursion have acceptable performance when used within R code.


s2_cell(x = character())



as_s2_cell(x, ...)

# S3 method for s2_cell
as_s2_cell(x, ...)

# S3 method for character
as_s2_cell(x, ...)

# S3 method for s2_geography
as_s2_cell(x, ...)

# S3 method for wk_xy
as_s2_cell(x, ...)

# S3 method for integer64
as_s2_cell(x, ...)




The canonical S2 cell identifier as a character vector.


Passed to methods


An object of class s2_cell


Under the hood, S2 cell vectors are represented in R as vectors of type double(). This works because S2 cell identifiers are 64 bits wide, as are doubles on all systems where R runs (The same trick is used by the bit64 package to represent signed 64-bit integers). As a happy accident, NA_real_ is not a valid or meaningful cell identifier, so missing value support in the way R users might expect is preserved. It is worth noting that the underlying value of s2_cell_sentinel() would normally be considered NA; however, as it is meaningful and useful when programming with S2 cells, custom and comparison methods are implemented such that s2_cell_sentinel() is greater than all valid S2 cells and not considered missing. Users can and should implement compiled code that uses the underlying bytes of the vector, ensuring that the class of any returned object that should be interpreted in this way is constructed with new_s2_cell().


#> <s2_cell[1]>
#> [1] 4b59a0cd83b5de49
as_s2_cell(s2_lnglat(-64, 45))
#> <s2_cell[1]>
#> [1] 4b59a0cd83b5de49
#> <s2_cell[1]>
#> [1] 4cce045470cbd267