|
| 1 | +import useGetState from 'api/getState'; |
| 2 | +import { useMemo } from 'react'; |
| 3 | +import { Layer, Source } from 'react-map-gl/maplibre'; |
| 4 | + |
| 5 | +import { useGetGeometries } from '../map-utils/getMapGrid'; |
| 6 | + |
| 7 | +// Helper function to calculate the centroid of a polygon |
| 8 | +const getPolygonCentroid = (coordinates: number[][][]) => { |
| 9 | + let x = 0, |
| 10 | + y = 0, |
| 11 | + area = 0; |
| 12 | + const ring = coordinates[0]; // Use outer ring |
| 13 | + |
| 14 | + for (let index = 0; index < ring.length - 1; index++) { |
| 15 | + const xi = ring[index][0]; |
| 16 | + const yi = ring[index][1]; |
| 17 | + const xi1 = ring[index + 1][0]; |
| 18 | + const yi1 = ring[index + 1][1]; |
| 19 | + const a = xi * yi1 - xi1 * yi; |
| 20 | + area += a; |
| 21 | + x += (xi + xi1) * a; |
| 22 | + y += (yi + yi1) * a; |
| 23 | + } |
| 24 | + |
| 25 | + area *= 0.5; |
| 26 | + x /= 6 * area; |
| 27 | + y /= 6 * area; |
| 28 | + |
| 29 | + return [x, y]; |
| 30 | +}; |
| 31 | + |
| 32 | +export default function GridAlertsLayer() { |
| 33 | + const { worldGeometries } = useGetGeometries(); |
| 34 | + const dataState = useGetState(); |
| 35 | + const dataState_ = dataState.data; |
| 36 | + console.log('State data:', dataState_); |
| 37 | + |
| 38 | + const warningIconData = useMemo(() => { |
| 39 | + if (!worldGeometries?.features) { |
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + // Create one point per unique zoneId that needs a warning |
| 44 | + const warningZones = dataState_.alerts; // Add more zone IDs as needed for example ['CA-ON', 'US-MIDA-PJM'] |
| 45 | + const features = []; |
| 46 | + |
| 47 | + for (const zoneId of warningZones) { |
| 48 | + const zoneFeature = worldGeometries.features.find( |
| 49 | + (feature) => feature.properties?.zoneId === zoneId |
| 50 | + ); |
| 51 | + |
| 52 | + if (zoneFeature?.geometry) { |
| 53 | + let centroid; |
| 54 | + |
| 55 | + if (zoneFeature.geometry.type === 'Polygon') { |
| 56 | + centroid = getPolygonCentroid(zoneFeature.geometry.coordinates); |
| 57 | + } else if (zoneFeature.geometry.type === 'MultiPolygon') { |
| 58 | + // Use the centroid of the largest polygon |
| 59 | + let largest = zoneFeature.geometry.coordinates[0]; |
| 60 | + for (let index = 1; index < zoneFeature.geometry.coordinates.length; index++) { |
| 61 | + if (zoneFeature.geometry.coordinates[index][0].length > largest[0].length) { |
| 62 | + largest = zoneFeature.geometry.coordinates[index]; |
| 63 | + } |
| 64 | + } |
| 65 | + centroid = getPolygonCentroid(largest); |
| 66 | + } |
| 67 | + |
| 68 | + if (centroid) { |
| 69 | + features.push({ |
| 70 | + type: 'Feature', |
| 71 | + geometry: { |
| 72 | + type: 'Point', |
| 73 | + coordinates: centroid, |
| 74 | + }, |
| 75 | + properties: { |
| 76 | + zoneId: zoneId, |
| 77 | + }, |
| 78 | + }); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return { |
| 84 | + type: 'FeatureCollection', |
| 85 | + features, |
| 86 | + }; |
| 87 | + }, [worldGeometries]); |
| 88 | + |
| 89 | + return ( |
| 90 | + <> |
| 91 | + {/* Separate source for warning icons */} |
| 92 | + {warningIconData && ( |
| 93 | + <Source id="warning-icons-source" type="geojson" data={warningIconData}> |
| 94 | + <Layer |
| 95 | + id="zones-warning-icon-layer" |
| 96 | + type="symbol" |
| 97 | + layout={{ |
| 98 | + 'icon-image': 'lucide-warning', |
| 99 | + 'icon-size': 1.2, |
| 100 | + 'icon-allow-overlap': true, |
| 101 | + 'symbol-placement': 'point', |
| 102 | + }} |
| 103 | + /> |
| 104 | + </Source> |
| 105 | + )} |
| 106 | + </> |
| 107 | + ); |
| 108 | +} |
0 commit comments