Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useEffect, useRef, useState } from 'react'
  2. import L from 'leaflet'
  3. import { hexGrid } from '@turf/turf'
  4. import count from 'turf-count'
  5.  
  6.  
  7. //highlight style
  8. var hexStyleHighlight = {
  9.     color: "#336",
  10.     weight: 4,
  11.     opacity: 1,
  12. };
  13.  
  14. //create color ramp
  15. function getColor(y) {
  16.     return y === undefined ? '#888' :
  17.         y < 1 ? '#ffffe9' :
  18.         y < 2 ? '#ffffe9' :
  19.         y < 5 ? '#ffffe9' :
  20.         y < 10 ? '#ffffe9' :
  21.         y < 20 ? '#ffffe9' :
  22.         y < 50 ? '#ffffe9' :
  23.         y < 100 ? '#ffffe9' :
  24.                     '#ffffe9';
  25. }
  26.  
  27. //create style, with fillColor picked from color ramp
  28. function style(feature) {
  29.     return {
  30.         fillColor: getColor(feature.properties.pt_count),
  31.         color: "#888",
  32.         weight: 1,
  33.         opacity: 1,
  34.         fillOpacity: 0.2
  35.     };
  36. }
  37. //attach styles and popups to the hex layer
  38. function highlightHex(e) {
  39.     var layer = e.target;
  40.     layer.setStyle(hexStyleHighlight);
  41.     if (!L.Browser.ie && !L.Browser.opera) {
  42.         layer.bringToFront();
  43.     }
  44. }
  45.  
  46. function resetHexHighlight(e) {
  47.     var layer = e.target;
  48.     var hexStyleDefault=style(layer.feature);
  49.     layer.setStyle(hexStyleDefault);
  50. }
  51.  
  52. function onEachHex(feature, layer) {
  53.     layer.on({
  54.         mouseover: highlightHex,
  55.         mouseout: resetHexHighlight
  56.     });
  57.     var hexStyleDefault=style(layer.feature);
  58.     layer.setStyle(hexStyleDefault);
  59.  
  60. }
  61.  
  62. // /cheapo normrand function
  63. function normish(mean, range) {
  64.     var num_out = ((Math.random() + Math.random() + Math.random() + Math.random() + Math.random() + Math.random() + Math.random() + Math.random() - 4) / 4) * range + mean;
  65.     return num_out;
  66. }
  67.  
  68. //create geojson data with random ~normal distribution
  69. function make_dots(dotcount) {
  70.  
  71.     var dots = {
  72.         type: "FeatureCollection",
  73.         features: []
  74.     };
  75.  
  76.     for(var i=0;i<dotcount;++i) {
  77.  
  78.     //set up random variables
  79.     const x = normish(0, 4);
  80.     const y = normish(0, 4);
  81.  
  82.     // set center coordinates
  83.     var centerlat = 50.798753;
  84.     var centerlon = -1.108770;
  85.  
  86.     //create points randomly distributed about center coordinates
  87.     var g = {
  88.         "type": "Point",
  89.             "coordinates": [((x * 0.11) + centerlon), ((y * 0.1) + centerlat)]
  90.     };
  91.  
  92.     //create feature properties, roughly proportional to distance from center coordinates
  93.     var p = {
  94.         "id": i,
  95.             "popup": "Dot_" + i,
  96.             "year": parseInt(Math.sqrt(x * x + y * y) * 60 * (1 - Math.random() / 1.5) + 1900),
  97.             "size": Math.round((parseFloat(Math.abs((normish(y*y, 2) + normish(x*x, 2)) * 50) + 10)) * 100) / 100
  98.     };
  99.  
  100.         //create features with proper geojson structure        
  101.         dots.features.push({
  102.             "geometry" : g,
  103.             "type": "Feature",
  104.             "properties": p
  105.         });
  106.     }
  107.     return dots;
  108. }
  109.  
  110. function Map() {
  111.  
  112.   const [markerPosition] =  useState({ lat:50.798753, lng:-1.108770  })
  113.    
  114.   //create some GeoJSON points to be sampled (function below)
  115.     var dotcount = 10000;
  116.     var dots = make_dots(dotcount);
  117.    
  118.     //parameters for hex grid
  119.     const bbox = [-3.3883,49.9624,1.1478,51.7974];
  120.     const cellWidth = 0.5;
  121.     const units = 'miles';
  122.  
  123.  
  124.     //create hex grid and count points within each cell
  125.     const hexgrid = hexGrid(bbox, cellWidth, {
  126.         units
  127.     });
  128.     const hexcounts = count(hexgrid, dots, 'pt_count');
  129.  
  130.        
  131.  
  132.   const mapRef = useRef(null)
  133.     useEffect(() => {
  134.  
  135.         mapRef.current = L.map('map', {
  136.             center: markerPosition,
  137.             zoom: 18,
  138.             layers: [
  139.                 L.tileLayer("http://{s}.tile.osm.org/{z}/{x}/{y}.png", {
  140.                     attribution:
  141.                         '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
  142.                 })
  143.             ]
  144.         })
  145.     }, [markerPosition])
  146.  
  147.  
  148.     const markerRef = useRef(null)
  149.     const customIcon = L.icon({
  150.         iconUrl: './boat-marker.svg',
  151.         iconSize: [80,80],
  152.         iconAnchor: [17,46]
  153.     });
  154.  
  155.     useEffect(() => {
  156.         if(markerRef.current)
  157.             markerRef.current.setLatLng(markerPosition)
  158.         else
  159.             markerRef.current = L.marker(markerPosition, {icon:customIcon, draggable: true }).addTo(mapRef.current).bindPopup('HMS Warrior')
  160.     }, [markerPosition, customIcon])
  161.  
  162.     const geoJsonRef = useRef(null)
  163.     useEffect(() => {
  164.         L.geoJson(hexcounts, {onEachFeature: onEachHex}).addTo(mapRef.current);
  165.  
  166.         if(geoJsonRef.currrent != null) {
  167.             geoJsonRef.current.clearLayers()
  168.         }
  169.     },[hexcounts])
  170.  
  171.     return <div id='map' style={{ width: '80%', height: '1000px' }}></div>
  172. }
  173.  
  174. export default Map
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement