difference是Turf中一个用于计算多边形之间异同的函数,它会将几何对象A和一个或多个几何对象B传入,返回A中不属于B的部分。即A与B的异形部分。
polygon
: (Feature<Polygon>) 输入的多边形。subtractPolygons
: (FeatureCollection<Polygon>) 需要减去的多边形集合。var polygon = turf.polygon([[
[-70.603637, -33.399918],
[-70.614624, -33.395332],
[-70.639343, -33.392466],
[-70.659942, -33.394759],
[-70.683975, -33.404504],
[-70.697021, -33.419406],
[-70.687408, -33.434306],
[-70.668411, -33.444163],
[-70.645370, -33.448467],
[-70.622673, -33.446828],
[-70.603637, -33.399918]
]]);
var subtractPolygons = turf.featureCollection([
turf.polygon([[
[-70.614624, -33.399918],
[-70.663452, -33.395332],
[-70.639343, -33.392466],
[-70.614624, -33.399918]
]]),
turf.polygon([[
[-70.668411, -33.434306],
[-70.622673, -33.446828],
[-70.645370, -33.448467],
[-70.668411, -33.444163],
[-70.683975, -33.404504],
[-70.668411, -33.434306]
]])
]);
var result = turf.difference(polygon, subtractPolygons);
//=result
该函数使用了turf-erase库和turf-flatten库的联合作用。该函数将几何A的坐标扁平化、和几何B中的每个坐标分别做比较,找出A中不属于B的部分进行生成新的多边形。