-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgeojson-polyline.js
444 lines (397 loc) · 12.1 KB
/
geojson-polyline.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.GeoJSONPolyline = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
var assign = require('object-assign')
var polyline = require('@mapbox/polyline')
var GeoJSONPolyline = module.exports = {
// Pick and choose your verbs
encode: geo2poly,
decode: poly2geo,
//
geojson: poly2geo,
geoJson: poly2geo,
geoJSON: poly2geo,
//
polyline: geo2poly,
polyLine: geo2poly,
//
toEncoded: geo2poly,
fromEncoded: poly2geo,
//
toGeoJSON: poly2geo,
fromGeoJSON: geo2poly,
//
toPolyline: geo2poly,
fromPolyline: poly2geo
}
/**
* Encodes a GeoJSON geometry as a polyline.
*
* @param {Object} geojson
* @param {Object} options
* @returns {Object} encoded geojson
*/
function geo2poly (geojson, options) {
var precision = typeof (options) === 'object' && options.precision
switch (geojson.type) {
// Coordinate arrays
case 'Point': {
return assign({}, geojson, {
coordinates: encode([geojson.coordinates], precision)
})
}
case 'MultiPoint':
case 'LineString': {
return assign({}, geojson, {
coordinates: encode(geojson.coordinates, precision)
})
}
// Arrays of coordinates
case 'MultiLineString':
case 'Polygon': {
return assign({}, geojson, {
coordinates: geojson.coordinates.map(function (coords) {
return encode(coords, precision)
})
})
}
case 'Feature': {
return assign({}, geojson, {
geometry: geo2poly(geojson.geometry, precision)
})
}
case 'FeatureCollection': {
return assign({}, geojson, {
features: geojson.features.map(function (feature) {
return geo2poly(feature, precision)
})
})
}
case 'GeometryCollection': {
return assign({}, geojson, {
geometries: geojson.geometries.map(function (geo) {
return geo2poly(geo, precision)
})
})
}
case 'MultiPolygon': {
return assign({}, geojson, {
coordinates: geojson.coordinates.map(function (polygons) {
return polygons.map(function (coords) {
return encode(coords, precision)
})
})
})
}
// Return the original object for unsupported types
default:
return geojson
}
}
/**
* Decode a polyline-encoded GeoJSON geometry.
*
* @param {Object} geojson
* @param {Object} options
* @returns {Object} decoded geojson
*/
function poly2geo (geojson, options) {
var precision = typeof (options) === 'object' && options.precision
switch (geojson.type) {
// Translate a single-element array back to a single coordinate array
case 'Point': {
return assign({}, geojson, {
coordinates: decode(geojson.coordinates, precision)[0]
})
}
// Decode into a coordinate array
case 'MultiPoint':
case 'LineString': {
return assign({}, geojson, {
coordinates: decode(geojson.coordinates, precision)
})
}
// Arrays of coordinate arrays
case 'MultiLineString':
case 'Polygon': {
return assign({}, geojson, {
coordinates: geojson.coordinates.map(function (coords) {
return decode(coords, precision)
})
})
}
// Arrays of polygons
case 'MultiPolygon': {
return assign({}, geojson, {
coordinates: geojson.coordinates.map(function (polygons) {
return polygons.map(function (coords) {
return decode(coords, precision)
})
})
})
}
// GeoJSON object is
case 'Feature': {
return assign({}, geojson, {
geometry: poly2geo(geojson.geometry, precision)
})
}
case 'FeatureCollection': {
return assign({}, geojson, {
features: geojson.features.map(function (feature) {
return poly2geo(feature, precision)
})
})
}
case 'GeometryCollection': {
return assign({}, geojson, {
geometries: geojson.geometries.map(function (geometry) {
return poly2geo(geometry, precision)
})
})
}
// Return the original object for unsupported types
default:
return geojson
}
}
function encode (coordinates, precision) {
return polyline.encode(flip(coordinates), precision)
}
function decode (str, precision) {
return flip(polyline.decode(str, precision))
}
function flip (coords) {
var flipped = []
for (var i = 0; i < coords.length; i++) {
flipped.push(coords[i].slice().reverse())
}
return flipped
}
if (typeof addEventListener !== 'undefined') {
/* global addEventListener, postMessage */
addEventListener('message', function (message) {
var method = message.data[0]
if (method in GeoJSONPolyline) {
var geojson = message.data[1]
var precision = message.data[2]
var converted = GeoJSONPolyline[method](geojson, precision)
postMessage(converted)
}
})
}
},{"@mapbox/polyline":2,"object-assign":3}],2:[function(require,module,exports){
'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 = {};
function py2_round(value) {
// Google's polyline algorithm uses the same rounding strategy as Python 2, which is different from JS for negative values
return Math.floor(Math.abs(value) + 0.5) * (value >= 0 ? 1 : -1);
}
function encode(current, previous, factor) {
current = py2_round(current * factor);
previous = py2_round(previous * factor);
var coordinate = current - previous;
coordinate <<= 1;
if (current - previous < 0) {
coordinate = ~coordinate;
}
var output = '';
while (coordinate >= 0x20) {
output += String.fromCharCode((0x20 | (coordinate & 0x1f)) + 63);
coordinate >>= 5;
}
output += String.fromCharCode(coordinate + 63);
return output;
}
/**
* 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) {
var index = 0,
lat = 0,
lng = 0,
coordinates = [],
shift = 0,
result = 0,
byte = null,
latitude_change,
longitude_change,
factor = Math.pow(10, precision || 5);
// Coordinates have variable length when encoded, so just keep
// track of whether we've hit the end of the string. In each
// loop iteration, a single coordinate is decoded.
while (index < str.length) {
// Reset shift, result, and byte
byte = null;
shift = 0;
result = 0;
do {
byte = str.charCodeAt(index++) - 63;
result |= (byte & 0x1f) << shift;
shift += 5;
} while (byte >= 0x20);
latitude_change = ((result & 1) ? ~(result >> 1) : (result >> 1));
shift = result = 0;
do {
byte = str.charCodeAt(index++) - 63;
result |= (byte & 0x1f) << shift;
shift += 5;
} while (byte >= 0x20);
longitude_change = ((result & 1) ? ~(result >> 1) : (result >> 1));
lat += latitude_change;
lng += longitude_change;
coordinates.push([lat / factor, lng / factor]);
}
return coordinates;
};
/**
* Encodes the given [latitude, longitude] coordinates array.
*
* @param {Array.<Array.<Number>>} coordinates
* @param {Number} precision
* @returns {String}
*/
polyline.encode = function(coordinates, precision) {
if (!coordinates.length) { return ''; }
var factor = Math.pow(10, precision || 5),
output = encode(coordinates[0][0], 0, factor) + encode(coordinates[0][1], 0, factor);
for (var i = 1; i < coordinates.length; i++) {
var a = coordinates[i], b = coordinates[i - 1];
output += encode(a[0], b[0], factor);
output += encode(a[1], b[1], factor);
}
return output;
};
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) {
if (geojson && geojson.type === 'Feature') {
geojson = geojson.geometry;
}
if (!geojson || geojson.type !== 'LineString') {
throw new Error('Input must be a GeoJSON LineString');
}
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;
}
},{}],3:[function(require,module,exports){
/*
object-assign
(c) Sindre Sorhus
@license MIT
*/
'use strict';
/* eslint-disable no-unused-vars */
var getOwnPropertySymbols = Object.getOwnPropertySymbols;
var hasOwnProperty = Object.prototype.hasOwnProperty;
var propIsEnumerable = Object.prototype.propertyIsEnumerable;
function toObject(val) {
if (val === null || val === undefined) {
throw new TypeError('Object.assign cannot be called with null or undefined');
}
return Object(val);
}
function shouldUseNative() {
try {
if (!Object.assign) {
return false;
}
// Detect buggy property enumeration order in older V8 versions.
// https://bugs.chromium.org/p/v8/issues/detail?id=4118
var test1 = new String('abc'); // eslint-disable-line no-new-wrappers
test1[5] = 'de';
if (Object.getOwnPropertyNames(test1)[0] === '5') {
return false;
}
// https://bugs.chromium.org/p/v8/issues/detail?id=3056
var test2 = {};
for (var i = 0; i < 10; i++) {
test2['_' + String.fromCharCode(i)] = i;
}
var order2 = Object.getOwnPropertyNames(test2).map(function (n) {
return test2[n];
});
if (order2.join('') !== '0123456789') {
return false;
}
// https://bugs.chromium.org/p/v8/issues/detail?id=3056
var test3 = {};
'abcdefghijklmnopqrst'.split('').forEach(function (letter) {
test3[letter] = letter;
});
if (Object.keys(Object.assign({}, test3)).join('') !==
'abcdefghijklmnopqrst') {
return false;
}
return true;
} catch (err) {
// We don't expect any of the above to throw, but better to be safe.
return false;
}
}
module.exports = shouldUseNative() ? Object.assign : function (target, source) {
var from;
var to = toObject(target);
var symbols;
for (var s = 1; s < arguments.length; s++) {
from = Object(arguments[s]);
for (var key in from) {
if (hasOwnProperty.call(from, key)) {
to[key] = from[key];
}
}
if (getOwnPropertySymbols) {
symbols = getOwnPropertySymbols(from);
for (var i = 0; i < symbols.length; i++) {
if (propIsEnumerable.call(from, symbols[i])) {
to[symbols[i]] = from[symbols[i]];
}
}
}
}
return to;
};
},{}]},{},[1])(1)
});