https://github.com/missinglink/s2js
Javascript port of the S2 Geometry library for the browser & nodejs
https://github.com/missinglink/s2js
geo geojson geometry javascript point-in-polygon s2 spherical-geometry tesselation typescript
Last synced: 9 months ago
JSON representation
Javascript port of the S2 Geometry library for the browser & nodejs
- Host: GitHub
- URL: https://github.com/missinglink/s2js
- Owner: missinglink
- License: apache-2.0
- Created: 2024-07-17T09:06:39.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-07-04T11:36:47.000Z (12 months ago)
- Last Synced: 2025-09-13T03:11:10.198Z (9 months ago)
- Topics: geo, geojson, geometry, javascript, point-in-polygon, s2, spherical-geometry, tesselation, typescript
- Language: TypeScript
- Homepage: https://missinglink.github.io/s2js/
- Size: 484 KB
- Stars: 70
- Watchers: 1
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# s2js
s2js is a Javascript port of the s2 spherical geometry library.
| [github](https://github.com/missinglink/s2js) | [npm](https://www.npmjs.com/package/s2js) | [documentation](https://missinglink.github.io/s2js) | [demo](https://bdon.github.io/s2js-demos/) |
### Installation
```bash
npm install s2js
```
### Usage
The library is available as both ESM & CJS modules:
**ESM**
```js
import { s2 } from 's2js'
```
**CJS**
```js
const { s2 } = require('s2js')
```
**CDN**
```html
import { s2 } from 'https://esm.sh/s2js'
```
### Features
s2js is a full-featured JavaScript/TypeScript port of Google's S2 Geometry library. It supports:
| Feature | Supported? | Notes |
|----------------------------------|:---------:|:------|
| **CellId, Cell** | ✅ | Encoding, decoding, neighbors |
| **Point, LatLng** | ✅ | Spherical & Planar projections |
| **Loop, Polygon** | ✅ | Polygons, potentially with holes |
| **Polyline** | ✅ | Represents linear paths |
| **RegionCoverer** | ✅ | Cover regions (loops, polys) with S2 cells |
| **GeoJSON <> S2 conversions** | ✅ | Supports all GeoJSON geometry types |
| **Polygon contains/intersects** | ✅ | Point-in-polygon & shape intersection |
| **Union, Intersection, Difference** | ✅ | Basic boolean polygon operations |
| **BigInt cell/token conversion** | ✅ | Handles full 64-bit cell IDs |
### Feature Completeness
The library is essentially feature compatible with the Golang library: https://github.com/golang/geo
### Typescript Support
The library is written in Typescript and exports type definitions.
### GeoJSON support
The supplementary `geojson` module provides convenience functions for working with GeoJSON data in S2:
```js
import { geojson } from 's2js'
const s2Polyline = geojson.fromGeoJSON({
type: 'LineString',
coordinates: [
[102.0, 0.0],
[103.0, 1.0],
[104.0, 0.0],
[105.0, 1.0]
]
})
```
The `RegionCoverer` supports all geometry types including multi-geometries:
```js
const coverer = new geojson.RegionCoverer({ maxCells: 30 })
const union = coverer.covering({
type: 'Polygon',
coordinates: [
[
[100.0, 0.0],
[101.0, 0.0],
[101.0, 1.0],
[100.0, 1.0],
[100.0, 0.0]
]
]
})
```
### Contributing
If you'd like to contribute a module please open an Issue to discuss.
### Copyright
© 2024 Peter Johnson <github:missinglink>
This source code is published under the Apache-2.0 license.