-
Notifications
You must be signed in to change notification settings - Fork 17
/
filter.js
73 lines (62 loc) · 1.82 KB
/
filter.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
import prune from "./prune.js";
export default function(topology, filter) {
var oldObjects = topology.objects,
newObjects = {},
key;
if (filter == null) filter = filterTrue;
function filterGeometry(input) {
var output, arcs;
switch (input.type) {
case "Polygon": {
arcs = filterRings(input.arcs);
output = arcs ? {type: "Polygon", arcs: arcs} : {type: null};
break;
}
case "MultiPolygon": {
arcs = input.arcs.map(filterRings).filter(filterIdentity);
output = arcs.length ? {type: "MultiPolygon", arcs: arcs} : {type: null};
break;
}
case "GeometryCollection": {
arcs = input.geometries.map(filterGeometry).filter(filterNotNull);
output = arcs.length ? {type: "GeometryCollection", geometries: arcs} : {type: null};
break;
}
default: return input;
}
if (input.id != null) output.id = input.id;
if (input.bbox != null) output.bbox = input.bbox;
if (input.properties != null) output.properties = input.properties;
return output;
}
function filterRings(arcs) {
return arcs.length && filterExteriorRing(arcs[0]) // if the exterior is small, ignore any holes
? [arcs[0]].concat(arcs.slice(1).filter(filterInteriorRing))
: null;
}
function filterExteriorRing(ring) {
return filter(ring, false);
}
function filterInteriorRing(ring) {
return filter(ring, true);
}
for (key in oldObjects) {
newObjects[key] = filterGeometry(oldObjects[key]);
}
return prune({
type: "Topology",
bbox: topology.bbox,
transform: topology.transform,
objects: newObjects,
arcs: topology.arcs
});
}
function filterTrue() {
return true;
}
function filterIdentity(x) {
return x;
}
function filterNotNull(geometry) {
return geometry.type != null;
}