gpt4 book ai didi

java - 如何分割 JTS 多边形

转载 作者:行者123 更新时间:2023-12-05 01:33:53 41 4
gpt4 key购买 nike

我有一个大多边形,我想找到与该多边形相交的要素,但由于多边形太大,我遇到超时异常。

我试图研究 JTS 方法,但不知道如何使用它。

final List<Coordinate> coordinates = List.of(new Coordinate(0, 0), new Coordinate(-1, 1),
new Coordinate(1, 3), new Coordinate(2, 3), new Coordinate(3, 1), new Coordinate(0, 0));
final GeometryFactory factory = new GeometryFactory();
final Polygon polygon = factory.createPolygon(coordinates.toArray(new Coordinate[0]));
final Geometry envelope = polygon.getEnvelope();

有人可以指导我如何分割多边形对象吗?

最佳答案

有很多(无数种)方法可以分割多边形,但我会这样做,通过计算封套的中线并将新的封套与多边形相交。

public List<Geometry> split(Polygon p) {
List<Geometry> ret = new ArrayList<>();
final Envelope envelope = p.getEnvelopeInternal();
double minX = envelope.getMinX();
double maxX = envelope.getMaxX();
double midX = minX + (maxX - minX) / 2.0;
double minY = envelope.getMinY();
double maxY = envelope.getMaxY();
double midY = minY + (maxY - minY) / 2.0;

Envelope llEnv = new Envelope(minX, midX, minY, midY);
Envelope lrEnv = new Envelope(midX, maxX, minY, midY);
Envelope ulEnv = new Envelope(minX, midX, midY, maxY);
Envelope urEnv = new Envelope(midX, maxX, midY, maxY);
Geometry ll = JTS.toGeometry(llEnv).intersection(p);
Geometry lr = JTS.toGeometry(lrEnv).intersection(p);
Geometry ul = JTS.toGeometry(ulEnv).intersection(p);
Geometry ur = JTS.toGeometry(urEnv).intersection(p);
ret.add(ll);
ret.add(lr);
ret.add(ul);
ret.add(ur);

return ret;
}

这为您的多边形提供了这个:

enter image description here

如果您在该输出上再次调用它:

enter image description here

在生产设置中,您需要进行一些错误检查以确保您可以处理生成的点。

关于java - 如何分割 JTS 多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64252638/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com