-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathprojection.ts
More file actions
182 lines (162 loc) · 4.81 KB
/
projection.ts
File metadata and controls
182 lines (162 loc) · 4.81 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import type { Feature, FeatureCollection, Geometry, Position } from 'geojson';
import { keyBy } from 'lodash';
import {
getBarycentricCoordinates,
getPointInTriangle,
type GridIndex,
isInTriangle,
type Triangle,
} from 'common/Map/WarpedMap/core/helpers';
import { getElements, type Quad } from 'common/Map/WarpedMap/core/quadtree';
import type { Zone } from 'types';
import { clip } from 'utils/mapHelper';
export type Projection = (position: Position) => Position | null;
/**
* This function maps the position of a point from one grid to another grid.
* It can take either a GridIndex or a QuadTree for the source grid, but beware
* that it is absolutely faster using a QuadTree.
*/
export function projectBetweenGrids(
gridFrom: GridIndex,
gridTo: GridIndex,
position: Position
): Position | null;
export function projectBetweenGrids(
quadTreeFrom: Quad<Triangle>,
gridTo: GridIndex,
position: Position
): Position | null;
export function projectBetweenGrids(
from: GridIndex | Quad<Triangle>,
gridTo: GridIndex,
position: Position
): Position | null {
let triangles: GridIndex;
if (from.type === 'quad') {
triangles = keyBy(
getElements(position, from as Quad<Triangle>),
(feature) => feature.properties.triangleId
);
} else {
triangles = from as GridIndex;
}
const keys = Object.keys(triangles);
for (let i = 0, l = keys.length; i < l; i++) {
const key = keys[i];
const triangle = triangles[key];
const barycentricCoordinates = getBarycentricCoordinates(position, triangle);
if (isInTriangle(barycentricCoordinates)) {
return getPointInTriangle(barycentricCoordinates, gridTo[key]);
}
}
return null;
}
/**
* This function projects any geometry, following a given projection function (ie. any function that transforms
* coordinates into new coordinates).
*/
export function projectGeometry<G extends Geometry = Geometry>(
geometry: G,
project: Projection
): G | null {
if (!geometry) return null;
switch (geometry.type) {
case 'Point': {
const newCoordinates = project(geometry.coordinates);
return newCoordinates
? {
...geometry,
coordinates: newCoordinates,
}
: null;
}
case 'MultiPoint':
case 'LineString': {
const newPoints = geometry.coordinates.flatMap((p) => {
const newP = project(p);
return newP ? [newP] : [];
});
return newPoints.length
? {
...geometry,
coordinates: newPoints,
}
: null;
}
case 'Polygon':
case 'MultiLineString': {
const newPaths = geometry.coordinates.flatMap((path) => {
const newPath = path.flatMap((p) => {
const newP = project(p);
return newP ? [newP] : [];
});
return newPath.length ? [newPath] : [];
});
return newPaths.length
? {
...geometry,
coordinates: newPaths,
}
: null;
}
case 'MultiPolygon': {
const newMultiPaths = geometry.coordinates.flatMap((paths) => {
const newPaths = paths.flatMap((path) => {
const newPath = path.flatMap((p) => {
const newP = project(p);
return newP ? [newP] : [];
});
return newPath.length ? [newPath] : [];
});
return newPaths.length ? [newPaths] : [];
});
return newMultiPaths.length
? {
...geometry,
coordinates: newMultiPaths,
}
: null;
}
case 'GeometryCollection':
return {
...geometry,
geometries: geometry.geometries.map((g) => projectGeometry(g, project)),
};
default:
return geometry;
}
}
/**
* This function takes a geometry, a feature or a features collection, clips them into a given zone, and projects them
* onto a given projection function. If everything is clipped out, then `null` is returned instead.
*/
export function clipAndProjectGeoJSON<T extends Geometry | Feature | FeatureCollection>(
geojson: T,
projection: Projection,
zone: Zone,
shouldClip: boolean
): T | null {
if (geojson.type === 'FeatureCollection')
return {
...geojson,
features: geojson.features.flatMap((f) => {
const res = clipAndProjectGeoJSON(f, projection, zone, shouldClip);
return res ? [res] : [];
}),
};
if (geojson.type === 'Feature') {
const feature: Feature = geojson;
const clippedFeature = shouldClip ? clip(feature, zone) : feature;
if (clippedFeature) {
const newGeometry = projectGeometry(clippedFeature.geometry, projection);
return newGeometry
? ({
...clippedFeature,
geometry: newGeometry,
} as T)
: null;
}
return null;
}
return projectGeometry(geojson, projection) as T | null;
}