CaiYuanYiTiHua/src/components/MapboxMaps/lib/splitpolygon.ts

158 lines
4.6 KiB
TypeScript
Raw Normal View History

import { generateUUID } from '../src/tool';
2024-07-18 13:43:11 +08:00
/**
* 使turf
* @param line 线
* @param outerPolygon
* return
*/
export function splitPolygonByLine(line, outerPolygon) {
let truncatedSplitter = turf.truncate(turf.lineString(outerPolygon.geometry.coordinates[0]), {
precision: 7,
});
2024-07-19 17:32:52 +08:00
//求交点
let intersectCollection = turf.lineIntersect(line, truncatedSplitter);
2024-07-30 11:35:48 +08:00
if (intersectCollection.features.length < 2) {
return null;
}
2024-07-19 17:32:52 +08:00
//将点合并成MultiPoint
let intersectCombined = turf.combine(intersectCollection).features[0];
2024-07-20 09:47:30 +08:00
//分别获取切割线
let outerPieceCollection = turf.lineSplit(line, intersectCombined);
let splitterPieceCollection = turf.lineSplit(truncatedSplitter, intersectCombined);
2024-07-19 17:32:52 +08:00
//将所有的线段放到一起
let pieceCollection = turf.featureCollection(
outerPieceCollection.features.concat(splitterPieceCollection.features),
);
//使用turf将闭合线组成多边形
let polygonCollection = turf.polygonize(pieceCollection);
//对多边形进行判断,切割外的多边形丢弃
let innerPolygons = polygonCollection.features.filter((polygon) => {
// 获取多边形质心 多边形不规则时有问题
// let center = turf.centroid(polygon);
2024-08-31 17:41:03 +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);
// 图斑面
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);
});
// let innerPolygons = polygonCollection.features;
2024-07-18 13:43:11 +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
//剔除掉镂空的部分数据
innerPolygons = innerPolygons.map((polygon) => {
let diff = polygon;
turf.featureEach(holeCollection, (hole) => {
diff = turf.difference(diff, hole);
});
return diff;
});
}
innerPolygons.forEach((item, index) => {
innerPolygons[index].properties.id = generateUUID();
});
return innerPolygons;
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;
}
export function splitPolygonByFill(fill, outerPolygon) {
var polygon1 = turf.polygon(fill.geometry.coordinates);
// var polygon1 = turf.polygon(
// [
// [
// [
// 118.08246593377775,
// 35.22214906870923
// ],
// [
// 118.0824468053414,
// 35.2221025269492
// ],
// [
// 118.08255783344151,
// 35.22206277960647
// ],
// [
// 118.08258527859043,
// 35.22213378126961
// ],
// [
// 118.08246593377775,
// 35.22214906870923
// ]
// ]
// ]
// )
2024-07-18 13:43:11 +08:00
var polygon2 = turf.polygon(outerPolygon.geometry.coordinates);
// var polygon2 = turf.polygon(
// [
// [
// [
// 118.082358,
// 35.222126
// ],
// [
// 118.082517,
// 35.222061
// ],
// [
// 118.082535,
// 35.222098
// ],
// [
// 118.082372,
// 35.222163
// ],
// [
// 118.082358,
// 35.222126
// ]
// ]
// ]
// )
var difference = turf.difference(polygon1, polygon2);
console.log('difference123', difference);
return difference;
}