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 is5
.
Aliases: polyline
, polyLine
, toEncoded
, fromGeoJSON
, toPolyline
.
Example
const encode = encodeconst polygon = type: 'Polygon' coordinates: -8163829 4148093 -8163628 4147993 -8163625 4147931 -8163829 4148033 -8163829 4148093 const encoded = // => { 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 is5
.
Aliases: decode
, geojson
, geoJson
, geoJSON
, fromEncoded
, toGeoJSON
fromPolyline
.
Example
const decode = decodeconst polygon = type: 'Polygon' coordinates: 'yvd|Fh~gqNfEqKzBEkEvKwB?'const geoJSON = // => { 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 = encode// var decode = require('geojson-polyline/stream').decode fs
Browser Usage
Standalone builds are available in /dist
for in-browser usage, which are ES3 compatible.
Web Worker
If you want to decode polylines using Web Workers, that is supported out of the box:
var worker = 'dist/geojson-polyline.min.js'var decoded = encodedworker { decoded ifdecodedlength === encodedlength // 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