Skip to main content

S2 API

From-v4.0

caution

This module is still experimental. It may have issues and functionality may change in minor releases.

Overview

S2 is a discrete global grid system built on the Hilbert curve. See s2geometry.io for more information.

The S2 functions in math.gl are currently focused on decoding S2 encoded data, not encoding it.

API Notes

The API provided by this module mainly works with S2 token strings, as those are typically found in data files.

But note that S2 cells have multiple representations. They can be represented as:

  • S2 token strings (stringified versions of the indexes)
  • S2 indexes which are 64 bit numbers represented by the Long type.
  • Hilbert QuadKey strings.
  • S2 Cells which are tuples of numbers.

Information on the the Long data type can be found here: TBA

Core Functions

getIndexFromS2Token(s2Token: string): Long

Decodes a string into the 64 bit token.

getS2TokenFromIndex(s2Index: Long): string

Encodes the 64 bit token into a string.

getS2ChildIndex(s2Index: Long, index: number): Long

Returns the specific index of a child cell.

getS2Center(s2Token: string): number[]

Returns the center lng, lat of the S2 cell

getS2Boundary(s2Token: string): Float64Array

Returns the boundary as a list of lng, lat of the S2 cell