-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
113 lines (78 loc) · 2.87 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const private = {};
private.removeDuplicatesFromPolygon = (polygon) => {
const cleanPolygon = [];
polygon.forEach(ring => {
const coordinateIndex = {};
const cleanRing = [];
ring.forEach((coordPair, i) => {
const key = JSON.stringify(coordPair);
if(i == (ring.length - 1) || ! (key in coordinateIndex)) {
coordinateIndex[key] = true;
cleanRing.push(coordPair);
}
});
cleanPolygon.push(cleanRing);
});
return cleanPolygon;
};
private.detectDuplicatesOnPolygon = (polygon) => {
const dupesOnPolygon = [];
polygon.forEach(ring => {
const coordinateIndex = {};
ring.forEach((coordPair, i) => {
const key = JSON.stringify(coordPair);
if(i != (ring.length - 1) && (key in coordinateIndex)) {
dupesOnPolygon.push(coordPair);
}
coordinateIndex[key] = true;
});
});
return dupesOnPolygon;
};
exports.detectDupeCoords = (geoJsonFeature) => {
if (typeof geoJsonFeature == 'string') {
geoJsonFeature = JSON.parse(geoJsonFeature);
}
var dupeCoords = [];
if (geoJsonFeature.geometry.type == 'MultiPolygon') {
geoJsonFeature.geometry.coordinates.forEach((polygon) => {
const dupesOnPolygon = private.detectDuplicatesOnPolygon(polygon);
dupeCoords = dupeCoords.concat(dupesOnPolygon);
});
} else if (geoJsonFeature.geometry.type == 'Polygon') {
const dupesOnPolygon = private.detectDuplicatesOnPolygon(geoJsonFeature.geometry.coordinates);
dupeCoords = dupesOnPolygon;
}
if (dupeCoords.length) {
return dupeCoords;
}
return false;
};
exports.removeDupeCoords = (geoJsonFeature) => {
if (typeof geoJsonFeature == 'object') {
geoJsonFeature = JSON.stringify(geoJsonFeature);
}
const cleaned = JSON.parse(geoJsonFeature);
if (cleaned.geometry.type == 'MultiPolygon') {
const cleanedPolygons = [];
cleaned.geometry.coordinates.forEach((polygon) => {
cleanedPolygons.push(private.removeDuplicatesFromPolygon(polygon));
});
cleaned.geometry.coordinates = cleanedPolygons;
} else if (cleaned.geometry.type == 'Polygon') {
const cleanedPolygon = private.removeDuplicatesFromPolygon(cleaned.geometry.coordinates);
cleaned.geometry.coordinates = cleanedPolygon;
}
return cleaned;
};
exports.detectAndRemoveDupeCoords = (geoJsonFeature, logFunction) => {
const dupes = exports.detectDupeCoords(geoJsonFeature);
if (dupes) {
if (typeof logFunction == 'function') {
logFunction(`Attempting to remove dupe coordinates.\n` +
` ${JSON.stringify(dupes)}`);
}
return exports.removeDupeCoords(geoJsonFeature);
}
return geoJsonFeature;
};