2024-07-25 10:01:57 +08:00
|
|
|
|
import { generateUUID } from '../src/tool';
|
2024-07-18 13:43:11 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 使用turf工具进行切割面
|
|
|
|
|
|
* @param line 线段数据
|
|
|
|
|
|
* @param outerPolygon 面数据
|
|
|
|
|
|
* return 返回切割的面数据
|
|
|
|
|
|
*/
|
2024-07-25 10:01:57 +08:00
|
|
|
|
export function splitPolygonByLine(line, outerPolygon) {
|
2024-09-09 09:11:36 +08:00
|
|
|
|
console.log("绘制的分割线数据",line);
|
|
|
|
|
|
|
|
|
|
|
|
// 处理被分割的面数据坐标小数点保留多少位
|
2024-07-25 10:01:57 +08:00
|
|
|
|
let truncatedSplitter = turf.truncate(turf.lineString(outerPolygon.geometry.coordinates[0]), {
|
|
|
|
|
|
precision: 7,
|
|
|
|
|
|
});
|
2024-09-09 09:11:36 +08:00
|
|
|
|
console.log("分割面转换为线数据",truncatedSplitter);
|
2024-07-19 17:32:52 +08:00
|
|
|
|
|
2024-07-25 10:01:57 +08:00
|
|
|
|
//求交点
|
|
|
|
|
|
let intersectCollection = turf.lineIntersect(line, truncatedSplitter);
|
2024-09-09 09:11:36 +08:00
|
|
|
|
console.log("与分割面的交点",intersectCollection);
|
|
|
|
|
|
// printLngLat(intersectCollection.geometry.coordinates);
|
|
|
|
|
|
|
|
|
|
|
|
// 交点小于2个 返回null
|
2024-07-25 10:01:57 +08:00
|
|
|
|
if (intersectCollection.features.length < 2) {
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2024-07-19 17:32:52 +08:00
|
|
|
|
|
2024-07-25 10:01:57 +08:00
|
|
|
|
//将点合并成MultiPoint
|
|
|
|
|
|
let intersectCombined = turf.combine(intersectCollection).features[0];
|
2024-09-09 09:11:36 +08:00
|
|
|
|
|
|
|
|
|
|
console.log("分割后所有点数据",intersectCombined)
|
|
|
|
|
|
|
2024-07-20 09:47:30 +08:00
|
|
|
|
|
2024-07-25 10:01:57 +08:00
|
|
|
|
//分别获取切割线
|
|
|
|
|
|
let outerPieceCollection = turf.lineSplit(line, intersectCombined);
|
|
|
|
|
|
let splitterPieceCollection = turf.lineSplit(truncatedSplitter, intersectCombined);
|
2024-07-19 17:32:52 +08:00
|
|
|
|
|
2024-09-09 09:11:36 +08:00
|
|
|
|
console.log("切割线被分割后所有线段",outerPieceCollection);
|
|
|
|
|
|
|
|
|
|
|
|
console.log("切割面被分割后所有线段",splitterPieceCollection);
|
|
|
|
|
|
|
2024-07-25 10:01:57 +08:00
|
|
|
|
//将所有的线段放到一起
|
|
|
|
|
|
let pieceCollection = turf.featureCollection(
|
|
|
|
|
|
outerPieceCollection.features.concat(splitterPieceCollection.features),
|
|
|
|
|
|
);
|
|
|
|
|
|
|
2024-09-09 09:11:36 +08:00
|
|
|
|
|
|
|
|
|
|
console.log("所有分割线段",pieceCollection);
|
|
|
|
|
|
|
2024-07-25 10:01:57 +08:00
|
|
|
|
//使用turf将闭合线组成多边形
|
|
|
|
|
|
let polygonCollection = turf.polygonize(pieceCollection);
|
|
|
|
|
|
|
2024-09-09 09:11:36 +08:00
|
|
|
|
console.log("分割线合并面数据",polygonCollection)
|
|
|
|
|
|
|
2024-07-25 10:01:57 +08:00
|
|
|
|
//对多边形进行判断,切割外的多边形丢弃
|
|
|
|
|
|
let innerPolygons = polygonCollection.features.filter((polygon) => {
|
|
|
|
|
|
// 获取多边形质心 多边形不规则时有问题
|
|
|
|
|
|
// let center = turf.centroid(polygon);
|
2024-08-31 17:41:03 +08:00
|
|
|
|
|
2024-07-25 10:01:57 +08:00
|
|
|
|
// 获取多边形表面一点
|
|
|
|
|
|
let newcenter = turf.pointOnFeature(polygon);
|
2024-07-18 13:43:11 +08:00
|
|
|
|
|
2024-08-31 17:41:03 +08:00
|
|
|
|
// 判断点是否在被分割图斑内
|
|
|
|
|
|
// 图斑中心点
|
|
|
|
|
|
let centerPoint = turf.point(newcenter.geometry.coordinates);
|
2024-09-09 09:11:36 +08:00
|
|
|
|
|
2024-08-31 17:41:03 +08:00
|
|
|
|
// 图斑面
|
|
|
|
|
|
let outPolygon = turf.polygon(outerPolygon.geometry.coordinates)
|
|
|
|
|
|
// return turf.booleanPointInPolygon(centerPoint, outPolygon);
|
|
|
|
|
|
|
|
|
|
|
|
// let innerPoint = getRandomPointInPolygon(outPolygon);
|
2024-07-18 13:43:11 +08:00
|
|
|
|
|
2024-08-31 17:41:03 +08:00
|
|
|
|
// 获取多边形边界
|
|
|
|
|
|
// let bbox = turf.bbox(outPolygon); // 获取多边形的边界框
|
|
|
|
|
|
|
|
|
|
|
|
// return turf.booleanPointInPolygon(randomPoint.features[0], outPolygon);
|
|
|
|
|
|
|
|
|
|
|
|
return turf.booleanWithin(centerPoint, outerPolygon);
|
2024-09-09 09:11:36 +08:00
|
|
|
|
|
2024-08-31 17:41:03 +08:00
|
|
|
|
});
|
2024-07-25 10:01:57 +08:00
|
|
|
|
// let innerPolygons = polygonCollection.features;
|
2024-07-18 13:43:11 +08:00
|
|
|
|
|
2024-07-25 10:01:57 +08:00
|
|
|
|
//处理镂空数据(多处镂空数据会导致计算错误,因为polygonize方法无法正常的返回数据)
|
|
|
|
|
|
if (outerPolygon.geometry.coordinates?.length > 1) {
|
|
|
|
|
|
//获取镂空的面数据
|
|
|
|
|
|
let holeCollection = turf.featureCollection(
|
|
|
|
|
|
outerPolygon.geometry.coordinates.slice(1).map((item) => turf.polygon([item])),
|
|
|
|
|
|
);
|
2024-07-18 13:43:11 +08:00
|
|
|
|
|
2024-07-25 10:01:57 +08:00
|
|
|
|
//剔除掉镂空的部分数据
|
|
|
|
|
|
innerPolygons = innerPolygons.map((polygon) => {
|
|
|
|
|
|
let diff = polygon;
|
|
|
|
|
|
turf.featureEach(holeCollection, (hole) => {
|
|
|
|
|
|
diff = turf.difference(diff, hole);
|
|
|
|
|
|
});
|
|
|
|
|
|
return diff;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2024-09-09 09:11:36 +08:00
|
|
|
|
|
2024-07-25 10:01:57 +08:00
|
|
|
|
innerPolygons.forEach((item, index) => {
|
|
|
|
|
|
innerPolygons[index].properties.id = generateUUID();
|
2024-09-09 09:11:36 +08:00
|
|
|
|
// console.log("innerPolygons_length",innerPolygons[index].geometry.coordinates[0].length);
|
|
|
|
|
|
// console.log("innerPolygons_array",innerPolygons[index].geometry.coordinates[0])
|
|
|
|
|
|
// console.log("innerPolygons_length",innerPolygons[index].geometry.coordinates[0].length);
|
|
|
|
|
|
// console.log("innerPolygons_string",JSON.stringify(innerPolygons[index].geometry.coordinates[0]));
|
2024-07-25 10:01:57 +08:00
|
|
|
|
});
|
2024-09-09 09:11:36 +08:00
|
|
|
|
|
|
|
|
|
|
// let arr = JSON.parse(JSON.stringify(innerPolygons))
|
|
|
|
|
|
|
|
|
|
|
|
// arr.forEach((item, index) => {
|
|
|
|
|
|
// arr[index].properties.id = generateUUID();
|
|
|
|
|
|
|
|
|
|
|
|
// if((arr[index].geometry.coordinates[0][0][0] == arr[index].geometry.coordinates[0][arr[index].geometry.coordinates[0].length-1][0]) && arr[index].geometry.coordinates[0][0][1] == arr[index].geometry.coordinates[0][arr[index].geometry.coordinates[0].length-1][1]){
|
|
|
|
|
|
|
|
|
|
|
|
// }else{
|
|
|
|
|
|
// arr[index].geometry.coordinates[0].push(arr[index].geometry.coordinates[0][0])
|
|
|
|
|
|
// console.log("coordinates789",arr);
|
|
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-07-25 10:01:57 +08:00
|
|
|
|
return innerPolygons;
|
2024-09-09 09:11:36 +08:00
|
|
|
|
|
2024-07-18 13:43:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-31 17:41:03 +08:00
|
|
|
|
function getRandomPointInPolygon(polygon) {
|
|
|
|
|
|
const bbox = turf.bbox(polygon); // 获取多边形的边界框
|
|
|
|
|
|
let point;
|
|
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
|
point = turf.randomPoint(1, {bbox: bbox}).features[0]; // 从边界框中生成一个随机点
|
|
|
|
|
|
} while (!turf.booleanPointInPolygon(point, polygon)); // 检查点是否在多边形内部
|
|
|
|
|
|
|
|
|
|
|
|
return point;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-09-09 09:11:36 +08:00
|
|
|
|
export function splitPolygonByFill(drawPolygon, outerPolygon) {
|
2024-07-18 13:43:11 +08:00
|
|
|
|
|
2024-09-09 09:11:36 +08:00
|
|
|
|
console.log("drawPolygon",drawPolygon);
|
|
|
|
|
|
console.log("outerPolygon",outerPolygon)
|
|
|
|
|
|
|
|
|
|
|
|
var polygon1 = turf.polygon(drawPolygon.geometry.coordinates)
|
2024-07-25 10:01:57 +08:00
|
|
|
|
var polygon2 = turf.polygon(outerPolygon.geometry.coordinates);
|
2024-09-09 09:11:36 +08:00
|
|
|
|
var difference = turf.difference(polygon2,polygon1);
|
|
|
|
|
|
var intersection = turf.intersect(polygon2, polygon1);
|
|
|
|
|
|
|
|
|
|
|
|
intersection.properties.id = generateUUID();
|
|
|
|
|
|
difference.properties.id = generateUUID();
|
|
|
|
|
|
|
|
|
|
|
|
let splitAfterFeatures = [intersection,difference];
|
|
|
|
|
|
|
|
|
|
|
|
// splitAfterFeatures?.forEach((item,index)=>{
|
|
|
|
|
|
// splitAfterFeatures[index].properties?.id = generateUUID();
|
|
|
|
|
|
// })
|
|
|
|
|
|
|
|
|
|
|
|
return splitAfterFeatures;
|
|
|
|
|
|
|
2024-07-25 10:01:57 +08:00
|
|
|
|
}
|
2024-09-09 09:11:36 +08:00
|
|
|
|
|
|
|
|
|
|
export function printLngLat(arr){
|
|
|
|
|
|
arr.forEach((item,index)=>{
|
|
|
|
|
|
console.log(item[1]+","+item[0]+"\n");
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|