Projekt

Obecné

Profil

Stáhnout (6.01 KB) Statistiky
| Větev: | Revize:
1 64bc2934 vastja
var mymap;
2 03c02899 vastja
var heatmapLayer = null;
3
4 c236b33a msebela
var startX = 49.7248;
5
var startY = 13.3521;
6
var startZoom = 17;
7 3fc08f2d vastja
8 a48642fb vastja
var dataSourceRoute;
9
var currentTime;
10
var name;
11
var date;
12
13
var timer;
14
var isAnimationRunning = false;
15
var data = [];
16
17 351696d5 Martin Sebela
18
19 c236b33a msebela
function initMap() {
20 64bc2934 vastja
  mymap = L.map('heatmap').setView([startX, startY], startZoom);
21 3fc08f2d vastja
22 c236b33a msebela
  L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
23
    attribution: '',
24
    maxZoom: 19
25 64bc2934 vastja
  }).addTo(mymap);
26 3ae59f75 vastja
27
  mymap.on('click', showInfo);
28
29 c236b33a msebela
}
30 3fc08f2d vastja
31 3ae59f75 vastja
var info = [];
32
var currenInfo = 0;
33
var infoLength = 0;
34
35
function showInfo(e) {
36
37
  info = []
38
  currenInfo = 0;
39
40
  // https://wiki.openstreetmap.org/wiki/Zoom_levels
41
  // Todo change to variable - it is used in heatmap init
42
  var stile =  40075016.686 * Math.cos(startX) / Math.pow(2, mymap.getZoom()) ;
43
  var radius = 25 * stile / 256;
44
45
  var i = 0;
46
  var lat = 0;
47
  var lng = 0;
48
49
  data[currentTime]['items'].forEach(element => {
50
    if (e.latlng.distanceTo(new L.LatLng(element.x, element.y)) < radius) {
51
      lat += element.x
52
      lng += element.y;
53
      info[i] = {'place' : element.place, 'number' : element.number};
54
      i++;
55
    }
56
  });
57
58
  
59
  if (info.length > 0) {
60 dfcface9 Tomáš Ballák
    var popup = L.popup({
61
      autoPan: false
62
                  })
63 3ae59f75 vastja
                  .setLatLng([lat / i, lng / i])
64 bc7738cd Martin Sebela
                  .setContent('<strong>Zařízení a počet:</strong><div id=\'place-info\'>' + info[currenInfo].place + '</div>' +
65
                              '<div id=\'number-info\'>' + info[currenInfo].number + '</div>' +
66 3ae59f75 vastja
                              '<div class=\'popup-controls\'>' +
67 dfcface9 Tomáš Ballák
                                '<button id=\'previous-info-btn\' class=\'circle-button\' onclick=\'previousInfo()\'></button>' +
68 3ae59f75 vastja
                                '<div id=\'count-info\'>' + (currenInfo + 1) + '/' + info.length + '</div>' +
69 dfcface9 Tomáš Ballák
                                '<button id=\'next-info-btn\' onclick=\'nextInfo()\' class=\'circle-button next\'></button>' +
70 3ae59f75 vastja
                              '</div>'
71
                              )
72
                  .openOn(mymap);
73
    
74
    if (info.length == 1) {
75
      $('#previous-info-btn').prop('disabled', true);
76
      $('#next-info-btn').prop('disabled', true);
77
      $('.popup-controls').hide();
78
    }
79
  }
80
81
}
82
83
function previousInfo() {
84
  currenInfo = (currenInfo + info.length - 1) % info.length;
85
  displayInfoText();
86
}
87
88
function nextInfo() {
89
  currenInfo = (currenInfo + 1) % info.length
90
  displayInfoText();
91
}
92
93
function displayInfoText() {
94 bc7738cd Martin Sebela
  $('#place-info').html(info[currenInfo].place)
95
  $('#number-info').html(info[currenInfo].number)
96 3ae59f75 vastja
  $('#count-info').html(currenInfo + 1 + '/' + info.length);
97
}
98 351696d5 Martin Sebela
99 c236b33a msebela
function setMapView(latitude = startX, longitude = startY, zoom = startZoom) {
100 64bc2934 vastja
  mymap.setView([latitude, longitude], zoom);
101 3fc08f2d vastja
}
102
103 a48642fb vastja
function changeAnimationState() {
104
105
  isAnimationRunning = !isAnimationRunning
106
  if (isAnimationRunning) {
107
    $('#play-pause').attr('class', 'pause');
108
    timer = setInterval(
109
      function() {
110
        next();
111
      },
112
      800
113
    );
114 351696d5 Martin Sebela
  }
115 a48642fb vastja
  else {
116
    clearTimeout(timer);
117
    $('#play-pause').attr('class', 'play');
118 351696d5 Martin Sebela
  }
119
120 a48642fb vastja
121 351696d5 Martin Sebela
}
122
123 a48642fb vastja
function previous() {
124
  currentTime = (currentTime + 23) % 24;
125
  drawHeatmap(data[currentTime]);
126
  setTimeline();
127 4e8c0e5b Martin Sebela
  mymap.closePopup();
128
  updateHeaderControls();
129 8b840eb7 vastja
  changeUrl();
130 a48642fb vastja
}
131
132
function next() {
133
  currentTime = (currentTime + 1) % 24;
134
  drawHeatmap(data[currentTime]);
135
  setTimeline();
136 4e8c0e5b Martin Sebela
  mymap.closePopup();
137
  updateHeaderControls();
138 8b840eb7 vastja
  changeUrl();
139
}
140
141
function changeUrl() {
142
  window.history.pushState(
143
    "",
144
    document.title,
145
    window.location.origin + window.location.pathname + `?data_set[date]=${$('#date').val()}&data_set[time]=${currentTime}&data_set[type]=${$('#type').children("option:selected").val()}`
146
  );
147 4e8c0e5b Martin Sebela
}
148
149
function updateHeaderControls() {
150
  document.getElementById('time').value = currentTime;
151 a48642fb vastja
}
152 351696d5 Martin Sebela
153 a48642fb vastja
function setTimeline() {
154
  $('#timeline').text(currentTime + ":00");
155
  $('#timeline').attr('class', 'time hour-' + currentTime);
156 351696d5 Martin Sebela
}
157
158 a48642fb vastja
function loadCurrentTimeHeatmap(route) {
159 64bc2934 vastja
160 a48642fb vastja
  dataSourceRoute = route;
161
  data = []
162
163
  name = $('#type').children("option:selected").text();
164 03ccdd65 vastja
  date = $('#date').val()
165 a48642fb vastja
  currentTime = parseInt($('#time').children("option:selected").val());
166
  setTimeline();
167 351696d5 Martin Sebela
168 c236b33a msebela
  $.ajax({
169
    type: "POST",
170 a48642fb vastja
    url: dataSourceRoute + '/' +  name + '/' + date + '/' + currentTime,
171 c236b33a msebela
    success: function(result) {
172 a48642fb vastja
      data[currentTime] = result;
173
      drawHeatmap(data[currentTime]);
174 c236b33a msebela
    }
175
  });
176 a48642fb vastja
177
  preload(currentTime, 1);
178
  preload(currentTime, -1);
179
180
}
181
182
function preload(time, change) {
183
184
  var ntime = time + change; 
185
  if (0 <=  ntime && ntime <= 23) {
186
      $.ajax({
187
        type: "POST",
188
        url: dataSourceRoute + '/' +  name + '/' + date + '/' + ntime,
189
        success: function(result) {
190
          data[ntime] = result;
191
          preload(ntime, change);
192
        }
193
      });
194
  }
195
196 3fc08f2d vastja
}
197
198 351696d5 Martin Sebela
199 3ae59f75 vastja
function drawHeatmap(data) {
200 03c02899 vastja
201
  // Todo still switched
202 3ae59f75 vastja
  if (data['items'] != null) {
203
    points = data['items'].map(function (p) { return [p['x'], p['y'], p['number']]; });
204 a48642fb vastja
    if (heatmapLayer != null) {
205
      mymap.removeLayer(heatmapLayer);
206
    }
207 3fc22d29 Martin Sebela
    heatmapLayer = L.heatLayer(points, {'max' : data['max'], 'minOpacity' : 0.5, 'radius' : 35, 'blur' : 30}).addTo(mymap);
208 03c02899 vastja
  }
209 a48642fb vastja
  else {
210
    if (heatmapLayer != null) {
211
      mymap.removeLayer(heatmapLayer);
212
    }
213
  }
214
215 03c02899 vastja
  // var heat_01 = ...
216
  // on background map.addLayer(heat_01) -> map.removeLayer(heat_01);
217 64bc2934 vastja
  // $(.leaflet-heatmap-layer).css('opacity', 'value');
218 03c02899 vastja
}
219 3fc08f2d vastja
220 03c02899 vastja
function checkDataSetsAvailability(route) {
221
222
  $.ajax({
223
    type: "POST",
224
    // Todo it might be good idea to change db collections format
225 03ccdd65 vastja
    url: route + '/' + $('#date').val(),
226 03c02899 vastja
    success: function(result) {
227
      updateAvailableDataSets(result);
228
    }
229
  });
230
}
231
232
function updateAvailableDataSets(available) {
233
  
234 5d599617 vastja
  var isOptionEnabled = true;
235
  $("#type > option").each(function() {
236
    if((this.value in available) == false) {
237
      $(this).prop('disabled', true)
238
      $(this).prop('selected', false)  
239
    }
240
    else {
241
      $(this).prop('disabled', false)
242
      if (isOptionEnabled) {
243
        $(this).prop('selected', true)
244
      } 
245
      isOptionEnabled = false;
246
    }
247 0919408a vastja
  });
248 03c02899 vastja
249 5d599617 vastja
  $('#submit-btn').prop('disabled', isOptionEnabled);
250 dfe43218 vastja
251 03c02899 vastja
}