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

geojson-polyline

v1.0.3

Published

Convert GeoJSON coordinates to and from encoded polylines. Supports all major GeoJSON types.

Downloads

2,813

Readme

GeoJSON PolyLine

Convert GeoJSON coordinates to and from encoded polylines. Supports encoding or decoding geometries/coordinates for all 9 standard GeoJSON types. Use it in Node.js (optionally as a transform stream), in the browser, as a Web Worker, or from the command line.

Install

npm install geojson-polyline

Usage

encode(geoJSON[, options])

Convert a GeoJSON object's coordinates to encoded polylines.

  • geoJSON : A GeoJSON object (required).
  • options : An optional Object with the following possible properties:
    • precision: A Number specifying the number of digits to keep for lat/lon. Default is 5.

Aliases: polyline, polyLine, toEncoded, fromGeoJSON, toPolyline.

Example

const encode = require('geojson-polyline').encode
const polygon = {
  type: 'Polygon',
  coordinates: [
    [[-81.63829, 41.48093], [-81.63628, 41.47993], [-81.63625, 41.47931], [-81.63829, 41.48033], [-81.63829, 41.48093]]
  ]
}
const encoded = encode(polygon)
// => { type: 'Polygon', coordinates: ['yvd|Fh~gqNfEqKzBEkEvKwB?'] }

decode(polyGeo[, options])

Convert a polyline-encoded GeoJSON to standard GeoJSON coordinate arrays.

  • polyGeo : A GeoJSON object whose coordinates are expressed as polylines (required).
  • options : An optional Object with the following possible properties:
    • precision: A Number specifying the precision that the polyline was encoded with. Default is 5.

Aliases: decode, geojson, geoJson, geoJSON, fromEncoded, toGeoJSON fromPolyline.

Example

const decode = require('geojson-polyline').decode
const polygon = {
  type: 'Polygon',
  coordinates: ['yvd|Fh~gqNfEqKzBEkEvKwB?']
}
const geoJSON = decode(polygon)
// => { type: 'Polygon', coordinates: [[[-81.63829, 41.48093], [-81.63628, 41.47993], [-81.63625, 41.47931], [-81.63829, 41.48033], [-81.63829, 41.48093]]]}

Stream API

Use the streaming API if you have streams instead of objects. Outputs newline-separated JSON.

Usage

var encode = require('geojson-polyline/stream').encode
// var decode = require('geojson-polyline/stream').decode

fs.createReadStream('./tl_2016_33_place.geojson')
  .pipe(encode({precision: 4}))
  .pipe(process.stdout)

Browser Usage

Standalone builds are available in /dist for in-browser usage, which are ES3 compatible.

<script src="dist/geojson-polyline.min.js"></script>
<script>
  var decoded = encoded.map(GeoJSONPolyline.decode);
</script>

Web Worker

If you want to decode polylines using Web Workers, that is supported out of the box:

var worker = new Worker('dist/geojson-polyline.min.js')
var decoded = []
encoded.forEach(function(encoded){
  worker.postMessage(['decode', encoded])
})
worker.onmessage = function(event) {
  decoded.push(event.data)
  if(decoded.length === encoded.length) {
    // Done!
  }
}

Currently, decoding and encoding is faster in the browser without using Web Workers.

Command Line Interface (CLI)

Convert GeoJSON objects, features, and feature collections from the command line. Works great for .geojson files from ogr2ogr or shp2json.

Install

npm install --global geojson-polyline

Usage

Usage:
  geojson-polyline <command> (read from stdin)
  geojson-polyline <command> -f <file> [<file2>] [...]
  geojson-polyline <command> <input>

Convert the coordinates of a GeoJSON object to and from encoded polylines. 

In all cases, input should be a valid JSON object or newline-separated JSON. 

Command is one of:
  encode
  decode
  geojson
  geoJson
  geoJSON
  polyline
  polyLine
  toEncoded
  fromEncoded
  toGeoJSON
  fromGeoJSON
  toPolyline
  fromPolyline

Example

geojson-polyline encode -f tabblock2010_56_pophu.geojson | mongoimport -c tabblock2010

Additional Information

Google's encoded polyline algorithm provides for very efficient encoding and storage of coordinate data. The @mapbox/polyline library from MapBox provides an implementation to encode/decode polylines, but only supports GeoJSON "LineString" features, not "Polygons" or "MultiLineStrings".

License

ISC