Repository navigation
Expand file tree
/
Copy pathModel.js
More file actions
225 lines (213 loc) · 9.94 KB
/
Copy pathModel.js
File metadata and controls
225 lines (213 loc) · 9.94 KB
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
// All chart coordinates share the same margin; the outer space holds compass labels.
// Reconcile snapshots by MMSI so Qt keeps existing delegates and Canvas textures.
// Sorting moves rows; only new/expired vessels create or destroy delegates.
function syncShips(model, ships) {
var wanted = {};
ships.forEach(function(ship) { wanted[ship.mmsi] = true; });
for (var i = model.count - 1; i >= 0; i--) {
if (!wanted[model.get(i).mmsi]) model.remove(i);
}
ships.forEach(function(ship, index) {
var position = index;
while (position < model.count && model.get(position).mmsi !== ship.mmsi) position++;
var signature = JSON.stringify(ship);
if (position === model.count) {
model.insert(index, {mmsi: ship.mmsi, ship: ship, signature: signature});
} else {
if (position !== index) model.move(position, index, 1);
if (model.get(index).signature !== signature) {
model.setProperty(index, "ship", ship);
model.setProperty(index, "signature", signature);
}
}
});
}
function radarRadius(size) { return Math.max(1, size / 2 - 24); }
// Low AIS speed means stationary, not necessarily moored or anchored.
function motion(ship) {
if (typeof ship.speed !== "number" || !isFinite(ship.speed) || ship.speed < 0) return "unknown";
return ship.speed < 0.5 ? "stationary" : "moving";
}
function inView(position, size, margin) {
// Absorb floating-point rounding at the range ring, especially at high zoom.
return Math.hypot(position.x - size / 2, position.y - size / 2) <= radarRadius(size) - (margin || 0) + 0.000001;
}
// Backend timestamps use seconds; QML Date.now() uses milliseconds.
function age(seconds, now) {
if (typeof seconds !== "number" || !isFinite(seconds)) return "No signal yet";
var elapsed = Math.max(0, Math.floor(now / 1000 - seconds));
if (elapsed < 60) return elapsed + "s ago";
return Math.floor(elapsed / 60) + "m ago";
}
// Round the bearing to the nearest of eight compass directions.
function compass(bearing) {
return ["N", "NE", "E", "SE", "S", "SW", "W", "NW"][Math.round(bearing / 45) % 8];
}
// Retain nautical miles internally and convert only the displayed label.
function distance(nm, unit) {
// One nautical mile is 1,852 metres; a statute mile is 1,609.344 metres.
if (unit === "km") return (nm * 1.852).toFixed(1) + " km";
if (unit === "mi") return (nm * 1852 / 1609.344).toFixed(1) + " mi";
return nm.toFixed(1) + " nm";
}
// Project bearing/range into screen pixels. center is a pixel offset from the
// observer, not the normalized geographic center used to constrain panning.
function point(ship, size, radius, center) {
var angle = ship.bearing * Math.PI / 180;
var r = (ship.distance / radius) * radarRadius(size);
// Canvas Y grows downwards, so north needs a negative cosine offset.
center = center || {x: 0, y: 0};
return {x: size / 2 + Math.sin(angle) * r - center.x, y: size / 2 - Math.cos(angle) * r - center.y};
}
// Labels use the same normalized coordinates as land and the same zoom as ships.
// Prefer larger settlements, reject collisions, and keep every text box inside
// the circular chart. Widths come from Canvas text metrics, not guessed glyphs.
function cityLabels(cities, size, zoom, occupied) {
var mid = size / 2, r = radarRadius(size);
var boxes = occupied.slice(), labels = [];
function overlaps(a, b) {
return a.x < b.x + b.width + 4 && a.x + a.width + 4 > b.x &&
a.y < b.y + b.height + 3 && a.y + a.height + 3 > b.y;
}
function inside(box) {
return [[box.x, box.y], [box.x + box.width, box.y],
[box.x, box.y + box.height], [box.x + box.width, box.y + box.height]].every(function(p) {
return Math.pow(p[0] - mid, 2) + Math.pow(p[1] - mid, 2) < Math.pow(r - 3, 2);
});
}
cities.forEach(function(city) {
if (labels.length >= 10 || Math.hypot(city.x, city.y) * zoom > 1) return;
var x = mid + city.x * zoom * r, y = mid + city.y * zoom * r;
var w = city.textWidth + 4, h = 14;
var choices = [{x: x + 5, y: y - 17, width: w, height: h},
{x: x - w - 5, y: y - 17, width: w, height: h},
{x: x + 5, y: y + 4, width: w, height: h},
{x: x - w - 5, y: y + 4, width: w, height: h},
{x: x + 5, y: y - 32, width: w, height: h},
{x: x - w - 5, y: y - 32, width: w, height: h}];
for (var i = 0; i < choices.length; i++) {
var box = choices[i];
if (inside(box) && !boxes.some(function(other) { return overlaps(box, other); })) {
boxes.push(box);
labels.push({name: city.name, x: box.x + 2, y: box.y + 10, dotX: x, dotY: y});
break;
}
}
});
return labels;
}
// Keep a 16-pixel click radius around each tiny mark. Distance, not draw order,
// resolves overlapping targets; contacts outside the current view are ignored.
function closestContact(ships, x, y, size, radius, center) {
var nearest = null, best = 16 * 16;
ships.forEach(function(ship) {
var p = point(ship, size, radius, center);
if (!inView(p, size)) return;
var squared = Math.pow(x - p.x, 2) + Math.pow(y - p.y, 2);
if (squared <= best && (nearest === null || squared < best)) {
best = squared; nearest = ship;
}
});
return nearest;
}
// The entire viewport must remain inside the observer's loaded coverage circle.
// Centers use coverage-radius units, independent of zoom and widget size.
function boundedCenter(x, y, zoom) {
var limit = Math.max(0, 1 - 1 / zoom), length = Math.hypot(x, y);
var scale = length > limit && length > 0 ? limit / length : 1;
return {x: x * scale, y: y * scale};
}
// A vessel's position in coverage-radius units, for explicit list-to-map navigation.
function shipCenter(ship, coverageRadius) {
var angle = ship.bearing * Math.PI / 180;
var range = ship.distance / coverageRadius;
return {x: Math.sin(angle) * range, y: -Math.cos(angle) * range};
}
// VesselFinder detail pages use IMO; MMSI search also covers vessels without one.
function vesselUrl(ship) {
if (!ship) return "";
if (/^[1-9][0-9]{6}$/.test(String(ship.imo)))
return "https://www.vesselfinder.com/vessels/details/" + ship.imo;
if (/^[0-9]{9}$/.test(String(ship.mmsi)))
return "https://www.vesselfinder.com/vessels?name=" + ship.mmsi;
return "";
}
// Course over ground orients a moving marker; true heading is the fallback.
// Returns null when neither is reported, so the symbol keeps a neutral pose.
function orientation(ship) {
function valid(value) { return typeof value === "number" && isFinite(value) && value >= 0 && value < 360; }
if (valid(ship.course)) return ship.course;
if (valid(ship.heading)) return ship.heading;
return null;
}
// The recent wake in screen pixels, oldest first, ending at the current position.
function trackPoints(ship, size, radius, center) {
if (!ship || !Array.isArray(ship.track)) return [];
var points = ship.track.filter(function(p) {
return Array.isArray(p) && typeof p[0] === "number" && isFinite(p[0])
&& typeof p[1] === "number" && isFinite(p[1]);
}).map(function(p) {
return point({distance: p[0], bearing: p[1]}, size, radius, center);
});
if (points.length) points.push(point(ship, size, radius, center));
return points;
}
// Hull size in metres; either dimension may be missing.
function hullSize(ship) {
if (!ship || (!ship.length && !ship.beam)) return "—";
if (ship.length && ship.beam) return ship.length + " × " + ship.beam + " m";
return (ship.length ? ship.length + " m long" : ship.beam + " m wide");
}
// Type groups in legend and shortcut order (keys 1–6). Hues name theme colors,
// following common chart-plotter conventions; "other" uses the foreground.
var typeGroups = [
{key: "cargo", label: "CARGO", hue: "green"},
{key: "tanker", label: "TANKER", hue: "red"},
{key: "passenger", label: "PASSENGER", hue: "blue"},
{key: "fishing", label: "FISHING", hue: "orange"},
{key: "pleasure", label: "PLEASURE", hue: "magenta"},
{key: "other", label: "OTHER", hue: ""}
];
function typeGroup(type) {
switch (type) {
case "Cargo": return "cargo";
case "Tanker": return "tanker";
case "Passenger": return "passenger";
case "Fishing": return "fishing";
case "Sailing": case "Pleasure craft": return "pleasure";
}
return "other";
}
function filterShips(ships, hidden) {
return ships.filter(function(ship) { return !hidden[typeGroup(ship.type)]; });
}
function typeCounts(ships) {
var counts = {};
ships.forEach(function(ship) {
var key = typeGroup(ship.type);
counts[key] = (counts[key] || 0) + 1;
});
return counts;
}
// Read hues from the theme's colors.toml: named keys first, then the matching
// ANSI slot. Orange falls back to yellow; a missing hue returns "".
function themeHues(text) {
var values = {};
String(text || "").split("\n").forEach(function(line) {
var match = line.match(/^\s*([A-Za-z0-9_]+)\s*=\s*["']?(#[0-9A-Fa-f]{6})(?![0-9A-Fa-f])/);
if (match) values[match[1]] = match[2];
});
var slots = {red: "color1", green: "color2", yellow: "color3", blue: "color4", magenta: "color5", cyan: "color6"};
var hues = {};
Object.keys(slots).concat(["orange"]).forEach(function(name) {
hues[name] = values[name] || values[slots[name]] || "";
});
if (!hues.orange) hues.orange = hues.yellow;
return hues;
}
// Identify the monitored location; empty while a report has no valid position.
function observerKey(report) {
if (!report || typeof report.latitude !== "number" || typeof report.longitude !== "number"
|| !isFinite(report.latitude) || !isFinite(report.longitude)) return "";
return report.latitude.toFixed(5) + "," + report.longitude.toFixed(5);
}