-
Notifications
You must be signed in to change notification settings - Fork 3
/
track.js
406 lines (340 loc) · 13.3 KB
/
track.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
"use strict"
export {
Point,
Path,
TrackFile,
PaintPoint,
PaintCmd,
PaintResult,
paint
};
import * as KML from './kml.js';
import * as GPX from './gpx.js';
import * as WP from './waypoint.js';
class Point
{
constructor(name, wayPoint, description)
{
this.name = name;
this.description = description;
this.wayPoint = wayPoint;
}
}
class Path
{
constructor(name, wayPoints, description)
{
this.name = name;
this.description = description;
this.wayPoints = wayPoints;
}
}
class TrackFile
{
constructor(name)
{
this.name = name; // 文档名字
this.description = undefined;
this.points = []; // Point对象数组
this.lines = []; // Path对象数组,没有时间戳
this.tracks = []; // Path对象数组
}
toKMLDocument()
{
let document = new KML.Document(this.name);
document.description = this.description;
const BlueStyle = new KML.Style('BlueStyle','A0FF0000',5);
const GreenStyle = new KML.Style('GreenStyle','D032FF30',5);
document.styles=[BlueStyle, GreenStyle];
const AltitudeMode = KML.AltitudeMode.Absolute;
// WP.WayPoint转KML坐标
const wp2KmlGxCoord = wp => new KML.GxCoord(wp.lat, wp.lon, wp.altitude);
const wp2KmlCoordinate = wp => new KML.Coordinate(wp.lat, wp.lon, wp.altitude);
// 点位
if(1==this.points.length){
const point = this.points[0];
let placeMark = new KML.PlaceMark(point.name);
placeMark.description = point.description;
placeMark.point = new KML.Point(wp2KmlCoordinate(point.wayPoint), AltitudeMode);
document.placeMarks.push(placeMark);
}else if(1<this.points.length){
let folder= new KML.Folder('Points');
folder.placeMarks = this.points.map(point => {
let placeMark = new KML.PlaceMark(point.name);
placeMark.description = point.description;
placeMark.point = new KML.Point(wp2KmlCoordinate(point.wayPoint), AltitudeMode);
return placeMark;
});
document.folders.push(folder);
}
// 线条
if(1==this.lines.length){
const path = this.lines[0];
let placeMark = new KML.PlaceMark(path.name);
placeMark.description = path.description;
placeMark.lineString = new KML.LineString(new KML.Coordinates(path.wayPoints.map(wp => wp2KmlCoordinate(wp))), KML.AltitudeMode.ClampToGround);
placeMark.styleId=GreenStyle.id;
document.placeMarks.push(placeMark);
}else if(1<this.lines.length){
let folder= new KML.Folder('Routes');
folder.placeMarks = this.lines.map(path => {
let placeMark = new KML.PlaceMark(path.name);
placeMark.description = path.description;
placeMark.lineString = new KML.LineString(new KML.Coordinates(path.wayPoints.map(wp => wp2KmlCoordinate(wp))), KML.AltitudeMode.ClampToGround);
placeMark.styleId=GreenStyle.id;
return placeMark;
});
document.folders.push(folder);
}
// 轨迹
if(1==this.tracks.length){
const path = this.tracks[0];
let placeMark = new KML.PlaceMark(path.name);
placeMark.description = path.description;
placeMark.gxTrack = new KML.GxTrack(AltitudeMode);
path.wayPoints.forEach(wp => { placeMark.gxTrack.append(new KML.When(wp.timestamp), wp2KmlGxCoord(wp)); });
placeMark.styleId=BlueStyle.id;
document.placeMarks.push(placeMark);
}else if(1<this.tracks.length){
let folder= new KML.Folder('Tracks');
folder.placeMarks = this.tracks.map(path => {
let placeMark = new KML.PlaceMark(path.name);
placeMark.description = path.description;
placeMark.gxTrack = new KML.GxTrack(AltitudeMode);
path.wayPoints.forEach(wp => { placeMark.gxTrack.append(new KML.When(wp.timestamp), wp2KmlGxCoord(wp)); });
placeMark.styleId=BlueStyle.id;
return placeMark;
});
document.folders.push(folder);
}
return document;
}
static fromKMLDocument(document)
{
if(undefined == document)
return undefined;
let ret = new TrackFile(document.name);
ret.description = document.description;
// KML坐标转WP.WayPoint
const kmlCoord2Wp = c => new WP.WayPoint(c.lat, c.lon, undefined, c.altitude);
const gxTrackToWayPoints = gxTrack => {
const timestamps = gxTrack.whenArray.map(w => w.timestamp);
let wayPoints = gxTrack.gxCoordArray.map(g => kmlCoord2Wp(g));
const len = Math.min(timestamps.length, wayPoints.length);
for(let i=0;i<len;++i)
wayPoints[i].timestamp=timestamps[i];
return wayPoints;
}
const parsePlaceMarks = placeMarks => {
placeMarks.forEach(placeMark => {
if(undefined != placeMark.point)
ret.points.push(new Point(placeMark.name, kmlCoord2Wp(placeMark.point.coordinate), placeMark.description));
if(undefined != placeMark.gxTrack)
ret.tracks.push(new Path(placeMark.name, gxTrackToWayPoints(placeMark.gxTrack), placeMark.description));
if(undefined != placeMark.gxMultiTrack){
// TODO: KML中MultiTrack是否要对应GPX的中的trkseg字段
let wayPoints = [];
placeMark.gxMultiTrack.gxTracks.forEach(gxTrack => {
wayPoints = wayPoints.concat(gxTrackToWayPoints(gxTrack))
});
ret.tracks.push(new Path(placeMark.name, wayPoints, placeMark.description));
}
if(undefined != placeMark.lineString)
ret.lines.push(new Path(placeMark.name,
placeMark.lineString.coordinates.coordinateArray.map(c => kmlCoord2Wp(c)),
placeMark.description));
});
}
parsePlaceMarks(document.placeMarks);
if(undefined!=document.folders){
document.folders.forEach(folder => {
parsePlaceMarks(folder.placeMarks);
});
}
return ret;
}
toGPXDocument()
{
let document = new GPX.Document(this.name);
document.description = this.description;
// WP.WayPoint转GPX.Wpt
const wp2GpxWpt = (wp,name,description) => new GPX.Wpt(name, wp.lat, wp.lon, wp.altitude, wp.timestamp, description);
document.wpts=this.points.map(point => wp2GpxWpt(point.wayPoint, point.name, point.description));
document.rtes=this.lines.map(path => new GPX.Rte(path.name, path.wayPoints.map(wp => wp2GpxWpt(wp)), path.description));
document.trks=this.tracks.map(path => new GPX.Trk(path.name, [new GPX.Trkseg(path.wayPoints.map(wp => wp2GpxWpt(wp)))], path.description));
return document;
}
static fromGPXDocument(document)
{
if(undefined == document)
return undefined;
let ret = new TrackFile(document.name);
ret.description = document.description;
// GPX.Wpt转WP.WayPoint
const gpxWpt2Wp = wpt => new WP.WayPoint(wpt.lat, wpt.lon, wpt.timestamp, wpt.altitude);
ret.points=document.wpts.map(wpt => new Point(wpt.name, gpxWpt2Wp(wpt), wpt.description));
ret.lines=document.rtes.map(rte => new Path(rte.name, rte.rtepts.map(rtept => gpxWpt2Wp(rtept)), rte.description));
ret.tracks=document.trks.map(trk => {
let wayPoints = [];
trk.trksegs.forEach(trkseg => {
trkseg.trkpts.forEach(trkpt => {
wayPoints.push(gpxWpt2Wp(trkpt));
});
});
return new Path(trk.name, wayPoints, trk.description);
});
return ret;
}
}
class Rect
{
constructor(x1,y1,x2,y2)
{
this.topLeftX = undefined == x1 ? Number.MAX_SAFE_INTEGER : x1;
this.topLeftY = undefined == y1 ? Number.MAX_SAFE_INTEGER : y1;
this.bottomRightX = undefined == x2 ? Number.MIN_SAFE_INTEGER : x2;
this.bottomRightY = undefined == y2 ? Number.MIN_SAFE_INTEGER : y2;
}
isValid()
{
return Number.MAX_SAFE_INTEGER!=this.topLeftX && Number.MAX_SAFE_INTEGER!=this.topLeftY && Number.MIN_SAFE_INTEGER!=this.bottomRightX && Number.MIN_SAFE_INTEGER!=this.bottomRightY;
}
center()
{
return [(this.topLeftX+this.bottomRightX )/2, (this.topLeftY+this.bottomRightY)/2];
}
width() {
return Math.abs(this.bottomRightX - this.topLeftX);
}
height(){
return Math.abs(this.bottomRightY - this.topLeftY);
}
scale(f)
{
this.topLeftX *= f;
this.topLeftY *= f;
this.bottomRightX *= f;
this.bottomRightY *= f;
}
merge(other)
{
this.topLeftX = Math.min(this.topLeftX, other.topLeftX);
this.topLeftY = Math.min(this.topLeftY, other.topLeftY);
this.bottomRightX = Math.max(this.bottomRightX, other.bottomRightX);
this.bottomRightY = Math.max(this.bottomRightY, other.bottomRightY);
}
static fromPoints(xyArray)
{
const MaxMinValue = arr => [Math.max.apply(null, arr), Math.min.apply(null, arr) ];
const X = MaxMinValue(xyArray.map(a => a[0]));
const Y = MaxMinValue(xyArray.map(a => a[1]));
return new Rect(X[1],Y[1],X[0],Y[0]);
}
}
const PaintCmd = {
TrackStart:0,
TrackPoint:1,
TrackEnd:2
};
class PaintPoint {
constructor(x, y, cmd) {
this.x = x;
this.y = y;
this.cmd = cmd; // PaintCmd
}
}
class PaintResult {
constructor(paintPoints=[], horizontalDistance=0, verticalDistance=0)
{
this.points = paintPoints;
this.horizontalDistance = horizontalDistance; // 画布的实际距离(米)
this.verticalDistance = verticalDistance;
this.topLeft = undefined; // PaintPoint对象,指示这些点位构成的Rect左上角
this.bottomRight = undefined;
}
}
// 简单比较两数是否相等
const fuzzyCompare = (a,b) => Math.abs(a-b) < 0.01;
/**
* 将多个轨迹打印到长宽(像素)的图片上
*
* @param {array} paths 多个路径,Path对象数组
* @param {int} width 图片长度
* @param {int} height 图片高度
* @return {PaintResult}
*/
function paint(paths, width, height) {
if(!width || !height)
return undefined; // invalid
let rect = new Rect();
let XY = [];
paths.forEach(path => {
let xy = path.wayPoints.map(wp => wp.toXY());
xy = xy.map(p => [p[0], -p[1]]); // 反转Y(屏幕坐标系Y方向不一致)
XY.push(xy);
rect.merge(Rect.fromPoints(xy));
});
let ret = new PaintResult();
if(!rect.isValid())
return ret; // invalid
let RWidth = rect.width();
let RHeight = rect.height();
const RWidthFact=RWidth; // 实际轨迹边框尺寸
const RHeightFact=RHeight;
{
// 将源点位平移到中点,然后归一化到[-0.5,0.5]
const center = rect.center();
XY = XY.map(xy =>
xy.map(p => [
(p[0] - center[0]) / RWidth,
(p[1] - center[1]) / RHeight
])
);
}
// 比较浮点数大小,注意这里比较大小不能直接使用a>b这种
// 两个相近的数可以满足a>b,如2.000001与2.0,这种情况下算出的scale永远是接近1的值,卡死在do循环中
// 也可以试另一种更简单的方法:最多缩放5次,就强制跳出do-while语句
const Epsilon = 0.001;
// 缩放rect直至适合窗口大小
do {
let changed = true;
if (width - RWidth > Epsilon && height - RHeight > Epsilon) {
rect.scale(width / RWidth); // 若rect比画布小,先任意调整,会继续循环体
} else if (RWidth - width > Epsilon)
rect.scale(width / RWidth);
else if (RHeight - height > Epsilon)
rect.scale(height / RHeight);
else
changed = false;
if (changed) {
RWidth = rect.width();
RHeight = rect.height();
} else
break; // finish
} while (1);
//console.log('fit rect to w=' + RWidth + ', h=' + RHeight);
if(fuzzyCompare(RWidth, width)){
ret.horizontalDistance=RWidthFact;
ret.verticalDistance=RWidthFact*(height/width);
}else{
ret.verticalDistance=RHeightFact;
ret.horizontalDistance=RHeightFact*(width/height);
}
const PicCenterX = width / 2;
const PicCenterY = height / 2;
XY.forEach(xy => {
const paintPoints = xy.map(p => new PaintPoint(
p[0] * RWidth + PicCenterX,
p[1] * RHeight + PicCenterY,
PaintCmd.TrackPoint
));
const p1=paintPoints[0];
const p2=paintPoints[paintPoints.length-1];
p1.cmd=PaintCmd.TrackStart;
p2.cmd=PaintCmd.TrackEnd;
ret.points = ret.points.concat(paintPoints);
});
ret.topLeft = new PaintPoint(-0.5*RWidth + PicCenterX, -0.5*RHeight + PicCenterY);
ret.bottomRight = new PaintPoint(0.5*RWidth + PicCenterX, 0.5*RHeight + PicCenterY);
return ret;
}