-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
51 lines (45 loc) · 1.52 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
var _ = require('lodash')
var t = {}
t.featurecollection = require('turf-featurecollection')
t.erase = require('turf-erase')
t.point = require('turf-point')
t.inside = require('turf-inside')
t.union = require('turf-union')
module.exports = function(fc){
donuts = t.featurecollection([])
_.each(fc.features, function(poly1){
_.each(fc.features, function(poly2){
// if the polys are identical
if(_.isEqual(poly1, poly2)){
// do nothing
}
else{
//check to see if poly2 is inside of poly1
var isContained = contained(poly1, poly2);
// if it is contained and has different properties, erase poly2 from poly1
if(isContained && !_.isEqual(poly1.properties, poly2.properties)){
// erase poly2 from poly1
var erased = t.erase(poly1, poly2);
if(!_.some(donuts.features, erased)){
poly1 = erased
}
}
// if it is contained and has the same properties, merge poly1 and poly2
if(isContained && _.isEqual(poly1.properties, poly2.properties)){
// merge poly1 and poly2
var unioned = t.union(poly1, poly2);
if(!_.some(donuts.features, unioned)){
poly1 = unioned
}
}
}
})
// push transformed poly1 to donuts
donuts.features.push(poly1)
})
return donuts;
}
function contained(poly1, poly2){
var sampleVertex = t.point(poly2.geometry.coordinates[0][0][0], poly2.geometry.coordinates[0][0][1])
return t.inside(sampleVertex, poly1);
}