-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdp_geo.js
191 lines (164 loc) · 5.7 KB
/
rdp_geo.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
var simplifyGeoPath = function (points, tolerance) {
var R = 6367444.6571225;
var GeoLine = function (p1, p2) {
this.A = p1;
this.B = p2;
this.distanceToPoint = function (point) {
var A = new Vector(this.A),
B = new Vector(this.B),
C = new Vector(point);
var G = A.Product(B);
var F = C.Product(G);
var T = G.Product(F);
T = T.getNormal().multiplyByScalar(R).getPoint();
return T.geoDistanceToPoint(point);
};
};
var Line = function (p1, p2) {
this.A = new GeoPoint(p1);
this.B = new GeoPoint(p2);
};
Line.prototype = GeoLine;
var Point = function () {
this.cartesian = false;
this.geo = false;
this.setGeo = function (p) {
this.lat = p[0];
this.lon = p[1];
this.geo = true;
this.cartesian = false;
};
this.setCartesian = function (p) {
this.x = p[0];
this.y = p[1];
this.z = p[2];
this.cartesian = true;
this.geo = false;
};
this.getCartesian = function () {
if (!this.cartesian) {
this.x = R * Math.cos(this.deg2rad(this.lat)) * Math.cos(this.deg2rad(this.lon));
this.y = R * Math.cos(this.deg2rad(this.lat)) * Math.sin(this.deg2rad(this.lon));
this.z = R * Math.sin(this.deg2rad(this.lat));
this.cartesian = true;
}
return this;
};
this.getGeo = function () {
if (!this.geo) {
this.lat = this.rad2deg(Math.asin(this.z / R));
this.lon = this.rad2deg(Math.atan2(this.y, this.x));
this.geo = true;
}
return this;
};
this.geoDistanceToPoint = function (point) {
this.getGeo();
point.getGeo();
return R * 2 *
Math.asin(
Math.sqrt(
this.pow(Math.sin(this.deg2rad(Math.abs(point.lat - this.lat) / 2)), 2) +
Math.cos(point.lat) *
Math.cos(this.lat) *
this.pow(Math.sin(this.deg2rad(Math.abs(point.lon - this.lon) / 2)), 2)
)
);
};
this.deg2rad = function (a) {
return a / 180 * Math.PI;
};
this.rad2deg = function (a) {
return a * 180 / Math.PI;
};
};
var Vector = function (point) {
this.point = point;
this.Product = function (point) {
var A = this.point.getCartesian();
var B = point.getCartesian();
//i(y1z2 - z1y2) - j(x1z2 - z1x2) + k(x1y2 - y1x2)
var c = [
A.y * B.z - A.z * B.y,
0 - (A.x * B.z - A.z * B.x),
A.x * B.y - A.y * B.x
];
this.point.setCartesian(c);
return this;
};
this.getLength = function () {
var A = this.point.getCartesian();
return Math.sqrt(A.x * A.x + A.y * A.y + A.z * A.z);
};
this.getNormal = function () {
var A = this.point.getCartesian();
var length = this.getLength();
var c = [
A.x / length,
A.y / length,
A.z / length
];
this.point.setCartesian(c);
return this;
};
this.multiplyByScalar = function (sc) {
var A = this.point.getCartesian();
var c = [
A.x * sc,
A.y * sc,
A.z * sc
];
this.point.setCartesian(c);
return this;
};
this.getPoint = function () {
return this.point;
};
};
var GeoPoint = function (p) {
this.setGeo(p);
};
GeoPoint.prototype = Point;
var CartesianPoint = function (p) {
this.setCartesian(p);
};
CartesianPoint.prototype = Point;
var douglasPeucker = function (points, tolerance) {
if (points.length <= 2) {
return [points[0]];
}
var returnPoints = [],
// make line from start to end
line = new Line(points[0], points[points.length - 1]),
// find the largest distance from intermediate points to this line
maxDistance = 0,
maxDistanceIndex = 0,
p;
for (var i = 1; i <= points.length - 2; i++) {
var distance = line.distanceToPoint(points[ i ]);
if (distance > maxDistance) {
maxDistance = distance;
maxDistanceIndex = i;
}
}
// check if the max distance is greater than our tolerance allows
if (maxDistance >= tolerance) {
p = points[maxDistanceIndex];
line.distanceToPoint(p, true);
// include this point in the output
returnPoints = returnPoints.concat(douglasPeucker(points.slice(0, maxDistanceIndex + 1), tolerance));
// returnPoints.push( points[maxDistanceIndex] );
returnPoints = returnPoints.concat(douglasPeucker(points.slice(maxDistanceIndex, points.length), tolerance));
} else {
// ditching this point
p = points[maxDistanceIndex];
line.distanceToPoint(p, true);
returnPoints = [points[0]];
}
return returnPoints;
};
var arr = douglasPeucker(points, tolerance);
// always have to push the very last point on so it doesn't get left off
arr.push(points[points.length - 1 ]);
return arr;
};