S2CellId

An S2CellId is a 64-bit unsigned integer that uniquely identifies a cell in the S2 cell decomposition.

It has the following format:

id = faceface_pos

face: a 3-bit number (range 0..5) encoding the cube face.

More...
struct S2CellId {}

Constructors

this
this(ulong id)

Construct a cell from a raw numerical S2 Cell ID.

this
this(S2Point p)

Construct a leaf cell containing the given point "p". Usually there is is exactly one such cell, but for points along the edge of a cell, any adjacent cell may be (deterministically) chosen. This is because S2CellIds are considered to be closed sets. The returned cell will always contain the given point, i.e.

this
this(S2LatLng ll)

Constructs a cell from a latitude-longitude coordinate.

Members

Functions

advance
S2CellId advance(long steps)

This method advances or retreats the indicated number of steps along the Hilbert curve at the current level, and returns the new position. The position is never advanced past End() or before Begin().

advanceWrap
S2CellId advanceWrap(long steps)

This method advances or retreats the indicated number of steps along the Hilbert curve at the current level, and returns the new position. The position wraps between the first and last faces as necessary. The input must be a valid cell id.

appendAllNeighbors
void appendAllNeighbors(int nbr_level, ORangeT output)

Append all neighbors of this cell at the given level to "output". Two cells X and Y are neighbors if their boundaries intersect but their interiors do not. In particular, two cells that intersect at a single point are neighbors. Note that for cells adjacent to a face vertex, the same neighbor may be appended more than once.

appendVertexNeighbors
void appendVertexNeighbors(int level, RangeT output)

Returns the neighbors of closest vertex to this cell at the given level, by appending them to "output". Normally there are four neighbors, but the closest vertex may only have three neighbors if it is one of the 8 cube vertices.

child
S2CellId child(int position)

Returns the immediate child of this cell at the given traversal order position (in the range 0 to 3). This cell must not be a leaf cell.

childBegin
S2CellId childBegin()

Iterator-style methods for traversing the immediate children of a cell or all of the children at a given level (greater than or equal to the current level). Note that the end value is exclusive, just like standard STL iterators, and may not even be a valid cell id. You should iterate using code like this:

childBegin
S2CellId childBegin(int level)
Undocumented in source.
childEnd
S2CellId childEnd()
Undocumented in source.
childEnd
S2CellId childEnd(int level)
Undocumented in source.
childPosition
int childPosition()

Return the child position (0..3) of this cell within its parent. REQUIRES: level() >= 1.

childPosition
int childPosition(int level)

Return the child position (0..3) of this cell's ancestor at the given level within its parent. For example, child_position(1) returns the position of this cell's level-1 ancestor within its top-level face cell. REQUIRES: 1 <= level <= this->level().

contains
bool contains(S2CellId other)

Returns true if the given cell is contained within this one.

decode
bool decode(Decoder!IRangeT decoder)

Decodes an S2CellId encoded by Encode(). Returns true on success.

distanceFromBegin
long distanceFromBegin()

Returns the number of steps that this cell is from Begin(level()). The return value is always non-negative.

encode
void encode(Encoder!ORangeT encoder)

Use encoder to generate a serialized representation of this cell id. Can also encode an invalid cell.

face
int face()

Which cube face this cell belongs to, in the range 0..5.

getBoundST
R2Rect getBoundST()

Returns the bounds of this cell in (s,t)-space.

getBoundUV
R2Rect getBoundUV()

Returns the bounds of this cell in (u,v)-space.

getCenterST
R2Point getCenterST()

Returns the center of the cell in (s,t) coordinates (see s2coords.d).

getCenterSiTi
int getCenterSiTi(int psi, int pti)

Returns the (face, si, ti) coordinates of the center of the cell. Note that although (si,ti) coordinates span the range [0,2**31] in general, the cell center coordinates are always in the range [1,2**31-1] and therefore can be represented using a signed 32-bit integer.

getCenterUV
R2Point getCenterUV()

Returns the center of the cell in (u,v) coordinates (see s2coords.h). Note that the center of the cell is defined as the point at which it is recursively subdivided into four children; in general, it is not at the midpoint of the (u,v) rectangle covered by the cell.

getCommonAncestorLevel
int getCommonAncestorLevel(S2CellId other)

Returns the level of the lowest common ancestor of this cell and "other", that is, the maximum level such that parent(level) == other.parent(level). Returns -1 if the two cells do not have any common ancestor (i.e., they are from different faces).

getEdgeNeighbors
void getEdgeNeighbors(S2CellId[4] neighbors)

Returns the four cells that are adjacent across the cell's four edges. Neighbors are returned in the order defined by S2Cell::GetEdge. All neighbors are guaranteed to be distinct.

getSizeIJ
int getSizeIJ()

Returns the edge length of this cell in (i,j)-space.

getSizeST
double getSizeST()

Returns the edge length of this cell in (s,t)-space.

intersects
bool intersects(S2CellId other)

Returns true if the given cell intersects this one.

isFace
bool isFace()

Return true if this is a top-level face cell (more efficient than checking whether level() == 0).

isLeaf
bool isLeaf()

Returns true if this is a leaf cell (more efficient than checking whether level() == MAX_LEVEL).

isValid
bool isValid()

Returns true if id() represents a valid cell.

level
int level()

Returns the subdivision level of the cell (range 0..MAX_LEVEL).

lsb
ulong lsb()

Returns the lowest-numbered bit that is on for this cell id, which is equal to (uint64(1) << (2 * (MAX_LEVEL - level))). So for example, a.lsb() <= b.lsb() if and only if a.level() >= b.level(), but the first test is more efficient.

maximumTile
S2CellId maximumTile(S2CellId limit)

Returns the largest cell with the same range_min() and such that range_max() < limit.range_min().

next
S2CellId next()

Returns the next/previous cell at the same level along the Hilbert curve. Works correctly when advancing from one face to the next, but does *not* wrap around from the last face to the first or vice versa.

nextWrap
S2CellId nextWrap()

Like next() and prev(), but these methods wrap around from the last face to the first and vice versa. They should *not* be used for iteration in conjunction with child_begin(), child_end(), Begin(), or End(). The input must be a valid cell id.

opCmp
int opCmp(S2CellId v)

Supports <, <=, >, and >= operators.

opEquals
bool opEquals(S2CellId v)

Supports the == and != operators.

parent
S2CellId parent()

Returns the cell at the previous level or at the given level (which must be less than or equal to the current level).

parent
S2CellId parent(int level)
Undocumented in source.
pos
ulong pos()

The position of the cell center along the Hilbert curve over this face, in the range 0..(2**POS_BITS-1).

prev
S2CellId prev()
Undocumented in source. Be warned that the author may not have intended to support it.
prevWrap
S2CellId prevWrap()
Undocumented in source.
rangeMax
S2CellId rangeMax()
Undocumented in source. Be warned that the author may not have intended to support it.
rangeMin
S2CellId rangeMin()

These methods return the range of cell ids that are contained within this cell \(including itself\).

toFaceIJOrientation
int toFaceIJOrientation(int pi, int pj)

Return the (face, i, j) coordinates for the leaf cell corresponding to this cell id. Since cells are represented by the Hilbert curve position at the center of the cell, the returned (i,j) for non-leaf cells will be a leaf cell adjacent to the cell center. If "orientation" is non-nullptr, also return the Hilbert curve orientation for the current cell.

toFaceIJOrientation
int toFaceIJOrientation(int pi, int pj, int orientation)
Undocumented in source. Be warned that the author may not have intended to support it.
toHash
size_t toHash()

The ID serves as a good hash for associative arrays.

toLatLng
S2LatLng toLatLng()

Returns the S2LatLng corresponding to the center of the given cell.

toS2Point
S2Point toS2Point()

Returns the direction vector corresponding to the center of the given cell. The vector returned by ToPointRaw is not necessarily unit length. This method returns the same result as S2Cell.getCenter().

toS2PointRaw
S2Point toS2PointRaw()
Undocumented in source. Be warned that the author may not have intended to support it.
toString
string toString()

Creates a human readable debug string. Used for << and available for direct usage as well. The format is "f/dd..d" where "f" is a digit in the range [0-5] representing the S2CellId face, and "dd..d" is a string of digits in the range [0-3] representing each child's position with respect to its parent. (Note that the latter string may be empty.)

toToken
string toToken()

Methods to encode and decode cell ids to compact text strings suitable for display or indexing. Cells at lower levels (i.e. larger cells) are encoded into fewer characters. The maximum token length is 16.

Properties

id
ulong id [@property getter]

The 64-bit unique identifier for this cell.

Static functions

IJLevelToBoundUV
R2Rect IJLevelToBoundUV(int[2] ij, int level)

Returns the bound in (u,v)-space for the cell at the given level containing the leaf cell with the given (i,j)-coordinates.

begin
S2CellId begin(int level)

Iterator-style methods for traversing all the cells along the Hilbert curve at a given level (across all 6 faces of the cube). Note that the end value is exclusive (just like standard STL iterators), and is not a valid cell id.

end
S2CellId end(int level)
Undocumented in source. Be warned that the author may not have intended to support it.
expandedByDistanceUV
R2Rect expandedByDistanceUV(R2Rect uv, S1Angle distance)

Expands a rectangle in (u,v)-space so that it contains all points within the given distance of the boundary, and return the smallest such rectangle. If the distance is negative, then instead shrink this rectangle so that it excludes all points within the given absolute distance of the boundary.

fromDebugString
S2CellId fromDebugString(string str)

Converts a string in the format returned by ToString() to an S2CellId. Returns S2CellId::None() if the string could not be parsed.

fromFace
S2CellId fromFace(int face)

Return the cell corresponding to a given S2 cube face.

fromFaceIJ
S2CellId fromFaceIJ(int face, int i, int j)

Returns a leaf cell given its cube face (range 0..5) and i- and j-coordinates (see s2coords.h).

fromFacePosLevel
S2CellId fromFacePosLevel(int face, ulong pos, int level)

Returns a cell given its face \(range 0..5\), Hilbert curve position within that face \(an unsigned integer with S2CellId::POS_BITS bits\), and level \(range 0..MAX_LEVEL\). The given position will be modified to correspond to the Hilbert curve position at the center of the returned cell. This is a static function rather than a constructor in order to indicate what the arguments represent.

fromToken
S2CellId fromToken(string token)
Undocumented in source. Be warned that the author may not have intended to support it.
getSizeIJ
int getSizeIJ(int level)

Like getSizeIJ(), but returns the size of cells at the given level.

getSizeST
double getSizeST(int level)

Returns the edge length in (s,t)-space of cells at the given level.

lsbForLevel
ulong lsbForLevel(int level)

Returns the lowest-numbered bit that is on for cells at the given level.

none
S2CellId none()

Returns an representative empty cell smaller than any valid cell..

sentinel
S2CellId sentinel()

Returns an invalid cell id guaranteed to be larger than any valid cell id. Useful for creating indexes.

Static variables

FACE_BITS
int FACE_BITS;
Undocumented in source.
MAX_LEVEL
int MAX_LEVEL;
Undocumented in source.
MAX_SIZE
int MAX_SIZE;
Undocumented in source.
NUM_FACES
int NUM_FACES;
Undocumented in source.
POS_BITS
int POS_BITS;
Undocumented in source.

Detailed Description

face pos

a 61-bit number encoding the position of the center of this cell along the Hilbert curve over this face (see the Wiki pages for details).

Sequentially increasing cell ids follow a continuous space-filling curve over the entire sphere. They have the following properties:

- The id of a cell at level k consists of a 3-bit face number followed by k bit pairs that recursively select one of the four children of each cell. The next bit is always 1, and all other bits are 0. Therefore, the level of a cell is determined by the position of its lowest-numbered bit that is turned on (for a cell at level k, this position is 2 * (MAX_LEVEL - k).)

- The id of a parent cell is at the midpoint of the range of ids spanned by its children (or by its descendants at any level).

Leaf cells are often used to represent points on the unit sphere, and this class provides methods for converting directly between these two representations. For cells that represent 2D regions rather than discrete point, it is better to use the S2Cell class.

This class is intended to be copied by value as desired. It uses the default copy constructor and assignment operator.

Meta