-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplify.js
executable file
·44 lines (37 loc) · 1.3 KB
/
simplify.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
import * as turf from '@turf/turf';
const removeHoles = (polygon) => {
polygon.geometry.coordinates.splice(1, 1);
return polygon;
};
/**
*
* @param {GeoJSON} featuresGeoJson source GeoJSON
* @param {number} tolerance higher value means more simplification default is 0.001
* @param {number} iterations higher value means more smoothing default is 2
* @returns
*/
export const simplifyFeatures = (featuresGeoJson, tolerance, iterations) => {
const { features } = turf.simplify(featuresGeoJson, {
tolerance: tolerance || 0.001,
highQuality: true
});
const transformedFeatures = features
.map((f) => turf.polygonSmooth(f, { iterations: iterations || 2, mutate: true }).features[0])
.map((f) => turf.transformScale(f, 1.07))
.map((f) => {
f.properties.area = turf.area(f);
return f;
})
.map((f) => removeHoles(f))
.sort((a, b) => a.properties.area - b.properties.area);
for (let i = 0; i < transformedFeatures.length; i++) {
let featureA = transformedFeatures[i];
for (let j = 0; j < transformedFeatures.length; j++) {
if (j === i) continue;
const featureB = transformedFeatures[j];
featureA = turf.difference(featureA, featureB);
}
transformedFeatures[i] = featureA;
}
return turf.featureCollection(transformedFeatures);
};