polygon移除一定角度的顶点连接线
polygon移除一定角度的顶点连接线
上文描述了如何求解每个顶点的角度,在此基础上需要将不符合要求的点删除留下的点链接成面即可
测试数据
POLYGON ((290.890041493776 392.188796680498,279.707468879668 191.919087136929,307.155601659751 327.126556016598,363.06846473029 170.570539419087,548.089211618257 237.665975103734,605.01867219917 407.4377593361,493.192946058091 301.711618257261,472.860995850622 232.582987551867,479.977178423236 311.877593360996,290.890041493776 392.188796680498))
流程注解
本次案例使用的角度是30° 两条直线间的距离小于30 删除这个点的连接线
移除30°以后的图像
代码实现
@Override
public PolygonAngleResult polygonWktAngleMerge(String wkt, Double angle) throws ParseException {
PolygonAngleResult polygonAngleResult = polygonWktAngle(wkt);
polygonAngleResult.setRemoveAngle(angle);
Polygon oldPolygon = polygonAngleResult.getPolygon();
Coordinate[] coordinates = oldPolygon.getCoordinates();
List<HashMap<PointF, Double>> polygonAngles = polygonAngleResult.getPolygonAngles();
List<Coordinate> collect = Arrays.stream(coordinates).collect(Collectors.toList());
for (int i = 0; i < polygonAngles.size(); i++) {
polygonAngles.get(i).forEach(
(k,v)->{
for (int j = 0; j < collect.size(); j++) {
Coordinate coordinate = new Coordinate(k.x, k.y);
if (collect.get(j).equals(coordinate) && (v < 30 || 180 - v < 30)) {
collect.remove(coordinate);
}
}
}
);
}
Polygon polygon = new GeometryFactory().createPolygon(createCoordinate(collect));
polygonAngleResult.setNewPolygon(polygon);
return polygonAngleResult;
}
/**
* 根据 List<Coordinate> collect创建 collect []
* @param collect List<Coordinate> collect
* @return Coordinate
*/
private Coordinate[] createCoordinate(List<Coordinate> collect) {
Coordinate[] coordinates = new Coordinate[collect.size()];
for (int i = 0; i < collect.size(); i++) {
coordinates[i] = collect.get(i);
}
return coordinates;
}
可视化
源码
本文代码及可视化代码均放在 gitee 码云上 欢迎star & fork