map.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * TraCINg-Server - Gathering and visualizing cyber incidents on the world
  3. *
  4. * Copyright 2013 Matthias Gazzari, Annemarie Mattmann, André Wolski
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. /**
  19. * Creates a 2d map based on jvectormaps
  20. * @param container the html element where the map will be displayed
  21. * @param map the name of the jvectormap map
  22. * @param backgroundColor the background color of the map
  23. */
  24. var map = function(container, map, backgroundColor) {
  25. var uniqueKey = 0; // unique key for marker id
  26. var maxKey = 500; // maximum amount of markers
  27. var incidentsPerCountry = {}; // incidents per country
  28. var incidents = 0; // total sum of incidents
  29. var countryCode = [maxKey]; // country code of every marker
  30. var mapObject = new jvm.WorldMap( { // jvectormap
  31. container: container,
  32. map: map,
  33. backgroundColor: backgroundColor,
  34. markers: [],
  35. series: {
  36. regions: [{ /* region means in our context country, as we are using a worldmap */
  37. // serie to display incidents per country
  38. attribute: "fill",
  39. min: -1,
  40. max: -2,
  41. scale: ['#FFFFFF', '#FF0000'],
  42. normalizeFunction: function(value) {
  43. // if min value
  44. if (value == -1)
  45. return 0;
  46. // if max value
  47. if (value == -2)
  48. return 1;
  49. // if there are no incidents return 0
  50. if (incidents == 0)
  51. return 0;
  52. // otherwise return (value/incidents)^(1/3)
  53. return Math.pow(value/incidents, 1/3);
  54. },
  55. }],
  56. },
  57. // show region label function
  58. onRegionLabelShow: function(e, el, code) {
  59. var attacks = incidentsPerCountry[code] || 0;
  60. if (attacks == 1)
  61. el.html(el.html() + " (" + attacks + " attack of " + incidents + " total)");
  62. else
  63. el.html(el.html() + " (" + attacks + " attacks of " + incidents + " total)");
  64. },
  65. onMarkerLabelShow: function(e, label, code) {
  66. // show only standard label information if advanced information is not requested (default is advanced)
  67. if (!advInfo) {
  68. var splittedName = label.text().split(";");
  69. label.html(splittedName[0]);
  70. } else {
  71. label.html(label.text());
  72. }
  73. }
  74. });
  75. /**
  76. * Reset the map removing every point
  77. */
  78. this.reset = function() {
  79. mapObject.removeAllMarkers();
  80. mapObject.series.regions[0].clear();
  81. uniqueKey = 0;
  82. incidents = 0;
  83. incidentsPerCountry = {};
  84. }
  85. /**
  86. * Zoom
  87. */
  88. this.zoom = function(value) {
  89. mapObject.setScale(mapObject.scale * value, mapObject.width / 2, mapObject.height / 2);
  90. }
  91. /**
  92. * Move the map
  93. */
  94. this.move = function(x, y) {
  95. var damp = mapObject.scale;
  96. mapObject.transX += x / damp;
  97. mapObject.transY += y / damp;
  98. mapObject.applyTransform();
  99. }
  100. /**
  101. * Mark incident on the map
  102. */
  103. this.addMarker = function(cc, ll, color, label, count) {
  104. // if the marker is already in use remove it first
  105. if (mapObject.markers[uniqueKey] != undefined) {
  106. removeMarker(uniqueKey);
  107. }
  108. incidents += count ? count : 1;
  109. incidentsPerCountry[cc] = (incidentsPerCountry[cc] | 0) + (count ? count : 1);
  110. countryCode[uniqueKey] = cc;
  111. mapObject.addMarker(uniqueKey, {latLng: ll.reverse(), style: {r: 5, fill: color}, name: label}, []);
  112. // redraw the region coloring
  113. // TODO consider doing this not on every new incident
  114. redrawIncidentsPerCountry();
  115. var returnMarker = uniqueKey;
  116. // increment key
  117. uniqueKey = (uniqueKey + 1) % maxKey;
  118. return returnMarker;
  119. }
  120. /**
  121. * Remove marker
  122. */
  123. function removeMarker(key) {
  124. var cc = countryCode[key];
  125. if (incidentsPerCountry[cc] > 0) {
  126. incidents--;
  127. incidentsPerCountry[cc] = incidentsPerCountry[cc] - 1;
  128. mapObject.removeMarkers([key]);
  129. redrawIncidentsPerCountry();
  130. }
  131. }
  132. this.removeMarker = removeMarker;
  133. /**
  134. * Get the pixel position of a geographic point
  135. */
  136. this.getPosition = function(latitude, longitude) {
  137. return mapObject.latLngToPoint(latitude, longitude);
  138. }
  139. /**
  140. * Redraw incidents per country
  141. */
  142. function redrawIncidentsPerCountry() {
  143. mapObject.series.regions[0].setValues(incidentsPerCountry);
  144. };
  145. /**
  146. * State whether any country has at least one marker
  147. */
  148. this.hasMarker = function() {
  149. if (incidents > 0)
  150. return true;
  151. return false;
  152. }
  153. }