terra-route 0.0.5 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,89 @@
1
+ import PathFinder, { pathToGeoJSON } from "geojson-path-finder";
2
+ import { FeatureCollection, LineString, Position } from "geojson";
3
+ import { haversineDistance, TerraRoute } from "./terra-route";
4
+ import { createPointFeature, routeLength } from "./test-utils/test-utils";
5
+ import { readFileSync } from 'fs';
6
+
7
+
8
+ /** Test on a "real" network to ensure that the path is as short or shorter than GeoJSON Path Finder */
9
+ describe("TerraRoute", () => {
10
+ describe('getRoute', () => {
11
+ it('matches route create from GeoJSON Path Finder', () => {
12
+
13
+ const network = JSON.parse(readFileSync('src/data/network.geojson', 'utf-8')) as FeatureCollection<LineString>;
14
+
15
+ const pairs: [Position, Position][] = [];
16
+
17
+ // Connect midpoints of lines across the network
18
+ for (let i = 0, j = network.features.length - 1; i < j; i++, j--) {
19
+ const coordsA = network.features[i].geometry.coordinates;
20
+ const coordsB = network.features[j].geometry.coordinates;
21
+
22
+ const midA = Math.floor(coordsA.length / 2);
23
+ const midB = Math.floor(coordsB.length / 2);
24
+
25
+ pairs.push([
26
+ coordsA[midA] as Position,
27
+ coordsB[midB] as Position
28
+ ]);
29
+ }
30
+
31
+ // Connect starting and ending points of lines across the network
32
+ for (let i = 0, j = network.features.length - 1; i < j; i++, j--) {
33
+ const coordsA = network.features[i].geometry.coordinates;
34
+ const coordsB = network.features[j].geometry.coordinates;
35
+
36
+ pairs.push([
37
+ coordsA[0] as Position,
38
+ coordsB[coordsB.length - 1] as Position
39
+ ]);
40
+ }
41
+
42
+ const terraRoute = new TerraRoute();
43
+
44
+ terraRoute.buildRouteGraph(network as FeatureCollection<LineString>);
45
+
46
+ const pathFinder = new PathFinder(network as FeatureCollection<LineString>, {
47
+ // Mimic points having to be identical
48
+ tolerance: 0.000000000000000000001,
49
+ weight: (a, b) => haversineDistance(a, b)
50
+ });
51
+
52
+ for (let i = 0; i < pairs.length; i++) {
53
+ // Start and end points are the same
54
+ const startIsEnd = pairs[i][0][0] === pairs[i][1][0] && pairs[i][0][1] === pairs[i][1][1]
55
+ if (startIsEnd) {
56
+ continue;
57
+ }
58
+
59
+ const start = createPointFeature(pairs[i][0]);
60
+ const end = createPointFeature(pairs[i][1]);
61
+
62
+ const route = pathFinder.findPath(start, end);
63
+ expect(route).not.toBeNull();
64
+
65
+ // Route not found
66
+ if (!route || route.path.length <= 1) {
67
+ const routeFromTerraRoute = terraRoute.getRoute(start, end);
68
+
69
+ // We want to check terra-route also returns null in this scenario
70
+ expect(routeFromTerraRoute).toBeNull();
71
+ continue
72
+ }
73
+
74
+ const routeFromPathFinder = pathToGeoJSON(route);
75
+ expect(routeFromPathFinder).toBeDefined();
76
+
77
+ const routeFromTerraRoute = terraRoute.getRoute(start, end);
78
+ expect(routeFromTerraRoute).not.toBeNull();
79
+
80
+ const pathFinderLength = routeLength(routeFromPathFinder!)
81
+ const terraDrawLength = routeLength(routeFromTerraRoute!);
82
+
83
+ expect(pathFinderLength).toBeGreaterThanOrEqual(terraDrawLength);
84
+
85
+ }
86
+ });
87
+
88
+ })
89
+ });