-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
92 lines (85 loc) · 1.86 KB
/
index.js
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
export default function bbox(geojson) {
let b = [
Number.POSITIVE_INFINITY,
Number.POSITIVE_INFINITY,
Number.NEGATIVE_INFINITY,
Number.NEGATIVE_INFINITY,
];
switch (geojson.type) {
case 'FeatureCollection':
const len = geojson.features.length;
for (let i = 0; i < len; i++) {
feature(geojson.features[i], b);
}
break;
case 'Feature':
feature(geojson, b);
break;
default:
geometry(geojson, b);
break;
}
return b;
}
function feature(f, b) {
geometry(f.geometry, b);
}
function geometry(g, b) {
if (!g) {
return;
}
switch (g.type) {
case 'Point':
point(g.coordinates, b);
break;
case 'MultiPoint':
line(g.coordinates, b);
break;
case 'LineString':
line(g.coordinates, b);
break;
case 'MultiLineString':
multiline(g.coordinates, b);
break;
case 'Polygon':
polygon(g.coordinates, b);
break;
case 'MultiPolygon':
multipolygon(g.coordinates, b);
break;
case 'GeometryCollection':
const len = g.geometries.length;
for (let i = 0; i < len; i++) {
geometry(g.geometries[i], b);
}
break;
}
}
function point(p, b) {
b[0] = Math.min(b[0], p[0]);
b[1] = Math.min(b[1], p[1]);
b[2] = Math.max(b[2], p[0]);
b[3] = Math.max(b[3], p[1]);
}
function line(l, b) {
for (let i = 0, len = l.length; i < len; i++) {
point(l[i], b);
}
}
function multiline(ml, b) {
for (let i = 0, len = ml.length; i < len; i++) {
line(ml[i], b);
}
}
function polygon(p, b) {
//Just calculate the outer ring,Don't participate in the calculation of holes
//测试10000个鄱阳湖的数据,表现为性能可以提高25%
if (p.length) {
line(p[0], b);
}
}
function multipolygon(mp, b) {
for (let i = 0, len = mp.length; i < len; i++) {
polygon(mp[i], b);
}
}