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
| import Feature from 'ol/Feature.js'; import IGC from 'ol/format/IGC.js'; import Map from 'ol/Map.js'; import OSM, {ATTRIBUTION} from 'ol/source/OSM.js'; import VectorSource from 'ol/source/Vector.js'; import View from 'ol/View.js'; import {Circle as CircleStyle, Fill, Stroke, Style} from 'ol/style.js'; import {LineString, Point} from 'ol/geom.js'; import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer.js'; import {getVectorContext} from 'ol/render.js';
const colors = { 'Clement Latour': 'rgba(0, 0, 255, 0.7)', 'Damien de Baesnt': 'rgba(0, 215, 255, 0.7)', 'Sylvain Dhonneur': 'rgba(0, 165, 255, 0.7)', 'Tom Payne': 'rgba(0, 255, 255, 0.7)', 'Ulrich Prinz': 'rgba(0, 215, 255, 0.7)', };
const styleCache = {}; const styleFunction = function (feature) { const color = colors[feature.get('PLT')]; let style = styleCache[color]; if (!style) { style = new Style({ stroke: new Stroke({ color: color, width: 3, }), }); styleCache[color] = style; } return style; };
const vectorSource = new VectorSource();
const igcUrls = [ 'data/igc/Clement-Latour.igc', 'data/igc/Damien-de-Baenst.igc', 'data/igc/Sylvain-Dhonneur.igc', 'data/igc/Tom-Payne.igc', 'data/igc/Ulrich-Prinz.igc', ];
function get(url, callback) { const client = new XMLHttpRequest(); client.open('GET', url); client.onload = function () { callback(client.responseText); }; client.send(); }
const igcFormat = new IGC(); for (let i = 0; i < igcUrls.length; ++i) { get(igcUrls[i], function (data) { const features = igcFormat.readFeatures(data, { featureProjection: 'EPSG:3857', }); vectorSource.addFeatures(features); }); }
const time = { start: Infinity, stop: -Infinity, duration: 0, }; vectorSource.on('addfeature', function (event) { const geometry = event.feature.getGeometry(); time.start = Math.min(time.start, geometry.getFirstCoordinate()[2]); time.stop = Math.max(time.stop, geometry.getLastCoordinate()[2]); time.duration = time.stop - time.start; });
const vectorLayer = new VectorLayer({ source: vectorSource, style: styleFunction, });
const map = new Map({ layers: [ new TileLayer({ source: new OSM({ attributions: [ 'All maps © <a href="https://www.opencyclemap.org/">OpenCycleMap</a>', ATTRIBUTION, ], url: 'https://{a-c}.tile.thunderforest.com/cycle/{z}/{x}/{y}.png' + '?apikey=Your API key from https://www.thunderforest.com/docs/apikeys/ here', }), }), vectorLayer, ], target: 'map', view: new View({ center: [703365.7089403362, 5714629.865071137], zoom: 9, }), });
let point = null; let line = null; const displaySnap = function (coordinate) { const closestFeature = vectorSource.getClosestFeatureToCoordinate(coordinate); const info = document.getElementById('info'); if (closestFeature === null) { point = null; line = null; info.innerHTML = ' '; } else { const geometry = closestFeature.getGeometry(); const closestPoint = geometry.getClosestPoint(coordinate); if (point === null) { point = new Point(closestPoint); } else { point.setCoordinates(closestPoint); } const date = new Date(closestPoint[2] * 1000); info.innerHTML = closestFeature.get('PLT') + ' (' + date.toUTCString() + ')'; const coordinates = [coordinate, [closestPoint[0], closestPoint[1]]]; if (line === null) { line = new LineString(coordinates); } else { line.setCoordinates(coordinates); } } map.render(); };
map.on('pointermove', function (evt) { if (evt.dragging) { return; } const coordinate = map.getEventCoordinate(evt.originalEvent); displaySnap(coordinate); });
map.on('click', function (evt) { displaySnap(evt.coordinate); });
const stroke = new Stroke({ color: 'rgba(255,0,0,0.9)', width: 1, }); const style = new Style({ stroke: stroke, image: new CircleStyle({ radius: 5, fill: null, stroke: stroke, }), }); vectorLayer.on('postrender', function (evt) { const vectorContext = getVectorContext(evt); vectorContext.setStyle(style); if (point !== null) { vectorContext.drawGeometry(point); } if (line !== null) { vectorContext.drawGeometry(line); } });
const featureOverlay = new VectorLayer({ source: new VectorSource(), map: map, style: new Style({ image: new CircleStyle({ radius: 5, fill: new Fill({ color: 'rgba(255,0,0,0.9)', }), }), }), });
const control = document.getElementById('time'); control.addEventListener('input', function () { const value = parseInt(control.value, 10) / 100; const m = time.start + time.duration * value; vectorSource.forEachFeature(function (feature) { const geometry = ( feature.getGeometry() ); const coordinate = geometry.getCoordinateAtM(m, true); let highlight = feature.get('highlight'); if (highlight === undefined) { highlight = new Feature(new Point(coordinate)); feature.set('highlight', highlight); featureOverlay.getSource().addFeature(highlight); } else { highlight.getGeometry().setCoordinates(coordinate); } }); map.render(); });
|