-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
288 lines (255 loc) · 8.54 KB
/
main.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
import 'ol/ol.css';
import Map from 'ol/Map';
import VectorLayer from 'ol/layer/Vector';
import TileLayer from 'ol/layer/Tile';
import XYZSource from 'ol/source/XYZ';
import VectorSource from 'ol/source/Vector';
//import VectorContext from 'ol/render/VectorContext';
import View from 'ol/View'
import OSMSource from 'ol/source/OSM'
import {GPX,KML} from 'ol/format';
import TileDebug from 'ol/source/TileDebug';
import {Style, Fill, Stroke} from 'ol/style';
import {fromLonLat} from 'ol/proj';
import Feature from 'ol/Feature';
import Point from 'ol/geom/Point';
import Icon from 'ol/style/Icon';
import {defaults as defaultInteractions, DragAndDrop} from 'ol/interaction';
let gpxParser = require('gpxparser');
import Chart from 'chart.js';
function rgb(r, g, b){
r = Math.floor(r);
g = Math.floor(g);
b = Math.floor(b);
return ["rgb(",r,",",g,",",b,")"].join("");
};
/**
* Returns a hash code for a string.
* (Compatible to Java's String.hashCode())
*
* The hash code for a string object is computed as
* s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
* using number arithmetic, where s[i] is the i th character
* of the given string, n is the length of the string,
* and ^ indicates exponentiation.
* (The hash value of the empty string is zero.)
*
* @param {string} s a string
* @return {number} a hash code value for the given string.
* https://gist.github.com/hyamamoto/fd435505d29ebfa3d9716fd2be8d42f0
*/
function hashCode(s){
var h = 0, l = s.length, i = 0;
if ( l > 0 )
while (i < l)
h = (h << 5) - h + s.charCodeAt(i++) | 0;
return h;
};
var tile= new TileLayer({
source: new OSMSource
});
var tiles= new TileLayer({
source: new TileDebug()
});
var dragAndDropInteraction = new DragAndDrop({
formatConstructors: [
GPX,
KML,
]
});
var marker = new Feature({
geometry: new Point(fromLonLat([-2.5,51.5])),
});
marker.setStyle(
new Style({
image: new Icon({
color: '#ffffff',
imgSize: [44,44],
src: 'bikeicon-44px.png',
anchor: [0.64, 0.1],
}),
})
);
//console.log(marker);
var vectorSource = new VectorSource({
features: [marker],
});
var vectorLayer = new VectorLayer({
source: vectorSource,
});
vectorLayer.setZIndex( 1001 );
console.log ("creating map");
var map= new Map({
interactions: defaultInteractions().extend([dragAndDropInteraction]),
target: 'map-container',
layers: [tile, tiles,vectorLayer],
view: new View({
center: fromLonLat([-2.5,51.5]),
zoom: 14
})
});
var gpx = new gpxParser(); //Create gpxParser Object
var trackFilenames={};
//Create the plot, empty for now (or rather, hidden dummy data)
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'scatter',
data: {
datasets: [{
label: 'test',
data: [{
x:0,
y:0
},{
x:1,
y:1
}],
backgroundColor:'rgba(0, 0,0,0)',
borderColor: 'rgba(0.0.0,0)',
pointBorderColor:'rgba(0,0,0,0)',
pointBorderWidth:0,
pointBackgroundColor:'rgba(0,0,0,0)',
borderWidth: 0,
tension: 0,
showLine: false,
fill:false,
pointHoverRadius: 10,
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
},
scaleLabel: {
display: true,
labelString:'Cumulative climb / m'
}
}],
xAxes: [{
ticks: {
beginAtZero: true
},
scaleLabel: {
display: true,
labelString:'Distance / km'
}
}]
},
legend: {
display:false
},
onHover: function(evt) {
var item = myChart.getElementAtEvent(evt);
if (item.length) {
//console.log("onHover",item, evt.type);
var index=item[0]._index
//console.log(">data", index, myChart.data.datasets[0].data[item[0]._index]);
var status = document.getElementById("location-status");
var series=myChart.data.datasets[item[0]._datasetIndex]
console.log(series.data[index]);
status.innerHTML = series.label+": "+parseFloat(series.data[index].x)+" km, = "+(100*parseFloat(series.data[index].x)/parseFloat(series.data[series.data.length-1].x)).toFixed(0)+"%, "+series.data[index].y+" m cumulative climb = "+(100*series.data[index].y/series.data[series.data.length-1].y).toFixed(0)+
"% <small><small>(altitude "+gpx.tracks[0].points[index].ele.toFixed(0)+" Lat, Long:"+gpx.tracks[0].points[index].lat.toFixed(4)+","+gpx.tracks[0].points[index].lon.toFixed(4)+")</small></small>";
var dist=series.data[index].x;//it looks like chartjs returns the index in resampled data, so we have to find the cumulative distance in gpxparser's data instead of just using the index
var trackNum=0
for (trackNum=0; trackNum<gpx.tracks.length; trackNum++) {
if (series.label==trackFilenames[gpx.tracks[trackNum].name]) {
console.log("breaking");
break;
}
}
var closestPoint = gpx.tracks[trackNum].distance.cumul.findIndex(function (element) {//not guaranteed to be closest but good enough
return element > (dist*1000)-10;
});
var currentPoint = new Point(fromLonLat([gpx.tracks[trackNum].points[closestPoint].lon,gpx.tracks[trackNum].points[closestPoint].lat]));
var marker_to_update=vectorSource.getFeatures()[0];
marker_to_update.set('geometry', currentPoint);
}
}}
});
//When a file is dropped on the map, do all the clever bits
dragAndDropInteraction.on('addfeatures', function(event) {
var StrokeColour
var vectorSource = new VectorSource({
features: event.features
});
//console.log(event.file)
if (event.file.type.includes("kml")){
StrokeColour="red";
} else { //GPX
var red_component=Math.abs(hashCode(event.file.name) %32)*6;
var green_component=(event.file.name.length%32)*6;
StrokeColour=rgb(red_component,green_component,255);
//Parse to get coordinates and elevations
var fr=new FileReader();
var total_climb=0
var threshold_climb=0.3 //minimum climb between consecutive points required to count. Should be zero but matches Strava better if 0.3 (metres)
var height_diff=0
var distance=[];
var climb_so_far=[];
fr.onload=function(){
var route_text=fr.result;
gpx.parse(route_text); //parse gpx file from string data
var trackNum=gpx.tracks.length-1;
trackFilenames[gpx.tracks[trackNum].name]=event.file.name;
console.log(trackFilenames);
var arrayLength = gpx.tracks[trackNum].points.length;
for (var i = 0; i < arrayLength; i++) {
if (i>0){
height_diff=gpx.tracks[trackNum].points[i].ele-gpx.tracks[trackNum].points[i-1].ele
if (height_diff>threshold_climb){
total_climb+=height_diff;
climb_so_far.push(Math.floor(total_climb));//Climb to nearest 1m
distance.push(gpx.tracks[trackNum].distance.cumul[i])//Distance to nearest 10m
}
}
}
total_climb=Math.floor(total_climb)
console.log("total_climb",total_climb )
var total_distance=Math.floor(gpx.tracks[trackNum].distance.total/100)/10 //round to nearest 100m
var para=document.createElement("p");
var node=document.createTextNode(event.file.name.concat(":\n\xa0Distance=",total_distance,"\xa0km ", "\xa0Climb=",total_climb,"\xa0m"));
para.appendChild(node);
para.style.color=StrokeColour
var element=document.getElementById("legend");
element.appendChild(para);
//chart.js to plot cumulative elevation from the arrays above
//perhaps also rolling elevation
const plotdata = distance.map((x, i) => {
return {
x: (x/1000).toFixed(1),
y: climb_so_far[i]
};
});
myChart.data.datasets.push({
label: event.file.name,
data: plotdata,
borderColor:StrokeColour,
borderwidth:5,
tension: 0,
showLine:true,
fill:false,
pointBorderColor:'rgba(0,0,0,0)',
pointBorderWidth:0,
pointBackgroundColor:'rgba(0,0,0,0)',
pointHitRadius:5,
});
myChart.update();
}
fr.readAsText(event.file);
}
map.addLayer(new VectorLayer({
source: vectorSource,
style: new Style({
fill: new Fill({
color: StrokeColour,
}),
stroke: new Stroke({
color: StrokeColour,
width: 5
})
})
}));
map.getView().fit(vectorSource.getExtent());
});