Encoding/decoding S2 polylines for JavaScript. This library is a TypeScript port of the S2 Geometry Library's polyline encoding/decoding functionality.
This package provides utilities for manipulating geometric shapes on a sphere, making it suitable for working with geographic data. It's based on the S2 Geometry Library, which is designed for spherical geometry rather than planar 2D maps.
This library specifically focuses on encoding and decoding S2 polylines, which are sequences of points on the sphere represented in a compact, encoded format.
npm install s2polyline-ts
A demo of the is published to GitHub Pages: https://regeter.github.io/s2polyline-ts/
import { encode, decode } from 's2polyline-ts';
import { S2LatLng } from 's2polyline-ts';
// Encoding
const coordinates: S2LatLng[] = [
S2LatLng.fromDegrees(37.7749, -122.4194), // San Francisco
S2LatLng.fromDegrees(40.7128, -74.0060), // New York
S2LatLng.fromDegrees(34.0522, -118.2437) // Los Angeles
];
console.log("Encoding::", coordinates);
const encoded: string = encode(coordinates);
console.log("Encoded::", encoded);
// Decoding
const encodedPolyline = 'AhoGJjx2y008lAIA1rLcAaXivALU9BzoKYu8cgA';
const decoded = decode(encodedPolyline);
console.log("Decoded::", decoded);
encode(coordinates: S2LatLng[]): string
: Encodes an array ofS2LatLng
objects into an S2 encoded polyline string.decode(encodedPolyline: string): S2LatLng[]
: Decodes an S2 encoded polyline string into an array ofS2LatLng
objects.S2LatLng
class for representing a latitude and longitudeS2LatLng.fromDegrees(latDegrees: number, lngDegrees: number)
: Create from degrees
Contributions are welcome! Please see the GitHub repository for issues and pull requests.
Apache 2.0
This library is based on the S2 Geometry Library. Initial code created by Sukolsak Sakshuwong.
This is not an official Google product.