-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeometry.js
108 lines (91 loc) · 2.09 KB
/
geometry.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
const simplifyJs = require("simplify-js");
/**
* Convert array of [lat,lng] to array of {x,y}
* @alias hm:coords2XY
* @param {array} coords array of [lat,lng]
* @return {array}array of {x,y}
*/
function coords2XY(coords) {
return coords.map(coord => {
return {
x: coord[1],
y: coord[0]
};
});
}
/**
* Convert array of {x,y} to array of [lat,lng]
* @alias hm:xy2Coords
* @param {array} coords array of {x,y}
* @return {array}array of [lat,lng]
*/
function xy2Coords(xys) {
return xys.map(xy => [xy.y, xy.x]);
}
/**
* Convert an object {lat,lng} to [lat,lng]
* @alias hm:coordO2A
* @param {object} obj {lat,lng}
* @return {array} [lat,lng]
*/
function coordO2A(obj) {
return [obj.lat, obj.lng];
}
/**
* Convert an array [lat,lng] to {lat,lng}
* @alias hm:coordA2O
* @param {object} arr [lat,lng]
* @return {array} {lat,lng}
*/
function coordA2O(arr) {
return {
lat: arr[0],
lng: arr[1]
};
}
/**
* Convert [lat,lng] to {lat,lng}
* @alias hm:coord2Point
* @param {array} [lat,lng]
* @return {array}{lat,lng}
*/
function coord2Point(coord) {
return {
lat: coord[0],
lng: coord[1]
};
}
/**
* Convert {lat,lng} to [lat,lng]
* @alias hm:point2Coord
* @param {array} {lat,lng}
* @return {array}[lat,lng]
*/
function point2Coord(point) {
return [point.lat, point.lng];
}
/**
* Simplify a polyline by using the Ramer-Douglas-Peucker algorithm
* @alias hm:simplify
* @param {array} coords array of [lat,lng]
* @param {number} tolerance
* @return {array} simplified polyline
*/
function simplify(coords, tolerance, highacc = false) {
// convert to xy
let xy = coords2XY(coords);
let simplified = simplifyJs(xy, tolerance, highacc);
if (simplified.length < 1) // not enough points
return coords;
// convert back to [lat,lng]
return xy2Coords(simplified);
}
module.exports = {
coordO2A: coordO2A,
coordA2O: coordA2O,
coords2XY: coords2XY,
xy2Coords: xy2Coords,
coord2Point: coord2Point,
point2Coord: point2Coord,
simplify: simplify
};