Debian Codegraph @

test/test.geometryutil.js

raw symbols SCIP
1 var assert = chai.assert;
2
3
4 assert.almostequal = function (a, b, n) {
5 n = n || 12;
6 return assert.equal(Math.round(a * Math.pow(10, n)) / Math.pow(10, n),
7 Math.round(b * Math.pow(10, n)) / Math.pow(10, n));
8 };
9 // use Leaflet equality functions for Point and LatLng
10 assert.pointEqual = function (a, b) {
11 return a.equals(b);
12 };
13 assert.latLngEqual = function (a, b, n) {
14 n = n || 2;
15 return assert.almostequal(a.lat, b.lat, 2) && assert.almostequal(a.lng, b.lng, n);
16 };
17
18 describe('Distance to segment', function() {
19 it('It should be 0 if point on segment', function(done) {
20 assert.equal(0, L.GeometryUtil.distanceSegment(map, L.latLng([10, 5]), L.latLng([10, 0]), L.latLng([10, 10])));
21 done();
22 });
23
24 it('It should not fail if segment has no length', function(done) {
25 assert.equal(1, L.GeometryUtil.distanceSegment(map, L.latLng([0, 1]), L.latLng([0, 0]), L.latLng([0, 0])));
26 done();
27 });
28
29 it('It should be the shortest distance', function(done) {
30 assert.equal(1, L.GeometryUtil.distanceSegment(map, L.latLng([0, 1]), L.latLng([0, 0]), L.latLng([10, 0])));
31 done();
32 });
33 });
34
35
36 describe('Length of line', function() {
37 it('It should be 0 for empty line', function(done) {
38 assert.equal(0, L.GeometryUtil.length([]));
39 done();
40 });
41
42 it('It should return length in meters', function(done) {
43 assert.equal(111319.49079327357, L.GeometryUtil.length(L.polyline([[0, 0], [1, 0]])));
44 done();
45 });
46 });
47
48
49 describe('Readable distances', function() {
50 it('It should be meters by default', function(done) {
51 assert.equal("0 m", L.GeometryUtil.readableDistance(0));
52 done();
53 });
54
55 it('It should be 0 yd if imperial', function(done) {
56 assert.equal("0 yd", L.GeometryUtil.readableDistance(0, 'imperial'));
57 done();
58 });
59
60 it('It should be kilometers if superior to 1000', function(done) {
61 assert.equal("1.01 km", L.GeometryUtil.readableDistance(1010));
62 done();
63 });
64
65 it('It should be miles if superior to 1760', function(done) {
66 assert.equal("1.24 miles", L.GeometryUtil.readableDistance(2000, 'imperial'));
67 done();
68 });
69 });
70
71
72 describe('Accumulated length of line', function() {
73 it('It should be empty for empty line', function(done) {
74 assert.deepEqual([], L.GeometryUtil.accumulatedLengths([]));
75 done();
76 });
77
78 it('It should return 0 and length in meters for a segment', function(done) {
79 assert.deepEqual([0, 111319.49079327357], L.GeometryUtil.accumulatedLengths(L.polyline([[0, 0], [1, 0]])));
80 done();
81 });
82
83 it('It should return accumulated lengths', function(done) {
84 assert.deepEqual([0, 55659.74539663678, 111319.49079327357], L.GeometryUtil.accumulatedLengths(L.polyline([[0, 0], [0.5, 0], [1, 0]])));
85 done();
86 });
87 });
88
89
90 describe('Closest on segment', function() {
91 it('It should be same point if point on segment', function(done) {
92 var ll = L.latLng([0, 0]),
93 closest = L.GeometryUtil.closestOnSegment(map, ll, L.latLng([0, 0]), L.latLng([10, 10]));
94 assert.equal(ll.toString(), closest.toString());
95 done();
96 });
97
98 it('It should be exactly on path', function(done) {
99 var ll = L.latLng([-1, 1]),
100 closest = L.GeometryUtil.closestOnSegment(map, ll, L.latLng([-10, -10]), L.latLng([10, 10]));
101 // TODO: should not be almost equal
102 assert.almostequal(0, closest.lat, 2);
103 assert.almostequal(0, closest.lng, 2);
104 done();
105 });
106 });
107
108
109 describe('Closest on path with precision', function() {
110 it('It should have distance at 0 if on path', function(done) {
111 var ll = L.latLng([0, 0]),
112 closest = L.GeometryUtil.closest(map, [[-30, -50], [-10, -10], [10, 10], [30, 50]], ll);
113 assert.equal(0, closest.distance);
114 assert.equal(ll.toString(), closest.toString());
115 done();
116 });
117
118 it('It should return same point if on path', function(done) {
119 var line = L.polyline([[0,0], [1, 1], [2, 2]]);
120 closest = L.GeometryUtil.closest(map, line, [1.7, 1.7]);
121 assert.almostequal(closest.lat, 1.7, 2);
122 assert.almostequal(closest.lng, 1.7, 2);
123 done();
124 });
125
126 it('It should be exactly on path', function(done) {
127 var ll = L.latLng([1, -1]),
128 closest = L.GeometryUtil.closest(map, [[-10, -10], [10, 10]], ll);
129 assert.equal(Math.sqrt(2), closest.distance);
130 // TODO: should not be almost equal
131 assert.almostequal(closest.lat, 0, 2);
132 assert.almostequal(closest.lng, 0, 2);
133 done();
134 });
135
136 it('It should not depend on zoom', function(done) {
137 // Test with plain value
138 var ll = L.latLng([5, 10]),
139 line = L.polyline([[-50, -10], [30, 40]]).addTo(map),
140 closest = L.GeometryUtil.closest(map, line, ll);
141 assert.isTrue(closest.distance > 0);
142 /*
143 SELECT ST_AsText(
144 ST_ClosestPoint(
145 ST_MakeLine('SRID=4326;POINT(-10 -50)'::geometry, 'SRID=4326;POINT(40 30)'::geometry),
146 'SRID=4326;POINT(10 5)'::geometry))
147 Gives:
148 "POINT(20.3370786516854 -1.46067415730337)"
149 TODO: find out what's going on with Longitudes :)
150 */
151 assert.equal('LatLng(-1.46743, 21.57294)', closest.toString());
152
153 // Change zoom and check that closest did not change.
154 assert.equal(0, map.getZoom());
155 L.Util.setOptions(map, {maxZoom: 18});
156
157 map.on('moveend', function () {
158 assert.notEqual(0, map.getZoom());
159
160 closest = L.GeometryUtil.closest(map, line, ll);
161 assert.equal('LatLng(-1.46743, 21.57294)', closest.toString());
162 // Restore zoom
163 map.off('moveend');
164 map._resetView(map.getCenter(), 0);
165 done();
166 });
167
168 map._resetView(map.getCenter(), 17);
169 });
170
171 it('It should work with last segment of polygon', function(done) {
172 var polygon = L.polygon([[0, 0], [10, 10], [0, 10]]),
173 ll = [-1, 5],
174 closest = L.GeometryUtil.closest(map, polygon, ll);
175 assert.almostequal(closest.lat, 0, 2);
176 assert.almostequal(closest.lng, 5, 2);
177 done();
178 });
179 });
180
181
182 describe('Closest among layers', function() {
183 it('It should return null if list is empty', function(done) {
184 var ll = L.latLng([0, 0]),
185 closest = L.GeometryUtil.closestLayer(map, [], ll);
186 assert.equal(null, closest);
187 done();
188 });
189
190 it('It should return an object with layer, latlng and distance', function(done) {
191 var ll = L.latLng([0, 0]),
192 layers = [L.marker([2, 2])],
193 closest = L.GeometryUtil.closestLayer(map, layers, ll);
194 assert.deepEqual(closest,
195 {layer: layers[0], latlng: layers[0].getLatLng(), distance: Math.sqrt(2)});
196 done();
197 });
198 });
199
200
201 describe('Closest snap', function() {
202 var square, diagonal, d, w, layers;
203
204 beforeEach(function() {
205 // Snapping distance
206 d = L.GeometryUtil.distance(map, L.latLng([0, 0]), L.latLng([0, 10]));
207 w = 3 * d;
208 square = L.rectangle([[-w, -w], [w, w]]);
209 diagonal = L.polyline([[-w, -w], [0, 0], [w, w]]);
210 layers = [square, diagonal];
211 });
212
213 it('It should snap even if over layer', function(done) {
214 var snap = L.GeometryUtil.closestLayerSnap(map, layers, L.latLng([0, 0]));
215 assert.equal(snap.distance, 0);
216 assert.equal(snap.layer, diagonal);
217 done();
218 });
219
220 it('It should not snap if tolerance exceeded', function(done) {
221 var snap = L.GeometryUtil.closestLayerSnap(map, layers, L.latLng([-w-d, w+d]), d);
222 assert.equal(null, snap);
223 done();
224 });
225
226 it('It should snap to corners by default', function(done) {
227 var snap = L.GeometryUtil.closestLayerSnap(map, layers, L.latLng([-w-d, w+d]));
228 assert.isTrue(snap.distance > d);
229 assert.equal(snap.layer, square);
230 done();
231 });
232
233 it('It should not snap to corners if vertices disabled', function(done) {
234 var corner = L.GeometryUtil.closestLayerSnap(map, layers, L.latLng([w-d, -w-d]));
235 assert.equal(corner.layer, square);
236 assert.almostequal(corner.latlng.lat, w);
237 assert.almostequal(corner.latlng.lng, -w);
238
239 var snap = L.GeometryUtil.closestLayerSnap(map, layers, L.latLng([w-d, -w-d]), Infinity, false);
240 assert.almostequal(snap.latlng.lat, w-d);
241 assert.almostequal(snap.latlng.lng, -w);
242 done();
243 });
244
245 it('It should not snap to corners if distance to vertice exceeds tolerance', function(done) {
246 var corner = L.GeometryUtil.closestLayerSnap(map, layers, L.latLng([w-d-d/2, -w-d]));
247 assert.equal(corner.layer, square);
248 assert.almostequal(corner.latlng.lat, w);
249 assert.almostequal(corner.latlng.lng, -w);
250
251 var snap = L.GeometryUtil.closestLayerSnap(map, layers, L.latLng([w-d-d/2, -w-d]), d);
252 assert.almostequal(snap.latlng.lat, w-d-d/2);
253 assert.almostequal(snap.latlng.lng, -w);
254 done();
255 });
256 });
257
258 describe('Interpolate on point segment', function() {
259 var p1 = L.point(0, 2),
260 p2 = L.point(0, 6);
261 it('It should be the first point if offset is 0', function(done) {
262 assert.pointEqual(p1, L.GeometryUtil.interpolateOnPointSegment(p1, p2, 0));
263 done();
264 });
265
266 it('It should be the last point if offset is 1', function(done) {
267 assert.pointEqual(p2, L.GeometryUtil.interpolateOnPointSegment(p1, p2, 1));
268 done();
269 });
270
271 it('It should return the correct interpolations', function(done) {
272 assert.pointEqual(L.point(0, 4), L.GeometryUtil.interpolateOnPointSegment(p1, p2, 0.5));
273 assert.pointEqual(L.point(0, 5), L.GeometryUtil.interpolateOnPointSegment(p1, p2, 0.75));
274 done();
275 });
276 });
277
278 describe('Interpolate on line', function() {
279 var llA = L.latLng(1, 2),
280 llB = L.latLng(3, 4),
281 llC = L.latLng(5, 6);
282
283 it('It should be null if the line has less than 2 vertices', function(done) {
284 assert.equal(null, L.GeometryUtil.interpolateOnLine(map, [], 0.5));
285 assert.equal(null, L.GeometryUtil.interpolateOnLine(map, [llA], 0.5));
286 done();
287 });
288
289 it('It should be the first vertex if offset is 0', function(done) {
290 var interp = L.GeometryUtil.interpolateOnLine(map, [llA, llB], 0);
291 assert.latLngEqual(interp.latLng, llA);
292 assert.equal(interp.predecessor, -1);
293 done();
294 });
295
296 it('It should be the last vertex if offset is 1', function(done) {
297 var interp = L.GeometryUtil.interpolateOnLine(map, [llA, llB, llC], 1);
298 assert.latLngEqual(interp.latLng, llC);
299 assert.equal(interp.predecessor, 1);
300 done();
301 });
302
303 it('It should not fail if line has no length', function(done) {
304 var interp = L.GeometryUtil.interpolateOnLine(map, [llA, llA, llA], 0.5);
305 assert.latLngEqual(interp.latLng, llA);
306 done();
307 });
308
309 it('It should return the correct interpolations', function(done) {
310 var interp1 = L.GeometryUtil.interpolateOnLine(map, [llA, llB, llC], 0.5);
311 assert.latLngEqual(interp1.latLng, llB);
312 var interp2 = L.GeometryUtil.interpolateOnLine(map, [llA, llB, llC], 0.75);
313 assert.latLngEqual(interp2.latLng, L.latLng([4, 5]));
314 done();
315 });
316
317 it('It should work the same with instances of L.PolyLine and arrays of L.LatLng', function(done) {
318 var lls = [llA, llB, llC];
319 var withArray = L.GeometryUtil.interpolateOnLine(map, lls, 0.75);
320 var withPolyLine = L.GeometryUtil.interpolateOnLine(map, L.polyline(lls), 0.75);
321 assert.deepEqual(withArray, withPolyLine);
322 done();
323 });
324
325 it('Should always return a LatLng object.', function() {
326 var interp1 = L.GeometryUtil.interpolateOnLine(map, [llA, llB, llC], 0);
327 var interp2 = L.GeometryUtil.interpolateOnLine(map, [llA, llB, llC], 1);
328
329 assert.isDefined(interp1.latLng.lat);
330 assert.isDefined(interp1.latLng.lng);
331 assert.isDefined(interp2.latLng.lat);
332 assert.isDefined(interp2.latLng.lng);
333 });
334 });
335
336
337 describe('Locate on line', function() {
338 var line = L.polyline([[0,0], [1, 1], [2, 2]]);
339
340 it('It should return 0 if start', function(done) {
341 assert.equal(0, L.GeometryUtil.locateOnLine(map, line, L.latLng([0, 0])));
342 done();
343 });
344
345 it('It should return 1 if end', function(done) {
346 assert.equal(1, L.GeometryUtil.locateOnLine(map, line, L.latLng([2, 2])));
347 done();
348 });
349
350 it('It should return ratio of point', function(done) {
351 assert.almostequal(0.5, L.GeometryUtil.locateOnLine(map, line, L.latLng([1, 1])), 4);
352 assert.almostequal(0.25, L.GeometryUtil.locateOnLine(map, line, L.latLng([0.5, 0.5])), 4);
353 assert.almostequal(0.85, L.GeometryUtil.locateOnLine(map, line, L.latLng([1.7, 1.7])), 4);
354 done();
355 });
356 });
357
358
359 describe('Reverse line', function() {
360 var line = L.polyline([[0,0], [1, 1]]);
361
362 it('It should invert coordinates', function(done) {
363 assert.latLngEqual(line.getLatLngs()[0], L.GeometryUtil.reverse(line).getLatLngs()[1]);
364 done();
365 });
366
367 it('It should not affect original', function(done) {
368 var start = line.getLatLngs()[0];
369 L.GeometryUtil.reverse(line);
370 assert.latLngEqual(start, line.getLatLngs()[0]);
371 done();
372 });
373 });
374
375
376 describe('Extract line', function() {
377 var line = L.polyline([[0,0], [1, 1], [2, 2], [3, 3]]);
378
379 it('It should return all coordinates from 0 to 1', function(done) {
380 assert.deepEqual(L.GeometryUtil.extract(map, line, 0, 1), line.getLatLngs());
381 done();
382 });
383
384 it('It should return inverted coordinates from 1 to 0', function(done) {
385 assert.deepEqual(L.GeometryUtil.extract(map, line, 1, 0), L.GeometryUtil.reverse(line).getLatLngs());
386 done();
387 });
388
389 it('It should return one coordinate if start equals end', function(done) {
390 assert.latLngEqual(L.latLng(0.7501691078194406, 0.7501524538236026),
391 L.GeometryUtil.extract(map, line, 0.25, 0.25)[0]);
392 done();
393 });
394
395 it('It should return extra coordinate if middle of segment', function(done) {
396 assert.deepEqual(L.GeometryUtil.extract(map, line, 0, 0.2),
397 [L.latLng([0, 0]), L.latLng([0.600141459027052, 0.6001219630588661])]);
398 assert.deepEqual(L.GeometryUtil.extract(map, line, 0, 0.6),
399 [L.latLng([0, 0]), L.latLng([1, 1]), L.latLng([1.800282914111311, 1.8002439493392906])]);
400 assert.deepEqual(L.GeometryUtil.extract(map, line, 0.6, 1.0),
401 [L.latLng([1.800282914111311, 1.8002439493392906]), L.latLng([2, 2]), L.latLng([3, 3])]);
402 assert.deepEqual(L.GeometryUtil.extract(map, line, 0.2, 0.8),
403 [L.latLng([0.600141459027052, 0.6001219630588661]), L.latLng([1, 1]), L.latLng([2, 2]), L.latLng([2.40024267258436, 2.4001524293923637])]);
404
405 // Should work symetrically
406 assert.deepEqual(L.GeometryUtil.extract(map, line, 1.0, 0.6),
407 [L.latLng([3, 3]), L.latLng([2, 2]), L.latLng([1.800282914111311, 1.8002439493392906])]);
408 done();
409 });
410 });
411
412
413 describe('Line order', function() {
414 var lineA = L.polyline([[0, 0], [1, 1]]),
415 lineB = L.polyline([[1, 1], [2, 2]]);
416
417 it('It should detect if line is before', function(done) {
418 assert.isTrue(L.GeometryUtil.isBefore(lineA, lineB));
419 assert.isFalse(L.GeometryUtil.isBefore(lineB, lineA));
420 done();
421 });
422
423 it('It should detect if line is after', function(done) {
424 assert.isTrue(L.GeometryUtil.isAfter(lineB, lineA));
425 assert.isFalse(L.GeometryUtil.isAfter(lineA, lineB));
426 done();
427 });
428
429 it('It should detect if line starts at extremity', function(done) {
430 var lineC = L.polyline([[0, 0], [1, 1]]);
431 assert.isTrue(L.GeometryUtil.startsAtExtremity(lineA, lineC));
432 assert.isTrue(L.GeometryUtil.startsAtExtremity(lineB, lineC));
433 assert.isFalse(L.GeometryUtil.startsAtExtremity(lineC, lineB));
434 done();
435 });
436 });
437
438 describe('Compute angle', function() {
439 it('It should return angle', function(done) {
440 var p1 = L.point(0, 0),
441 p2 = L.point(6, 6);
442 assert.equal(L.GeometryUtil.computeAngle(p1, p2), 45);
443 done();
444 });
445 });
446
447 describe('Compute slope', function() {
448 it('It should return A and B', function(done) {
449 var p1 = L.point(0, 2),
450 p2 = L.point(5, 7);
451 assert.deepEqual(L.GeometryUtil.computeSlope(p1, p2), {a: 1, b: 2})
452 done();
453 });
454 });
455
456 describe('Point rotation', function() {
457 it('It should return the same point if angle is 0', function(done) {
458 var llPoint = L.latLng([3, 3]),
459 llCenter = L.latLng([2, 2]),
460 rotated = L.GeometryUtil.rotatePoint(map, llPoint, 0, llCenter);
461 assert.latLngEqual(llPoint, rotated);
462 done();
463 });
464
465 it('It should return the same point if center and point are the same', function(done) {
466 var llPoint = L.latLng([1, 1]),
467 llCenter = L.latLng([1, 1]),
468 rotated = L.GeometryUtil.rotatePoint(map, llPoint, 90, llCenter);
469 assert.latLngEqual(llPoint, rotated);
470 done();
471 });
472
473 it('It should return a rotated point', function(done) {
474 var llPoint = L.latLng([1, 1]),
475 llCenter = L.latLng([2, 2]),
476 rotated = L.GeometryUtil.rotatePoint(map, llPoint, 90, llCenter);
477 assert.latLngEqual(rotated, L.latLng([3, 1]));
478 done();
479 });
480 });
481
482 describe('Compute Bearing', function() {
483
484 it('It should be degrees clockwise from north, 0 degrees.', function(done) {
485 var latlng1 = L.latLng([0.0, 0.0]),
486 latlng2 = L.latLng([90.0, 0.0]);
487 assert.equal(0.0, L.GeometryUtil.bearing(latlng1,latlng2));
488 done();
489 });
490
491 it('Same point, should be zero.', function(done) {
492 var latlng1 = L.latLng([0.0, 0.0]),
493 latlng2 = L.latLng([0.0, 0.0]);
494 assert.equal(0, L.GeometryUtil.bearing(latlng1,latlng2));
495 done();
496 });
497
498 it('Crossing Prime Meridian.', function(done) {
499 var latlng1 = L.latLng([10.0, -10.0]),
500 latlng2 = L.latLng([-10.0, 10.0]);
501 assert.equal(134.5614514132577, L.GeometryUtil.bearing(latlng1,latlng2));
502 done();
503 });
504
505 it('Negative value for bearing greater than / equal to 180', function(done) {
506 var latlng1 = L.latLng([33.0, -120.0]),
507 latlng2 = L.latLng([34.0, -122.0]);
508 assert.equal(-58.503883697887375, L.GeometryUtil.bearing(latlng1,latlng2));
509 done();
510 });
511
512 });
513
514 describe('Destination', function() {
515
516 it('It should be [90.0,0.0]', function(done) {
517 var latlng1 = L.latLng([0.0, 0.0]),
518 heading = 0.0;
519 dist = 6378137 * Math.PI / 2.0; // 1/4 Earth's circumference.
520 result = L.latLng([90.0,0.0]);
521 assert.latLngEqual(result, L.GeometryUtil.destination(latlng1, heading, dist));
522 done();
523 });
524
525 it('Crossing the International Date Line', function(done) {
526 var latlng1 = L.latLng([0.0, -175.0]),
527 heading = -90.0;
528 dist = 6378137 * Math.PI / 8.0;
529 result = L.latLng([0.0, 162.5]);
530 assert.latLngEqual(result, L.GeometryUtil.destination(latlng1, heading, dist));
531 done();
532 });
533
534 it('Crossing the Prime Meridian', function(done) {
535 var latlng1 = L.latLng([10.0, -10.0]),
536 heading = 134.5614514132577;
537 dist = 3140555.3283872544;
538 result = L.latLng([-10, 10.0]);
539 assert.latLngEqual(result, L.GeometryUtil.destination(latlng1, heading, dist));
540 done();
541 });
542 });