使用turf进行处理
方法参数说明:layer 图层对象,选中的图元feature,layerID 图层ID
polygonFn(layer, features, layerID) {
let self = this;
let drawSourceVector = new VectorSource();
self.drawFaceSplit = new Draw({
source: drawSourceVector,
type: "Polygon",
style: new Style({
image: new sCircle({
radius: 5,
fill: new Fill({
color: "#03a9f4",
}),
}),
stroke: new Stroke({
color: "#03a9f4",
width: 2,
}),
fill: new Fill({
color: "rgba(255, 255, 255, 0.7)",
}),
}),
});
window.map.addInteraction(self.drawFaceSplit);
// 监听绘制结束事件
self.drawFaceSplit.on("drawend", (evt) => {
window.map.removeInteraction(self.drawFaceSplit);
let splitResult = [];
let featureGeoJson = JSON.parse(
new GeoJSON().writeFeature(evtFeature)
);
let coorArr = [];
for (let i = 0; i < featureGeoJson.geometry.coordinates[0].length; i++) {
let coor = featureGeoJson.geometry.coordinates[0][i];
coorArr.push(coor);
}
let polyDikuai = turf.polygon(this.coordinates);
let polyDraw = turf.polygon([coorArr]);
let intersection = turf.intersect(polyDikuai, polyDraw);
let difference1 = turf.difference(polyDikuai, polyDraw);
// 判断面是否包含面
let contains = turf.booleanContains(polyDikuai, polyDraw);
let intersectionArray = intersection.geometry ? intersection.geometry.coordinates : [];
let differenceArray = difference1.geometry ? difference1.geometry.coordinates : [];
splitResult.push(intersectionArray);
if (differenceArray && differenceArray.length > 1 && !contains) {
for (let item of differenceArray) {
splitResult.push(item);
}
}
else {
splitResult.push(differenceArray);
}
if (intersection && splitResult.length > 0) {
for (let item in splitResult) {
let polygonResult = new Polygon(splitResult[item]);
let featureResult = new Feature(polygonResult);
let Area = getPolygonArea(featureResult);
let length = getPolygonLength(featureResult);
let name =
features.get("region_name") === undefined
? ("采集地块-" + Area)
: features.get("region_name");
featureResult.set("Area", Area);
featureResult.set("Length", length);
featureResult.set("region_name", name);
if (layer) {
layer.getSource().addFeature(featureResult);
}
}
try {
setTimeout(() => {
layer.getSource().removeFeature(features);
}, 100);
}
catch (e) {
console.warn(e);
}
}
})
},