Projekt

Obecné

Profil

Stáhnout (5.85 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 3ae59f75 vastja
        mymap.closePopup();
111 a48642fb vastja
        next();
112
      },
113
      800
114
    );
115 351696d5 Martin Sebela
  }
116 a48642fb vastja
  else {
117
    clearTimeout(timer);
118
    $('#play-pause').attr('class', 'play');
119 351696d5 Martin Sebela
  }
120
121 a48642fb vastja
122 351696d5 Martin Sebela
}
123
124 a48642fb vastja
function previous() {
125
  currentTime = (currentTime + 23) % 24;
126
  drawHeatmap(data[currentTime]);
127
  setTimeline();
128 4e8c0e5b Martin Sebela
  mymap.closePopup();
129
  updateHeaderControls();
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
}
139
140
function updateHeaderControls() {
141
  document.getElementById('time').value = currentTime;
142 a48642fb vastja
}
143 351696d5 Martin Sebela
144 a48642fb vastja
function setTimeline() {
145
  $('#timeline').text(currentTime + ":00");
146
  $('#timeline').attr('class', 'time hour-' + currentTime);
147 351696d5 Martin Sebela
}
148
149 a48642fb vastja
function loadCurrentTimeHeatmap(route) {
150 64bc2934 vastja
151 a48642fb vastja
  dataSourceRoute = route;
152
  data = []
153
154
  name = $('#type').children("option:selected").text();
155 03ccdd65 vastja
  date = $('#date').val()
156 a48642fb vastja
  currentTime = parseInt($('#time').children("option:selected").val());
157
  setTimeline();
158 351696d5 Martin Sebela
159 c236b33a msebela
  $.ajax({
160
    type: "POST",
161 a48642fb vastja
    url: dataSourceRoute + '/' +  name + '/' + date + '/' + currentTime,
162 c236b33a msebela
    success: function(result) {
163 a48642fb vastja
      data[currentTime] = result;
164
      drawHeatmap(data[currentTime]);
165 c236b33a msebela
    }
166
  });
167 a48642fb vastja
168
  preload(currentTime, 1);
169
  preload(currentTime, -1);
170
171
}
172
173
function preload(time, change) {
174
175
  var ntime = time + change; 
176
  if (0 <=  ntime && ntime <= 23) {
177
      $.ajax({
178
        type: "POST",
179
        url: dataSourceRoute + '/' +  name + '/' + date + '/' + ntime,
180
        success: function(result) {
181
          data[ntime] = result;
182
          preload(ntime, change);
183
        }
184
      });
185
  }
186
187 3fc08f2d vastja
}
188
189 351696d5 Martin Sebela
190 3ae59f75 vastja
function drawHeatmap(data) {
191 03c02899 vastja
192
  // Todo still switched
193 3ae59f75 vastja
  if (data['items'] != null) {
194
    points = data['items'].map(function (p) { return [p['x'], p['y'], p['number']]; });
195 a48642fb vastja
    if (heatmapLayer != null) {
196
      mymap.removeLayer(heatmapLayer);
197
    }
198 3fc22d29 Martin Sebela
    heatmapLayer = L.heatLayer(points, {'max' : data['max'], 'minOpacity' : 0.5, 'radius' : 35, 'blur' : 30}).addTo(mymap);
199 03c02899 vastja
  }
200 a48642fb vastja
  else {
201
    if (heatmapLayer != null) {
202
      mymap.removeLayer(heatmapLayer);
203
    }
204
  }
205
206 03c02899 vastja
  // var heat_01 = ...
207
  // on background map.addLayer(heat_01) -> map.removeLayer(heat_01);
208 64bc2934 vastja
  // $(.leaflet-heatmap-layer).css('opacity', 'value');
209 03c02899 vastja
}
210 3fc08f2d vastja
211 03c02899 vastja
function checkDataSetsAvailability(route) {
212
213
  $.ajax({
214
    type: "POST",
215
    // Todo it might be good idea to change db collections format
216 03ccdd65 vastja
    url: route + '/' + $('#date').val(),
217 03c02899 vastja
    success: function(result) {
218
      updateAvailableDataSets(result);
219
    }
220
  });
221
}
222
223 2dd5d57f vastja
var allOptionsDisabled = false;
224
225 03c02899 vastja
function updateAvailableDataSets(available) {
226
  
227 5d599617 vastja
  var isOptionEnabled = true;
228
  $("#type > option").each(function() {
229
    if((this.value in available) == false) {
230 2dd5d57f vastja
      $(this).prop('disabled', true);
231
      $(this).prop('selected', false);  
232 5d599617 vastja
    }
233
    else {
234
      $(this).prop('disabled', false)
235 2dd5d57f vastja
      if (allOptionsDisabled) {
236
        $(this).prop('selected', true);
237
        allOptionsDisabled = false;
238
      }
239 5d599617 vastja
      isOptionEnabled = false;
240
    }
241 0919408a vastja
  });
242 2dd5d57f vastja
  allOptionsDisabled = isOptionEnabled;
243 03c02899 vastja
244 5d599617 vastja
  $('#submit-btn').prop('disabled', isOptionEnabled);
245 dfe43218 vastja
246 03c02899 vastja
}