Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********************************************************
  2.  
  3. TABLE OF CONTENTS
  4.  
  5. 1. MAP CONTAINER
  6. 2. MAP LAYERS
  7. 3. MAP LOADING
  8. 4. ZOOM - AUXILIARY FUNCTIONS
  9. 5. CITY NAMES - AUXILIARY FUNCTIONS
  10.  
  11. ********************************************************/
  12.  
  13. /******************************************************
  14.  
  15. 1. MAP CONTAINER
  16.  
  17. *******************************************************/
  18.  
  19. /* Make map fullscreen */
  20. var width = window.innerWidth,
  21.     height = window.innerHeight,
  22.     active = d3.select(null);
  23.  
  24. var svg = d3.select('svg')
  25.     .attr('width', width)
  26.     .attr('height', height)
  27.     .on('click', stopped, true);
  28.  
  29. svg.append('rect')
  30.     .attr('class', 'background')
  31.     .attr('width', width)
  32.     .attr('height', height)
  33.     .on('click', reset);
  34.  
  35. /* Define city random colors */
  36. var color = d3.scaleLinear()
  37.     .domain([1, 20])
  38.     .clamp(true)
  39.     .range(['#D5EA9F', '#91B92B']);
  40.  
  41. /* Define the appropriate map projection */
  42. var projection = d3.geoMercator()
  43.     .scale(40000)
  44.     .center([-16.85, 32.85])
  45.     .translate([width / 2, height / 2]);
  46.  
  47. var path = d3.geoPath()
  48.     .projection(projection);
  49.  
  50. /* Define zooming limits */
  51. var zoom = d3.zoom()
  52.     .scaleExtent([1, 45])
  53.     .on('zoom', zoomed);
  54.  
  55. /******************************************************
  56.  
  57. 2. MAP LAYERS
  58.  
  59. *******************************************************/
  60.  
  61. var mapContainer = svg.append('g');
  62. var mapText = svg.append('g');
  63.  
  64. var effectLayer = mapContainer.append('g').classed('effect-layer', true);
  65. var mapLayer = mapContainer.append('g').classed('map-layer', true);
  66.  
  67. /******************************************************
  68.  
  69. 3. MAP LOADING
  70.  
  71. *******************************************************/
  72.  
  73. /* Enable Zooming on the Map */
  74. svg.call(zoom);
  75.  
  76. /* Load the Map itself */
  77. d3.json('data/madeira.geo.json', function(error, mapData) {
  78.     if (error) throw error;
  79.  
  80.     var features = mapData.features;
  81.     color.domain([0, d3.max(features, nameLength)]);
  82.  
  83.     /* Display the Map */
  84.     mapLayer.selectAll('path')
  85.         .data(features)
  86.         .enter().append('path')
  87.         .attr('d', path)
  88.         .attr('vector-effect', 'non-scaling-stroke')
  89.         .style('fill', fillFn)
  90.         .on('mouseover', mouseover)
  91.         .on('mouseout', mouseout)
  92.         .on('click', clicked);
  93.    
  94.     /* Append City Information Section */
  95.     var city_section = d3.select('#city_controler')
  96.         .selectAll('text').data(features).enter().append('div')
  97.         .attr( 'class', function(d) {
  98.             return 'dicofre_' + d.properties.Dicofre + ' city_info';
  99.         })
  100.         .style('visibility', 'hidden');
  101.        
  102.         /* City Island */
  103.         city_section.append('p')
  104.             .attr('class','city_island')
  105.             .text( function(d) {
  106.                 return d.properties.Ilha;
  107.             });
  108.        
  109.         /* City District */
  110.         city_section.append('p')
  111.             .attr('class','city_district')
  112.             .text( function(d) {
  113.                 return 'Concelho - ' + d.properties.Concelho;
  114.             });
  115.        
  116.         /* City Name */
  117.         city_section.append('h1')
  118.             .attr('class','city_name')
  119.             .text( function(d) {
  120.                 return d.properties.Freguesia;
  121.             });
  122.  
  123. });
  124.  
  125. var se = [{
  126.     latitude : 32.651542054545445,
  127.     longitude : -16.90799122727272
  128. }];
  129.  
  130. mapContainer.append('g')
  131.         .attr('id', 'routers')
  132.         .selectAll('.pin').data(se).enter().append('circle', '.pin')
  133.         .attr('r', 10)
  134.         .attr('fill', function(d) {
  135.             //return d.color
  136.             return '#000'
  137.         })
  138.         .attr('transform', function(d) {
  139.             return 'translate(' + projection([d.longitude, d.latitude]) + ')'
  140.         });
  141.  
  142. /* Load the Routers */
  143. d3.json('data/routers.json', function(error, routers){
  144.     if (error) throw error;
  145.  
  146.     routerGroups = groupRouters(routers);
  147.     mapContainer.append('g')
  148.         .attr('id', 'routers')
  149.         .selectAll('.pin').data(routers).enter().append('circle', '.pin')
  150.         .attr('r', 3)
  151.         .attr('fill', function(d) {
  152.             //return d.color
  153.             return '#F15F22'
  154.         })
  155.         .attr('transform', function(d) {
  156.             return 'translate(' + projection([d.longitude, d.latitude]) + ')'
  157.         })
  158.         .attr('visibility', 'hidden');
  159.  
  160.     mapContainer.append('g')
  161.         .attr('id', 'routers')
  162.         .selectAll('.pin').data(routerGroups).enter().append('circle', '.pin')
  163.         .attr('r', 6)
  164.         .attr('fill', function(d) {
  165.             //return d.color
  166.             return '#F15F22'
  167.         })
  168.         .attr('transform', function(d) {
  169.             return 'translate(' + projection([d.longitude, d.latitude]) + ')'
  170.         });
  171.  
  172. });
  173.  
  174. /******************************************************
  175.  
  176. 4. ZOOM - AUXILIARY FUNCTIONS
  177.  
  178. *******************************************************/
  179.  
  180. /*
  181. Zoomed - Zooms the elements in and out. Makes circles and text
  182.          smaller or bigger.
  183. */
  184. function zoomed() {
  185.     mapContainer.attr('transform', d3.event.transform);
  186.     mapContainer.selectAll('circle').attr('r', 3/d3.event.transform.k);
  187.     mapText.attr('transform', d3.event.transform);
  188. }
  189.  
  190. /*
  191. Clicked - Zooms a specific city if it was clicked and centers it
  192.           on the screen.
  193. */
  194. function clicked(d) {
  195.     if (active.node() === this) return reset();
  196.  
  197.     /* Finds the correspondent city name to show */
  198.     var className = '.dicofre_' + d.properties.Dicofre;
  199.  
  200.     /* Fade In City Information */
  201.     $('#city_controler').fadeIn( "fast", function() {
  202.         d3.select(this).selectAll('div').style('visibility', 'hidden');
  203.         d3.select(this).select(className).style('visibility', 'visible');
  204.     });
  205.  
  206.     /* Marks the city as selected */
  207.     active.classed('active', false)
  208.                 .style('fill', function(d) { return fillFn(d); });
  209.     active = d3.select(this).classed('active', true)
  210.                 .style('fill', '#417505');
  211.  
  212.     /* Centers the screen on the city */
  213.     var bounds = path.bounds(d),
  214.         dx = bounds[1][0] - bounds[0][0],
  215.         dy = bounds[1][1] - bounds[0][1],
  216.         x = (bounds[0][0] + bounds[1][0]) / 2,
  217.         y = (bounds[0][1] + bounds[1][1]) / 2,
  218.         scale = Math.max(1, Math.min(8, 0.9 / Math.max(dx / width, dy / height))),
  219.         translate = [width / 2 - scale * x, height / 2 - scale * y];
  220.  
  221.     /* Animates the zooming in */
  222.     svg.transition()
  223.         .duration(750)
  224.         .call( zoom.transform, d3.zoomIdentity.translate(translate[0],translate[1]).scale(scale) );
  225.  
  226. }
  227.  
  228. /*
  229. Reset - Zooms out of specific city if it was clicked.
  230. */
  231. function reset() {
  232.  
  233.     /* Unmarks the city as selected */
  234.     active.classed('active', false);
  235.     active = d3.select(null);
  236.  
  237.     /* Hides any city name visible */
  238.     d3.select('#city_controler').selectAll('div').style('visibility', 'hidden');
  239.     $('#city_controler').hide();
  240.  
  241.     /* Animates the zooming out */
  242.     svg.transition()
  243.         .duration(750)
  244.         .call( zoom.transform, d3.zoomIdentity );
  245. }
  246.  
  247. /*
  248. Stopped - Stops zoom.
  249. */
  250. function stopped() {
  251.     if (d3.event.defaultPrevented) d3.event.stopPropagation();
  252. }
  253.  
  254. /******************************************************
  255.  
  256. 5. CITY NAMES - AUXILIARY FUNCTIONS
  257.  
  258. *******************************************************/
  259.  
  260. /*
  261. nameFn - Returns the name of a city.
  262. */
  263. function nameFn(d) {
  264.     return d && d.properties ? d.properties.Freguesia : null;
  265. }
  266.  
  267. /*
  268. nameLength - Returns the size of the name of a city.
  269. */
  270. function nameLength(d) {
  271.     var n = nameFn(d);
  272.     return n ? n.length : 0;
  273. }
  274.  
  275. /*
  276. fillFn - Returns a random color based on the name lenght of a city.
  277. */
  278. function fillFn(d) {
  279.     return color(nameLength(d));
  280. }
  281.  
  282. /*
  283. mouseover - Animates a city color when hovered.
  284. */
  285. function mouseover(d) {
  286.     d3.select(this).style('fill', '#417505');
  287. }
  288.  
  289. /*
  290. mouseout - Animates a city color when it stops being hovered.
  291. */
  292. function mouseout(d) {
  293.     d3.select(this).style('fill', function(d) { return d===active ? '#417505' : fillFn(d); });
  294. }
  295.  
  296. /******************************************************
  297.  
  298. 6. ROUTERS - AUXILIARY FUNCTIONS
  299.  
  300. *******************************************************/
  301.  
  302. /*
  303. groupRouters - Returns multiple groups of routers
  304. */
  305. function groupRouters(d) {
  306.     var groups = new Map()
  307.     var list = []
  308.     d.forEach(function(router) {
  309.         var id = router.cityId;
  310.         if (groups.has(id)) {
  311.             var oldData = groups.get(id);
  312.             var newData = {}
  313.             newData.latitude = (router.latitude + oldData.latitude * oldData.size) / (oldData.size + 1)
  314.             newData.longitude = (router.longitude + oldData.longitude * oldData.size) / (oldData.size + 1)
  315.             newData.size = oldData.size + 1;
  316.             newData.id = id;
  317.         } else {
  318.             var newData = {
  319.                 latitude : router.latitude,
  320.                 longitude : router.longitude,
  321.                 size : 1,
  322.                 id : id
  323.             };
  324.         }
  325.         groups.set(id, newData)
  326.     });
  327.  
  328.     groups.forEach(function (group, id, groups) {
  329.         list.push(group)
  330.     });
  331.     console.log(list)
  332.     return list;
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement