-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathdata.test.ts
370 lines (335 loc) · 11.3 KB
/
data.test.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import * as assert from 'assert';
import { tail, last } from 'lodash';
import distance from '@turf/distance';
import along from '@turf/along';
import { LineString, FeatureCollection } from 'geojson';
import {
getLineStringDistance,
LinearMetadataItem,
update,
resizeSegment,
splitAt,
mergeIn,
} from './data';
const DEBUG = false;
interface Degree {
degree: number;
}
const defaultLine: LineString = {
type: 'LineString',
coordinates: [
[-1.5416908264160156, 47.21717794690891],
[-1.5247821807861328, 47.220675713408426],
[-1.512765884399414, 47.22254109452638],
[-1.5050411224365234, 47.22568877633443],
[-1.5014362335205078, 47.229302550889734],
],
};
// Follow the LineString (simpliest)
// Line : ----- ----- ----- -----
// Wrapper : ----- ----- ----- -----
const wrapperThatFollowLine = tail(defaultLine.coordinates).reduce<
Array<LinearMetadataItem<Degree>>
>((acc, coord, index) => {
const from = defaultLine.coordinates[index];
const begin = acc.length > 0 ? acc[index - 1].end : 0;
const end = begin + distance(from, coord) * 1000;
acc.push({ begin, end, degree: index });
return acc;
}, []);
// console.log(wrapper1);
// [
// { begin: 0, end: 1334.9166539490695, degree: 0 },
// { begin: 1334.9166539490695, end: 2265.7879100768832, degree: 1 },
// { begin: 2265.7879100768832, end: 2946.078713959997, degree: 2 },
// { begin: 2946.078713959997, end: 3431.4330805212953, degree: 3 }
// ]
// Overlaps
// Line : ----- ----- ----- -----
// Wrapper : ------- ------ --------
const wrapperOverlaps = [
{ begin: 0, end: 1400, degree: 0 },
{ begin: 1400, end: 2500, degree: 1 },
{ begin: 2500, end: 3431.4330805212953, degree: 2 },
];
// Overlaps with one breaking point that is the same
// Line : ----- ----- ----- -----
// Wrapper : ------- --- -----------
const wrapperOneInCommon = [
{ begin: 0, end: 1400, degree: 0 },
{ begin: 1400, end: 2265.7879100768832, degree: 1 },
{ begin: 2265.7879100768832, end: 3431.4330805212953, degree: 2 },
];
const wrapperTests = [
{ title: 'line', wrapper: wrapperThatFollowLine },
{ title: 'overlaps', wrapper: wrapperOverlaps },
{ title: 'oneInCommon', wrapper: wrapperOneInCommon },
];
/**
* Check the validity of linear metadata.
*/
function checkWrapperValidity<T = any>(
result: Array<LinearMetadataItem<T>>,
newLine?: LineString,
message?: string
) {
// Checking extrimities
assert.equal(result[0].begin, 0, message);
// we round due to some approximation that result to a diff (below millimeter)
if (newLine)
assert.equal(
Math.round(last(result)?.end || 0),
Math.round(getLineStringDistance(newLine)),
message
);
// Checking the continuity
tail(result).forEach((value, index) => {
assert.equal(value.begin <= value.end, true, message);
const prev = result[index];
assert.equal(value.begin, prev.end, message);
});
}
/**
* Generate a GeoJSON of the line plus its linear metadata
* that can be pasted on https://geojson.io/ to see the result.
*/
function generateGeoJson<T>(
linearMetadata: Array<LinearMetadataItem<T>>,
line: LineString
): FeatureCollection<LineString, T> {
return {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
geometry: line,
properties: { stroke: '#ef2929', 'stroke-width': 2, 'stroke-opacity': 1 },
},
...linearMetadata.map((item) => ({
type: 'Feature',
properties: {
...item,
distance: Math.round(item.end - item.begin),
stroke: '#729fcf',
'stroke-width': 10,
'stroke-opacity': 0.5,
},
geometry: {
type: 'LineString',
coordinates: [
along(line, item.begin / 1000).geometry.coordinates,
along(line, item.end / 1000).geometry.coordinates,
],
},
})),
],
} as unknown as FeatureCollection<LineString, T>;
}
describe('Testing linear metadata functions', () => {
it('Impact on move point should work', () => {
const newLine: LineString = {
type: 'LineString',
coordinates: [
[-1.5416908264160156, 47.21717794690891],
[-1.53, 47.3],
[-1.512765884399414, 47.22254109452638],
[-1.5050411224365234, 47.22568877633443],
[-1.5014362335205078, 47.229302550889734],
],
};
wrapperTests.forEach((test) => {
if (DEBUG)
console.log('start', JSON.stringify(generateGeoJson(test.wrapper, defaultLine), null, 2));
const result = update(defaultLine, newLine, test.wrapper);
if (DEBUG) console.log('result', JSON.stringify(generateGeoJson(result, newLine), null, 2));
// validity test
checkWrapperValidity(result, newLine, test.title);
// same size
assert.equal(result.length, test.wrapper.length, test.title);
// Checking that properties are kept
result.forEach((value, index) => {
assert.equal(value.degree, index, test.title);
});
});
});
it('Impact on delete point should work', () => {
const newLine: LineString = {
type: 'LineString',
coordinates: [
[-1.5416908264160156, 47.21717794690891],
[-1.512765884399414, 47.22254109452638],
[-1.5050411224365234, 47.22568877633443],
[-1.5014362335205078, 47.229302550889734],
],
};
wrapperTests.forEach((test) => {
if (DEBUG)
console.log('start', JSON.stringify(generateGeoJson(test.wrapper, defaultLine), null, 2));
const result = update(defaultLine, newLine, test.wrapper);
if (DEBUG) console.log('result', JSON.stringify(generateGeoJson(result, newLine), null, 2));
// validity test
checkWrapperValidity(result, newLine, test.title);
// same size
assert.equal(result.length, test.wrapper.length, test.title);
// Checking that properties are kept
result.forEach((value, index) => {
assert.equal(value.degree, index, test.title);
});
});
});
describe('resizing a segment', () => {
it('increase should fail on wrapper of size 1', () => {
const wrapper: Array<LinearMetadataItem<Degree>> = [{ begin: 0, end: 10, degree: 0 }];
try {
resizeSegment(wrapper, 0, 10);
assert.fail();
} catch (e) {
assert.ok(e);
}
});
it('decrease should fail on wrapper of size 1', () => {
const wrapper: Array<LinearMetadataItem<Degree>> = [{ begin: 0, end: 10, degree: 0 }];
try {
resizeSegment(wrapper, 0, -5);
assert.fail();
} catch (e) {
assert.ok(e);
}
});
it('increase on the last item should do nothing', () => {
const wrapper: Array<LinearMetadataItem<Degree>> = [
{ begin: 0, end: 10, degree: 0 },
{ begin: 10, end: 20, degree: 0 },
{ begin: 20, end: 30, degree: 0 },
{ begin: 30, end: 40, degree: 0 },
];
const result = resizeSegment(wrapper, 3, 5);
assert.equal(result.result[3].end, 40);
});
it('decrease on the last item should work', () => {
const wrapper: Array<LinearMetadataItem<Degree>> = [
{ begin: 0, end: 10, degree: 0 },
{ begin: 10, end: 20, degree: 0 },
{ begin: 20, end: 30, degree: 0 },
{ begin: 30, end: 40, degree: 0 },
];
const result = resizeSegment(wrapper, 3, -5);
assert.equal(result.result[3].end, 40);
});
it('increase should work', () => {
const wrapper: Array<LinearMetadataItem<Degree>> = [
{ begin: 0, end: 10, degree: 0 },
{ begin: 10, end: 20, degree: 0 },
{ begin: 20, end: 30, degree: 0 },
{ begin: 30, end: 40, degree: 0 },
];
const result = resizeSegment(wrapper, 2, 5);
// check the decrease of the prev element
assert.equal(result.result[2].begin, 20);
assert.equal(result.result[2].end, 35);
// check the increase of the last element
assert.equal(result.result[3].begin, 35);
assert.equal(result.result[3].end, 40);
});
it('decrease should work', () => {
const wrapper: Array<LinearMetadataItem<Degree>> = [
{ begin: 0, end: 10, degree: 0 },
{ begin: 10, end: 20, degree: 0 },
{ begin: 20, end: 30, degree: 0 },
{ begin: 30, end: 40, degree: 0 },
];
const result = resizeSegment(wrapper, 2, -5);
// check the increase of the prev element
assert.equal(result.result[2].begin, 20);
assert.equal(result.result[2].end, 25);
// check the decrease of the last element
assert.equal(result.result[3].begin, 25);
assert.equal(result.result[3].end, 40);
});
it('decrease more than the element size should fail', () => {
const wrapper: Array<LinearMetadataItem<Degree>> = [
{ begin: 0, end: 10, degree: 0 },
{ begin: 10, end: 20, degree: 0 },
{ begin: 20, end: 30, degree: 0 },
{ begin: 30, end: 40, degree: 0 },
];
try {
resizeSegment(wrapper, 3, -10);
assert.fail();
} catch (e) {
assert.ok(e);
}
});
it('increase more than the sibling element size should fail', () => {
const wrapper: Array<LinearMetadataItem<Degree>> = [
{ begin: 0, end: 10, degree: 0 },
{ begin: 10, end: 20, degree: 0 },
{ begin: 20, end: 30, degree: 0 },
{ begin: 30, end: 40, degree: 0 },
];
try {
resizeSegment(wrapper, 1, 10);
assert.fail();
} catch (e) {
assert.ok(e);
}
});
it('should throw an error on bad index', () => {
const wrapper: Array<LinearMetadataItem<Degree>> = [
{ begin: 0, end: 10, degree: 0 },
{ begin: 10, end: 20, degree: 0 },
{ begin: 20, end: 30, degree: 0 },
{ begin: 30, end: 40, degree: 0 },
];
try {
resizeSegment(wrapper, 3, -5);
assert.fail();
} catch (e) {
assert.ok(e);
}
});
});
describe('split a segment', () => {
it('should work', () => {
const wrapper: Array<LinearMetadataItem<Degree>> = [
{ begin: 0, end: 10, degree: 0 },
{ begin: 10, end: 20, degree: 0 },
{ begin: 20, end: 30, degree: 0 },
{ begin: 30, end: 40, degree: 0 },
];
const result = splitAt(wrapper, 25);
// validity test
checkWrapperValidity(result);
// check that the size increased of 1
assert.equal(result.length, 5);
});
});
describe('merge a segment', () => {
it('merge on left should work', () => {
const wrapper: Array<LinearMetadataItem<Degree>> = [
{ begin: 0, end: 10, degree: 0 },
{ begin: 10, end: 20, degree: 1 },
{ begin: 20, end: 30, degree: 2 },
{ begin: 30, end: 40, degree: 3 },
];
const result = mergeIn(wrapper, 1, 'left');
// validity test
checkWrapperValidity(result);
// check that the size decreased of 1
assert.equal(result.length, 3);
});
it('merge on right should work', () => {
const wrapper: Array<LinearMetadataItem<Degree>> = [
{ begin: 0, end: 10, degree: 0 },
{ begin: 10, end: 20, degree: 1 },
{ begin: 20, end: 30, degree: 2 },
{ begin: 30, end: 40, degree: 3 },
];
const result = mergeIn(wrapper, 1, 'right');
// validity test
checkWrapperValidity(result);
// check that the size decreased of 1
assert.equal(result.length, 3);
});
});
});