-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSummaryMap.tsx
288 lines (271 loc) · 9.23 KB
/
SummaryMap.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
'use client';
// Preserve spaces to avoid auto-sorting
import 'leaflet/dist/leaflet.css';
import 'leaflet-defaulticon-compatibility/dist/leaflet-defaulticon-compatibility.webpack.css';
import 'leaflet-defaulticon-compatibility';
import parse from 'html-react-parser';
import {
LayerGroup,
LayersControl,
MapContainer,
Marker,
Polygon,
Polyline,
Popup,
TileLayer,
ScaleControl,
} from 'react-leaflet';
import { useEffect, useRef, useState } from 'react';
import styled from 'styled-components';
import styles from './Tooltip.module.css';
interface SummaryMapProps {
height: string;
width: string;
expanded?: boolean;
}
const StyledMapContainer = styled(MapContainer)<SummaryMapProps>`
height: ${(props) => props.height};
width: ${(props) => props.width};
.leaflet-control-layers {
visibility: ${(props) => (props.expanded ? 'visible' : 'hidden')};
}
`;
const generateUniqueKey = () => {
return `${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
};
const convertKmlColorToHex = (
kmlColor: string
): {
hex: string;
alpha: number;
} => {
if (kmlColor === null || typeof kmlColor === 'undefined') {
return { hex: '#000000', alpha: 1 };
}
if (!/^([0-9a-fA-F]{8})$/.test(kmlColor)) {
return { hex: '#000000', alpha: 1 };
}
// Extract components from the KML color (AABBGGRR format)
const alpha = parseInt(kmlColor.slice(0, 2), 16);
const blue = kmlColor.slice(2, 4);
const green = kmlColor.slice(4, 6);
const red = kmlColor.slice(6, 8);
// Rearrange to #RRGGBB format
const hex = `#${red}${green}${blue}`;
return { hex, alpha: alpha / 255 }; // Alpha is normalized to a 0-1 range
};
const getBounds = (data) => {
if (data?.finalizedMapUpload?.[0]?.bounds?.length === 2) {
return [
data?.finalizedMapUpload?.[0]?.bounds[0],
data?.finalizedMapUpload?.[0]?.bounds[1],
];
}
if (data?.geographicCoverageMap?.[0]?.bounds?.length === 2) {
return [
data?.geographicCoverageMap?.[0]?.bounds[0],
data?.geographicCoverageMap?.[0]?.bounds[1],
];
}
return [
[47.768, -145.59],
[60.283, -103.403],
];
};
const RenderMarkers = ({ markers, name }) => (
<>
{markers?.map((marker) => (
<Marker
key={`${name}-${generateUniqueKey()}`}
position={marker.coordinates}
>
<Popup>
<h4>{marker?.name}</h4>
<p>
{typeof marker?.description === 'object'
? parse(marker?.description?.value || 'No description')
: marker?.description}
</p>
</Popup>
</Marker>
))}
</>
);
const SummaryMap = ({ initialData, height, width, expanded = true }) => {
const data = initialData;
const tooltipClass = styles['tooltip-map'];
const tooltipTextClass = styles['tooltip-text-map'];
const mapRef = useRef(null);
const [mapReady, setMapReady] = useState(false);
useEffect(() => {
const map = mapRef.current;
if (map) {
map.fitBounds([getBounds(data)]);
}
}, [data, mapReady]);
return (
<StyledMapContainer
preferCanvas
attributionControl={false}
ref={mapRef}
// BC Bounds
bounds={[
[47.768, -145.59],
[60.283, -103.403],
]}
// Zoom is automatically set when using bounds
// zoom={5}
scrollWheelZoom
whenReady={() => {
setMapReady(true);
}}
height={height}
width={width}
expanded={expanded}
>
<TileLayer
attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<ScaleControl position="bottomleft" imperial={false} />
<LayersControl position="topright">
{data?.geographicCoverageMap?.length > 0 && (
<>
{data?.geographicCoverageMap?.map((geoData) => (
<LayersControl.Overlay
checked
// Name does not accept anything but string, but it will render basic HTML
name={`<span class='${tooltipClass}'>Geographic Coverage (${geoData?.source})<span class='${tooltipTextClass}'>${geoData?.fileName}</span></span>`}
key={`geo-overlay-${generateUniqueKey()}`}
>
<LayerGroup>
<RenderMarkers markers={geoData?.markers} name="geo-marker" />
{geoData?.polygons?.map((polygon) => (
<Polygon
key={`geo-polygon-${generateUniqueKey()}`}
positions={polygon.coordinates}
color="blue"
>
<Popup>
{polygon?.balloonData && (
<div>{parse(polygon.balloonData)}</div>
)}
</Popup>
</Polygon>
))}
</LayerGroup>
</LayersControl.Overlay>
))}
</>
)}
{data?.currentNetworkInfrastructure?.length > 0 && (
<>
{data?.currentNetworkInfrastructure?.map((geoData) => (
<LayersControl.Overlay
checked={false}
name={`<span class='${tooltipClass}'>Current Network Infrastructure (${geoData?.source})<span class='${tooltipTextClass}'>${geoData?.fileName}</span></span>`}
key={`current-overlay-${generateUniqueKey()}`}
>
<LayerGroup>
<RenderMarkers
markers={geoData?.markers}
name="current-marker"
/>
{geoData?.polygons?.map((polygon) => (
<Polygon
key={`current-polygon-${generateUniqueKey()}`}
positions={polygon.coordinates}
color="red"
>
<Popup>
<h4>{polygon?.name}</h4>
<p>{polygon?.description}</p>
</Popup>
</Polygon>
))}
</LayerGroup>
</LayersControl.Overlay>
))}
</>
)}
{data?.upgradedNetworkInfrastructure?.length > 0 && (
<>
{data?.upgradedNetworkInfrastructure?.map((geoData) => (
<LayersControl.Overlay
checked={false}
name={`<span class='${tooltipClass}'>Upgraded Network Infrastructure (${geoData?.source})<span class='${tooltipTextClass}'>${geoData?.fileName}</span></span>`}
key={`upgraded-overlay-${generateUniqueKey()}`}
>
<LayerGroup>
<RenderMarkers
markers={geoData.markers}
name="upgraded-marker"
/>
{geoData?.polygons?.map((polygon) => (
<Polygon
key={`upgraded-polygon-${generateUniqueKey()}`}
positions={polygon.coordinates}
color="green"
>
<Popup>
<h4>{polygon?.name}</h4>
<p>{polygon?.description}</p>
</Popup>
</Polygon>
))}
</LayerGroup>
</LayersControl.Overlay>
))}
</>
)}
{data?.finalizedMapUpload?.length > 0 && (
<>
{data?.finalizedMapUpload?.map((geoData) => (
<LayersControl.Overlay
checked
name={`<span class='${tooltipClass}'>Finalized Map Upload (${geoData?.source})<span class='${tooltipTextClass}'>${geoData?.fileName}</span></span>`}
key={`finalized-overlay-${generateUniqueKey()}`}
>
<LayerGroup>
<RenderMarkers
markers={geoData.markers}
name="finalized-marker"
/>
{geoData?.polygons?.map((polygon) => (
<Polygon
key={`finalized-polygon-${polygon?.fileNam}`}
positions={polygon.coordinates}
color="purple"
>
<Popup>
<h4>{polygon?.name}</h4>
<p>{polygon?.description}</p>
</Popup>
</Polygon>
))}
{geoData?.lineStrings?.map((line) => (
<Polyline
key={`finalized-line-${line?.fileName}`}
positions={line.coordinates}
color={
convertKmlColorToHex(line?.style?.lineStyle?.color).hex
}
>
<Popup>
<h4>{line?.name}</h4>
{line?.description && (
<div>{parse(line.description)}</div>
)}
</Popup>
</Polyline>
))}
</LayerGroup>
</LayersControl.Overlay>
))}
</>
)}
</LayersControl>
</StyledMapContainer>
);
};
export default SummaryMap;