Projekt

Obecné

Profil

Stáhnout (6.73 KB) Statistiky
| Větev: | Revize:
1
var mymap;
2
var heatmapLayer = null;
3
var marksLayer = null;
4

    
5
var startX = 49.7248;
6
var startY = 13.3521;
7
var startZoom = 17;
8

    
9
var dataSourceRoute;
10
var currentTime;
11
var name;
12
var date;
13

    
14
var timer;
15
var isAnimationRunning = false;
16
var data = [];
17

    
18

    
19

    
20
function initMap() {
21
  mymap = L.map('heatmap').setView([startX, startY], startZoom);
22

    
23
  L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
24
    attribution: '',
25
    maxZoom: 19
26
  }).addTo(mymap);
27

    
28
  mymap.on('click', showInfo);
29

    
30
}
31

    
32
var info = [];
33
var currenInfo = 0;
34
var infoLength = 0;
35

    
36
function showInfo(e) {
37

    
38
  info = []
39
  currenInfo = 0;
40

    
41
  // https://wiki.openstreetmap.org/wiki/Zoom_levels
42
  // Todo change to variable - it is used in heatmap init
43
  var stile =  40075016.686 * Math.cos(startX) / Math.pow(2, mymap.getZoom()) ;
44
  var radius = 25 * stile / 256;
45

    
46
  var i = 0;
47
  var lat = 0;
48
  var lng = 0;
49

    
50
  var total = 0;
51
  data[currentTime]['items'].forEach(element => {
52
    if (e.latlng.distanceTo(new L.LatLng(element.x, element.y)) < radius) {
53
      lat += element.x
54
      lng += element.y;
55
      info[i] = {'place' : element.place, 'number' : element.number};
56
      total += parseInt(element.number);
57
      i++;
58
    }
59
  });
60

    
61
  
62
  if (info.length > 0) {
63
    var popup = L.popup({
64
      autoPan: false
65
                  })
66
                  .setLatLng([lat / i, lng / i])
67
                  .setContent('<strong>Zařízení a počet:</strong><div id=\'place-info\'>' + info[currenInfo].place + '</div>' +
68
                              '<div id=\'number-info\'><span id=\'digit-info\'>' + info[currenInfo].number + '</span>' + '<span id=\'total-info\' style="font-size: large">/' + total +'</span></div>' + 
69
                              '<div class=\'popup-controls\'>' +
70
                                '<button id=\'previous-info-btn\' class=\'circle-button\' onclick=\'previousInfo()\'></button>' +
71
                                '<div id=\'count-info\'>' + (currenInfo + 1) + ' z ' + info.length + '</div>' +
72
                                '<button id=\'next-info-btn\' onclick=\'nextInfo()\' class=\'circle-button next\'></button>' +
73
                              '</div>'
74
                              )
75
                  .openOn(mymap);
76
    
77
    if (info.length == 1) {
78
      $('#previous-info-btn').prop('disabled', true);
79
      $('#next-info-btn').prop('disabled', true);
80
      $('.popup-controls').hide();
81
    }
82
  }
83

    
84
}
85

    
86
function previousInfo() {
87
  currenInfo = (currenInfo + info.length - 1) % info.length;
88
  displayInfoText();
89
}
90

    
91
function nextInfo() {
92
  currenInfo = (currenInfo + 1) % info.length
93
  displayInfoText();
94
}
95

    
96
function displayInfoText() {
97
  $('#place-info').html(info[currenInfo].place)
98
  $('#digit-info').html(info[currenInfo].number)
99
  $('#count-info').html(currenInfo + 1 + ' z ' + info.length);
100
}
101

    
102
function setMapView(latitude = startX, longitude = startY, zoom = startZoom) {
103
  mymap.setView([latitude, longitude], zoom);
104
}
105

    
106
function changeAnimationState() {
107

    
108
  isAnimationRunning = !isAnimationRunning
109
  if (isAnimationRunning) {
110
    $('#play-pause').attr('class', 'pause');
111
    timer = setInterval(
112
      function() {
113
        next();
114
      },
115
      800
116
    );
117
  }
118
  else {
119
    clearTimeout(timer);
120
    $('#play-pause').attr('class', 'play');
121
  }
122

    
123

    
124
}
125

    
126
function previous() {
127
  currentTime = (currentTime + 23) % 24;
128
  drawHeatmap(data[currentTime]);
129
  setTimeline();
130
  mymap.closePopup();
131
  updateHeaderControls();
132
  changeUrl();
133
}
134

    
135
function next() {
136
  currentTime = (currentTime + 1) % 24;
137
  drawHeatmap(data[currentTime]);
138
  setTimeline();
139
  mymap.closePopup();
140
  updateHeaderControls();
141
  changeUrl();
142
}
143

    
144
function changeUrl() {
145
  window.history.pushState(
146
    "",
147
    document.title,
148
    window.location.origin + window.location.pathname + `?data_set[date]=${$('#date').val()}&data_set[time]=${currentTime}&data_set[type]=${$('#type').children("option:selected").val()}`
149
  );
150
}
151

    
152
function updateHeaderControls() {
153
  document.getElementById('time').value = currentTime;
154
}
155

    
156
function setTimeline() {
157
  $('#timeline').text(currentTime + ":00");
158
  $('#timeline').attr('class', 'time hour-' + currentTime);
159
}
160

    
161
function loadCurrentTimeHeatmap(opendataRoute, positionsRoute) {
162

    
163
  data = []
164

    
165
  name = $('#type').children("option:selected").val();
166
  date = $('#date').val()
167
  currentTime = parseInt($('#time').children("option:selected").val());
168
  setTimeline();
169

    
170
  $.ajax({
171
    type: "POST",
172
    url: opendataRoute + '/' +  name + '/' + date + '/' + currentTime,
173
    success: function(result) {
174
      data[currentTime] = result;
175
      drawHeatmap(data[currentTime]);
176
    }
177
  });
178

    
179
  $.ajax({
180
    type: 'POST',
181
    url: positionsRoute + '/' + name,
182
    success: function(result) {
183
      drawDataSourceMarks(result)
184
    }
185
  });
186

    
187
  preload(currentTime, 1);
188
  preload(currentTime, -1);
189

    
190
}
191

    
192
function drawDataSourceMarks(data) {
193

    
194
  if (marksLayer != null) {
195
    L.removeLayer(marksLayer);
196
  }
197

    
198
  marksLayer = L.layerGroup();
199
  for (var key in data) {
200
    marksLayer.addLayer(L.circle([data[key]['x'], data[key]['y']], {radius: 1, color: 'black'}));
201
  }
202

    
203
  marksLayer.addTo(mymap);
204

    
205
}
206

    
207
function preload(time, change) {
208

    
209
  var ntime = time + change; 
210
  if (0 <=  ntime && ntime <= 23) {
211
      $.ajax({
212
        type: "POST",
213
        url: dataSourceRoute + '/' +  name + '/' + date + '/' + ntime,
214
        success: function(result) {
215
          data[ntime] = result;
216
          preload(ntime, change);
217
        }
218
      });
219
  }
220

    
221
}
222

    
223

    
224
function drawHeatmap(data) {
225

    
226
  // Todo still switched
227
  if (data['items'] != null) {
228
    points = data['items'].map(function (p) { return [p['x'], p['y'], p['number']]; });
229
    if (heatmapLayer != null) {
230
      mymap.removeLayer(heatmapLayer);
231
    }
232
    heatmapLayer = L.heatLayer(points, {'max' : data['max'], 'minOpacity' : 0.5, 'radius' : 35, 'blur' : 30}).addTo(mymap);
233
  }
234
  else {
235
    if (heatmapLayer != null) {
236
      mymap.removeLayer(heatmapLayer);
237
    }
238
  }
239

    
240
  // var heat_01 = ...
241
  // on background map.addLayer(heat_01) -> map.removeLayer(heat_01);
242
  // $(.leaflet-heatmap-layer).css('opacity', 'value');
243
}
244

    
245
function checkDataSetsAvailability(route) {
246

    
247
  $.ajax({
248
    type: "POST",
249
    // Todo it might be good idea to change db collections format
250
    url: route + '/' + $('#date').val(),
251
    success: function(result) {
252
      updateAvailableDataSets(result);
253
    }
254
  });
255
}
256

    
257
var allOptionsDisabled = false;
258

    
259
function updateAvailableDataSets(available) {
260
  
261
  var isOptionEnabled = true;
262
  $("#type > option").each(function() {
263
    if((this.value in available) == false) {
264
      $(this).prop('disabled', true);
265
      $(this).prop('selected', false);  
266
    }
267
    else {
268
      $(this).prop('disabled', false)
269
      if (allOptionsDisabled) {
270
        $(this).prop('selected', true);
271
        allOptionsDisabled = false;
272
      }
273
      isOptionEnabled = false;
274
    }
275
  });
276
  allOptionsDisabled = isOptionEnabled;
277

    
278
  $('#submit-btn').prop('disabled', isOptionEnabled);
279

    
280
}
(8-8/8)