source: src/webapp/webclient/js/GnuBoat.js@ 008bce9

0.5 debian/bullseye debian/jessie
Last change on this file since 008bce9 was 008bce9, checked in by Joachim Langenbach <joachim.langenbach@…>, 9 years ago

First more or less working version of visualising logfiles on the map

  • Property mode set to 100644
File size: 22.5 KB
RevLine 
[4ce069e]1/*
2
3 Copyright (C) 2016 - 2017 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 MA 02110-1301 USA.
19
20*/
21
22/** @namespace GnuBoat JavaScript namespace */
23var GnuBoat = GnuBoat || {};
24
25/**
26 * @brief Sets the webservice base url, used by any other functions
27 */
28GnuBoat.setServer = function(baseUrl)
29{
30 if(baseUrl.slice(-1) != "/")
31 baseUrl += "/";
32 GnuBoat.private.serverBaseUrl = baseUrl;
33
[79a1004]34 var link = document.createElement("a");
35 link.setAttribute("href", baseUrl);
36 $("#opencpnHint").empty();
37 $("<p>To connect OpenCPN with the GPSD running on this webservice, use <b>"+ link.hostname +":2947</b> and <b>"+ link.hostname +":2597</b> for the messages recieved on NMEA2000 bus.</p>").appendTo("#opencpnHint");
[4ce069e]38}
39
40/**
41 * @brief Used to display user messages
42 */
43GnuBoat.displayMessage = function(text)
44{
[e2cdddf]45 $("#message").empty();
[2a1e934]46 $(text).appendTo("#message");
[4ce069e]47 location.hash = "#messages";
48}
49
50/**
51 * @brief Fetches the data shown in the display area from the webservice
52 */
53GnuBoat.displayDataGet = function()
54{
55 if(GnuBoat.private.serverBaseUrl == null){
[008bce9]56 GnuBoat.displayMessage('<span class="error">No server set/found</span>');
[4ce069e]57 return;
58 }
59 var gnuboatApi = GnuBoat.private.serverBaseUrl;
60 $.getJSON(gnuboatApi, GnuBoat.private.displayDataSet);
61};
62
63/**
64 * @brief Gets the metadata of all maps, provided by the tileserver
65 */
66GnuBoat.getMapInfos = function()
67{
68 if(GnuBoat.private.serverBaseUrl == null){
[008bce9]69 GnuBoat.displayMessage('<span class="error">No server set/found</span>');
[4ce069e]70 return;
71 }
72 $.getJSON(GnuBoat.private.serverBaseUrl +"tileserver.php?/index.json", GnuBoat.private.mapInfos);
73};
74
75/**
76 * @brief Fetches the list of available logfiles and adds them to the log file download form
77 */
78GnuBoat.logfilesLoad = function()
79{
80 if(GnuBoat.private.serverBaseUrl == null){
[008bce9]81 GnuBoat.displayMessage('<span class="error">No server set/found</span>');
[4ce069e]82 return;
83 }
84 $("#logfilesForm").empty();
85 $.getJSON(GnuBoat.private.serverBaseUrl +"?request=logfiles", function(data){
[008bce9]86 GnuBoat.private.updateMapModus(data);
[4ce069e]87 if(!Array.isArray(data) || data.length < 1){
88 $("<p>No Logfiles found!</p>").appendTo("#logfilesForm");
89 console.log(data);
90 return;
91 }
92 if(("error" in data) && data["error"] == 1){
[008bce9]93 GnuBoat.displayMessage('<span class="error">Failed to get log files: '+ data["errormessage"] +'</span>');
94 $("<p>No Logfiles found!</p>").appendTo("#logfilesForm");
95 console.log(data);
[4ce069e]96 return;
97 }
98 $("<select id=\"logfilesSelect\" name=\"logfilesSelected[]\" class=\"pure-input-1\" multiple></select>").appendTo("#logfilesForm");
99 $("<input type=\"submit\" class=\"pure-button pure-button-primary\" name=\"logfilesDownload\" value=\"Download\"/>").appendTo("#logfilesForm");
100 data.forEach(function(value, index){
101 $("<option value=\""+ value +"\">"+ value +"</option>").appendTo("#logfilesSelect");
102 });
103 });
104};
105/**
106 * @brief Sends a shutdown request to the webservice
107 */
108GnuBoat.shutdown = function()
109{
110 if(GnuBoat.private.serverBaseUrl == null){
[008bce9]111 GnuBoat.displayMessage('<span class="error">No server set/found</span>');
[4ce069e]112 return;
113 }
114 $.getJSON(GnuBoat.private.serverBaseUrl +"?request=shutdown", function(data){
115 var shutdownTime = data["shutdownstart"];
116 var shutdownDate = Date.parse(shutdownTime);
117 var currentDate = new Date();
[563c04b]118 var shutdownHint = '<span class="info">Shutting down in '+ Math.floor((shutdownDate - currentDate)/1000) +"s at "+ shutdownTime.toLocaleString() +"</span>";
[4ce069e]119 if(shutdownTime == -1){
[563c04b]120 shutdownHint = '<span class="error">Failed to initiate shutdown: '+ data["errormessage"] +"</span>";
[4ce069e]121 }
122 GnuBoat.displayMessage(shutdownHint);
123 });
124};
125
126/**
127 * @brief Uploads a map and refreshes the maps metadata on success
128 */
129GnuBoat.uploadMap = function(form)
130{
131 if(GnuBoat.private.serverBaseUrl == null){
[008bce9]132 displayMessage('<span class="error">No server set/found</span>');
[4ce069e]133 return;
134 }
135 $("#mapFileInput").hide();
136 $("#mapTileUploadProgress").show();
137 $.ajax({
138 type: 'POST',
139 url: GnuBoat.private.serverBaseUrl +'?request=map',
140 data: new FormData($("#mapTileUpload")[0]),
141 contentType: false,
142 processData: false,
143 success: function(data){
144 if(("error" in data) && data["error"] == 1){
[008bce9]145 GnuBoat.displayMessage('<span class="error">Failed to upload map: '+ data["errormessage"] +'</span>');
[4ce069e]146 $("#mapTileUploadProgress").hide();
147 $("#mapFileInput").show();
148 return;
149 }
[008bce9]150 GnuBoat.displayMessage('<span class="error">Map upload successfully</span>');
[4ce069e]151 $("#mapTileUploadProgress").hide();
152 $("#mapFileInput").show();
153 GnuBoat.getMapInfos();
154 },
155 error: function(jqXHR, exception, errorThrown){
[008bce9]156 GnuBoat.displayMessage('<span class="error">Failed to upload map (XHR): '+ exception +'</span>');
[4ce069e]157 $("#mapTileUploadProgress").hide();
158 $("#mapFileInput").show();
159 }
160 }).progress(function(e){
161 $("#mapTileUploadProgress").attr({value: e.oloaded, max: e.total});
162 });
163};
164
165/**
166 * @brief Setups up GnuBoat to be able to start() after configured a server with setServer().
167 */
168GnuBoat.initialize = function()
169{
[008bce9]170 $("#mapModusSelect").change(function(){ GnuBoat.private.mapModusChanged(); });
[4ce069e]171 $("#mapTileUploadProgress").hide();
172 $("#logfilesReload").click(GnuBoat.logfilesLoad);
173 $("#shutdownButton").click(GnuBoat.shutdown);
174
175 $("#mapTileUpload").validate({
176 rules: {
177 mapFile: {
178 required: true
179 },
180 mapName: {
181 required: true
182 }
183 },
184 submitHandler: function(form){
185 GnuBoat.uploadMap(form);
186 }
187 });
188
189 $("#logfilesForm").validate({
190 rules: {
191 logfilesSelect: {
192 required: true
193 },
194 }
195 });
[17bc29b]196
[9b1c16f]197 $("#rendering").empty();
198 var width = $("#rendering").width();
199 var height = $("#rendering").height();
[17bc29b]200
201 var fieldOfView = 75;
202
203 GnuBoat.private.renderScene = new THREE.Scene();
[9b1c16f]204 GnuBoat.private.renderScene.background = new THREE.Color(0xEEEEEE);
[17bc29b]205
206 GnuBoat.private.renderCamera = new THREE.PerspectiveCamera(75, width/height, 0.1, 1000000);
207 GnuBoat.private.renderCamera.up = new THREE.Vector3(0, 0, 1);
208 GnuBoat.private.renderCamera.position.set(-0.01, -0.01, 15000);
209 GnuBoat.private.renderCamera.lookAt(GnuBoat.private.renderScene.position);
210
211 GnuBoat.private.renderRenderer = new THREE.WebGLRenderer();
212 GnuBoat.private.renderRenderer.setSize(width, height);
[9b1c16f]213 GnuBoat.private.renderRenderer.setPixelRatio( window.devicePixelRatio );
214 document.getElementById("rendering").appendChild(GnuBoat.private.renderRenderer.domElement);
215
216 GnuBoat.private.renderControls = new THREE.OrbitControls( GnuBoat.private.renderCamera, GnuBoat.private.renderRenderer.domElement );
217 GnuBoat.private.renderControls.zoomSpeed = 2;
218
219
220
221 var deep = new THREE.Color(0x347C7C);
222 var shallow = new THREE.Color(0xBCD9D9);
223 var size = 5;
224 GnuBoat.private.renderDepthSteps = 50;
[17bc29b]225
[9b1c16f]226 var deltaR = (Math.max(deep.r, shallow.r) - Math.min(deep.r, shallow.r))/GnuBoat.private.renderDepthSteps;
227 var deltaG = (Math.max(deep.g, shallow.g) - Math.min(deep.g, shallow.g))/GnuBoat.private.renderDepthSteps;
228 var deltaB = (Math.max(deep.b, shallow.b) - Math.min(deep.b, shallow.b))/GnuBoat.private.renderDepthSteps;
229 for(var i = 0; i < GnuBoat.private.renderDepthSteps; i++){
230 var stepColor = new THREE.Color(Math.max(deep.r, shallow.r) - i * deltaR,
231 Math.max(deep.g, shallow.g) - i * deltaG,
232 Math.max(deep.b, shallow.b) - i * deltaB);
233 GnuBoat.private.renderDepthMaterials.push(stepColor);
234 }
235 GnuBoat.private.renderDepthMaterials.push(shallow);
[4ce069e]236}
237
238/**
239 * @brief Starts querying the webservice
240 *
241 * Call initialize() and setServer() before start()
242 */
243GnuBoat.start = function()
244{
245 if(GnuBoat.private.serverBaseUrl == null){
[008bce9]246 GnuBoat.displayMessage('<span class="error">No server set/found</span>');
[4ce069e]247 return;
248 }
249
250 GnuBoat.getMapInfos();
251 GnuBoat.displayDataGet();
252 if(GnuBoat.private.timerDisplay == null)
253 GnuBoat.private.timerDisplay = setInterval(GnuBoat.displayDataGet, 10000);
[008bce9]254 GnuBoat.logfilesLoad();
[9b1c16f]255
256 GnuBoat.private.animate();
[4ce069e]257}
258
259/**
260 * @brief Stops querying the webservice
261 */
262GnuBoat.stop = function()
263{
264 if(GnuBoat.private.timerDisplay != null){
265 clearInterval(GnuBoat.private.timerDisplay);
266 GnuBoat.private.timerDisplay = null;
267 }
268}
269
270/**
271 * @brief Private methods and properties of GnuBoat
272 */
273GnuBoat.private = {
274 map: null,
275 mapLayerCurrent: null,
[fb91143]276 mapPolylines: [],
277 mapControl: null,
[4ce069e]278 mapsMetadata: null,
[17bc29b]279 mapBounds: null,
[008bce9]280 mapModusLive: true,
[fb91143]281 dataMappings: {
282 speedthroughwater: {display: true, map: true, mapActive: false, titleLong: "Speed Through Water", titleShort: "STW", unit: "kts"},
283 speedoverground: {display: true, map: true, mapActive: false, titleLong: "Speed Over Ground", titleShort: "SOG", unit: "kts"},
284 waterdepth: {display: true, map: true, mapActive: true, titleLong: "Waterdepth", titleShort: "Depth", unit: "m"},
285 watertemperature: {display: true, map: true, mapActive: false, titleLong: "Watertemperature", titleShort: "Water", unit: "°C"},
286 gpsfix: {display: true, map: false, mapActive: false, titleLong: "GPS Fix", titleShort: "Fix", unit: ""},
287 },
[4ce069e]288 serverBaseUrl: null,
289 timerDisplay: null,
[17bc29b]290 renderScene: null,
291 renderCamera: null,
292 renderRenderer: null,
293 renderMapBounds: null,
294 renderGrid: null,
[9b1c16f]295 renderControls: null,
296 renderCloud: null,
297 renderDepthMaterials: [],
298 renderDepthSteps: null,
[4ce069e]299
300 openLink: function(target)
301 {
302 window.location=target;
303 },
304
305 displayDataSet: function(data)
306 {
[008bce9]307// if(!Array.isArray(data) || data.length < 1){
308// console.log("No data array returned!");
309// console.log(data);
310// return;
311// }
312 if(("error" in data) && data["error"] == 1){
313 GnuBoat.displayMessage('<span class="error">Failed to get display data and tracks: '+ data["errormessage"] +'</span>');
314 return;
315 }
316
[fb91143]317 var keysDisplay = [];
318 for(var key in GnuBoat.private.dataMappings){
319 if(GnuBoat.private.dataMappings[key]["display"] === true)
320 keysDisplay.push(key);
321 }
[008bce9]322
[4ce069e]323 if(!("lastvalid" in data)){
324 console.log("No last valid values given!");
325 }
326 else{
327 var lastValid = data["lastvalid"];
[fb91143]328 for(var i = 0; i < keysDisplay.length; i++){
329 var key = keysDisplay[i];
[4ce069e]330 var value = lastValid[key];
331 if($.isNumeric(value)){
332 var text = value.toFixed(1);
333 if(key == "watertemperature"){
334 text = value.toFixed(0);
335 }
[fb91143]336 $("#display #"+ key +" #value").text(text);
[4ce069e]337 }
338 else if(key == "gpsfix"){
[fb91143]339 $("#display #"+ key +" #value").text(value);
[4ce069e]340 }
341 }
342 }
343
[008bce9]344 // map displays logged data
345 if(!GnuBoat.private.mapModusLive)
346 return;
347 else if(!("track" in data)){
348 console.log("No track given!");
349 return;
[4ce069e]350 }
[008bce9]351 GnuBoat.private.mapAddTracks(data["track"]);
[4ce069e]352 },
353
354 mapSetup: function ()
355 {
356 var mapMetadataIndex = $("#mapSelect").val();
357 if(GnuBoat.private.mapsMetadata == null){
358 console.log("No mapsMetadata set");
359 return;
360 }
361 if(mapMetadataIndex < 0 || mapMetadataIndex >= GnuBoat.private.mapsMetadata.length){
362 console.log("Map metadata is outside of range :"+ mapMetadataIndex.toString());
363 console.log(mapsMetadata);
364 return;
365 }
366 var mapMetadata = GnuBoat.private.mapsMetadata[mapMetadataIndex];
367
368 var latitudeTop = mapMetadata["bounds"][3];
369 var latitudeBottom = mapMetadata["bounds"][1];
370 var longitudeLeft = mapMetadata["bounds"][0];
371 var longitudeRight = mapMetadata["bounds"][2];
372
373 var mapZoomMin = mapMetadata["minzoom"];
374 var mapZoomMax = mapMetadata["maxzoom"];
375
376 var latitudeCenter = (latitudeTop - latitudeBottom)/2 + latitudeBottom;
377 var longitudeCenter = (longitudeLeft - longitudeRight)/2 + longitudeRight;
378 var mapZoomStart = (mapZoomMax - mapZoomMin)/2 + mapZoomMin;
379
[17bc29b]380 GnuBoat.private.mapBounds = new L.LatLngBounds(
[4ce069e]381 new L.LatLng(latitudeBottom, longitudeLeft),
382 new L.LatLng(latitudeTop, longitudeRight));
383
384 if(GnuBoat.private.map == null){
385 // only initialize the map (viewport) once
386 GnuBoat.private.map = L.map('mapViewport');
387 }
388 GnuBoat.private.map.setView([latitudeCenter, longitudeCenter], mapZoomStart);
389
390 if(GnuBoat.private.mapLayerCurrent != null){
391 GnuBoat.private.map.removeLayer(GnuBoat.private.mapLayerCurrent);
[fb91143]392 if(GnuBoat.private.mapControl)
393 GnuBoat.private.mapControl.removeLayer(GnuBoat.private.mapLayerCurrent);
[4ce069e]394 GnuBoat.private.mapLayerCurrent = null;
395 }
396 GnuBoat.private.mapLayerCurrent = L.tileLayer(mapMetadata["tiles"][0], {
397 minZoom: mapZoomMin, maxZoom: mapZoomMax,
[17bc29b]398 bounds: GnuBoat.private.mapBounds,
[4ce069e]399 attribution: mapMetadata["name"],
400 opacity: 0.85
401 }).addTo(GnuBoat.private.map);
[17bc29b]402 if(GnuBoat.private.mapControl != null)
[fb91143]403 GnuBoat.private.mapControl.addBaseLayer(GnuBoat.private.mapLayerCurrent, "Map");
404 else{
405 GnuBoat.private.mapControl = L.control.layers({"Map": GnuBoat.private.mapLayerCurrent}, GnuBoat.private.mapOverlaysToList(), {hideSingleBase: true, sortLayers: true, }).addTo(GnuBoat.private.map);
406 }
[17bc29b]407
408 // configure render scene for new map
409 GnuBoat.private.renderSetupMap();
[4ce069e]410 },
411
412 mapInfos: function(data)
413 {
414 GnuBoat.private.mapsMetadata = data;
415
416 $("#mapSelect").empty();
417 var selected = "selected";
418 data.forEach(function(value, index){
419 $("<option value=\""+ index.toString() +"\" "+ selected +">"+ value["name"] +"</option>").appendTo("#mapSelect");
420 selected = "";
421 });
422
423 GnuBoat.private.mapSetup();
[172e90d]424 $("#mapSelect").change(function(){ GnuBoat.private.mapSetup(); });
[4ce069e]425 },
[fb91143]426
427 mapOverlaysToList: function()
428 {
429 var overlays = {};
430 for(var key in GnuBoat.private.mapPolylines){
431 overlays[key] = GnuBoat.private.mapPolylines[key];
432 }
433 return overlays;
434 },
[17bc29b]435
436 renderSetupMap: function()
437 {
438 if(GnuBoat.private.mapBounds == null)
439 return;
440
441 // add map bounds
442 if(GnuBoat.private.renderMapBounds != null)
443 GnuBoat.private.renderScene.remove(GnuBoat.private.renderMapBounds);
444 var m1 = GnuBoat.private.mercator(GnuBoat.private.mapBounds.getSouth(), GnuBoat.private.mapBounds.getWest());
445 var m2 = GnuBoat.private.mercator(GnuBoat.private.mapBounds.getSouth(), GnuBoat.private.mapBounds.getEast());
446 var m3 = GnuBoat.private.mercator(GnuBoat.private.mapBounds.getNorth(), GnuBoat.private.mapBounds.getEast());
447 var m4 = GnuBoat.private.mercator(GnuBoat.private.mapBounds.getNorth(), GnuBoat.private.mapBounds.getWest());
448 var geometryMapBounds = new THREE.Geometry();
[9b1c16f]449 var materialMapBounds = new THREE.LineBasicMaterial({color: 0x66BD66, linewidth: 1});
[17bc29b]450 // z is greator 0, to draw the map boundings over the grid
451 var vertex = new THREE.Vector3(0, 0, 2);
452 geometryMapBounds.vertices.push(vertex);
453 vertex = new THREE.Vector3(m2.x - m1.x, m2.y - m1.y, 2);
454 geometryMapBounds.vertices.push(vertex);
455 vertex = new THREE.Vector3(m3.x - m1.x, m3.y - m1.y, 2);
456 geometryMapBounds.vertices.push(vertex);
457 vertex = new THREE.Vector3(m4.x - m1.x, m4.y - m1.y, 2);
458 geometryMapBounds.vertices.push(vertex);
459 GnuBoat.private.renderMapBounds = new THREE.LineLoop(geometryMapBounds, materialMapBounds);
460 GnuBoat.private.renderScene.add(GnuBoat.private.renderMapBounds);
461
462 // add 500m grid
463 if(GnuBoat.private.renderGrid != null)
464 GnuBoat.private.renderScene.remove(GnuBoat.private.renderGrid);
465 geometryMapBounds.computeBoundingBox();
466 var maxLength = Math.max(geometryMapBounds.boundingBox.getSize().x, geometryMapBounds.boundingBox.getSize().y);
[9b1c16f]467 GnuBoat.private.renderGrid = new THREE.GridHelper(maxLength, Math.round(maxLength / 500.0), 0xCCCCCC, 0xCCCCCC);
[17bc29b]468 GnuBoat.private.renderGrid.geometry.rotateX( Math.PI / 2 );
469 GnuBoat.private.renderGrid.translateX(maxLength / 2.0);
470 GnuBoat.private.renderGrid.translateY(maxLength / 2.0);
471 GnuBoat.private.renderScene.add(GnuBoat.private.renderGrid);
472
[9b1c16f]473 var distance = 150;
[17bc29b]474 var fieldOfView = 2 * Math.atan(maxLength / (2.0 * distance) ) * 180.0 / Math.PI;
[9b1c16f]475 GnuBoat.private.renderCamera.position.set(-10, -5, 100);
[17bc29b]476 GnuBoat.private.renderCamera.fov = fieldOfView;
[9b1c16f]477 GnuBoat.private.renderCamera.lookAt(GnuBoat.private.renderScene.position);
478 GnuBoat.private.renderCamera.zoom = 5;
[17bc29b]479 GnuBoat.private.renderCamera.updateProjectionMatrix();
480 },
481
[9b1c16f]482 drawDensityEstimation: function(densityPoints, maximum, minimum)
[17bc29b]483 {
[9b1c16f]484 var material = new THREE.PointsMaterial({vertexColors: THREE.VertexColors, size: 5});
[17bc29b]485 var geometry = new THREE.Geometry();
486 var mercatorSouthWest = {x: 0, y: 0};
[9b1c16f]487 if(GnuBoat.private.mapBounds != null)
[17bc29b]488 mercatorSouthWest = GnuBoat.private.mercator(GnuBoat.private.mapBounds.getSouth(), GnuBoat.private.mapBounds.getWest());
489
490 for(var i = 0; i < densityPoints.length; i++){
491 var mercator = GnuBoat.private.mercator(densityPoints[i].latitude, densityPoints[i].longitude);
[9b1c16f]492 var vertex = new THREE.Vector3(mercator.x - mercatorSouthWest.x, mercator.y - mercatorSouthWest.y, (densityPoints[i].depth * -1) + maximum);
493 var color = new THREE.Color(0x000000);
494 if(GnuBoat.private.renderDepthSteps != null){
495 var step = Math.floor((densityPoints[i].depth - minimum)/((maximum-minimum)/GnuBoat.private.renderDepthSteps));
496 if(step >= 0 && step < GnuBoat.private.renderDepthMaterials.length)
497 color = GnuBoat.private.renderDepthMaterials[step];
498 }
[17bc29b]499 geometry.vertices.push(vertex);
[9b1c16f]500 geometry.colors.push(color);
[17bc29b]501 }
502 // point cloud
503 if(GnuBoat.private.renderCloud != null)
504 GnuBoat.private.renderScene.remove(GnuBoat.private.renderCloud);
505 GnuBoat.private.renderCloud = new THREE.Points(geometry, material);
506 GnuBoat.private.renderScene.add(GnuBoat.private.renderCloud);
507 },
508
509 animate: function()
510 {
511 requestAnimationFrame(GnuBoat.private.animate);
[9b1c16f]512 if(GnuBoat.private.renderCloud != null){
513 var scaleZ = $("#renderScaleZ").val();
514 if(GnuBoat.private.renderCloud.scale.z != scaleZ)
515 GnuBoat.private.renderCloud.scale.set(1, 1, scaleZ);
516 }
[17bc29b]517
[9b1c16f]518 GnuBoat.private.renderControls.update();
[17bc29b]519 GnuBoat.private.renderRenderer.render(GnuBoat.private.renderScene, GnuBoat.private.renderCamera);
520 },
521
522 mercator: function(latitude, longitude)
523 {
524 var result = {};
525
526 var earthRadius = 6378137.0;
527
528 result["x"] = longitude / 180.0 * Math.PI * earthRadius;
529 result["y"] = Math.log(Math.tan(latitude / 180.0 * Math.PI / 2.0 + Math.PI / 4.0)) * earthRadius;
530
531 return result;
532 },
[008bce9]533
534 updateMapModus: function(data)
535 {
536 var selected = $("#mapModusSelect").val();
537 $("#mapModusSelect").empty();
538 var selectedFound = false;
539 if(selected != null && Array.isArray(data)){
540 data.forEach(function(value, index){
541 if(value == selected)
542 selectedFound = true;
543 });
544 }
545 var selectedString = "";
546 if(!selectedFound)
547 selectedString = " selected";
548 $('<option value="live"'+ selectedString +'>Live View</option>').appendTo("#mapModusSelect");
549 if(!Array.isArray(data) || data.length < 1){
550 console.log(data);
551 return;
552 }
553 if(("error" in data) && data["error"] == 1){
554 GnuBoat.displayMessage('<span class="error">Failed to upload map: '+ data["errormessage"] +'</span>');
555 return;
556 }
557 data.forEach(function(value, index){
558 var selectedString = "";
559 if(value == selected)
560 selectedString = " selected";
561 $("<option value=\""+ value +"\""+ selected +">"+ value +"</option>").appendTo("#mapModusSelect");
562 });
563 },
564
565 mapModusChanged: function()
566 {
567 var modus = $("#mapModusSelect").val();
568 // live modus is automatically activated on displayDataSet
569 if(modus == "live"){
570 GnuBoat.private.mapModusLive = true;
571 return;
572 }
573 // remove overlays
574 if(GnuBoat.private.mapControl != null){
575 for(var key in GnuBoat.private.dataMappings){
576 if(GnuBoat.private.dataMappings[key]["map"] === true)
577 if(key in GnuBoat.private.mapPolylines){
578 GnuBoat.private.mapControl.removeLayer(GnuBoat.private.mapPolylines[key]);
579 GnuBoat.private.mapPolylines[key].remove();
580 delete GnuBoat.private.mapPolylines[key];
581 }
582 }
583 }
584 GnuBoat.private.mapModusLive = false;
585 $.post({
586 url: GnuBoat.private.serverBaseUrl +'?request=replay',
587 data: { logfile: modus },
588 success: function(data){
589// if(!Array.isArray(data) || data.length < 1){
590// console.log("No data array returned!");
591// console.log(data);
592// return;
593// }
594 if(("error" in data) && data["error"] == 1){
595 GnuBoat.displayMessage('<span class="error">Failed to get logged tracks: '+ data["errormessage"] +'</span>');
596 return;
597 }
598 if(!("track" in data)){
599 console.log("No track given!");
600 return;
601 }
602 GnuBoat.private.mapAddTracks(data["track"]);
603 }
604 });
605 },
606
607 mapAddTracks: function(track)
608 {
609 if(GnuBoat.private.map == null){
610 console.log("No map in use");
611 return;
612 }
613
614 var maximas = [];
615 var minimas = [];
616 var points = [];
617 var densityPoints = [];
618
619 var keysMap = [];
620 for(var key in GnuBoat.private.dataMappings){
621 if(GnuBoat.private.dataMappings[key]["map"] === true)
622 keysMap.push(key);
623 }
624 for(var i = 0; i < keysMap.length; i++){
625 points[keysMap[i]] = [];
626 }
627
628 // getting maximas, minimums for normalization and the tracks themselves
629 for(var time in track){
630 for(var i = 0; i < keysMap.length; i++){
631 var key = keysMap[i];
632 if(key == "gpsfix")
633 continue;
634 if(!(key in maximas))
635 maximas[key] = track[time][key]
636 else if(maximas[key] < track[time][key])
637 maximas[key] = track[time][key];
638 if(!(key in minimas))
639 minimas[key] = track[time][key]
640 else if(minimas[key] > track[time][key])
641 minimas[key] = track[time][key];
642 points[key].push(new L.latLng(track[time]["latitude"], track[time]["longitude"], track[time][key]));
643 densityPoints.push({latitude: track[time]["latitude"], longitude: track[time]["longitude"], depth: track[time]["waterdepth"]});
644 }
645 }
646 for(var i = 0; i < keysMap.length; i++){
647 var key = keysMap[i];
648 if(points[key].length > 0){
649 if(key in GnuBoat.private.mapPolylines)
650 GnuBoat.private.mapPolylines[key].setLatLngs(points[key]);
651 else{
652 GnuBoat.private.mapPolylines[key] = L.hotline(points[key], {weight: 3, outlineWidth: 0, min: minimas[key], max: maximas[key]});
653 if(GnuBoat.private.mapControl){
654 var name = GnuBoat.private.dataMappings[key]["titleLong"] +" in "+ GnuBoat.private.dataMappings[key]["unit"] +" ("+ GnuBoat.private.dataMappings[key]["titleShort"] +")";
655 GnuBoat.private.mapControl.addOverlay(GnuBoat.private.mapPolylines[key], name);
656 }
657 if(GnuBoat.private.dataMappings[key]["mapActive"] === true)
658 GnuBoat.private.mapPolylines[key].addTo(GnuBoat.private.map);
659 }
660 }
661 }
662 GnuBoat.private.drawDensityEstimation(densityPoints, maximas["waterdepth"], minimas["waterdepth"]);
663 },
[4ce069e]664};
Note: See TracBrowser for help on using the repository browser.