ol-elevation-profile 0.5.0 → 1.0.0
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/README.md +18 -4
- package/dist/ol-elevation-profile.css +24 -6
- package/dist/ol-elevation-profile.esm.js +488 -57
- package/dist/ol-elevation-profile.esm.min.js +7 -5
- package/dist/ol-elevation-profile.js +488 -57
- package/dist/ol-elevation-profile.min.js +7 -5
- package/package.json +3 -3
- package/src/ol-elevation-profile.css +24 -6
- package/src/ol-elevation-profile.js +488 -57
|
@@ -10,9 +10,11 @@ import * as d3 from 'd3';
|
|
|
10
10
|
/**
|
|
11
11
|
* Synchronized elevation profile control for OpenLayers, rendered with d3.
|
|
12
12
|
*
|
|
13
|
-
* Reads elevation (Z) directly from 3D line geometries (`[lon, lat, z]`)
|
|
14
|
-
*
|
|
15
|
-
*
|
|
13
|
+
* Reads elevation (Z) directly from 3D line geometries (`[lon, lat, z]`). A track without
|
|
14
|
+
* Z is completed from a terrain model - keyless AWS Terrain Tiles by default, which means
|
|
15
|
+
* the control fetches tiles on its own; set `dem: null` to keep it entirely offline.
|
|
16
|
+
* Clicking (or hovering) a track shows its profile; a marker stays synchronized on both
|
|
17
|
+
* the map and the chart.
|
|
16
18
|
*
|
|
17
19
|
* Peer dependencies (provided by the host application, not bundled):
|
|
18
20
|
* - OpenLayers >= 6 (https://openlayers.org/)
|
|
@@ -36,26 +38,26 @@ import * as d3 from 'd3';
|
|
|
36
38
|
const DEFAULTS = {
|
|
37
39
|
immersion: 'docked',
|
|
38
40
|
position: 'bottom',
|
|
39
|
-
width: 520, //
|
|
41
|
+
width: 520, // number (px, capped to the map width) or 'auto'/'100%'/'full' = map width
|
|
40
42
|
height: 180,
|
|
41
43
|
margins: { unit: 'px', top: 20, right: 24, bottom: 30, left: 48 },
|
|
42
44
|
units: 'meters',
|
|
43
45
|
dataProjection: null,
|
|
46
|
+
dem: 'terrarium', // AWS tiles by default; null = off; or { url, encoding, zoom, maxTiles }
|
|
44
47
|
maxPoints: 2000,
|
|
45
|
-
smoothing: 0, //
|
|
48
|
+
smoothing: 0, // elevation smoothing: window in METRES (0 = none)
|
|
46
49
|
theme: 'steelblue',
|
|
47
|
-
color: null, // null=
|
|
50
|
+
color: null, // null = theme, 'auto' = track colour, or a CSS colour
|
|
48
51
|
trackLayer: null,
|
|
49
52
|
transparency: false, // false | true | nombre 0..1
|
|
50
53
|
transparencyLevel: 0.45,
|
|
51
54
|
grid: true,
|
|
52
55
|
slope: false,
|
|
53
56
|
slopeClassSize: 2.5,
|
|
54
|
-
slopeColors: null, // null =
|
|
55
|
-
slopeSeparators: true, //
|
|
57
|
+
slopeColors: null, // null = blue->red ramp (HSL); otherwise an interpolated array
|
|
58
|
+
slopeSeparators: true, // vertical line at each class change
|
|
56
59
|
slopeLegend: true,
|
|
57
|
-
maxClasses: 8, //
|
|
58
|
-
maxLegendClasses: 8,
|
|
60
|
+
maxClasses: 8, // maximum number of slope classes (colours + legend)
|
|
59
61
|
xTicks: null,
|
|
60
62
|
yTicks: null,
|
|
61
63
|
show: 'click',
|
|
@@ -64,9 +66,11 @@ import * as d3 from 'd3';
|
|
|
64
66
|
followMap: true,
|
|
65
67
|
marker: true,
|
|
66
68
|
hideOnMapClick: true,
|
|
67
|
-
responsive: true, //
|
|
68
|
-
mobileBreakpoint: 640, // <=
|
|
69
|
-
zoom: false, //
|
|
69
|
+
responsive: true, // adapts width/placement, mobile included
|
|
70
|
+
mobileBreakpoint: 640, // <= screen width -> mobile mode (100% width, top/bottom)
|
|
71
|
+
zoom: false, // start/end buttons cropping map + profile to A..B
|
|
72
|
+
ignoreStops: true, // duration = moving time (stops excluded)
|
|
73
|
+
stopSpeed: 0.5, // stop threshold in m/s (~1.8 km/h)
|
|
70
74
|
tooltipItems: ['distance', 'elevation'],
|
|
71
75
|
headerItems: ['distance', 'ascent', 'descent', 'minmax'],
|
|
72
76
|
titleProperty: 'name',
|
|
@@ -74,7 +78,10 @@ import * as d3 from 'd3';
|
|
|
74
78
|
labels: {
|
|
75
79
|
distance: 'Distance', elevation: 'Altitude', slope: 'Pente',
|
|
76
80
|
ascent: 'D+', descent: 'D-', empty: 'Cliquez un tracé',
|
|
77
|
-
|
|
81
|
+
time: 'Temps', duration: 'Durée',
|
|
82
|
+
durationUnits: { s: 'sec', m: 'min', h: 'h', d: 'j' },
|
|
83
|
+
zoomStart: 'Définir le début (A)', zoomEnd: 'Définir la fin (B)', zoomAll: 'Tout voir',
|
|
84
|
+
loading: 'Chargement du profil altimétrique'
|
|
78
85
|
}
|
|
79
86
|
};
|
|
80
87
|
|
|
@@ -91,6 +98,59 @@ import * as d3 from 'd3';
|
|
|
91
98
|
const esc = (s) => String(s).replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]));
|
|
92
99
|
const isUrl = (v) => typeof v === 'string' && /^(https?:)?\/\/|^mailto:/i.test(v);
|
|
93
100
|
|
|
101
|
+
/**
|
|
102
|
+
* Coordinate segments to profile, whatever the geometry type: an array of point arrays.
|
|
103
|
+
*
|
|
104
|
+
* A polygon is profiled along its **outer ring only**; the holes are not part of the
|
|
105
|
+
* outline itself. The ring being closed, the profile returns to its starting point -
|
|
106
|
+
* that is the outline, faithfully, not a defect.
|
|
107
|
+
*
|
|
108
|
+
* One place for the five that used to test `getType()` themselves: they all wrapped
|
|
109
|
+
* `getCoordinates()` in one extra array unless it was a MultiLineString, which silently
|
|
110
|
+
* made a polygon iterate over its rings as if they were points.
|
|
111
|
+
*/
|
|
112
|
+
function geomLines(geom) {
|
|
113
|
+
if (!geom || !geom.getCoordinates) return [];
|
|
114
|
+
const c = geom.getCoordinates();
|
|
115
|
+
switch (geom.getType()) {
|
|
116
|
+
case 'LineString':
|
|
117
|
+
case 'LinearRing': return [c];
|
|
118
|
+
case 'MultiLineString': return c;
|
|
119
|
+
case 'Polygon': return c.length ? [c[0]] : [];
|
|
120
|
+
case 'MultiPolygon': return c.map((poly) => poly[0]).filter(Boolean);
|
|
121
|
+
default: return [];
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Whether a geometry can be profiled at all - the map-selection filter and nothing more. */
|
|
126
|
+
function isProfilable(geom) {
|
|
127
|
+
return !!geom && /^(Multi)?(LineString|Polygon)$|^LinearRing$/.test(geom.getType());
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Flat array of timestamps (epoch ms) aligned on the flat coordinate order, read from
|
|
131
|
+
// properties.coordTimes (ISO or number), coordinateProperties.times, or the 4th (M) dimension.
|
|
132
|
+
function extractTimes(feature, lines) {
|
|
133
|
+
const props = (feature && feature.getProperties) ? feature.getProperties() : {};
|
|
134
|
+
let raw = props.coordTimes;
|
|
135
|
+
if (raw == null && props.coordinateProperties) raw = props.coordinateProperties.times || props.coordinateProperties.coordTimes;
|
|
136
|
+
let flat = null;
|
|
137
|
+
if (Array.isArray(raw)) flat = Array.isArray(raw[0]) ? raw.reduce((a, b) => a.concat(b), []) : raw.slice();
|
|
138
|
+
if (!flat && lines) { // fallback: 4th M dimension (XYZM layout)
|
|
139
|
+
const tmp = []; let any = false;
|
|
140
|
+
for (const seg of lines) for (const c of seg) { const v = c.length > 3 ? c[3] : null; tmp.push(v); if (v != null && isFinite(v)) any = true; }
|
|
141
|
+
flat = any ? tmp : null;
|
|
142
|
+
}
|
|
143
|
+
if (!flat) return null;
|
|
144
|
+
let valid = 0;
|
|
145
|
+
const ms = flat.map((v) => {
|
|
146
|
+
if (v == null || v === '') return null;
|
|
147
|
+
const t = (typeof v === 'number') ? (v > 1e12 ? v : v * 1000) : Date.parse(v); // epoch ms / epoch s / ISO
|
|
148
|
+
if (!isFinite(t)) return null;
|
|
149
|
+
valid++; return t;
|
|
150
|
+
});
|
|
151
|
+
return valid >= 2 ? ms : null;
|
|
152
|
+
}
|
|
153
|
+
|
|
94
154
|
function colorToCss(c) {
|
|
95
155
|
if (c == null) return null;
|
|
96
156
|
if (typeof c === 'string') return c;
|
|
@@ -128,6 +188,186 @@ import * as d3 from 'd3';
|
|
|
128
188
|
const ICON_B = '<svg viewBox="0 0 24 24"><path d="M17 5v14M13 12H5m0 0 3-3m-3 3 3 3" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>';
|
|
129
189
|
const ICON_ALL = '<svg viewBox="0 0 24 24"><path d="M4 12h16M4 12l4-4M4 12l4 4M20 12l-4-4M20 12l-4 4" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>';
|
|
130
190
|
|
|
191
|
+
// ----- digital elevation model (DEM) -----------------------------------
|
|
192
|
+
/**
|
|
193
|
+
* Known terrain-tile sources.
|
|
194
|
+
*
|
|
195
|
+
* A PNG tile carries elevation in its R/G/B channels; it is read on a canvas rather
|
|
196
|
+
* than queried point by point from an API. That is what makes a 10 000-point track a
|
|
197
|
+
* handful of requests, with no key, no quota and no rate limit — where the free
|
|
198
|
+
* elevation APIs cap out at 100 or 200 points per call.
|
|
199
|
+
*
|
|
200
|
+
* Attribution is not automatic: the library does not own the map. It is up to the
|
|
201
|
+
* application to carry `attributions` in its own basemap source - and since the DEM is
|
|
202
|
+
* on by default, that obligation arrives without having been asked for. `dem: null`
|
|
203
|
+
* turns the whole thing off.
|
|
204
|
+
*/
|
|
205
|
+
const DEM_PRESETS = {
|
|
206
|
+
terrarium: {
|
|
207
|
+
url: 'https://s3.amazonaws.com/elevation-tiles-prod/terrarium/{z}/{x}/{y}.png',
|
|
208
|
+
encoding: 'terrarium',
|
|
209
|
+
maxZoom: 14,
|
|
210
|
+
attributions: 'Elevation: <a href="https://registry.opendata.aws/terrain-tiles/">Terrain Tiles</a> (AWS Open Data)'
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
const DEM_DEFAULTS = { source: 'terrarium', zoom: 'auto', maxZoom: 14, maxTiles: 32, concurrency: 6, tileSize: 256 };
|
|
215
|
+
|
|
216
|
+
/** RGB → metres decoders, one per encoding convention. */
|
|
217
|
+
const DEM_DECODERS = {
|
|
218
|
+
terrarium: (r, g, b) => (r * 256 + g + b / 256) - 32768,
|
|
219
|
+
mapbox: (r, g, b) => -1e4 + (r * 65536 + g * 256 + b) * 0.1
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
/** Web Mercator pixel coordinates at zoom `z`, origin at the top-left corner of the world. */
|
|
223
|
+
function worldPixel(lon, lat, z, tileSize) {
|
|
224
|
+
const n = tileSize * Math.pow(2, z);
|
|
225
|
+
const s = Math.sin(lat * Math.PI / 180);
|
|
226
|
+
return [(lon + 180) / 360 * n, (0.5 - Math.log((1 + s) / (1 - s)) / (4 * Math.PI)) * n];
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Reads elevations from a set of terrain tiles.
|
|
231
|
+
*
|
|
232
|
+
* Positions are held in **world pixels** rather than tile by tile: the four neighbours
|
|
233
|
+
* of a point can fall in two different tiles whenever it runs along an edge, which is
|
|
234
|
+
* the common case on a track. Converting to world pixels first, then resolving the
|
|
235
|
+
* tile, makes the edge case disappear instead of handling it.
|
|
236
|
+
*/
|
|
237
|
+
class DemSampler {
|
|
238
|
+
/** @param {Object} cfg `url`, `encoding`, `tileSize`, `concurrency`. */
|
|
239
|
+
constructor(cfg) {
|
|
240
|
+
this.cfg = cfg;
|
|
241
|
+
this.decode = DEM_DECODERS[cfg.encoding] || DEM_DECODERS.terrarium;
|
|
242
|
+
this.tiles = new Map(); // "z/x/y" -> RGBA pixels, or null if the tile was lost
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* North-west neighbour of the point plus the interpolation fractions, in world pixels.
|
|
247
|
+
*
|
|
248
|
+
* The half-pixel subtracted is not a fudge: pixel centres fall on half-integers, and
|
|
249
|
+
* without that shift `floor()` would return the cell containing the point rather than
|
|
250
|
+
* its western neighbour, offsetting the whole profile by half a pixel.
|
|
251
|
+
*/
|
|
252
|
+
_neighbours(lon, lat, z) {
|
|
253
|
+
const wp = worldPixel(lon, lat, z, this.cfg.tileSize);
|
|
254
|
+
const px = wp[0] - 0.5, py = wp[1] - 0.5;
|
|
255
|
+
const i0 = Math.floor(px), j0 = Math.floor(py);
|
|
256
|
+
return { i0, j0, tx: px - i0, ty: py - j0 };
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** Tile key of a world pixel; x wraps around the globe, y clamps at the poles. */
|
|
260
|
+
_key(i, j, z) {
|
|
261
|
+
const ts = this.cfg.tileSize, span = ts * Math.pow(2, z);
|
|
262
|
+
const jj = Math.min(span - 1, Math.max(0, j)), ii = ((i % span) + span) % span;
|
|
263
|
+
return { key: z + '/' + Math.floor(ii / ts) + '/' + Math.floor(jj / ts), ii, jj };
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** Elevation of one pixel, or null if its tile is missing. */
|
|
267
|
+
_at(i, j, z) {
|
|
268
|
+
const ts = this.cfg.tileSize;
|
|
269
|
+
const r = this._key(i, j, z);
|
|
270
|
+
const data = this.tiles.get(r.key);
|
|
271
|
+
if (!data) return null;
|
|
272
|
+
const o = ((r.jj % ts) * ts + (r.ii % ts)) * 4;
|
|
273
|
+
return this.decode(data[o], data[o + 1], data[o + 2]);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Elevation **bilinearly interpolated** between the four surrounding pixels; null as
|
|
278
|
+
* soon as a single one is missing.
|
|
279
|
+
*
|
|
280
|
+
* Bilinear rather than "the pixel containing the point": on a 30 or 90 m model, taking
|
|
281
|
+
* the pixel value makes the profile advance in stairs, and every stair counts as a
|
|
282
|
+
* climb then a descent in the ascent total. The interpolation invents no relief — it
|
|
283
|
+
* renders the same surface, without the steps of the sampling grid.
|
|
284
|
+
*/
|
|
285
|
+
sample(lon, lat, z) {
|
|
286
|
+
const n = this._neighbours(lon, lat, z);
|
|
287
|
+
const a = this._at(n.i0, n.j0, z), b = this._at(n.i0 + 1, n.j0, z);
|
|
288
|
+
const c = this._at(n.i0, n.j0 + 1, z), d = this._at(n.i0 + 1, n.j0 + 1, z);
|
|
289
|
+
if (a == null || b == null || c == null || d == null) return null;
|
|
290
|
+
const north = a + (b - a) * n.tx, south = c + (d - c) * n.tx;
|
|
291
|
+
return north + (south - north) * n.ty;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/** Keys of the tiles needed by the four neighbours of each of the points. */
|
|
295
|
+
tilesFor(lonlats, z) {
|
|
296
|
+
const keys = new Set();
|
|
297
|
+
for (const ll of lonlats) {
|
|
298
|
+
const n = this._neighbours(ll[0], ll[1], z);
|
|
299
|
+
for (let di = 0; di < 2; di++) for (let dj = 0; dj < 2; dj++) keys.add(this._key(n.i0 + di, n.j0 + dj, z).key);
|
|
300
|
+
}
|
|
301
|
+
return keys;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** Pixels of one tile, or null if it could not be read. */
|
|
305
|
+
_fetch(key) {
|
|
306
|
+
const parts = key.split('/');
|
|
307
|
+
const url = this.cfg.url.replace('{z}', parts[0]).replace('{x}', parts[1]).replace('{y}', parts[2]);
|
|
308
|
+
return new Promise((resolve) => {
|
|
309
|
+
const img = new Image();
|
|
310
|
+
// Without this attribute the canvas is tainted and getImageData throws: the tile
|
|
311
|
+
// would display, but stay unreadable. The intended sources answer with
|
|
312
|
+
// Access-Control-Allow-Origin: *.
|
|
313
|
+
img.crossOrigin = 'anonymous';
|
|
314
|
+
img.onload = () => {
|
|
315
|
+
try {
|
|
316
|
+
const ts = this.cfg.tileSize;
|
|
317
|
+
const cv = document.createElement('canvas');
|
|
318
|
+
cv.width = ts; cv.height = ts;
|
|
319
|
+
const ctx = cv.getContext('2d', { willReadFrequently: true });
|
|
320
|
+
ctx.drawImage(img, 0, 0, ts, ts);
|
|
321
|
+
resolve(ctx.getImageData(0, 0, ts, ts).data);
|
|
322
|
+
} catch (e) { resolve(null); }
|
|
323
|
+
};
|
|
324
|
+
img.onerror = () => resolve(null);
|
|
325
|
+
img.src = url;
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Loads the missing tiles, `concurrency` in flight. Resolves false if one is missing.
|
|
331
|
+
*
|
|
332
|
+
* Lost tiles are remembered as such: without that, a second pass over the same track
|
|
333
|
+
* would fire the same requests, doomed to fail again.
|
|
334
|
+
*/
|
|
335
|
+
load(keys) {
|
|
336
|
+
const todo = Array.from(keys).filter((k) => !this.tiles.has(k));
|
|
337
|
+
let next = 0, ok = true;
|
|
338
|
+
const worker = () => {
|
|
339
|
+
if (next >= todo.length) return Promise.resolve();
|
|
340
|
+
const k = todo[next++];
|
|
341
|
+
return this._fetch(k).then((data) => { this.tiles.set(k, data); if (!data) ok = false; return worker(); });
|
|
342
|
+
};
|
|
343
|
+
const lanes = [];
|
|
344
|
+
for (let i = 0; i < Math.min(this.cfg.concurrency, todo.length); i++) lanes.push(worker());
|
|
345
|
+
return Promise.all(lanes).then(() => ok);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** The `dem` option (true | source name | object) → a full configuration, or null. */
|
|
350
|
+
function demConfig(opt) {
|
|
351
|
+
if (!opt) return null;
|
|
352
|
+
const raw = (opt === true || typeof opt === 'string') ? { source: opt === true ? 'terrarium' : opt } : Object.assign({}, opt);
|
|
353
|
+
const preset = DEM_PRESETS[raw.source] || (raw.url ? {} : DEM_PRESETS.terrarium);
|
|
354
|
+
const cfg = Object.assign({}, DEM_DEFAULTS, preset, raw);
|
|
355
|
+
return cfg.url ? cfg : null;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* Zoom at which to download the model: the finest one that fits within `maxTiles`.
|
|
360
|
+
*
|
|
361
|
+
* It is the tiling that widens as the track grows, not a model that degrades: a 10 km
|
|
362
|
+
* track is read at the finest step available, a 300 km one at a coarser step rather
|
|
363
|
+
* than in three hundred requests.
|
|
364
|
+
*/
|
|
365
|
+
function demZoomFor(sampler, lonlats, cfg) {
|
|
366
|
+
if (typeof cfg.zoom === 'number') return cfg.zoom;
|
|
367
|
+
for (let z = cfg.maxZoom; z > 0; z--) if (sampler.tilesFor(lonlats, z).size <= cfg.maxTiles) return z;
|
|
368
|
+
return 1;
|
|
369
|
+
}
|
|
370
|
+
|
|
131
371
|
// =======================================================================
|
|
132
372
|
/**
|
|
133
373
|
* @typedef {Object} ElevationProfileOptions
|
|
@@ -161,12 +401,15 @@ import * as d3 from 'd3';
|
|
|
161
401
|
* @property {boolean} [responsive=true] Adapt width/placement; mobile included.
|
|
162
402
|
* @property {number} [mobileBreakpoint=640] Below this width: mobile mode (100% width, top/bottom only).
|
|
163
403
|
* @property {boolean} [zoom=false] A/B buttons to crop map + profile to a sub-range.
|
|
164
|
-
* @property {
|
|
165
|
-
* @property {
|
|
404
|
+
* @property {boolean} [ignoreStops=true] When computing time, ignore stopped segments (moving time).
|
|
405
|
+
* @property {number} [stopSpeed=0.5] Speed threshold (m/s) below which a segment counts as a stop.
|
|
406
|
+
* @property {Array<'distance'|'elevation'|'slope'|'time'>} [tooltipItems=['distance','elevation']] Tooltip content (`'time'` = elapsed time at the cursor, if the track has time data).
|
|
407
|
+
* @property {Array<string|{property:string,label?:string,asLink?:boolean,linkText?:string}>} [headerItems] Header content (string tokens: distance, ascent, descent, min, max, minmax, `'duration'` = total elapsed time).
|
|
166
408
|
* @property {string} [titleProperty='name'] Feature property used as the title.
|
|
167
409
|
* @property {?string} [titleLink=null] Feature property holding a URL → clickable title.
|
|
168
410
|
* @property {number} [maxPoints=2000] Decimation for render/interaction (stats use full data).
|
|
169
411
|
* @property {?import('ol/proj/Projection').default|string} [dataProjection=null] Projection of the feature coordinates.
|
|
412
|
+
* @property {?(boolean|string|Object)} [dem='terrarium'] Fill missing elevations from a terrain model. `'terrarium'` (default) or `true` = AWS Terrain Tiles (keyless); `null` disables it; or `{url,encoding:'terrarium'|'mapbox',zoom,maxZoom,maxTiles,tileSize,concurrency}`.
|
|
170
413
|
*/
|
|
171
414
|
|
|
172
415
|
/**
|
|
@@ -194,12 +437,13 @@ import * as d3 from 'd3';
|
|
|
194
437
|
this._marker = null;
|
|
195
438
|
this._collapsed = !!o.collapsed;
|
|
196
439
|
this._cropMode = false; this._zoomA = null; this._zoomB = null; this._armed = null; this._fitRes = null;
|
|
440
|
+
this._demZ = null; this._demFor = null; this._demSeq = 0; this._demLoading = false;
|
|
197
441
|
this._onResize = () => { if (this._feature && !this._collapsed) this._render(); };
|
|
198
442
|
this._buildDom(element);
|
|
199
443
|
element.style.display = 'none';
|
|
200
444
|
}
|
|
201
445
|
|
|
202
|
-
// ----------
|
|
446
|
+
// ---------- static helpers ----------------------------------------
|
|
203
447
|
/**
|
|
204
448
|
* Whether a feature has any Z (elevation) coordinate.
|
|
205
449
|
* @param {import('ol/Feature').default} feature
|
|
@@ -208,11 +452,21 @@ import * as d3 from 'd3';
|
|
|
208
452
|
static featureHasZ(feature) {
|
|
209
453
|
const g = feature && feature.getGeometry && feature.getGeometry();
|
|
210
454
|
if (!g) return false;
|
|
211
|
-
const
|
|
212
|
-
for (const seg of coords) for (const c of seg) if (c.length > 2 && isFinite(c[2])) return true;
|
|
455
|
+
for (const seg of geomLines(g)) for (const c of seg) if (c.length > 2 && isFinite(c[2])) return true;
|
|
213
456
|
return false;
|
|
214
457
|
}
|
|
215
458
|
|
|
459
|
+
/**
|
|
460
|
+
* Whether a feature carries per-point time data (coordTimes / XYZM).
|
|
461
|
+
* @param {import('ol/Feature').default} feature
|
|
462
|
+
* @returns {boolean}
|
|
463
|
+
*/
|
|
464
|
+
static featureHasTime(feature) {
|
|
465
|
+
const g = feature && feature.getGeometry && feature.getGeometry();
|
|
466
|
+
if (!g) return false;
|
|
467
|
+
return !!extractTimes(feature, geomLines(g));
|
|
468
|
+
}
|
|
469
|
+
|
|
216
470
|
// ---------- DOM ---------------------------------------------------
|
|
217
471
|
_buildDom(root) {
|
|
218
472
|
const o = this.options;
|
|
@@ -299,7 +553,7 @@ import * as d3 from 'd3';
|
|
|
299
553
|
this._adjustAttribution();
|
|
300
554
|
}
|
|
301
555
|
|
|
302
|
-
// ---------- responsive
|
|
556
|
+
// ---------- responsive --------------------------------------------
|
|
303
557
|
_availWidth() {
|
|
304
558
|
const map = this.getMap();
|
|
305
559
|
const t = map && map.getTargetElement && map.getTargetElement();
|
|
@@ -315,7 +569,7 @@ import * as d3 from 'd3';
|
|
|
315
569
|
return mobile;
|
|
316
570
|
}
|
|
317
571
|
|
|
318
|
-
//
|
|
572
|
+
// Lift the OL attribution above the profile when the profile takes the bottom-right corner
|
|
319
573
|
_adjustAttribution() {
|
|
320
574
|
const map = this.getMap();
|
|
321
575
|
const target = map && map.getTargetElement && map.getTargetElement();
|
|
@@ -326,17 +580,17 @@ import * as d3 from 'd3';
|
|
|
326
580
|
const mapR = target.getBoundingClientRect();
|
|
327
581
|
const pR = this.element.getBoundingClientRect();
|
|
328
582
|
if (!pR.height) return;
|
|
329
|
-
const bottomGap = mapR.bottom - pR.bottom; //
|
|
583
|
+
const bottomGap = mapR.bottom - pR.bottom; // map bottom edge <-> profile bottom
|
|
330
584
|
const rightGap = mapR.right - pR.right;
|
|
331
|
-
const atBottom = bottomGap < pR.height; //
|
|
332
|
-
const reachesRight = rightGap < 24; //
|
|
585
|
+
const atBottom = bottomGap < pR.height; // profile sits in the bottom band
|
|
586
|
+
const reachesRight = rightGap < 24; // reaches the bottom-right corner (attributions)
|
|
333
587
|
if (atBottom && reachesRight) {
|
|
334
588
|
attr.style.right = `${Math.max(0, Math.round(rightGap))}px`;
|
|
335
|
-
attr.style.bottom = `${Math.round(pR.height + 2 * bottomGap)}px`; //
|
|
589
|
+
attr.style.bottom = `${Math.round(pR.height + 2 * bottomGap)}px`; // same gap above the profile
|
|
336
590
|
}
|
|
337
591
|
}
|
|
338
592
|
|
|
339
|
-
// ----------
|
|
593
|
+
// ---------- map ---------------------------------------------------
|
|
340
594
|
setMap(map) {
|
|
341
595
|
const prev = this.getMap();
|
|
342
596
|
super.setMap(map);
|
|
@@ -355,7 +609,7 @@ import * as d3 from 'd3';
|
|
|
355
609
|
if (typeof window !== 'undefined') window.addEventListener('resize', this._onResize);
|
|
356
610
|
|
|
357
611
|
const lineAt = (pixel) => map.forEachFeatureAtPixel(pixel, (f) => {
|
|
358
|
-
const g = f.getGeometry(); return (g
|
|
612
|
+
const g = f.getGeometry(); return isProfilable(g) ? f : undefined;
|
|
359
613
|
});
|
|
360
614
|
|
|
361
615
|
this._mapKeys = [];
|
|
@@ -365,11 +619,11 @@ import * as d3 from 'd3';
|
|
|
365
619
|
if (o.hideOnMapClick) this._mapKeys.push(map.on('click', (evt) => { if (!lineAt(evt.pixel) && this._feature) this.clear(); }));
|
|
366
620
|
if (o.followMap) this._mapKeys.push(map.on('pointermove', (evt) => {
|
|
367
621
|
if (!this._feature || this._collapsed) return;
|
|
368
|
-
const cp = this.
|
|
622
|
+
const cp = this._closestOnProfile(evt.coordinate); if (!cp) return;
|
|
369
623
|
const px = map.getPixelFromCoordinate(cp); if (!px) return;
|
|
370
624
|
if (Math.hypot(px[0] - evt.pixel[0], px[1] - evt.pixel[1]) < 14) this._focusByCoord(cp); else this._clearFocus();
|
|
371
625
|
}));
|
|
372
|
-
//
|
|
626
|
+
// leave the A/B crop when the map is zoomed out
|
|
373
627
|
this._mapKeys.push(map.on('moveend', () => {
|
|
374
628
|
if (this._cropMode && this._fitRes && map.getView().getResolution() > this._fitRes * 1.25) this._exitZoom();
|
|
375
629
|
}));
|
|
@@ -378,7 +632,8 @@ import * as d3 from 'd3';
|
|
|
378
632
|
|
|
379
633
|
// ---------- API ---------------------------------------------------
|
|
380
634
|
/**
|
|
381
|
-
* Show the profile for the given feature (LineString
|
|
635
|
+
* Show the profile for the given feature (LineString, MultiLineString, or a Polygon /
|
|
636
|
+
* MultiPolygon profiled along its outer ring), ideally 3D.
|
|
382
637
|
* Passing a falsy value hides the control.
|
|
383
638
|
* @param {?import('ol/Feature').default} feature
|
|
384
639
|
* @returns {this}
|
|
@@ -386,17 +641,21 @@ import * as d3 from 'd3';
|
|
|
386
641
|
setFeature(feature) {
|
|
387
642
|
this._feature = feature || null;
|
|
388
643
|
this._cropMode = false; this._zoomA = null; this._zoomB = null; this._armed = null;
|
|
389
|
-
if (!feature) { this._fullSamples = this._samples = null; this._clear(); return this; }
|
|
644
|
+
if (!feature) { this._fullSamples = this._samples = null; this._demZ = this._demFor = null; this._clear(); return this; }
|
|
390
645
|
this.element.style.display = '';
|
|
391
646
|
this._compute();
|
|
647
|
+
// Before the render, not after: it is _fillFromDem that knows whether a fill is
|
|
648
|
+
// starting, and the render needs that to draw the spinner instead of a flat profile.
|
|
649
|
+
this._fillFromDem(feature);
|
|
392
650
|
this._updateZoomButtons();
|
|
393
|
-
if (
|
|
651
|
+
if (this._collapsed) this._renderTitle(); else this._render();
|
|
394
652
|
return this;
|
|
395
653
|
}
|
|
396
654
|
/** Hide the profile and clear the current feature. @returns {this} */
|
|
397
655
|
clear() { return this.setFeature(null); }
|
|
398
656
|
/**
|
|
399
|
-
* @returns {?{distance:number,ascent:number,descent:number,min:number,max:number,maxAbsSlope:number,points:number}}
|
|
657
|
+
* @returns {?{distance:number,duration:?number,ascent:number,descent:number,min:number,max:number,maxAbsSlope:number,points:number}}
|
|
658
|
+
* `duration` is `null` when the track carries no time data.
|
|
400
659
|
*/
|
|
401
660
|
getStats() { return this._stats; }
|
|
402
661
|
/** @param {string|Object} t Theme name or colors object. @returns {this} */
|
|
@@ -417,24 +676,102 @@ import * as d3 from 'd3';
|
|
|
417
676
|
if (patch && 'collapsable' in patch) this._applyCollapsable();
|
|
418
677
|
if (patch && 'zoom' in patch) this._updateZoomButtons();
|
|
419
678
|
if (patch && typeof patch.width !== 'undefined' && typeof patch.width === 'number') this.options.width = patch.width;
|
|
420
|
-
if (
|
|
679
|
+
if (patch && 'dem' in patch) { this._demZ = null; this._demFor = null; }
|
|
680
|
+
if (this._feature) { this._compute(); this._fillFromDem(this._feature); this._updateZoomButtons(); if (this._collapsed) this._renderTitle(); else this._render(); }
|
|
421
681
|
return this;
|
|
422
682
|
}
|
|
423
683
|
|
|
424
|
-
// ----------
|
|
684
|
+
// ---------- digital elevation model -------------------------------
|
|
685
|
+
/**
|
|
686
|
+
* Fills the missing elevations from a terrain model, then redraws.
|
|
687
|
+
*
|
|
688
|
+
* **A track is filled entirely or not at all.** A profile missing a few points is not
|
|
689
|
+
* an incomplete profile: points without Z count as zero, the line dives to sea level
|
|
690
|
+
* and the ascent total becomes absurd. Better the flat profile we would have had
|
|
691
|
+
* without the DEM.
|
|
692
|
+
*
|
|
693
|
+
* Nothing is reported to the user on failure — the fill is a supplement, it has no
|
|
694
|
+
* business breaking a display that succeeded without it. The `demload` event lets the
|
|
695
|
+
* application know if it wants to.
|
|
696
|
+
*
|
|
697
|
+
* @param {import('ol/Feature').default} feature
|
|
698
|
+
* @fires demload
|
|
699
|
+
* @private
|
|
700
|
+
*/
|
|
701
|
+
_fillFromDem(feature) {
|
|
702
|
+
const cfg = demConfig(this.options.dem);
|
|
703
|
+
if (!cfg || !feature || this._demFor === feature) return;
|
|
704
|
+
if (ElevationProfile.featureHasZ(feature)) return; // the track already carries its Z
|
|
705
|
+
if (typeof Image === 'undefined' || typeof document === 'undefined') return;
|
|
706
|
+
|
|
707
|
+
const geom = feature.getGeometry && feature.getGeometry();
|
|
708
|
+
if (!geom) return;
|
|
709
|
+
const lines = geomLines(geom);
|
|
710
|
+
const dataProj = this.options.dataProjection || (this.getMap() && this.getMap().getView().getProjection()) || 'EPSG:3857';
|
|
711
|
+
const lonlats = [];
|
|
712
|
+
for (const seg of lines) for (const c of seg) lonlats.push(toLonLat(c, dataProj));
|
|
713
|
+
if (!lonlats.length) return;
|
|
714
|
+
|
|
715
|
+
// Sequence number: a track clicked while another is loading must win, otherwise the
|
|
716
|
+
// slowest response would overwrite the profile on screen.
|
|
717
|
+
const seq = ++this._demSeq;
|
|
718
|
+
const sampler = new DemSampler(cfg);
|
|
719
|
+
const z = demZoomFor(sampler, lonlats, cfg);
|
|
720
|
+
this._demLoading = true;
|
|
721
|
+
sampler.load(sampler.tilesFor(lonlats, z)).then((ok) => {
|
|
722
|
+
// A newer fill has taken over: it owns _demLoading and will clear it itself.
|
|
723
|
+
if (seq !== this._demSeq || this._feature !== feature) return;
|
|
724
|
+
const zs = ok ? lonlats.map((ll) => sampler.sample(ll[0], ll[1], z)) : null;
|
|
725
|
+
const complete = !!zs && zs.every((v) => v != null && isFinite(v));
|
|
726
|
+
this._demLoading = false;
|
|
727
|
+
if (complete) { this._demZ = zs; this._demFor = feature; this._compute(); }
|
|
728
|
+
// Redrawn even on failure: the spinner has to give way to the flat profile.
|
|
729
|
+
this._updateZoomButtons();
|
|
730
|
+
if (!this._collapsed) this._render();
|
|
731
|
+
/**
|
|
732
|
+
* Fired once a terrain-model fill has completed (or failed).
|
|
733
|
+
* @event demload
|
|
734
|
+
* @property {boolean} ok Whether every point could be sampled.
|
|
735
|
+
* @property {number} zoom Tile zoom level used.
|
|
736
|
+
* @property {number} tiles Tiles fetched.
|
|
737
|
+
*/
|
|
738
|
+
this.dispatchEvent({ type: 'demload', ok: complete, zoom: z, tiles: sampler.tiles.size });
|
|
739
|
+
});
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
// ---------- computation -------------------------------------------
|
|
425
743
|
_compute() {
|
|
426
744
|
const o = this.options;
|
|
427
745
|
const geom = this._feature.getGeometry();
|
|
428
|
-
const lines =
|
|
746
|
+
const lines = geomLines(geom);
|
|
429
747
|
const dataProj = o.dataProjection || (this.getMap() && this.getMap().getView().getProjection()) || 'EPSG:3857';
|
|
430
748
|
|
|
749
|
+
// Terrain-model elevations, if they were loaded for THIS feature.
|
|
750
|
+
const demZ = (this._demFor === this._feature) ? this._demZ : null;
|
|
751
|
+
|
|
752
|
+
const times = extractTimes(this._feature, lines);
|
|
753
|
+
this._hasTime = !!times;
|
|
754
|
+
const ignoreStops = o.ignoreStops !== false; // default: stops are excluded
|
|
755
|
+
const stopSpeed = (o.stopSpeed != null ? o.stopSpeed : 0.5); // m/s
|
|
431
756
|
const pts = [];
|
|
432
|
-
let cum = 0, prev = null;
|
|
757
|
+
let cum = 0, prev = null, i = -1, tAcc = 0, prevMs = null;
|
|
433
758
|
for (const seg of lines) for (const c of seg) {
|
|
759
|
+
i++;
|
|
434
760
|
const ll = toLonLat(c, dataProj);
|
|
435
|
-
|
|
761
|
+
let dseg = 0;
|
|
762
|
+
if (prev) { dseg = getDistance(prev, ll); cum += dseg; }
|
|
763
|
+
let t = null;
|
|
764
|
+
if (times && times[i] != null) {
|
|
765
|
+
const ms = times[i];
|
|
766
|
+
if (prevMs != null) {
|
|
767
|
+
const dt = (ms - prevMs) / 1000; // seconds over the segment
|
|
768
|
+
if (dt > 0 && (!ignoreStops || (dseg / dt) >= stopSpeed)) tAcc += dt;
|
|
769
|
+
}
|
|
770
|
+
t = tAcc; prevMs = ms;
|
|
771
|
+
}
|
|
436
772
|
prev = ll;
|
|
437
|
-
|
|
773
|
+
const z = (c.length > 2 && isFinite(c[2])) ? c[2] : (demZ ? demZ[i] : 0);
|
|
774
|
+
pts.push({ x: cum, z, coord: c, t });
|
|
438
775
|
}
|
|
439
776
|
if (o.smoothing > 0) this._smooth(pts, o.smoothing);
|
|
440
777
|
|
|
@@ -452,11 +789,15 @@ import * as d3 from 'd3';
|
|
|
452
789
|
const s = Math.abs(samples[k].slope || 0); if (s > maxAbs) maxAbs = s;
|
|
453
790
|
}
|
|
454
791
|
const distance = samples.length ? samples[samples.length - 1].x - samples[0].x : 0;
|
|
455
|
-
|
|
792
|
+
const ta = samples.length ? samples[0].t : null, tb = samples.length ? samples[samples.length - 1].t : null;
|
|
793
|
+
const duration = (ta != null && tb != null) ? (tb - ta) : null;
|
|
794
|
+
// Reported for a closed ring too, where they are equal by construction: on a loop,
|
|
795
|
+
// D+ is exactly the figure one is after.
|
|
796
|
+
return { distance, duration, ascent, descent, min: isFinite(zmin) ? zmin : 0, max: isFinite(zmax) ? zmax : 0, maxAbsSlope: maxAbs, points: samples.length };
|
|
456
797
|
}
|
|
457
798
|
_smooth(pts, meters) {
|
|
458
799
|
if (!(meters > 0) || pts.length < 3) return;
|
|
459
|
-
const half = meters / 2; //
|
|
800
|
+
const half = meters / 2; // window = +/-(meters/2) along the track
|
|
460
801
|
const z = pts.map((p) => p.z);
|
|
461
802
|
let lo = 0, hi = 0, sum = 0;
|
|
462
803
|
for (let i = 0; i < pts.length; i++) {
|
|
@@ -467,7 +808,7 @@ import * as d3 from 'd3';
|
|
|
467
808
|
}
|
|
468
809
|
}
|
|
469
810
|
_decimate(pts, maxPoints) {
|
|
470
|
-
const copy = (p) => ({ x: p.x, z: p.z, coord: p.coord });
|
|
811
|
+
const copy = (p) => ({ x: p.x, z: p.z, coord: p.coord, t: p.t });
|
|
471
812
|
if (!maxPoints || pts.length <= maxPoints) return pts.map(copy);
|
|
472
813
|
const step = pts.length / maxPoints, out = [];
|
|
473
814
|
for (let i = 0; i < maxPoints; i++) out.push(copy(pts[Math.floor(i * step)]));
|
|
@@ -481,20 +822,20 @@ import * as d3 from 'd3';
|
|
|
481
822
|
if (samples.length) samples[0].slope = samples.length > 1 ? samples[1].slope : 0;
|
|
482
823
|
}
|
|
483
824
|
|
|
484
|
-
// ----------
|
|
825
|
+
// ---------- slope: colours ----------------------------------------
|
|
485
826
|
_slopeScale() {
|
|
486
827
|
const cs = this.options.slopeClassSize || 2.5;
|
|
487
828
|
const maxClasses = this.options.maxClasses || 8;
|
|
488
829
|
const realIdx = Math.max(1, Math.floor((this._stats.maxAbsSlope || 0) / cs));
|
|
489
|
-
const maxIdx = Math.min(realIdx, maxClasses - 1); //
|
|
490
|
-
const capped = realIdx > maxIdx; //
|
|
830
|
+
const maxIdx = Math.min(realIdx, maxClasses - 1); // at most `maxClasses` classes (default 8)
|
|
831
|
+
const capped = realIdx > maxIdx; // some slopes overflow the last class
|
|
491
832
|
let colorByIndex;
|
|
492
833
|
if (this.slopeColors && this.slopeColors.length) {
|
|
493
834
|
const interp = d3.interpolateRgbBasis(this.slopeColors);
|
|
494
835
|
colorByIndex = (idx) => interp(maxIdx ? idx / maxIdx : 0);
|
|
495
836
|
} else {
|
|
496
|
-
//
|
|
497
|
-
//
|
|
837
|
+
// Ramp with non-uniform stops: the cold end (blue-green, hard to read) is squeezed,
|
|
838
|
+
// PURE yellow in the middle, then orange, red. Class 0 = blue, top class = red.
|
|
498
839
|
const ramp = d3.scaleLinear()
|
|
499
840
|
.domain([0, 0.16, 0.42, 0.68, 1])
|
|
500
841
|
.range(['#2166ac', '#27a35a', '#ffe000', '#f4791f', '#d7191c'])
|
|
@@ -506,14 +847,53 @@ import * as d3 from 'd3';
|
|
|
506
847
|
}
|
|
507
848
|
_classIndex(slope, sc) { return Math.min(sc.maxIdx, Math.floor(Math.abs(slope) / sc.classSize)); }
|
|
508
849
|
|
|
509
|
-
// ----------
|
|
510
|
-
|
|
511
|
-
|
|
850
|
+
// ---------- time: adaptive format ---------------------------------
|
|
851
|
+
// 7 sec | 26 min | 1 h 48 min | 2 d 3 h (days + hours normalised)
|
|
852
|
+
_fmtDuration(sec) {
|
|
853
|
+
if (sec == null || !isFinite(sec)) return '';
|
|
854
|
+
const u = (this.options.labels && this.options.labels.durationUnits) || { s: 'sec', m: 'min', h: 'h', d: 'j' };
|
|
855
|
+
sec = Math.max(0, Math.round(sec));
|
|
856
|
+
if (sec < 60) return sec + ' ' + u.s;
|
|
857
|
+
if (sec < 3600) return Math.round(sec / 60) + ' ' + u.m;
|
|
858
|
+
if (sec < 86400) {
|
|
859
|
+
let h = Math.floor(sec / 3600), m = Math.round((sec % 3600) / 60);
|
|
860
|
+
if (m === 60) { h++; m = 0; }
|
|
861
|
+
return m ? `${h} ${u.h} ${m} ${u.m}` : `${h} ${u.h}`;
|
|
862
|
+
}
|
|
863
|
+
let d = Math.floor(sec / 86400), h = Math.round((sec % 86400) / 3600);
|
|
864
|
+
if (h === 24) { d++; h = 0; }
|
|
865
|
+
return h ? `${d} ${u.d} ${h} ${u.h}` : `${d} ${u.d}`;
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
// ---------- header + legend ---------------------------------------
|
|
869
|
+
/**
|
|
870
|
+
* Track title, and its optional link.
|
|
871
|
+
*
|
|
872
|
+
* Its own method because it is the only part of the header that stays visible once
|
|
873
|
+
* collapsed — the CSS hides the body, the stats, the legend and the toolbar. `_render`
|
|
874
|
+
* is skipped while collapsed, so without a separate entry point the title would keep
|
|
875
|
+
* naming the previous track after a change of feature.
|
|
876
|
+
*/
|
|
877
|
+
_renderTitle() {
|
|
878
|
+
const o = this.options, f = this._feature;
|
|
879
|
+
if (!f) return;
|
|
512
880
|
const name = (f.get && f.get(o.titleProperty)) || 'Profil';
|
|
513
881
|
const linkUrl = o.titleLink && f.get && f.get(o.titleLink);
|
|
514
882
|
if (isUrl(linkUrl)) this._titleEl.innerHTML = `<a href="${esc(linkUrl)}" target="_blank" rel="noopener">${esc(name)}</a>`;
|
|
515
883
|
else this._titleEl.textContent = name;
|
|
516
884
|
this._titleEl.setAttribute('title', name);
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
_renderHeader() {
|
|
888
|
+
const o = this.options, s = this._stats, f = this._feature;
|
|
889
|
+
this._renderTitle();
|
|
890
|
+
// While the elevations are still unknown, every figure would read zero: a D+ of 0 m
|
|
891
|
+
// then jumping to 1 200 is worse than no figure at all.
|
|
892
|
+
if (this._demLoading) {
|
|
893
|
+
this._statsEl.innerHTML = ''; this._statsEl.removeAttribute('title');
|
|
894
|
+
this._legendEl.innerHTML = ''; this._legendEl.style.display = 'none';
|
|
895
|
+
return;
|
|
896
|
+
}
|
|
517
897
|
|
|
518
898
|
const html = [], text = [];
|
|
519
899
|
o.headerItems.forEach((it) => {
|
|
@@ -524,6 +904,7 @@ import * as d3 from 'd3';
|
|
|
524
904
|
else if (it === 'min') { html.push(fmtElevation(s.min, o.units)); text.push(fmtElevation(s.min, o.units)); }
|
|
525
905
|
else if (it === 'max') { html.push(fmtElevation(s.max, o.units)); text.push(fmtElevation(s.max, o.units)); }
|
|
526
906
|
else if (it === 'minmax') { const v = `${fmtElevation(s.min, o.units)}–${fmtElevation(s.max, o.units)}`; html.push(v); text.push(v); }
|
|
907
|
+
else if (it === 'duration') { if (s.duration != null) { const v = this._fmtDuration(s.duration); html.push(`<span class="oep-time">${esc(o.labels.duration)} ${v}</span>`); text.push(`${o.labels.duration} ${v}`); } }
|
|
527
908
|
} else if (it && it.property) {
|
|
528
909
|
const val = f.get && f.get(it.property); if (val == null || val === '') return;
|
|
529
910
|
const lbl = it.label ? `${it.label} ` : '';
|
|
@@ -548,18 +929,40 @@ import * as d3 from 'd3';
|
|
|
548
929
|
} else this._legendEl.style.display = 'none';
|
|
549
930
|
}
|
|
550
931
|
|
|
551
|
-
|
|
932
|
+
/**
|
|
933
|
+
* Spinner shown in place of the chart while the terrain model loads.
|
|
934
|
+
*
|
|
935
|
+
* It keeps the chart's height so the panel does not jump when the profile replaces it,
|
|
936
|
+
* and it carries no colour of its own: the CSS takes `--oep-area`, which `_applyTheme`
|
|
937
|
+
* has just set - the theme colour, or the track colour under `color: 'auto'`.
|
|
938
|
+
*/
|
|
939
|
+
_renderSpinner() {
|
|
940
|
+
const H = typeof this.options.height === 'number' ? this.options.height : 180;
|
|
941
|
+
this._body.innerHTML = '';
|
|
942
|
+
const box = document.createElement('div');
|
|
943
|
+
box.className = 'oep-loading';
|
|
944
|
+
box.style.height = `${H}px`;
|
|
945
|
+
box.setAttribute('role', 'status');
|
|
946
|
+
box.setAttribute('aria-label', this.options.labels.loading);
|
|
947
|
+
const sp = document.createElement('div');
|
|
948
|
+
sp.className = 'oep-spinner';
|
|
949
|
+
box.appendChild(sp);
|
|
950
|
+
this._body.appendChild(box);
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
// ---------- rendering ---------------------------------------------
|
|
552
954
|
_render() {
|
|
553
955
|
const o = this.options, s = this._stats, data = this._samples;
|
|
554
956
|
if (!data || !data.length) return;
|
|
555
957
|
this._applyTheme();
|
|
556
958
|
const mobile = this._applyPlacement();
|
|
557
959
|
this._renderHeader();
|
|
960
|
+
if (this._demLoading) { this._renderSpinner(); return; }
|
|
558
961
|
|
|
559
962
|
const m = o.margins, u = m.unit || 'px';
|
|
560
963
|
const toPx = (v) => u === 'px' ? v : v * (parseFloat(getComputedStyle(this.element).fontSize) || 16);
|
|
561
964
|
|
|
562
|
-
//
|
|
965
|
+
// Width: 100% on mobile; 'auto'/'100%'/'full' = map width; otherwise a number capped to the map
|
|
563
966
|
const avail = this._availWidth();
|
|
564
967
|
const isAuto = (o.width === 'auto' || o.width === '100%' || o.width === 'full');
|
|
565
968
|
const desktopW = isAuto ? avail : Math.min(typeof o.width === 'number' ? o.width : (parseFloat(o.width) || avail), avail);
|
|
@@ -596,7 +999,7 @@ import * as d3 from 'd3';
|
|
|
596
999
|
const cls = this._classIndex(data[i].slope, sc);
|
|
597
1000
|
let j = i; while (j + 1 < data.length && this._classIndex(data[j + 1].slope, sc) === cls) j++;
|
|
598
1001
|
g.append('path').datum(data.slice(i - 1, j + 1)).attr('class', 'oep-area-slope').attr('fill', sc.colorByIndex(cls)).attr('d', areaGen);
|
|
599
|
-
if (i > 1) seps.push(data[i - 1]); //
|
|
1002
|
+
if (i > 1) seps.push(data[i - 1]); // a class change is a boundary
|
|
600
1003
|
i = j + 1;
|
|
601
1004
|
}
|
|
602
1005
|
if (o.slopeSeparators) seps.forEach((d) => {
|
|
@@ -612,7 +1015,7 @@ import * as d3 from 'd3';
|
|
|
612
1015
|
g.append('text').attr('class', 'oep-axis-label').attr('x', innerW).attr('y', innerH + mb - 4).attr('text-anchor', 'end').text(distAxisLabel(o.units));
|
|
613
1016
|
g.append('g').attr('class', 'oep-axis oep-axis-y').call(d3.axisLeft(y).ticks(yTicks).tickFormat((d) => o.units === 'imperial' ? Math.round(d * 3.28084) : d));
|
|
614
1017
|
|
|
615
|
-
//
|
|
1018
|
+
// A / B markers while a range is being picked
|
|
616
1019
|
if (o.zoom && !this._cropMode) {
|
|
617
1020
|
[['A', this._zoomA], ['B', this._zoomB]].forEach(([nm, val]) => {
|
|
618
1021
|
if (val == null) return;
|
|
@@ -664,7 +1067,8 @@ import * as d3 from 'd3';
|
|
|
664
1067
|
const raw = this._fullSamples.filter((p) => p.x >= a && p.x <= b);
|
|
665
1068
|
if (raw.length < 2) return;
|
|
666
1069
|
const off = raw[0].x; // A devient 0
|
|
667
|
-
const
|
|
1070
|
+
const tOff = raw[0].t != null ? raw[0].t : 0;
|
|
1071
|
+
const cropped = raw.map((p) => ({ x: p.x - off, z: p.z, coord: p.coord, slope: p.slope, t: (p.t != null ? p.t - tOff : null) }));
|
|
668
1072
|
const coords = raw.map((p) => p.coord);
|
|
669
1073
|
this._samples = cropped; this._stats = this._statsOf(cropped, false); this._cropMode = true;
|
|
670
1074
|
this._updateZoomButtons(); this._render();
|
|
@@ -684,6 +1088,29 @@ import * as d3 from 'd3';
|
|
|
684
1088
|
if (map && this._feature) map.getView().fit(this._feature.getGeometry().getExtent(), { padding: [40, 40, 40, 40], duration: 400 });
|
|
685
1089
|
}
|
|
686
1090
|
|
|
1091
|
+
/**
|
|
1092
|
+
* Point of the profiled outline closest to a map coordinate.
|
|
1093
|
+
*
|
|
1094
|
+
* A polygon's own `getClosestPoint` answers for its **surface**: with the cursor inside
|
|
1095
|
+
* the ring it returns the cursor itself, so the marker would follow the pointer across
|
|
1096
|
+
* the whole shape instead of sliding along the outline. Lines keep the geometry's own
|
|
1097
|
+
* answer, which is exact rather than limited to the decimated samples.
|
|
1098
|
+
*/
|
|
1099
|
+
_closestOnProfile(coordinate) {
|
|
1100
|
+
const g = this._feature && this._feature.getGeometry();
|
|
1101
|
+
if (!g) return null;
|
|
1102
|
+
if (!/Polygon/.test(g.getType())) return g.getClosestPoint(coordinate);
|
|
1103
|
+
const data = this._fullSamples;
|
|
1104
|
+
if (!data || !data.length) return null;
|
|
1105
|
+
let best = null, bd = Infinity;
|
|
1106
|
+
for (const p of data) {
|
|
1107
|
+
const dx = p.coord[0] - coordinate[0], dy = p.coord[1] - coordinate[1];
|
|
1108
|
+
const dd = dx * dx + dy * dy;
|
|
1109
|
+
if (dd < bd) { bd = dd; best = p.coord; }
|
|
1110
|
+
}
|
|
1111
|
+
return best;
|
|
1112
|
+
}
|
|
1113
|
+
|
|
687
1114
|
// ---------- focus -------------------------------------------------
|
|
688
1115
|
_tooltipText(d) {
|
|
689
1116
|
const o = this.options, parts = [];
|
|
@@ -691,6 +1118,7 @@ import * as d3 from 'd3';
|
|
|
691
1118
|
if (it === 'distance') parts.push(fmtDistance(d.x, o.units));
|
|
692
1119
|
else if (it === 'elevation') parts.push(fmtElevation(d.z, o.units));
|
|
693
1120
|
else if (it === 'slope') parts.push(fmtSlope(d.slope || 0));
|
|
1121
|
+
else if (it === 'time') { if (d.t != null) parts.push(this._fmtDuration(d.t)); }
|
|
694
1122
|
});
|
|
695
1123
|
return parts.join(' · ');
|
|
696
1124
|
}
|
|
@@ -720,7 +1148,7 @@ import * as d3 from 'd3';
|
|
|
720
1148
|
this._legendEl.innerHTML = ''; this._legendEl.style.display = 'none';
|
|
721
1149
|
this._titleEl.textContent = this.options.labels.empty; this._titleEl.removeAttribute('title');
|
|
722
1150
|
this._statsEl.innerHTML = ''; this._statsEl.removeAttribute('title');
|
|
723
|
-
this._cropMode = false; this._updateZoomButtons();
|
|
1151
|
+
this._cropMode = false; this._demLoading = false; this._updateZoomButtons();
|
|
724
1152
|
this._clearFocus();
|
|
725
1153
|
this.element.style.display = 'none';
|
|
726
1154
|
this._adjustAttribution();
|
|
@@ -734,7 +1162,10 @@ import * as d3 from 'd3';
|
|
|
734
1162
|
*/
|
|
735
1163
|
ElevationProfile.addTheme = (name, colors) => { THEMES[name] = colors; };
|
|
736
1164
|
ElevationProfile.THEMES = THEMES;
|
|
1165
|
+
/** Known keyless terrain-tile sources, keyed by `dem.source` name. */
|
|
1166
|
+
ElevationProfile.DEM_PRESETS = DEM_PRESETS;
|
|
1167
|
+
ElevationProfile.DemSampler = DemSampler;
|
|
737
1168
|
ElevationProfile.POSITIONS = POSITIONS;
|
|
738
|
-
ElevationProfile.version = '0.
|
|
1169
|
+
ElevationProfile.version = '0.6.0';
|
|
739
1170
|
|
|
740
1171
|
export { ElevationProfile as default };
|