@tmlmobilidade/geo 20260624.1309.39 → 20260625.1443.7
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/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/polyline.d.ts +5 -0
- package/dist/polyline.js +88 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -8,5 +8,6 @@ export * from './get-geofence-on-point.js';
|
|
|
8
8
|
export * from './hashed-shapes-to-geojson.js';
|
|
9
9
|
export * from './is-point-in-polygon.js';
|
|
10
10
|
export * from './measurements.js';
|
|
11
|
+
export * from './polyline.js';
|
|
11
12
|
export * from './vehicle-event-to-geojson.js';
|
|
12
13
|
export * from '@turf/turf';
|
package/dist/index.js
CHANGED
|
@@ -8,5 +8,6 @@ export * from './get-geofence-on-point.js';
|
|
|
8
8
|
export * from './hashed-shapes-to-geojson.js';
|
|
9
9
|
export * from './is-point-in-polygon.js';
|
|
10
10
|
export * from './measurements.js';
|
|
11
|
+
export * from './polyline.js';
|
|
11
12
|
export * from './vehicle-event-to-geojson.js';
|
|
12
13
|
export * from '@turf/turf';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type Feature, type GeoJsonProperties, type LineString } from 'geojson';
|
|
2
|
+
export declare const DEFAULT_POLYLINE_PRECISION = 5;
|
|
3
|
+
export declare function encodePolylineFromGeoJson(geojson: Feature<LineString> | LineString, precision?: number): string;
|
|
4
|
+
export declare function decodePolylineToGeoJson(encodedPolyline: string, precision?: number): LineString;
|
|
5
|
+
export declare function lineFeatureFromEncodedPolyline<TProperties extends GeoJsonProperties = GeoJsonProperties>(encodedPolyline: string, properties?: TProperties, precision?: number): Feature<LineString, TProperties>;
|
package/dist/polyline.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/* * */
|
|
2
|
+
export const DEFAULT_POLYLINE_PRECISION = 5;
|
|
3
|
+
/* * */
|
|
4
|
+
function roundCoordinate(value) {
|
|
5
|
+
return Math.floor(Math.abs(value) + 0.5) * (value >= 0 ? 1 : -1);
|
|
6
|
+
}
|
|
7
|
+
function encodeCoordinate(current, previous, factor) {
|
|
8
|
+
let coordinate = (roundCoordinate(current * factor) - roundCoordinate(previous * factor)) * 2;
|
|
9
|
+
if (coordinate < 0)
|
|
10
|
+
coordinate = -coordinate - 1;
|
|
11
|
+
let output = '';
|
|
12
|
+
while (coordinate >= 0x20) {
|
|
13
|
+
output += String.fromCharCode((0x20 | (coordinate & 0x1f)) + 63);
|
|
14
|
+
coordinate = Math.floor(coordinate / 32);
|
|
15
|
+
}
|
|
16
|
+
output += String.fromCharCode(coordinate + 63);
|
|
17
|
+
return output;
|
|
18
|
+
}
|
|
19
|
+
function getPrecisionFactor(precision) {
|
|
20
|
+
return Math.pow(10, Number.isInteger(precision) ? precision : DEFAULT_POLYLINE_PRECISION);
|
|
21
|
+
}
|
|
22
|
+
function getLineString(geojson) {
|
|
23
|
+
if (geojson.type === 'Feature')
|
|
24
|
+
return geojson.geometry;
|
|
25
|
+
return geojson;
|
|
26
|
+
}
|
|
27
|
+
function encodeLineString(lineString, precision) {
|
|
28
|
+
if (lineString.type !== 'LineString')
|
|
29
|
+
throw new Error('Input must be a GeoJSON LineString');
|
|
30
|
+
if (!lineString.coordinates.length)
|
|
31
|
+
return '';
|
|
32
|
+
const factor = getPrecisionFactor(precision);
|
|
33
|
+
let previousLatitude = 0;
|
|
34
|
+
let previousLongitude = 0;
|
|
35
|
+
let output = '';
|
|
36
|
+
for (const [longitude, latitude] of lineString.coordinates) {
|
|
37
|
+
output += encodeCoordinate(latitude, previousLatitude, factor);
|
|
38
|
+
output += encodeCoordinate(longitude, previousLongitude, factor);
|
|
39
|
+
previousLatitude = latitude;
|
|
40
|
+
previousLongitude = longitude;
|
|
41
|
+
}
|
|
42
|
+
return output;
|
|
43
|
+
}
|
|
44
|
+
/* * */
|
|
45
|
+
export function encodePolylineFromGeoJson(geojson, precision = DEFAULT_POLYLINE_PRECISION) {
|
|
46
|
+
return encodeLineString(getLineString(geojson), precision);
|
|
47
|
+
}
|
|
48
|
+
export function decodePolylineToGeoJson(encodedPolyline, precision = DEFAULT_POLYLINE_PRECISION) {
|
|
49
|
+
const factor = getPrecisionFactor(precision);
|
|
50
|
+
const coordinates = [];
|
|
51
|
+
let index = 0;
|
|
52
|
+
let latitude = 0;
|
|
53
|
+
let longitude = 0;
|
|
54
|
+
while (index < encodedPolyline.length) {
|
|
55
|
+
let byte = 0;
|
|
56
|
+
let result = 0;
|
|
57
|
+
let shift = 1;
|
|
58
|
+
do {
|
|
59
|
+
byte = encodedPolyline.charCodeAt(index++) - 63;
|
|
60
|
+
result += (byte & 0x1f) * shift;
|
|
61
|
+
shift *= 32;
|
|
62
|
+
} while (byte >= 0x20);
|
|
63
|
+
const latitudeChange = result & 1 ? (-result - 1) / 2 : result / 2;
|
|
64
|
+
result = 0;
|
|
65
|
+
shift = 1;
|
|
66
|
+
do {
|
|
67
|
+
byte = encodedPolyline.charCodeAt(index++) - 63;
|
|
68
|
+
result += (byte & 0x1f) * shift;
|
|
69
|
+
shift *= 32;
|
|
70
|
+
} while (byte >= 0x20);
|
|
71
|
+
const longitudeChange = result & 1 ? (-result - 1) / 2 : result / 2;
|
|
72
|
+
latitude += latitudeChange;
|
|
73
|
+
longitude += longitudeChange;
|
|
74
|
+
coordinates.push([longitude / factor, latitude / factor]);
|
|
75
|
+
}
|
|
76
|
+
return {
|
|
77
|
+
coordinates,
|
|
78
|
+
type: 'LineString',
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
export function lineFeatureFromEncodedPolyline(encodedPolyline, properties, precision = DEFAULT_POLYLINE_PRECISION) {
|
|
82
|
+
return {
|
|
83
|
+
geometry: decodePolylineToGeoJson(encodedPolyline, precision),
|
|
84
|
+
properties: properties ?? {},
|
|
85
|
+
type: 'Feature',
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/* * */
|