-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathDataLoader.tsx
More file actions
167 lines (149 loc) · 5.12 KB
/
DataLoader.tsx
File metadata and controls
167 lines (149 loc) · 5.12 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
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
/* eslint-disable no-console */
import { useEffect, useMemo, useState } from 'react';
import { featureCollection } from '@turf/helpers';
import type { FeatureCollection } from 'geojson';
import { uniqBy } from 'lodash';
import { createPortal } from 'react-dom';
import type {
BackgroundLayerSpecification,
LayerSpecification,
MapRef,
} from 'react-map-gl/maplibre';
import ReactMapGL, { Source } from 'react-map-gl/maplibre';
import type { Layer } from 'applications/editor/consts';
import { OSM_URL } from 'common/Map/const';
import { GeoJSONs, OrderedLayer, useMapBlankStyle } from 'common/Map/Layers';
import { colors, getOSMStyle } from 'common/Map/theme';
import { simplifyFeature, type BBox2d } from 'common/Map/WarpedMap/core/helpers';
import { useInfraID } from 'common/osrdContext';
import { useMapSettings } from 'reducers/commonMap';
const TIME_LABEL = 'Loading OSRD and OSM data around warped path';
const OSM_LAYERS = new Set(['building', 'water', 'water_name', 'waterway', 'poi']);
/**
* This component handles loading entities from MapLibre vector servers, and retrieving them as GeoJSONs from the
* MapLibre `querySourceFeatures` method.
* It's quite dirty (it has to mount a map in the DOM, but somewhere it won't be visible), but necessary until we get
* proper APIs for both OSRD data and OSM data.
*
* It is designed as a component instead of a hook to simplify mounting/unmounting the temporary invisible map.
*/
type DataLoaderProps = {
bbox: BBox2d;
getGeoJSONs: (
osrdData: Partial<Record<Layer, FeatureCollection>>,
osmData: Record<string, FeatureCollection>
) => void;
layers: Set<Layer>;
};
const DataLoader = ({ bbox, getGeoJSONs, layers }: DataLoaderProps) => {
const mapBlankStyle = useMapBlankStyle();
const { mapStyle, layersSettings } = useMapSettings();
const infraID = useInfraID();
const [mapRef, setMapRef] = useState<MapRef | null>(null);
const [state, setState] = useState<'idle' | 'render' | 'loaded'>('idle');
const osmLayers = useMemo(() => {
const osmStyle = getOSMStyle('normal').filter((layer) => layer.id && OSM_LAYERS.has(layer.id));
return osmStyle.map((layer) => ({
...layer,
id: `osm/${layer.id}`,
}));
}, []);
useEffect(() => {
if (!mapRef) return;
mapRef.fitBounds(bbox, { animate: false });
setTimeout(() => {
console.time(TIME_LABEL);
setState('render');
}, 0);
}, [mapRef, bbox]);
useEffect(() => {
if (state === 'render' && mapRef) {
const m = mapRef;
const querySources = () => {
// Retrieve OSRD data:
const osrdData: Partial<Record<Layer, FeatureCollection>> = {};
layers.forEach((layer) => {
osrdData[layer] = featureCollection(
uniqBy(
m
.querySourceFeatures(`editor/geo/${layer}`, { sourceLayer: layer })
.map((f) => simplifyFeature(f, layer)),
(f) => f.id
)
);
});
// Retrieve OSM data
const osmSourceLayerIds = m
.getStyle()
.layers.filter(
(l): l is Exclude<LayerSpecification, BackgroundLayerSpecification> =>
l.type !== 'background' && l.source === 'osm' && l['source-layer'] !== undefined
)
.map((l) => `${l['source-layer']}`);
let incrementalID = 1;
const osmData: Record<string, FeatureCollection> = osmSourceLayerIds.reduce(
(iter, sourceLayer) =>
OSM_LAYERS.has(sourceLayer)
? {
...iter,
[sourceLayer]: featureCollection(
uniqBy(
m
.querySourceFeatures('osm', { sourceLayer })
.map((f) => simplifyFeature(f, sourceLayer)),
(f) => (f.id ? `osm-${f.id}` : `generated-${++incrementalID}`) // only deduplicate features with IDs
)
),
}
: iter,
{}
);
// Finalize:
getGeoJSONs(osrdData, osmData);
setState('loaded');
};
m.on('idle', querySources);
return () => {
m.off('idle', querySources);
};
}
return undefined;
}, [state, mapRef]);
if (state === 'loaded') return null;
return createPortal(
<div
className="position-absolute"
style={{
bottom: '110%',
height: 1200,
width: 1200,
}}
>
<ReactMapGL
ref={setMapRef}
mapStyle={mapBlankStyle}
style={{ width: '100%', height: '100%' }}
>
{state === 'render' && (
<>
<Source id="osm" type="vector" url={OSM_URL}>
{osmLayers.map((layer) => (
<OrderedLayer key={layer.id} {...layer} />
))}
</Source>
<GeoJSONs
colors={colors[mapStyle]}
layersSettings={layersSettings}
isEmphasized={false}
layers={layers}
renderAll
infraID={infraID}
/>
</>
)}
</ReactMapGL>
</div>,
document.body
);
};
export default DataLoader;