@placemarkio/polyline 1.1.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/README.md +7 -1
- package/dist/index.d.ts +4 -4
- package/dist/polyline.cjs +8 -7
- package/dist/polyline.cjs.map +1 -1
- package/dist/polyline.es.mjs +8 -7
- package/dist/polyline.es.mjs.map +1 -1
- package/dist/polyline.umd.js +1 -1
- package/dist/polyline.umd.js.map +1 -1
- package/package.json +5 -8
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
|
+
|
|
5
|
+
### [1.1.1](https://github.com/placemark/polyline/compare/v1.1.0...v1.1.1) (2022-10-06)
|
|
6
|
+
|
|
7
|
+
## 1.1.0 (2022-10-06)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
### Features
|
|
11
|
+
|
|
12
|
+
* Consistent order ([2a6fe45](https://github.com/placemark/polyline/commit/2a6fe45936c9c64018c07485515c2ac4200f0f2a))
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### Bug Fixes
|
|
16
|
+
|
|
17
|
+
* Improve docs ([03fcd10](https://github.com/placemark/polyline/commit/03fcd10f5353174d03ef6221e2b5889f24136c6b))
|
package/README.md
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
# polyline
|
|
1
|
+
# @placemarkio/polyline
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/js/@placemarkio%2Fpolyline)
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
@placemarkio/polyline
|
|
7
|
+
```
|
|
2
8
|
|
|
3
9
|
_polyline development is supported by 🌎 [placemark.io](https://placemark.io/)_
|
|
4
10
|
|
package/dist/index.d.ts
CHANGED
|
@@ -6,23 +6,23 @@ import type { LineString, Position } from "geojson";
|
|
|
6
6
|
* with an arbitrary string, it'll produce coordinates well
|
|
7
7
|
* outside of the normal range.
|
|
8
8
|
*/
|
|
9
|
-
export declare function decode(str: string): Position[];
|
|
9
|
+
export declare function decode(str: string, precision?: number): Position[];
|
|
10
10
|
/**
|
|
11
11
|
* Encodes the given [latitude, longitude] coordinates array.
|
|
12
12
|
*
|
|
13
13
|
* @param coordinates Coordinates, in longitude, latitude order
|
|
14
14
|
* @returns encoded polyline
|
|
15
15
|
*/
|
|
16
|
-
export declare function encode(coordinates: number[][]): string;
|
|
16
|
+
export declare function encode(coordinates: number[][], precision?: number): string;
|
|
17
17
|
/**
|
|
18
18
|
* Encodes a GeoJSON LineString feature/geometry.
|
|
19
19
|
*
|
|
20
20
|
* @param geojson A LineString
|
|
21
21
|
*/
|
|
22
|
-
export declare function geoJSONToPolyline(geojson: LineString): string;
|
|
22
|
+
export declare function geoJSONToPolyline(geojson: LineString, precision?: number): string;
|
|
23
23
|
/**
|
|
24
24
|
* Decodes to a GeoJSON LineString geometry.
|
|
25
25
|
*
|
|
26
26
|
* @param str An encoded polyline as a string.
|
|
27
27
|
*/
|
|
28
|
-
export declare function polylineToGeoJSON(str: string): LineString;
|
|
28
|
+
export declare function polylineToGeoJSON(str: string, precision?: number): LineString;
|
package/dist/polyline.cjs
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
const factor = 100000; // Math.pow(10, 5);
|
|
6
5
|
// https://github.com/mapbox/polyline/blob/master/src/polyline.js
|
|
7
6
|
// Based off of [the offical Google document](https://developers.google.com/maps/documentation/utilities/polylinealgorithm)
|
|
8
7
|
//
|
|
@@ -39,7 +38,8 @@ function resultChange(result) {
|
|
|
39
38
|
* with an arbitrary string, it'll produce coordinates well
|
|
40
39
|
* outside of the normal range.
|
|
41
40
|
*/
|
|
42
|
-
function decode(str) {
|
|
41
|
+
function decode(str, precision = 5) {
|
|
42
|
+
const factor = Math.pow(10, precision);
|
|
43
43
|
let index = 0;
|
|
44
44
|
let lat = 0;
|
|
45
45
|
let lng = 0;
|
|
@@ -82,10 +82,11 @@ function decode(str) {
|
|
|
82
82
|
* @param coordinates Coordinates, in longitude, latitude order
|
|
83
83
|
* @returns encoded polyline
|
|
84
84
|
*/
|
|
85
|
-
function encode(coordinates) {
|
|
85
|
+
function encode(coordinates, precision = 5) {
|
|
86
86
|
if (!coordinates.length) {
|
|
87
87
|
return "";
|
|
88
88
|
}
|
|
89
|
+
const factor = Math.pow(10, precision);
|
|
89
90
|
let output = encodeNumber(coordinates[0][1], 0, factor) +
|
|
90
91
|
encodeNumber(coordinates[0][0], 0, factor);
|
|
91
92
|
for (let i = 1; i < coordinates.length; i++) {
|
|
@@ -101,16 +102,16 @@ function encode(coordinates) {
|
|
|
101
102
|
*
|
|
102
103
|
* @param geojson A LineString
|
|
103
104
|
*/
|
|
104
|
-
function geoJSONToPolyline(geojson) {
|
|
105
|
-
return encode(geojson.coordinates);
|
|
105
|
+
function geoJSONToPolyline(geojson, precision = 5) {
|
|
106
|
+
return encode(geojson.coordinates, precision);
|
|
106
107
|
}
|
|
107
108
|
/**
|
|
108
109
|
* Decodes to a GeoJSON LineString geometry.
|
|
109
110
|
*
|
|
110
111
|
* @param str An encoded polyline as a string.
|
|
111
112
|
*/
|
|
112
|
-
function polylineToGeoJSON(str) {
|
|
113
|
-
const coords = decode(str);
|
|
113
|
+
function polylineToGeoJSON(str, precision = 5) {
|
|
114
|
+
const coords = decode(str, precision);
|
|
114
115
|
return {
|
|
115
116
|
type: "LineString",
|
|
116
117
|
coordinates: coords,
|
package/dist/polyline.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"polyline.cjs","sources":["../lib/lib/index.ts"],"sourcesContent":["import type { LineString, Position } from \"geojson\";\n\
|
|
1
|
+
{"version":3,"file":"polyline.cjs","sources":["../lib/lib/index.ts"],"sourcesContent":["import type { LineString, Position } from \"geojson\";\n\n// https://github.com/mapbox/polyline/blob/master/src/polyline.js\n\n// Based off of [the offical Google document](https://developers.google.com/maps/documentation/utilities/polylinealgorithm)\n//\n// Some parts from [this implementation](http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/PolylineEncoder.js)\n// by [Mark McClure](http://facstaff.unca.edu/mcmcclur/)\n\nfunction py2_round(value: number) {\n // Google's polyline algorithm uses the same rounding strategy as Python 2,\n // which is different from JS for negative values\n return Math.floor(Math.abs(value) + 0.5) * (value >= 0 ? 1 : -1);\n}\n\nfunction encodeNumber(current: number, previous: number, factor: number) {\n current = py2_round(current * factor);\n previous = py2_round(previous * factor);\n let coordinate = current - previous;\n coordinate <<= 1;\n if (current - previous < 0) {\n coordinate = ~coordinate;\n }\n let output = \"\";\n while (coordinate >= 0x20) {\n output += String.fromCharCode((0x20 | (coordinate & 0x1f)) + 63);\n coordinate >>= 5;\n }\n output += String.fromCharCode(coordinate + 63);\n return output;\n}\n\nfunction resultChange(result: number) {\n return result & 1 ? ~(result >> 1) : result >> 1;\n}\n\n/**\n * Decodes any string into a [longitude, latitude] coordinates array.\n *\n * Any string is a valid polyline, but if you provide this\n * with an arbitrary string, it'll produce coordinates well\n * outside of the normal range.\n */\nexport function decode(str: string, precision: number = 5): Position[] {\n const factor = Math.pow(10, precision);\n let index = 0;\n let lat = 0;\n let lng = 0;\n const coordinates = [];\n let shift = 0;\n let result = 0;\n let byte = null;\n\n let latitude_change: number;\n let longitude_change: number;\n\n // Coordinates have variable length when encoded, so just keep\n // track of whether we've hit the end of the string. In each\n // loop iteration, a single coordinate is decoded.\n while (index < str.length) {\n // Reset shift, result, and byte\n byte = null;\n shift = 0;\n result = 0;\n\n do {\n byte = str.charCodeAt(index++) - 63;\n result |= (byte & 0x1f) << shift;\n shift += 5;\n } while (byte >= 0x20);\n\n latitude_change = resultChange(result);\n\n shift = result = 0;\n\n do {\n byte = str.charCodeAt(index++) - 63;\n result |= (byte & 0x1f) << shift;\n shift += 5;\n } while (byte >= 0x20);\n\n longitude_change = resultChange(result);\n\n lat += latitude_change;\n lng += longitude_change;\n\n coordinates.push([lng / factor, lat / factor]);\n }\n\n return coordinates;\n}\n\n/**\n * Encodes the given [latitude, longitude] coordinates array.\n *\n * @param coordinates Coordinates, in longitude, latitude order\n * @returns encoded polyline\n */\nexport function encode(coordinates: number[][], precision: number = 5) {\n if (!coordinates.length) {\n return \"\";\n }\n const factor = Math.pow(10, precision);\n\n let output =\n encodeNumber(coordinates[0][1], 0, factor) +\n encodeNumber(coordinates[0][0], 0, factor);\n\n for (let i = 1; i < coordinates.length; i++) {\n const a = coordinates[i];\n const b = coordinates[i - 1];\n output += encodeNumber(a[1], b[1], factor);\n output += encodeNumber(a[0], b[0], factor);\n }\n\n return output;\n}\n\n/**\n * Encodes a GeoJSON LineString feature/geometry.\n *\n * @param geojson A LineString\n */\nexport function geoJSONToPolyline(geojson: LineString, precision: number = 5) {\n return encode(geojson.coordinates, precision);\n}\n\n/**\n * Decodes to a GeoJSON LineString geometry.\n *\n * @param str An encoded polyline as a string.\n */\nexport function polylineToGeoJSON(str: string, precision: number = 5): LineString {\n const coords = decode(str, precision);\n return {\n type: \"LineString\",\n coordinates: coords,\n };\n}\n"],"names":[],"mappings":";;;;AAEA;AAEA;AACA;AACA;AACA;AAEA,SAAS,SAAS,CAAC,KAAa,EAAA;;;AAG9B,IAAA,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,GAAG,CAAC,IAAI,KAAK,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;AACnE,CAAC;AAED,SAAS,YAAY,CAAC,OAAe,EAAE,QAAgB,EAAE,MAAc,EAAA;AACrE,IAAA,OAAO,GAAG,SAAS,CAAC,OAAO,GAAG,MAAM,CAAC,CAAC;AACtC,IAAA,QAAQ,GAAG,SAAS,CAAC,QAAQ,GAAG,MAAM,CAAC,CAAC;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YAAY,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,CAAC,CAAC;AAE7C,IAAA,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,WAAW,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;AAC3C,QAAA,MAAM,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC;QACzB,MAAM,CAAC,GAAG,WAAW,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;AAC7B,QAAA,MAAM,IAAI,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;AAC3C,QAAA,MAAM,IAAI,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;AAC5C,KAAA;AAED,IAAA,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;AAIG;SACa,iBAAiB,CAAC,OAAmB,EAAE,YAAoB,CAAC,EAAA;IAC1E,OAAO,MAAM,CAAC,OAAO,CAAC,WAAW,EAAE,SAAS,CAAC,CAAC;AAChD,CAAC;AAED;;;;AAIG;SACa,iBAAiB,CAAC,GAAW,EAAE,YAAoB,CAAC,EAAA;IAClE,MAAM,MAAM,GAAG,MAAM,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;IACtC,OAAO;AACL,QAAA,IAAI,EAAE,YAAY;AAClB,QAAA,WAAW,EAAE,MAAM;KACpB,CAAC;AACJ;;;;;;;"}
|
package/dist/polyline.es.mjs
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
const factor = 100000; // Math.pow(10, 5);
|
|
2
1
|
// https://github.com/mapbox/polyline/blob/master/src/polyline.js
|
|
3
2
|
// Based off of [the offical Google document](https://developers.google.com/maps/documentation/utilities/polylinealgorithm)
|
|
4
3
|
//
|
|
@@ -35,7 +34,8 @@ function resultChange(result) {
|
|
|
35
34
|
* with an arbitrary string, it'll produce coordinates well
|
|
36
35
|
* outside of the normal range.
|
|
37
36
|
*/
|
|
38
|
-
function decode(str) {
|
|
37
|
+
function decode(str, precision = 5) {
|
|
38
|
+
const factor = Math.pow(10, precision);
|
|
39
39
|
let index = 0;
|
|
40
40
|
let lat = 0;
|
|
41
41
|
let lng = 0;
|
|
@@ -78,10 +78,11 @@ function decode(str) {
|
|
|
78
78
|
* @param coordinates Coordinates, in longitude, latitude order
|
|
79
79
|
* @returns encoded polyline
|
|
80
80
|
*/
|
|
81
|
-
function encode(coordinates) {
|
|
81
|
+
function encode(coordinates, precision = 5) {
|
|
82
82
|
if (!coordinates.length) {
|
|
83
83
|
return "";
|
|
84
84
|
}
|
|
85
|
+
const factor = Math.pow(10, precision);
|
|
85
86
|
let output = encodeNumber(coordinates[0][1], 0, factor) +
|
|
86
87
|
encodeNumber(coordinates[0][0], 0, factor);
|
|
87
88
|
for (let i = 1; i < coordinates.length; i++) {
|
|
@@ -97,16 +98,16 @@ function encode(coordinates) {
|
|
|
97
98
|
*
|
|
98
99
|
* @param geojson A LineString
|
|
99
100
|
*/
|
|
100
|
-
function geoJSONToPolyline(geojson) {
|
|
101
|
-
return encode(geojson.coordinates);
|
|
101
|
+
function geoJSONToPolyline(geojson, precision = 5) {
|
|
102
|
+
return encode(geojson.coordinates, precision);
|
|
102
103
|
}
|
|
103
104
|
/**
|
|
104
105
|
* Decodes to a GeoJSON LineString geometry.
|
|
105
106
|
*
|
|
106
107
|
* @param str An encoded polyline as a string.
|
|
107
108
|
*/
|
|
108
|
-
function polylineToGeoJSON(str) {
|
|
109
|
-
const coords = decode(str);
|
|
109
|
+
function polylineToGeoJSON(str, precision = 5) {
|
|
110
|
+
const coords = decode(str, precision);
|
|
110
111
|
return {
|
|
111
112
|
type: "LineString",
|
|
112
113
|
coordinates: coords,
|
package/dist/polyline.es.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"polyline.es.mjs","sources":["../lib/lib/index.ts"],"sourcesContent":["import type { LineString, Position } from \"geojson\";\n\
|
|
1
|
+
{"version":3,"file":"polyline.es.mjs","sources":["../lib/lib/index.ts"],"sourcesContent":["import type { LineString, Position } from \"geojson\";\n\n// https://github.com/mapbox/polyline/blob/master/src/polyline.js\n\n// Based off of [the offical Google document](https://developers.google.com/maps/documentation/utilities/polylinealgorithm)\n//\n// Some parts from [this implementation](http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/PolylineEncoder.js)\n// by [Mark McClure](http://facstaff.unca.edu/mcmcclur/)\n\nfunction py2_round(value: number) {\n // Google's polyline algorithm uses the same rounding strategy as Python 2,\n // which is different from JS for negative values\n return Math.floor(Math.abs(value) + 0.5) * (value >= 0 ? 1 : -1);\n}\n\nfunction encodeNumber(current: number, previous: number, factor: number) {\n current = py2_round(current * factor);\n previous = py2_round(previous * factor);\n let coordinate = current - previous;\n coordinate <<= 1;\n if (current - previous < 0) {\n coordinate = ~coordinate;\n }\n let output = \"\";\n while (coordinate >= 0x20) {\n output += String.fromCharCode((0x20 | (coordinate & 0x1f)) + 63);\n coordinate >>= 5;\n }\n output += String.fromCharCode(coordinate + 63);\n return output;\n}\n\nfunction resultChange(result: number) {\n return result & 1 ? ~(result >> 1) : result >> 1;\n}\n\n/**\n * Decodes any string into a [longitude, latitude] coordinates array.\n *\n * Any string is a valid polyline, but if you provide this\n * with an arbitrary string, it'll produce coordinates well\n * outside of the normal range.\n */\nexport function decode(str: string, precision: number = 5): Position[] {\n const factor = Math.pow(10, precision);\n let index = 0;\n let lat = 0;\n let lng = 0;\n const coordinates = [];\n let shift = 0;\n let result = 0;\n let byte = null;\n\n let latitude_change: number;\n let longitude_change: number;\n\n // Coordinates have variable length when encoded, so just keep\n // track of whether we've hit the end of the string. In each\n // loop iteration, a single coordinate is decoded.\n while (index < str.length) {\n // Reset shift, result, and byte\n byte = null;\n shift = 0;\n result = 0;\n\n do {\n byte = str.charCodeAt(index++) - 63;\n result |= (byte & 0x1f) << shift;\n shift += 5;\n } while (byte >= 0x20);\n\n latitude_change = resultChange(result);\n\n shift = result = 0;\n\n do {\n byte = str.charCodeAt(index++) - 63;\n result |= (byte & 0x1f) << shift;\n shift += 5;\n } while (byte >= 0x20);\n\n longitude_change = resultChange(result);\n\n lat += latitude_change;\n lng += longitude_change;\n\n coordinates.push([lng / factor, lat / factor]);\n }\n\n return coordinates;\n}\n\n/**\n * Encodes the given [latitude, longitude] coordinates array.\n *\n * @param coordinates Coordinates, in longitude, latitude order\n * @returns encoded polyline\n */\nexport function encode(coordinates: number[][], precision: number = 5) {\n if (!coordinates.length) {\n return \"\";\n }\n const factor = Math.pow(10, precision);\n\n let output =\n encodeNumber(coordinates[0][1], 0, factor) +\n encodeNumber(coordinates[0][0], 0, factor);\n\n for (let i = 1; i < coordinates.length; i++) {\n const a = coordinates[i];\n const b = coordinates[i - 1];\n output += encodeNumber(a[1], b[1], factor);\n output += encodeNumber(a[0], b[0], factor);\n }\n\n return output;\n}\n\n/**\n * Encodes a GeoJSON LineString feature/geometry.\n *\n * @param geojson A LineString\n */\nexport function geoJSONToPolyline(geojson: LineString, precision: number = 5) {\n return encode(geojson.coordinates, precision);\n}\n\n/**\n * Decodes to a GeoJSON LineString geometry.\n *\n * @param str An encoded polyline as a string.\n */\nexport function polylineToGeoJSON(str: string, precision: number = 5): LineString {\n const coords = decode(str, precision);\n return {\n type: \"LineString\",\n coordinates: coords,\n };\n}\n"],"names":[],"mappings":"AAEA;AAEA;AACA;AACA;AACA;AAEA,SAAS,SAAS,CAAC,KAAa,EAAA;;;AAG9B,IAAA,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,GAAG,CAAC,IAAI,KAAK,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;AACnE,CAAC;AAED,SAAS,YAAY,CAAC,OAAe,EAAE,QAAgB,EAAE,MAAc,EAAA;AACrE,IAA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EvC,IAAA,IAAI,MAAM,GACR,YAAY,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,CAAC;AAC1C,QAAA,YAAY,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,CAAC,CAAC;AAE7C,IAAA,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,WAAW,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;AAC3C,QAAA,MAAM,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC;QACzB,MAAM,CAAC,GAAG,WAAW,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;AAC7B,QAAA,MAAM,IAAI,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;AAC3C,QAAA,MAAM,IAAI,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;AAC5C,KAAA;AAED,IAAA,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;AAIG;SACa,iBAAiB,CAAC,OAAmB,EAAE,YAAoB,CAAC,EAAA;IAC1E,OAAO,MAAM,CAAC,OAAO,CAAC,WAAW,EAAE,SAAS,CAAC,CAAC;AAChD,CAAC;AAED;;;;AAIG;SACa,iBAAiB,CAAC,GAAW,EAAE,YAAoB,CAAC,EAAA;IAClE,MAAM,MAAM,GAAG,MAAM,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;IACtC,OAAO;AACL,QAAA,IAAI,EAAE,YAAY;AAClB,QAAA,WAAW,EAAE,MAAM;KACpB,CAAC;AACJ;;;;"}
|
package/dist/polyline.umd.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).toGeoJSON={})}(this,(function(e){"use strict";
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).toGeoJSON={})}(this,(function(e){"use strict";function t(e){return Math.floor(Math.abs(e)+.5)*(e>=0?1:-1)}function o(e,o,n){let r=(e=t(e*n))-(o=t(o*n));r<<=1,e-o<0&&(r=~r);let i="";for(;r>=32;)i+=String.fromCharCode(63+(32|31&r)),r>>=5;return i+=String.fromCharCode(r+63),i}function n(e){return 1&e?~(e>>1):e>>1}function r(e,t=5){const o=Math.pow(10,t);let r=0,i=0,f=0;const l=[];let u,c,d=0,s=0,h=null;for(;r<e.length;){h=null,d=0,s=0;do{h=e.charCodeAt(r++)-63,s|=(31&h)<<d,d+=5}while(h>=32);u=n(s),d=s=0;do{h=e.charCodeAt(r++)-63,s|=(31&h)<<d,d+=5}while(h>=32);c=n(s),i+=u,f+=c,l.push([f/o,i/o])}return l}function i(e,t=5){if(!e.length)return"";const n=Math.pow(10,t);let r=o(e[0][1],0,n)+o(e[0][0],0,n);for(let t=1;t<e.length;t++){const i=e[t],f=e[t-1];r+=o(i[1],f[1],n),r+=o(i[0],f[0],n)}return r}e.decode=r,e.encode=i,e.geoJSONToPolyline=function(e,t=5){return i(e.coordinates,t)},e.polylineToGeoJSON=function(e,t=5){return{type:"LineString",coordinates:r(e,t)}},Object.defineProperty(e,"__esModule",{value:!0})}));
|
|
2
2
|
//# sourceMappingURL=polyline.umd.js.map
|
package/dist/polyline.umd.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"polyline.umd.js","sources":["../lib/lib/index.ts"],"sourcesContent":["import type { LineString, Position } from \"geojson\";\n\
|
|
1
|
+
{"version":3,"file":"polyline.umd.js","sources":["../lib/lib/index.ts"],"sourcesContent":["import type { LineString, Position } from \"geojson\";\n\n// https://github.com/mapbox/polyline/blob/master/src/polyline.js\n\n// Based off of [the offical Google document](https://developers.google.com/maps/documentation/utilities/polylinealgorithm)\n//\n// Some parts from [this implementation](http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/PolylineEncoder.js)\n// by [Mark McClure](http://facstaff.unca.edu/mcmcclur/)\n\nfunction py2_round(value: number) {\n // Google's polyline algorithm uses the same rounding strategy as Python 2,\n // which is different from JS for negative values\n return Math.floor(Math.abs(value) + 0.5) * (value >= 0 ? 1 : -1);\n}\n\nfunction encodeNumber(current: number, previous: number, factor: number) {\n current = py2_round(current * factor);\n previous = py2_round(previous * factor);\n let coordinate = current - previous;\n coordinate <<= 1;\n if (current - previous < 0) {\n coordinate = ~coordinate;\n }\n let output = \"\";\n while (coordinate >= 0x20) {\n output += String.fromCharCode((0x20 | (coordinate & 0x1f)) + 63);\n coordinate >>= 5;\n }\n output += String.fromCharCode(coordinate + 63);\n return output;\n}\n\nfunction resultChange(result: number) {\n return result & 1 ? ~(result >> 1) : result >> 1;\n}\n\n/**\n * Decodes any string into a [longitude, latitude] coordinates array.\n *\n * Any string is a valid polyline, but if you provide this\n * with an arbitrary string, it'll produce coordinates well\n * outside of the normal range.\n */\nexport function decode(str: string, precision: number = 5): Position[] {\n const factor = Math.pow(10, precision);\n let index = 0;\n let lat = 0;\n let lng = 0;\n const coordinates = [];\n let shift = 0;\n let result = 0;\n let byte = null;\n\n let latitude_change: number;\n let longitude_change: number;\n\n // Coordinates have variable length when encoded, so just keep\n // track of whether we've hit the end of the string. In each\n // loop iteration, a single coordinate is decoded.\n while (index < str.length) {\n // Reset shift, result, and byte\n byte = null;\n shift = 0;\n result = 0;\n\n do {\n byte = str.charCodeAt(index++) - 63;\n result |= (byte & 0x1f) << shift;\n shift += 5;\n } while (byte >= 0x20);\n\n latitude_change = resultChange(result);\n\n shift = result = 0;\n\n do {\n byte = str.charCodeAt(index++) - 63;\n result |= (byte & 0x1f) << shift;\n shift += 5;\n } while (byte >= 0x20);\n\n longitude_change = resultChange(result);\n\n lat += latitude_change;\n lng += longitude_change;\n\n coordinates.push([lng / factor, lat / factor]);\n }\n\n return coordinates;\n}\n\n/**\n * Encodes the given [latitude, longitude] coordinates array.\n *\n * @param coordinates Coordinates, in longitude, latitude order\n * @returns encoded polyline\n */\nexport function encode(coordinates: number[][], precision: number = 5) {\n if (!coordinates.length) {\n return \"\";\n }\n const factor = Math.pow(10, precision);\n\n let output =\n encodeNumber(coordinates[0][1], 0, factor) +\n encodeNumber(coordinates[0][0], 0, factor);\n\n for (let i = 1; i < coordinates.length; i++) {\n const a = coordinates[i];\n const b = coordinates[i - 1];\n output += encodeNumber(a[1], b[1], factor);\n output += encodeNumber(a[0], b[0], factor);\n }\n\n return output;\n}\n\n/**\n * Encodes a GeoJSON LineString feature/geometry.\n *\n * @param geojson A LineString\n */\nexport function geoJSONToPolyline(geojson: LineString, precision: number = 5) {\n return encode(geojson.coordinates, precision);\n}\n\n/**\n * Decodes to a GeoJSON LineString geometry.\n *\n * @param str An encoded polyline as a string.\n */\nexport function polylineToGeoJSON(str: string, precision: number = 5): LineString {\n const coords = decode(str, precision);\n return {\n type: \"LineString\",\n coordinates: coords,\n };\n}\n"],"names":["py2_round","value","Math","floor","abs","encodeNumber","current","previous","factor","coordinate","output","String","fromCharCode","resultChange","result","decode","str","precision","pow","index","lat","lng","coordinates","latitude_change","longitude_change","shift","byte","length","charCodeAt","push","encode","i","a","b","geojson","type"],"mappings":"iPASA,SAASA,EAAUC,GAGjB,OAAOC,KAAKC,MAAMD,KAAKE,IAAIH,GAAS,KAAQA,GAAS,EAAI,GAAK,EAChE,CAEA,SAASI,EAAaC,EAAiBC,EAAkBC,GAGvD,IAAIC,GAFJH,EAAUN,EAAUM,EAAUE,KAC9BD,EAAWP,EAAUO,EAAWC,IAEhCC,IAAe,EACXH,EAAUC,EAAW,IACvBE,GAAcA,GAEhB,IAAIC,EAAS,GACb,KAAOD,GAAc,IACnBC,GAAUC,OAAOC,aAA4C,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DAOkCwB,EAAqBjB,EAAoB,GACzE,OAAOa,EAAOI,EAAQZ,YAAaL,EACrC,+BAOkCD,EAAaC,EAAoB,GAEjE,MAAO,CACLkB,KAAM,aACNb,YAHaP,EAAOC,EAAKC,GAK7B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@placemarkio/polyline",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "encode and decode polylines",
|
|
5
5
|
"source": "lib/index.ts",
|
|
6
6
|
"umd:main": "dist/polyline.umd.js",
|
|
@@ -18,8 +18,8 @@
|
|
|
18
18
|
"dist"
|
|
19
19
|
],
|
|
20
20
|
"scripts": {
|
|
21
|
-
"test": "
|
|
22
|
-
"test:ci": "
|
|
21
|
+
"test": "vitest run",
|
|
22
|
+
"test:ci": "vitest run",
|
|
23
23
|
"lint": "eslint",
|
|
24
24
|
"doc": "typedoc --options ./typedoc.json",
|
|
25
25
|
"prepare": "rollup -c rollup.config.js && npm run doc",
|
|
@@ -39,12 +39,8 @@
|
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@rollup/plugin-typescript": "^8.5.0",
|
|
42
|
-
"@swc/core": "^1.3.4",
|
|
43
|
-
"@swc/jest": "^0.2.23",
|
|
44
|
-
"@types/jest": "^29.1.1",
|
|
45
42
|
"eslint": "^8.24.0",
|
|
46
43
|
"husky": "^8.0.1",
|
|
47
|
-
"jest": "^29.1.2",
|
|
48
44
|
"prettier": "^2.7.1",
|
|
49
45
|
"rollup": "^2.79.1",
|
|
50
46
|
"rollup-plugin-dts": "^4.2.2",
|
|
@@ -52,7 +48,8 @@
|
|
|
52
48
|
"standard-version": "^9.5.0",
|
|
53
49
|
"tslib": "^2.4.0",
|
|
54
50
|
"typedoc": "^0.23.15",
|
|
55
|
-
"typescript": "^4.8.4"
|
|
51
|
+
"typescript": "^4.8.4",
|
|
52
|
+
"vitest": "^0.24.4"
|
|
56
53
|
},
|
|
57
54
|
"keywords": [
|
|
58
55
|
"polyline",
|