turf.js实现多边形分割
2023-04-18 12:31:25 时间
概述
在做编辑的时候,难免会遇到多边形的分割问题,本文用turf.js
实现此功能。
效果
实现思路
实现代码
function polygonCut(poly, line, tolerance = .001, toleranceType ='kilometers') {
// 1. 条件判断
if (poly.geometry === void 0 || poly.geometry.type !== 'Polygon')
throw ('传入的必须为polygon');
if (line.geometry === void 0 || line.geometry.type.toLowerCase().indexOf('linestring') === -1)
throw ('传入的必须为linestring');
if (line.geometry.type === "LineString") {
if (turf.booleanPointInPolygon(turf.point(line.geometry.coordinates[0]), poly)
|| turf.booleanPointInPolygon(turf.point(line.geometry.coordinates[line.geometry.coordinates.length - 1]), poly))
throw ('起点和终点必须在多边形之外');
}
// 2. 计算交点,并把线的点合并
let lineIntersect = turf.lineIntersect(line, poly);
const lineExp = turf.explode(line);
for (let i = 0; i < lineExp.features.length - 1; i++) {
lineIntersect.features.push(turf.point(lineExp.features[i].geometry.coordinates));
}
// 3. 计算线的缓冲区
const lineBuffer = turf.buffer(line, tolerance, {
units: toleranceType
});
// 4. 计算线缓冲和多边形的difference,返回"MultiPolygon",所以将其拆开
const _body = turf.difference(poly, lineBuffer);
let pieces = [];
if (_body.geometry.type === 'Polygon') {
pieces.push(turf.polygon(_body.geometry.coordinates));
} else {
_body.geometry.coordinates.forEach(function (a) { pieces.push(turf.polygon(a)) });
}
// 5. 处理点数据
for (p in pieces) {
const piece = pieces[p];
for (let c in piece.geometry.coordinates[0]) {
const coord = piece.geometry.coordinates[0][c];
const p = turf.point(coord);
for (let lp in lineIntersect.features) {
const lpoint = lineIntersect.features[lp];
if (turf.distance(lpoint, p, toleranceType) <= tolerance*2) {
piece.geometry.coordinates[0][c] = lpoint.geometry.coordinates;
}
}
}
}
// 6. 过滤掉重复点
for (p in pieces) {
const coords = pieces[p].geometry.coordinates[0]
pieces[p].geometry.coordinates[0] = filterDuplicatePoints(coords);
}
// 7. 将属性赋予每一个polygon,并处理id
pieces.forEach((a, index) => {
a.properties = Object.assign({}, poly.properties)
a.properties.id += `-${index}`
});
return turf.featureCollection(pieces);
}
相关文章
- 直接在代码里面对list集合进行分页
- .NET Framework 4.5新特性详解
- 大数据的简要介绍
- 大数据的由来
- 高斯混合模型的自然梯度变量推理
- timing-wheel 仿Kafka实现的时间轮算法
- 使用Navicat软件连接自建数据库(Linux系统)
- 那一天,我被Redis主从架构支配的恐惧
- Redis 深入了解键的过期时间
- C#使用委托调用实现用户端等待闪屏
- 基于流计算 Oceanus 和 Elasticsearch Service 构建百亿级实时监控系统
- GRAND | 转录调控网络预测数据库
- JFreeChart API中文文档
- 临床相关突变查询数据库
- TIGER | 人类胰岛基因变化查询数据库
- 视频边缘计算网关EasyNVR在视频整体监控解决方案中的应用分析
- Apache Arrow - 大数据在数据湖后的下一个风向标
- 常见的电商数据指标体系
- AKShare-艺人数据-艺人流量价值
- MySQL中多表联合查询与子查询的这些区别,你可能不知道!