Projekt

Obecné

Profil

Stáhnout (6.73 KB) Statistiky
| Větev: | Revize:
1 64bc2934 vastja
var mymap;
2 03c02899 vastja
var heatmapLayer = null;
3 61ff7718 vastja
var marksLayer = null;
4 03c02899 vastja
5 c236b33a msebela
var startX = 49.7248;
6
var startY = 13.3521;
7
var startZoom = 17;
8 3fc08f2d vastja
9 a48642fb vastja
var dataSourceRoute;
10
var currentTime;
11
var name;
12
var date;
13
14
var timer;
15
var isAnimationRunning = false;
16
var data = [];
17
18 351696d5 Martin Sebela
19
20 c236b33a msebela
function initMap() {
21 64bc2934 vastja
  mymap = L.map('heatmap').setView([startX, startY], startZoom);
22 3fc08f2d vastja
23 c236b33a msebela
  L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
24
    attribution: '',
25
    maxZoom: 19
26 64bc2934 vastja
  }).addTo(mymap);
27 3ae59f75 vastja
28
  mymap.on('click', showInfo);
29
30 c236b33a msebela
}
31 3fc08f2d vastja
32 3ae59f75 vastja
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 61ff7718 vastja
  var total = 0;
51 3ae59f75 vastja
  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 61ff7718 vastja
      total += parseInt(element.number);
57 3ae59f75 vastja
      i++;
58
    }
59
  });
60
61
  
62
  if (info.length > 0) {
63 dfcface9 Tomáš Ballák
    var popup = L.popup({
64
      autoPan: false
65
                  })
66 3ae59f75 vastja
                  .setLatLng([lat / i, lng / i])
67 bc7738cd Martin Sebela
                  .setContent('<strong>Zařízení a počet:</strong><div id=\'place-info\'>' + info[currenInfo].place + '</div>' +
68 61ff7718 vastja
                              '<div id=\'number-info\'><span id=\'digit-info\'>' + info[currenInfo].number + '</span>' + '<span id=\'total-info\' style="font-size: large">/' + total +'</span></div>' + 
69 3ae59f75 vastja
                              '<div class=\'popup-controls\'>' +
70 dfcface9 Tomáš Ballák
                                '<button id=\'previous-info-btn\' class=\'circle-button\' onclick=\'previousInfo()\'></button>' +
71 61ff7718 vastja
                                '<div id=\'count-info\'>' + (currenInfo + 1) + ' z ' + info.length + '</div>' +
72 dfcface9 Tomáš Ballák
                                '<button id=\'next-info-btn\' onclick=\'nextInfo()\' class=\'circle-button next\'></button>' +
73 3ae59f75 vastja
                              '</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 bc7738cd Martin Sebela
  $('#place-info').html(info[currenInfo].place)
98 61ff7718 vastja
  $('#digit-info').html(info[currenInfo].number)
99
  $('#count-info').html(currenInfo + 1 + ' z ' + info.length);
100 3ae59f75 vastja
}
101 351696d5 Martin Sebela
102 c236b33a msebela
function setMapView(latitude = startX, longitude = startY, zoom = startZoom) {
103 64bc2934 vastja
  mymap.setView([latitude, longitude], zoom);
104 3fc08f2d vastja
}
105
106 a48642fb vastja
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 351696d5 Martin Sebela
  }
118 a48642fb vastja
  else {
119
    clearTimeout(timer);
120
    $('#play-pause').attr('class', 'play');
121 351696d5 Martin Sebela
  }
122
123 a48642fb vastja
124 351696d5 Martin Sebela
}
125
126 a48642fb vastja
function previous() {
127
  currentTime = (currentTime + 23) % 24;
128
  drawHeatmap(data[currentTime]);
129
  setTimeline();
130 4e8c0e5b Martin Sebela
  mymap.closePopup();
131
  updateHeaderControls();
132 8b840eb7 vastja
  changeUrl();
133 a48642fb vastja
}
134
135
function next() {
136
  currentTime = (currentTime + 1) % 24;
137
  drawHeatmap(data[currentTime]);
138
  setTimeline();
139 4e8c0e5b Martin Sebela
  mymap.closePopup();
140
  updateHeaderControls();
141 8b840eb7 vastja
  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 4e8c0e5b Martin Sebela
}
151
152
function updateHeaderControls() {
153
  document.getElementById('time').value = currentTime;
154 a48642fb vastja
}
155 351696d5 Martin Sebela
156 a48642fb vastja
function setTimeline() {
157
  $('#timeline').text(currentTime + ":00");
158
  $('#timeline').attr('class', 'time hour-' + currentTime);
159 351696d5 Martin Sebela
}
160
161 61ff7718 vastja
function loadCurrentTimeHeatmap(opendataRoute, positionsRoute) {
162 64bc2934 vastja
163 a48642fb vastja
  data = []
164
165 61ff7718 vastja
  name = $('#type').children("option:selected").val();
166 03ccdd65 vastja
  date = $('#date').val()
167 a48642fb vastja
  currentTime = parseInt($('#time').children("option:selected").val());
168
  setTimeline();
169 351696d5 Martin Sebela
170 c236b33a msebela
  $.ajax({
171
    type: "POST",
172 61ff7718 vastja
    url: opendataRoute + '/' +  name + '/' + date + '/' + currentTime,
173 c236b33a msebela
    success: function(result) {
174 a48642fb vastja
      data[currentTime] = result;
175
      drawHeatmap(data[currentTime]);
176 c236b33a msebela
    }
177
  });
178 a48642fb vastja
179 61ff7718 vastja
  $.ajax({
180
    type: 'POST',
181
    url: positionsRoute + '/' + name,
182
    success: function(result) {
183
      drawDataSourceMarks(result)
184
    }
185
  });
186
187 a48642fb vastja
  preload(currentTime, 1);
188
  preload(currentTime, -1);
189
190
}
191
192 61ff7718 vastja
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 a48642fb vastja
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 3fc08f2d vastja
}
222
223 351696d5 Martin Sebela
224 3ae59f75 vastja
function drawHeatmap(data) {
225 03c02899 vastja
226
  // Todo still switched
227 3ae59f75 vastja
  if (data['items'] != null) {
228
    points = data['items'].map(function (p) { return [p['x'], p['y'], p['number']]; });
229 a48642fb vastja
    if (heatmapLayer != null) {
230
      mymap.removeLayer(heatmapLayer);
231
    }
232 3fc22d29 Martin Sebela
    heatmapLayer = L.heatLayer(points, {'max' : data['max'], 'minOpacity' : 0.5, 'radius' : 35, 'blur' : 30}).addTo(mymap);
233 03c02899 vastja
  }
234 a48642fb vastja
  else {
235
    if (heatmapLayer != null) {
236
      mymap.removeLayer(heatmapLayer);
237
    }
238
  }
239
240 03c02899 vastja
  // var heat_01 = ...
241
  // on background map.addLayer(heat_01) -> map.removeLayer(heat_01);
242 64bc2934 vastja
  // $(.leaflet-heatmap-layer).css('opacity', 'value');
243 03c02899 vastja
}
244 3fc08f2d vastja
245 03c02899 vastja
function checkDataSetsAvailability(route) {
246
247
  $.ajax({
248
    type: "POST",
249
    // Todo it might be good idea to change db collections format
250 03ccdd65 vastja
    url: route + '/' + $('#date').val(),
251 03c02899 vastja
    success: function(result) {
252
      updateAvailableDataSets(result);
253
    }
254
  });
255
}
256
257 2dd5d57f vastja
var allOptionsDisabled = false;
258
259 03c02899 vastja
function updateAvailableDataSets(available) {
260
  
261 5d599617 vastja
  var isOptionEnabled = true;
262
  $("#type > option").each(function() {
263
    if((this.value in available) == false) {
264 2dd5d57f vastja
      $(this).prop('disabled', true);
265
      $(this).prop('selected', false);  
266 5d599617 vastja
    }
267
    else {
268
      $(this).prop('disabled', false)
269 2dd5d57f vastja
      if (allOptionsDisabled) {
270
        $(this).prop('selected', true);
271
        allOptionsDisabled = false;
272
      }
273 5d599617 vastja
      isOptionEnabled = false;
274
    }
275 0919408a vastja
  });
276 2dd5d57f vastja
  allOptionsDisabled = isOptionEnabled;
277 03c02899 vastja
278 5d599617 vastja
  $('#submit-btn').prop('disabled', isOptionEnabled);
279 dfe43218 vastja
280 03c02899 vastja
}