-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathtest.ts
More file actions
122 lines (107 loc) · 3.13 KB
/
Copy pathtest.ts
File metadata and controls
122 lines (107 loc) · 3.13 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
import fs from "fs";
import test from "tape";
import path from "path";
import { fileURLToPath } from "url";
import { loadJsonFileSync } from "load-json-file";
import { writeJsonFileSync } from "write-json-file";
import { bboxPolygon } from "@turf/bbox-polygon";
import { featureCollection, polygons } from "@turf/helpers";
import { geojsonRbush } from "./index.js";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const directories = {
in: path.join(__dirname, "test", "in") + path.sep,
out: path.join(__dirname, "test", "out") + path.sep,
};
const fixtures = fs.readdirSync(directories.in).map((filename) => {
return {
filename,
name: path.parse(filename).name,
geojson: loadJsonFileSync(directories.in + filename),
};
});
test("geojson-rbush", (t) => {
for (const fixture of fixtures) {
const name = fixture.name;
const filename = fixture.filename;
const geojson = fixture.geojson;
const tree = geojsonRbush();
tree.load(geojson as any);
// Retrive all features inside the RBush index
const all = tree.all();
// Search using the first item in the FeatureCollection
const search = tree.search((geojson as any).features[0]);
if (process.env.REGEN) {
writeJsonFileSync(directories.out + "all." + filename, all);
writeJsonFileSync(directories.out + "search." + filename, search);
}
t.deepEqual(
all,
loadJsonFileSync(directories.out + "all." + filename),
"all." + name
);
t.deepEqual(
search,
loadJsonFileSync(directories.out + "search." + filename),
"search." + name
);
}
t.end();
});
test("geojson-rbush -- bbox", (t) => {
const tree = geojsonRbush();
tree.insert(bboxPolygon([-150, -60, 150, 60]));
t.equal(tree.collides([-140, -50, 140, 50]), true);
t.equal(tree.search([-140, -50, 140, 50]).features.length, 1);
t.equal(tree.search(bboxPolygon([-150, -60, 150, 60])).features.length, 1);
t.equal(
tree.search(featureCollection([bboxPolygon([-150, -60, 150, 60])])).features
.length,
1
);
t.equal(tree.collides([-180, -80, -170, -60]), false);
// Errors
t.throws(() => tree.search("foo" as any));
t.end();
});
test("geojson-rbush -- fromJSON", (t) => {
const tree = geojsonRbush();
const poly = bboxPolygon([-150, -60, 150, 60]);
tree.insert(poly);
const newTree = geojsonRbush();
newTree.fromJSON(tree.toJSON());
t.equal(newTree.all().features.length, 1);
newTree.remove(poly);
t.equal(newTree.all().features.length, 0);
t.end();
});
test("geojson-rbush -- Array of Features -- Issue #5", (t) => {
// https://github.com/DenisCarriere/geojson-rbush/issues/5
const tree = geojsonRbush();
const polys = polygons([
[
[
[-78, 41],
[-67, 41],
[-67, 48],
[-78, 48],
[-78, 41],
],
],
[
[
[-93, 32],
[-83, 32],
[-83, 39],
[-93, 39],
[-93, 32],
],
],
]);
// Load Feature Collection
tree.load(polys);
t.equal(tree.all().features.length, 2);
// Load Array of Features
tree.load(polys.features);
t.equal(tree.all().features.length, 4);
t.end();
});