A native JavaScript port of Google's S2 Geometry library
tag.
html
`
$3
Some s2-geometry functions that do not access nor modify the DOM can be used in web workers.
In order to be able to use s2-geometry in those web workers, you need to import the source file as an ES6 module using:
`javascript
import {S2LatLng, S2Cell} from "//unpkg.com/@raha.group/s2-geometry";
`
NPM registry
Use the package manager npm to install s2-geometry.
`shell
$ npm install @raha.group/s2-geometry
`
Library will be copied to node_modules/@raha.group/s2-geometry directory.
Direct Download
Grab the latest release file.
Implemented APIs
S2LatLng API
$3
`javascript
S2LatLng := ObjectModel {
@Static S2LatLng from(Number latitude, Number longitude);
@Static S2LatLng fromInteger(BigInt identifier);
BigInt toInteger(Level level = MAX_LEVEL);
SequenceOf(S2Cell) getNeighbors(level);
BigInt compareTo(S2LatLng other);
};
Level := RangeLimit {
lower := 1,
upper := 30
};
MAX_LEVEL := 30;
`
$3
`javascript
let latitude = 40.2574448;
let longitude = -111.7089464;
// Main factory method to create a S2LatLng object from pair (latitude, longitude)
let point = S2LatLng.from(latitude, longitude);
let level = 15; // Range of level is between 1 and 30
let cellId = point.toInteger(level);
console.log(cellId);
// It prints 9749618446378729472n
// Factory method to create a S2LatLng object from BigInt
point = S2LatLng.fromInteger(cellId);
console.log(point);
// It prints {lat: 40.2574448, lng: -111.7089464}
for (let cell of point.getNeighbors(level)) {
console.log(cell);
}
`
S2Cell API
Currently contains basic support for S2Cell.
Map of Faces
Face: 2
Orientation: A
Cover: Africa
Face: 0
Orientation: A
Cover: Africa Face: 1
Orientation: D
Cover: Asia Face: 3
Orientation: D
Cover: Australia Face: 4
Orientation: A
Cover: Americas, Provo, UT
Face: 5
Orientation: D
Cover: Antarctica
The S2 hierarchy is useful for spatial indexing and for approximating regions as a collection of cells. Cells can be used to represent both points and regions: points are generally represented as leaf cells, while regions are represented as collections of cells at any level(s).
Each cell is uniquely identified by a 64-bit S2CellId. The S2 cells are numbered in a special way in order to maximize locality of reference when they are used for spatial indexing (compared to other methods of numbering the cells).
S2CellId combine face and the hilbert curve position into a single 64 bit integer. this gives efficient space and speed.
$3
`javascript
S2Cell := ObjectModel {
@Static from(Face face, OrderedPair ij, Level level = MAX_LEVEL); // Internal use
@Static S2Cell fromLatLng(S2LatLng latLng, Level level = MAX_LEVEL);
S2LatLng toLatLng();
@Static S2Cell fromInteger(BigInt identifier);
BigInt toInteger();
SequenceOf(S2LatLng) getCornerLatLngs();
SequenceOf(S2Cell) getNeighbors();
S2Cell move(Number step);
Boolean includes(S2LatLng point);
Boolean includes(S2Cell cell);
BigInt compareTo(S2Cell other);
};
Face := RangeLimit {
lower := 0,
upper := 5
};
`
$3
`javascript
// Factory method to create a S2Cell object from S2LatLng and level
let cell = S2Cell.fromLatLng(point, level);
console.log(cell.toLatLng().compareTo(point) == 0n);
// It prints true
// Factory method to create a S2Cell object from BigInt
cell = S2Cell.fromInteger(cellId);
console.log(cell.toInteger() == point.toInteger());
// It prints true
for (let cell of point.getNeighbors()) {
console.log(cell);
}
// It prints the neighbors from left, down, right and up.
// You can get the previous and next S2Cell from any given cell:
let nextCell = cell.move(1);
let previousCell = cell.move(-1);
// Does cell include other item?
console.log(cell.includes(point));
``