npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@aws/polyline

v0.1.0

Published

A library for encoding and decoding polylines

Downloads

313

Readme

@aws/polyline

This library simplifies the process of using compressed geometry with maplibre-gl-js in JavaScript applications.

Location-based service providers sometimes use variations of the Encoded Polyline Algorithm Format to reduce the response size for APIs that can return large arrays of coordinates, such as routing and isoline APIs. The utility methods in this library compresses the data and decompresses into GeoJSON that can be directly rendered in MapLibre.

Installation

Install this library from NPM for usage with modules:

npm install @aws/polyline

You can also import the Javascript file for usage directly in the browser.

<script src="https://cdn.jsdelivr.net/npm/@aws/polyline/dist/polyline.min.js"></script>

Usage

Import the library and call the utility functions in the top-level namespace as needed. You can find more details about these functions in the Documentation section.

Usage with Modules

import { decodeToLineStringFeature } from "@aws/polyline";

var decodedGeoJSON = decodeToLineStringFeature(response.EncodedPolyline);
map.addLayer({
  id: "route",
  type: "line",
  source: {
    type: "geojson",
    data: decodedGeoJSON,
  },
  layout: {
    "line-join": "round",
    "line-cap": "round",
  },
  paint: {
    "line-color": "#3887be",
    "line-width": 5,
    "line-opacity": 0.75,
  },
});

Usage with a browser

<!-- Import the Polyline library -->
<script src="https://cdn.jsdelivr.net/npm/@aws/polyline/dist/polyline.min.js"></script>
var decodedGeoJSON = polyline.decodeToLineStringFeature(
  response.EncodedPolyline,
);
map.addLayer({
  id: "route",
  type: "line",
  source: {
    type: "geojson",
    data: decodedGeoJSON,
  },
  layout: {
    "line-join": "round",
    "line-cap": "round",
  },
  paint: {
    "line-color": "#3887be",
    "line-width": 5,
    "line-opacity": 0.75,
  },
});

Documentation

Detailed documentation can be found under /docs/index.html after generating it by running:

npm run typedoc

getCompressionAlgorithm

Returns the currently-selected compression algorithm. This can be either FlexiblePolyline, Polyline5, or Polyline6.

const compressionType = polyline.getCompressionAlgorithm();

setCompressionAlgorithm

Sets the compression algorithm to use for subsequent encode/decode calls. This can be either FlexiblePolyline, Polyline5, or Polyline6.

polyline.setCompressionAlgorithm(polyline.FlexiblePolyline);

encodeFromLngLatArray

This encodes an array of coordinates in longitude, latitude order into compressed polyline data. The data can include an optional 3rd dimension when encoding with the FlexiblePolyline algorithm. While this isn't needed for MapLibre rendering, you might need it to compress data for a request to a Location Service Provider when requesting route-related data.

const polylineString = polyline.encodeFromLngLatArray([
  [5.0, 0.0],
  [10.0, 0.0],
]);

decodeToLineStringFeature

This is the most common method to use. It decodes compressed polyline data into a GeoJSON Feature containing a LineString that can directly be used as a MapLibre source for rendering.

const geoJsonLineStringFeature =
  polyline.decodeToLineStringFeature(polylineString);

decodeToPolygonFeature

Similar to decodeToLineStringFeature it decodes an array of compressed polyline rings into a GeoJSON Feature containing a Polygon that can directly be used as a MapLibre source for rendering. This should be used when the compressed data is meant to represent polygon rings, as it will also generate the correct winding order of the rings for use with GeoJSON.

const geoJsonPolygonFeature = polyline.decodeToPolygonFeature(polylineString);

decodeToLineString

This decodes a compressed polyline into a GeoJSON LineString. The LineString can be embedded into the geometry section of a GeoJSON Feature which can then be rendered with MapLibre.

const geoJsonLineString = polyline.decodeToLineString(polylineString);

decodeToPolygon

This decodes an array of compressed polyline rings into a GeoJSON Polygon. The Polygon can be embedded into the geometry section of a GeoJSON Feature which can then be rendered with MapLibre.

const geoJsonPolygon = polyline.decodeToPolygon(polylineString);

decodeToLngLatArray

This decodes compressed polyline data into an array of coordinates in longitude, latitude order. This method is helpful when you need to directly work with the coordinate data.

const lngLatArray = polyline.decodeToLngLatArray(polylineString);

Security

See CONTRIBUTING for more information.

Getting Help

The best way to interact with our team is through GitHub. You can open an issue and choose from one of our templates for bug reports or feature requests. If you have a support plan with AWS Support, you can also create a new support case.

Contributing

We welcome community contributions and pull requests. See CONTRIBUTING for information on how to set up a development environment and submit code.

License

This library is licensed under the MIT-0 License. See the LICENSE file.