S2CellUnion

An S2CellUnion is a region consisting of cells of various sizes. Typically a cell union is used to approximate some other shape. There is a tradeoff between the accuracy of the approximation and how many cells are used. Unlike polygons, cells have a fixed hierarchical structure. This makes them more suitable for optimizations based on preprocessing.

An S2CellUnion is represented as a vector of sorted, non-overlapping S2CellIds. By default the vector is also "normalized", meaning that groups of 4 child cells have been replaced by their parent cell whenever possible. S2CellUnions are not required to be normalized, but certain operations will return different results if they are not (e.g., Contains(S2CellUnion).)

S2CellUnion is movable and copyable.

final
class S2CellUnion : S2Region {}

Constructors

this
this()

Creates an empty cell union.

this
this(S2CellId[] cell_ids)

Constructs a cell union with the given S2CellIds, then calls Normalize() to sort them, remove duplicates, and merge cells when possible. (See FromNormalized if your vector is already normalized.)

this
this(ulong[] cell_ids)
Undocumented in source.
this
this(S2CellId[] cell_ids, VerbatimFlag verbatim)
Undocumented in source.

Members

Enums

VerbatimFlag
enum VerbatimFlag
Undocumented in source.

Functions

ExactArea
double ExactArea()

Calculates this cell union's area in steradians by summing the exact area for each contained cell, using the Exact method from the S2Cell class.

approxArea
double approxArea()
Undocumented in source. Be warned that the author may not have intended to support it.
averageBasedArea
double averageBasedArea()

Approximates this cell union's area in steradians by summing the average area of each contained cell's average area, using the AverageArea method from the S2Cell class. This is equivalent to the number of leaves covered, multiplied by the average area of a leaf. Note that AverageArea does not take into account distortion of cell, and thus may be off by up to a factor of up to 1.7.

begin
Iterator begin()

Standard begin/end methods, to allow range-based for loops:

cellId
S2CellId cellId(int i)
Undocumented in source. Be warned that the author may not have intended to support it.
cellIds
inout(S2CellId[]) cellIds()

Direct access to the underlying vector for STL algorithms.

clear
void clear()

Clears the contents of the cell union and minimizes memory usage.

clone
S2Region clone()

/////////////////////////////////////////////////////////////////////

contains
bool contains(S2CellId id)

Returns true if the cell union contains the given cell id. Containment is defined with respect to regions, e.g. a cell contains its 4 children. This is a fast operation (logarithmic in the size of the cell union).

contains
bool contains(S2CellUnion y)
Undocumented in source. Be warned that the author may not have intended to support it.
contains
bool contains(S2Cell cell)

This is a fast operation (logarithmic in the size of the cell union).

contains
bool contains(S2Point p)

The point 'p' does not need to be normalized. This is a fast operation (logarithmic in the size of the cell union).

decode
bool decode(Decoder!IRangeT decoder)

Decodes an S2CellUnion encoded with Encode(). Returns true on success.

denormalize
void denormalize(int min_level, int level_mod, S2CellId[] output)

Replaces "output" with an expanded version of the cell union where any cells whose level is less than "min_level" or where (level - min_level) is not a multiple of "level_mod" are replaced by their children, until either both of these conditions are satisfied or the maximum level is reached.

difference
S2CellUnion difference(S2CellUnion y)

Returns the difference of the two given cell unions.

empty
bool empty()
Undocumented in source. Be warned that the author may not have intended to support it.
encode
void encode(Encoder!ORangeT encoder)

Appends a serialized representation of the S2CellUnion to "encoder".

end
Iterator end()
Undocumented in source. Be warned that the author may not have intended to support it.
expand
void expand(int expand_level)

Expands the cell union by adding a buffer of cells at "expand_level" around the union boundary.

expand
void expand(S1Angle min_radius, int max_level_diff)

Expands the cell union such that it contains all points whose distance to the cell union is at most "min_radius", but do not use cells that are more than "max_level_diff" levels higher than the largest cell in the input. The second parameter controls the tradeoff between accuracy and output size when a large region is being expanded by a small amount (e.g. expanding Canada by 1km). For example, if max_level_diff == 4 the region will always be expanded by approximately 1/16 the width of its largest cell. Note that in the worst case, the number of cells in the output can be up to 4 * (1 + 2 ** max_level_diff) times larger than the number of cells in the input.

getCapBound
S2Cap getCapBound()
Undocumented in source. Be warned that the author may not have intended to support it.
getCellUnionBound
void getCellUnionBound(S2CellId[] cellIds)
Undocumented in source. Be warned that the author may not have intended to support it.
getRectBound
S2LatLngRect getRectBound()
Undocumented in source. Be warned that the author may not have intended to support it.
initFromBeginEnd
void initFromBeginEnd(S2CellId begin, S2CellId end)
Undocumented in source.
initFromMinMax
void initFromMinMax(S2CellId min_id, S2CellId max_id)
Undocumented in source.
initialize
void initialize(S2CellId[] cell_ids)

initialize() methods corresponding to the constructors/factory methods above. TODO(ericv): Consider deprecating these methods in favor of using the constructors and move assignment operator.

initialize
void initialize(ulong[] cell_ids)
Undocumented in source. Be warned that the author may not have intended to support it.
intersect
S2CellUnion intersect(S2CellUnion y)
Undocumented in source.
intersect
S2CellUnion intersect(S2CellId id)
Undocumented in source.
intersects
bool intersects(S2CellId id)

Returns true if the cell union intersects the given cell id. This is a fast operation (logarithmic in the size of the cell union).

intersects
bool intersects(S2CellUnion y)
Undocumented in source. Be warned that the author may not have intended to support it.
isNormalized
bool isNormalized()

Returns true if the cell union is normalized, meaning that it is satisfies IsValid() and that no four cells have a common parent. Certain operations such as Contains(S2CellUnion) will return a different result if the cell union is not normalized.

isValid
bool isValid()

Returns true if the cell union is valid, meaning that the S2CellIds are valid, non-overlapping, and sorted in increasing order.

leafCellsCovered
ulong leafCellsCovered()

The number of leaf cells covered by the union. This will be no more than 6*2^60 for the whole sphere.

mayIntersect
bool mayIntersect(S2Cell cell)

This is a fast operation (logarithmic in the size of the cell union).

normalize
bool normalize()

Normalizes the cell union by discarding cells that are contained by other cells, replacing groups of 4 child cells by their parent cell whenever possible, and sorting all the cell ids in increasing order.

numCells
int numCells()

Convenience methods for accessing the individual cell ids.

opEquals
bool opEquals(Object o)

Return true if two cell unions are identical.

opIndex
S2CellId opIndex(size_t i)
Undocumented in source. Be warned that the author may not have intended to support it.
pack
void pack(int excess)

If there are more than "excess" elements of the cell_ids() vector that are allocated but unused, reallocates the array to eliminate the excess space. This reduces memory usage when many cell unions need to be held in memory at once.

release
S2CellId[] release()

Gives ownership of the vector data to the client without copying, and clears the content of the cell union. The original data in cell_ids is lost if there was any.

size
size_t size()

Vector-like methods for accessing the individual cell ids.

unite
S2CellUnion unite(S2CellUnion y)
Undocumented in source. Be warned that the author may not have intended to support it.

Static functions

areSiblings
bool areSiblings(S2CellId a, S2CellId b, S2CellId c, S2CellId d)
Undocumented in source. Be warned that the author may not have intended to support it.
denormalize
void denormalize(S2CellId[] input, int min_level, int level_mod, S2CellId[] output)
Undocumented in source.
fromBeginEnd
S2CellUnion fromBeginEnd(S2CellId begin, S2CellId end)

Like FromMinMax() except that the union covers the range of leaf cells from "begin" (inclusive) to "end" (exclusive), as with Python ranges or STL iterator ranges. If (begin == end) the result is empty.

fromMinMax
S2CellUnion fromMinMax(S2CellId min_id, S2CellId max_id)

Constructs a cell union that corresponds to a continuous range of cell ids. The output is a normalized collection of cell ids that covers the leaf cells between "min_id" and "max_id" inclusive.

fromNormalized
S2CellUnion fromNormalized(S2CellId[] cell_ids)

Constructs a cell union from S2CellIds that have already been normalized (typically because they were extracted from another S2CellUnion).

fromVerbatim
S2CellUnion fromVerbatim(S2CellId[] cell_ids)

Constructs a cell union from a vector of sorted, non-overlapping S2CellIds. Unlike the other constructors, FromVerbatim does not require that groups of 4 child cells have been replaced by their parent cell. In other words, "cell_ids" must satisfy the requirements of IsValid() but not necessarily IsNormalized().

getIntersection
void getIntersection(S2CellId[] x, S2CellId[] y, S2CellId[] output)

Like GetIntersection(), but works directly with vectors of S2CellIds, Equivalent to:

normalize
bool normalize(S2CellId[] ids)

Like Normalize(), but works with a vector of S2CellIds. Equivalent to: *cell_ids = S2CellUnion(std::move(*cell_ids)).Release();

Structs

Iterator
struct Iterator
Undocumented in source.

Inherited Members

From S2Region

clone
S2Region clone()

Returns a deep copy of the region.

getCapBound
S2Cap getCapBound()

Returns a bounding spherical cap that contains the region. The bound may not be tight.

getRectBound
S2LatLngRect getRectBound()

Returns a bounding latitude-longitude rectangle that contains the region. The bound may not be tight.

getCellUnionBound
void getCellUnionBound(S2CellId[] cellIds)

Returns a small collection of S2CellIds whose union covers the region. The cells are not sorted, may have redundancies (such as cells that contain other cells), and may cover much more area than necessary.

contains
bool contains(S2Cell cell)

Returns true if the region completely contains the given cell, otherwise returns false.

mayIntersect
bool mayIntersect(S2Cell cell)

If this method returns false, the region does not intersect the given cell. Otherwise, either region intersects the cell, or the intersection relationship could not be determined.

contains
bool contains(S2Point p)

Returns true if and only if the given point is contained by the region. The point 'p' is generally required to be unit length, although some subtypes may relax this restriction.

Meta