Advertisement
Guest User

spatial.js

a guest
May 5th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 9.14 KB | None | 0 0
  1. $(document).ready(function () {
  2.     let currentShape;
  3.  
  4.     let layer = null;
  5.     let marker = null;
  6.  
  7.     let latX = null;
  8.     let latY = null;
  9.  
  10.     let soilShape = 'soil';
  11.     let earthquakeShape = 'earthquake';
  12.  
  13.     $(document).on('click', '.with-gap', function () {
  14.  
  15.         currentShape = $(this).val();
  16.  
  17.         layerGroup.clearLayers();
  18.         info.update();
  19.         if (marker !== null) {
  20.             map.removeLayer(marker);
  21.         }
  22.     });
  23.  
  24.  
  25.     //MAP ZOOM/PAN CONTROL START
  26.     let southWest = L.latLng(-89.98155760646617, -180);
  27.     let northEast = L.latLng(89.99346179538875, 180);
  28.     let bounds = L.latLngBounds(southWest, northEast);
  29.     //MAP ZOOM/PAN CONTROL END
  30.  
  31.     //CURSOR CONTROL START
  32.     let map = L.map('mapid', {
  33.         minZoom: 3
  34.     }).setView([42.7, 25.5], 7);
  35.  
  36.     $('.leaflet-container').css('cursor', 'crosshair');
  37.     map.setMaxBounds(bounds);
  38.     map.on('drag', function () {
  39.         map.panInsideBounds(bounds, {animate: false});
  40.     });
  41.  
  42.     map.on('movestart', function () {
  43.         $('.leaflet-container').css('cursor', '');
  44.     });
  45.  
  46.     map.on('moveend', function () {
  47.         $('.leaflet-container').css('cursor', 'crosshair');
  48.     });
  49.     //CURSOR CONTROL END
  50.  
  51.     //SHAPE STYLING START
  52.     var shapeStyle = {
  53.         "color": "#000000",
  54.         "weight": 2,
  55.         "fillOpacity": 0.6,
  56.         "opacity": 0.75
  57.     };
  58.     //SHAPE STYLING END
  59.  
  60.     //COLORING START
  61.     const colors = new Map(
  62.         [
  63.             ['A', '#ff0000'], ['B', '#b26559'], ['C', '#f28100'], ['D', '#a7b300'], ['E', '#ffff00'], ['F', '#005359'],
  64.             ['G', '#3d55f2'], ['H', '#ff00cc'], ['I', '#ff80a2'], ['J', '#cc0000'], ['K', '#733f1d'], ['L', '#736739'],
  65.             ['M', '#134d13'], ['N', '#26332d'], ['O', '#0058a6'], ['P', '#140033'], ['Q', '#99738c'], ['R', '#331a1a'],
  66.             ['S', '#42ceb6'], ['T', '#fff2bf'], ['U', '#79f279'], ['V', '#00becc'], ['W', '#bfd9ff'], ['X', '#660080'],
  67.             ['Y', '#021412'], ['Z', '#992663']
  68.         ]
  69.     );
  70.  
  71.     const colorEarthquakeArray = ['#ff0000', '#b26559', '#ff80a2', '#00becc', '#bfd9ff',
  72.         '#021412', '#e31a1c', '#992663', '#800026', '#720413'];
  73.  
  74.     function getColorBySoilGroup(obj) {
  75.         return colors.get(obj.properties.domsoi.charAt(0));
  76.     }
  77.  
  78.     function getColorByEQFrequency(obj) {
  79.         return colorEarthquakeArray[obj.properties.dn - 1];
  80.     }
  81.  
  82.     function getColor(obj) {
  83.         if (obj.properties.domsoi !== undefined) {
  84.             return getColorBySoilGroup(obj);
  85.         } else if (obj.properties.dn !== undefined) {
  86.             return getColorByEQFrequency(obj);
  87.         }
  88.     }
  89.  
  90.     function style(obj) {
  91.         return {
  92.             fillColor: getColor(obj),
  93.             weight: 2,
  94.             opacity: 1,
  95.             color: 'white',
  96.             dashArray: '3',
  97.             fillOpacity: 0.5
  98.         };
  99.     }
  100.  
  101.     map.on("contextmenu", function () {
  102.         layerGroup.clearLayers();
  103.         info.update();
  104.         if (marker !== null) {
  105.             map.removeLayer(marker);
  106.         }
  107.     });
  108.  
  109.     var layerGroup = L.layerGroup().addTo(map);
  110.     //AJAX START
  111.  
  112.     var geojson;
  113.  
  114.  
  115.     function updateInfoOnClick(e) {
  116.         info.update(layer.feature)
  117.     }
  118.  
  119.     function onEachFeature(feature, layer) {
  120.         layer.on({
  121.             click: updateInfoOnClick,
  122.         });
  123.     }
  124.  
  125.     map.on("dblclick", function (ev) {
  126.  
  127.         var latlng = map.mouseEventToLatLng(ev.originalEvent);
  128.  
  129.         latX = latlng.lng;
  130.         latY = latlng.lat;
  131.  
  132.         currentShape = $(".with-gap:checked").val();
  133.  
  134.         $.ajax({
  135.             url: "http://localhost:8080/spatial/getByCoords",
  136.             type: "POST",
  137.             dataType: "text",
  138.             contentType: "application/json",
  139.             data: JSON.stringify({
  140.                 "x": latX,
  141.                 "y": latY,
  142.                 "requestType": currentShape
  143.             }),
  144.             success: function (response) {
  145.  
  146.                 // layerGroup.clearLayers();
  147.  
  148.                 geojson = $.parseJSON(response);
  149.  
  150.                 shapeStyle.fillColor = getColor(geojson);
  151.  
  152.                 var geometry = wellknown.parse(geojson.geometry);
  153.                 geojson.geometry = geometry;
  154.  
  155.                 geojson = L.geoJSON(geojson, {
  156.                     style: style,
  157.                     onEachFeature: onEachFeature
  158.                 });
  159.  
  160.                 // printPropertiesOnConsole(geojson.properties);
  161.  
  162.                 geojson.addTo(layerGroup,);
  163.  
  164.                 // info.update(geojson);
  165.             },
  166.             error: function (response) {
  167.                 var errorMessage = jQuery.parseJSON(response.responseText);
  168.                 console.log(errorMessage.message);
  169.                 if (layer !== null) {
  170.                     map.removeLayer(layer);
  171.                 }
  172.                 info.updateOnError();
  173.                 // popup.setContent('No data found!')
  174.             }
  175.         });
  176.     });
  177.     //AJAX END
  178.  
  179.     layerGroup.on('dbclick', function (e) {
  180.         info.update(e);
  181.  
  182.     });
  183.  
  184.  
  185.     //LEAFLET START
  186.     L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
  187.         maxZoom: 18,
  188.         attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
  189.             '<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
  190.             'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
  191.         id: 'mapbox.streets'
  192.     }).addTo(map);
  193.  
  194.     map.doubleClickZoom.disable();
  195.  
  196.     var popup = L.popup();
  197.  
  198.     function onMapClick(e) {
  199.         if (marker !== null) {
  200.             map.removeLayer(marker);
  201.         }
  202.  
  203.         marker = L.marker(e.latlng).addTo(map);
  204.  
  205.         // popup
  206.         //     .setLatLng(e.latlng)
  207.         //     .setContent('<b>X: ' + parseFloat(Math.round(e.latlng.lng * 100000) / 100000).toFixed(5).toString() + '</b></br> + ')
  208.         //     .openOn(map);
  209.     }
  210.  
  211.     //LEAFLET END
  212.  
  213.  
  214.     //INFO CONTROL START
  215.     var info = L.control();
  216.  
  217.     info.onAdd = function (map) {
  218.         this._div = L.DomUtil.create('div', 'info'); // create a div with a class "info"
  219.         this.update();
  220.         return this._div;
  221.     };
  222.  
  223. // method that we will use to update the control based on feature properties passed
  224.  
  225.  
  226.     // info.update = function (obj) {
  227.     //     if (currentShape === soilShape) {
  228.     //         this._div.innerHTML = '<h2><u><span class=capitalize>' + currentShape + ' Information</span></u></h2>';
  229.     //         for(prop in obj.properties){
  230.     //             console.log(prop);
  231.     //             console.log(obj.properties[prop]);
  232.     //         }
  233.     //         this._div.innerHTML += (obj ?
  234.     //             '<h3>Faosoil: ' + obj.properties.faosoil + '</h3>' +
  235.     //             '<h3>Domsoi: ' + obj.properties.domsoi + '</h3>' +
  236.     //             '<h3>Country: <span class = capitalize>' + obj.properties.country.toLowerCase() + '</span></h3>' +
  237.     //             '<h3>Size: ' + obj.properties.sqkm + ' km<sup>2</sup></h3>'
  238.     //             : '<h3>Double click on location...</h3>');
  239.     //     } else if (currentShape === earthquakeShape) {
  240.     //         this._div.innerHTML = '<h2><u><span class=capitalize>' + currentShape + ' Information</span></u></h2>';
  241.     //
  242.     //         this._div.innerHTML += (obj ?
  243.     //             '<span>' + '<h3>Dn: ' + obj.properties.dn + '</h3></span>'
  244.     //             : '<h3>Double click on location...</h3>');
  245.     //     }
  246.     // };
  247.  
  248.     info.update = function (obj) {
  249.         if (currentShape !== null) {
  250.             this._div.innerHTML = '<h2><u><span class=capitalize>' + currentShape + ' Information</span></u></h2>';
  251.  
  252.             if (obj) {
  253.  
  254.                 let toAdd = "";
  255.  
  256.                 for (prop in obj.properties) {
  257.                     toAdd += '<h3><span class=capitalize>' + prop + ': ' + obj.properties[prop] + '</span></h3>';
  258.                 }
  259.  
  260.                 this._div.innerHTML += toAdd;
  261.  
  262.             } else {
  263.                 this._div.innerHTML += '<h3>Double click on location...</h3>';
  264.             }
  265.         }
  266.     };
  267.  
  268.  
  269.     info.updateOnError = function () {
  270.         this._div.innerHTML = '<h2><u><span class=capitalize>' + currentShape + ' Information</span></u></h2>';
  271.         this._div.innerHTML += '<h3>Double click on location...</h3>';
  272.     };
  273.  
  274.     map.on('dblclick', onMapClick);
  275.     info.addTo(map);
  276.     //INFO CONTROL END
  277.  
  278.     //HIGHLIGHT START
  279.     function highlightFeature(e) {
  280.         var layer = e.target;
  281.  
  282.         layer.setStyle({
  283.             weight: 4,
  284.             color: '#FFFF00',
  285.             dashArray: '',
  286.             fillOpacity: 0.8
  287.         });
  288.  
  289.         if (!L.Browser.ie && !L.Browser.opera && !L.Browser.edge) {
  290.             layer.bringToFront();
  291.         }
  292.  
  293.         info.update(layer.feature);
  294.     }
  295.  
  296.     function resetHighlight(e) {
  297.         geojson.resetStyle(e.target);
  298.         info.update();
  299.     }
  300.  
  301.     function onEachFeature(feature, layer) {
  302.         layer.on({
  303.             mouseover: highlightFeature,
  304.             mouseout: resetHighlight,
  305.         });
  306.     }
  307.  
  308. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement