Comparing version 0.1.0 to 0.2.0
@@ -15,1 +15,5 @@ ### polyline.decode(string[, precision]) | ||
Takes a GeoJSON LineString feature and returns an encoded string. If not specified, precision defaults to 5. | ||
### polyline.toGeoJSON(string[, precision]) | ||
Takes an encoded string and returns a GeoJSON LineString geometry. If not specified, precision defaults to 5. |
@@ -0,1 +1,6 @@ | ||
## 0.2.0 | ||
* Add `.toGeoJSON` method to convert an encoded polyline to GeoJSON | ||
LineString. | ||
## 0.1.0 | ||
@@ -2,0 +7,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"description": "Polyline encoding and decoding", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "type": "git", |
@@ -29,3 +29,3 @@ [![Build Status](https://secure.travis-ci.org/mapbox/polyline.png?branch=master)](http://travis-ci.org/mapbox/polyline) [![Coverage Status](https://coveralls.io/repos/mapbox/polyline/badge.svg)](https://coveralls.io/r/mapbox/polyline) | ||
"type": "LineString", | ||
"coordinates": example_flipped | ||
"coordinates": [[-120.2, 38.5], [-120.95, 40.7], [-126.453, 43.252]] | ||
}, | ||
@@ -32,0 +32,0 @@ "properties": {} |
'use strict'; | ||
/** | ||
* Based off of [the offical Google document](https://developers.google.com/maps/documentation/utilities/polylinealgorithm) | ||
* | ||
* Some parts from [this implementation](http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/PolylineEncoder.js) | ||
* by [Mark McClure](http://facstaff.unca.edu/mcmcclur/) | ||
* | ||
* @module polyline | ||
*/ | ||
var polyline = {}; | ||
// Based off of [the offical Google document](https://developers.google.com/maps/documentation/utilities/polylinealgorithm) | ||
// | ||
// Some parts from [this implementation](http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/PolylineEncoder.js) | ||
// by [Mark McClure](http://facstaff.unca.edu/mcmcclur/) | ||
function encode(coordinate, factor) { | ||
@@ -25,4 +29,13 @@ coordinate = Math.round(coordinate * factor); | ||
// This is adapted from the implementation in Project-OSRM | ||
// https://github.com/DennisOSRM/Project-OSRM-Web/blob/master/WebContent/routing/OSRM.RoutingGeometry.js | ||
/** | ||
* Decodes to a [latitude, longitude] coordinates array. | ||
* | ||
* This is adapted from the implementation in Project-OSRM. | ||
* | ||
* @param {String} str | ||
* @param {Number} precision | ||
* @returns {Array} | ||
* | ||
* @see https://github.com/Project-OSRM/osrm-frontend/blob/master/WebContent/routing/OSRM.RoutingGeometry.js | ||
*/ | ||
polyline.decode = function(str, precision) { | ||
@@ -77,2 +90,9 @@ var index = 0, | ||
/** | ||
* Encodes the given [latitude, longitude] coordinates array. | ||
* | ||
* @param {Array.<Array.<Number>>} coordinates | ||
* @param {Number} precision | ||
* @returns {String} | ||
*/ | ||
polyline.encode = function(coordinates, precision) { | ||
@@ -93,2 +113,17 @@ if (!coordinates.length) { return ''; } | ||
function flipped(coords) { | ||
var flipped = []; | ||
for (var i = 0; i < coords.length; i++) { | ||
flipped.push(coords[i].slice().reverse()); | ||
} | ||
return flipped; | ||
} | ||
/** | ||
* Encodes a GeoJSON LineString feature/geometry. | ||
* | ||
* @param {Object} geojson | ||
* @param {Number} precision | ||
* @returns {String} | ||
*/ | ||
polyline.fromGeoJSON = function(geojson, precision) { | ||
@@ -101,12 +136,22 @@ if (geojson && geojson.type === 'Feature') { | ||
} | ||
var coords = geojson.coordinates; | ||
var flipped = []; | ||
for (var i = 0; i < coords.length; i++) { | ||
flipped.push(coords[i].slice().reverse()); | ||
} | ||
return polyline.encode(flipped, precision); | ||
return polyline.encode(flipped(geojson.coordinates), precision); | ||
}; | ||
/** | ||
* Decodes to a GeoJSON LineString geometry. | ||
* | ||
* @param {String} str | ||
* @param {Number} precision | ||
* @returns {Object} | ||
*/ | ||
polyline.toGeoJSON = function(str, precision) { | ||
var coords = polyline.decode(str, precision); | ||
return { | ||
type: 'LineString', | ||
coordinates: flipped(coords) | ||
}; | ||
}; | ||
if (typeof module === 'object' && module.exports) { | ||
module.exports = polyline; | ||
} |
@@ -93,3 +93,12 @@ 'use strict'; | ||
t.test('#toGeoJSON()', function(t) { | ||
t.test('flips coordinates and decodes geometry', function(t) { | ||
t.deepEqual(polyline.toGeoJSON('_p~iF~ps|U_ulLnnqC_mqNvxq`@'), geojson.geometry); | ||
t.end(); | ||
}); | ||
t.end(); | ||
}); | ||
t.end(); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
214
0
12102
12