forked from valhalla/web-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisochrone-polygons.tsx
More file actions
75 lines (65 loc) · 2.22 KB
/
isochrone-polygons.tsx
File metadata and controls
75 lines (65 loc) · 2.22 KB
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
import { useMemo } from 'react';
import { Source, Layer } from 'react-map-gl/maplibre';
import { useIsochronesStore } from '@/stores/isochrones-store';
import type { Feature, FeatureCollection } from 'geojson';
import { getIsochroneColor } from '@/utils/isochrone-colors';
export function IsochronePolygons() {
const isoResults = useIsochronesStore((state) => state.results);
const isoSuccessful = useIsochronesStore((state) => state.successful);
const colorPalette = useIsochronesStore((state) => state.colorPalette);
const opacity = useIsochronesStore((state) => state.opacity);
const maxRange = useIsochronesStore((state) => state.maxRange);
const data = useMemo(() => {
if (!isoResults || !isoSuccessful) return null;
if (!isoResults.data || !isoResults.show) return null;
const hasNoFeatures = Object.keys(isoResults.data).length === 0;
if (hasNoFeatures) return null;
const features: Feature[] = [];
for (const feature of isoResults.data.features) {
if (['Polygon', 'MultiPolygon'].includes(feature.geometry.type)) {
const contourValue = feature.properties?.contour ?? maxRange;
let fillColor: string;
if (colorPalette === 'current') {
fillColor = feature.properties?.fill || '#6200ea';
} else {
const normalizedValue = contourValue / maxRange;
fillColor = getIsochroneColor(normalizedValue, colorPalette);
}
features.push({
...feature,
properties: {
...feature.properties,
fillColor,
contourValue,
},
});
}
}
return {
type: 'FeatureCollection',
features,
} as FeatureCollection;
}, [isoResults, isoSuccessful, colorPalette, maxRange]);
if (!data) return null;
return (
<Source id="isochrones" type="geojson" data={data}>
<Layer
id="isochrones-fill"
type="fill"
paint={{
'fill-color': ['get', 'fillColor'],
'fill-opacity': opacity,
}}
/>
<Layer
id="isochrones-outline"
type="line"
paint={{
'line-color': '#fff',
'line-width': 1,
'line-opacity': 1,
}}
/>
</Source>
);
}