-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathhelpers.ts
228 lines (208 loc) · 6.31 KB
/
helpers.ts
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
/* eslint-disable prefer-destructuring, no-plusplus */
import along from '@turf/along';
import distance from '@turf/distance';
import { point } from '@turf/helpers';
import length from '@turf/length';
import type {
Feature,
FeatureCollection,
LineString,
Point,
Polygon,
Position,
BBox,
} from 'geojson';
import _, { first, last, mapValues } from 'lodash';
import type { MapGeoJSONFeature } from 'maplibre-gl';
import WebMercatorViewport from 'viewport-mercator-project';
import { type EditoastType, LAYER_TO_EDITOAST_DICT, type Layer } from 'applications/editor/consts';
import { getMixedEntities } from 'applications/editor/data/api';
import { flattenEntity } from 'applications/editor/data/utils';
import vec, { type Vec2 } from 'common/Map/WarpedMap/core/vec-lib';
import type { Viewport } from 'reducers/map';
import type { AppDispatch } from 'store';
/*
* Useful types:
*/
export type TriangleProperties = { triangleId: string };
export type Triangle = Feature<Polygon, TriangleProperties>;
export type BarycentricCoordinates = [number, number, number];
export type GridFeature = FeatureCollection<Polygon, TriangleProperties>;
export type GridIndex = Record<string, Triangle>;
export type Grids = {
warped: GridFeature;
original: GridFeature;
};
export type PointsGrid = Record<number, Position>[];
export type BBox2d = [number, number, number, number];
/*
* Path manipulation helpers:
*/
export function getSamples(
line: Feature<LineString>,
samples: number
): { step: number; points: Feature<Point>[] } {
if (samples <= 1) throw new Error('samples must be an integer greater than 1');
const points: Feature<Point>[] = [];
const l = length(line, { units: 'meters' });
const step = l / (samples - 1);
for (let i = 0; i < samples; i++) {
if (!i) {
points.push(point(first(line.geometry.coordinates)!));
} else if (i === samples - 1) {
points.push(point(last(line.geometry.coordinates)!));
} else {
points.push(along(line, step * i, { units: 'meters' }));
}
}
return { step, points };
}
/**
* Given a line and a lengthToAdd, extend the line at its two extremities by lengthToAdd meters.
*/
export function extendLine(line: Feature<LineString>, lengthToAdd: number): Feature<LineString> {
if (lengthToAdd < 0) throw new Error('lengthToAdd must be positive');
const points = line.geometry.coordinates;
const firstPoint = points[0] as Vec2;
const second = points[1] as Vec2;
const lastPoint = points[points.length - 1] as Vec2;
const secondToLast = points[points.length - 2] as Vec2;
return {
...line,
geometry: {
...line.geometry,
coordinates: [
vec.add(
firstPoint,
vec.multiply(
vec.vector(second, firstPoint),
lengthToAdd / distance(second, firstPoint, { units: 'meters' })
)
),
...points,
vec.add(
lastPoint,
vec.multiply(
vec.vector(secondToLast, lastPoint),
lengthToAdd / distance(secondToLast, lastPoint, { units: 'meters' })
)
),
],
},
};
}
/*
* Triangle helpers:
*/
export function getBarycentricCoordinates(
[x, y]: Position,
{
geometry: {
coordinates: [[[x0, y0], [x1, y1], [x2, y2]]],
},
}: Triangle
): BarycentricCoordinates {
const denominator = (y1 - y2) * (x0 - x2) + (x2 - x1) * (y0 - y2);
const a = ((y1 - y2) * (x - x2) + (x2 - x1) * (y - y2)) / denominator;
const b = ((y2 - y0) * (x - x2) + (x0 - x2) * (y - y2)) / denominator;
const c = 1 - a - b;
return [a, b, c];
}
export function isInTriangle([a, b, c]: BarycentricCoordinates): boolean {
return a >= 0 && a <= 1 && b >= 0 && b <= 1 && c >= 0 && c <= 1;
}
export function getPointInTriangle(
[a, b, c]: BarycentricCoordinates,
{
geometry: {
coordinates: [[[x0, y0], [x1, y1], [x2, y2]]],
},
}: Triangle
): Position {
return [a * x0 + b * x1 + c * x2, a * y0 + b * y1 + c * y2];
}
/*
* Data helpers:
*/
const OSRD_BATCH_SIZE = 500;
export async function getImprovedOSRDData(
infra: number,
data: Partial<Record<Layer, FeatureCollection>>,
dispatch: AppDispatch
): Promise<Record<string, Feature>> {
const queries = _(data)
.flatMap((collection: FeatureCollection, layerType: Layer) => {
const editoastType = LAYER_TO_EDITOAST_DICT[layerType];
return collection.features.flatMap((feature) =>
feature.properties?.fromEditoast || typeof feature.properties?.id !== 'string'
? []
: [
{
id: feature.properties.id,
type: editoastType,
},
]
);
})
.take(OSRD_BATCH_SIZE)
.value() as unknown as { id: string; type: EditoastType }[];
if (!queries.length) return {};
return mapValues(await getMixedEntities(infra, queries, dispatch), (e) =>
flattenEntity({
...e,
properties: {
...e.properties,
fromEditoast: true,
},
})
);
}
/**
* This helper takes a MapboxGeoJSONFeature (ie a data item extracted from a MapLibre instance through the
* `querySourceFeatures` method), and returns a proper and clean GeoJSON Feature object.
*/
export function simplifyFeature(feature: MapGeoJSONFeature): Feature {
return {
type: 'Feature',
id: feature.id,
properties: { ...feature.properties, sourceLayer: feature.sourceLayer },
// eslint-disable-next-line no-underscore-dangle
geometry: feature.geometry || feature._geometry,
};
}
export function computeBBoxViewport(
boundingBox: BBox | Position,
initialViewport: Viewport,
opts?: { width?: number; height?: number; padding?: number }
): Viewport {
const [minLng, minLat, maxLng, maxLat] = boundingBox;
const viewportTemp = new WebMercatorViewport({
...initialViewport,
width: opts?.width || 600,
height: opts?.height || 400,
});
const { longitude, latitude, zoom } = viewportTemp.fitBounds(
[
[minLng, minLat],
[maxLng, maxLat],
],
{ padding: opts?.padding ?? 40 }
);
return {
...initialViewport,
longitude,
latitude,
zoom: zoom > 5 ? zoom : 5,
};
}
/**
* Zoom helpers:
*/
export function zoomToFeature(
boundingBox: BBox | Position,
mapViewport: Viewport,
updateExtViewport: (viewport: Viewport) => void
) {
const newViewport = computeBBoxViewport(boundingBox, mapViewport);
updateExtViewport(newViewport);
}