如何分割JTS多边形?

3

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

我尝试研究了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();

有人可以给我指点如何拆分多边形对象吗?

1个回答

7

有许多(无限数量)方法可以分割多边形,但我会通过计算外包矩形的中线,并将新外包矩形与多边形相交来完成它。

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 在生产环境中,您需要进行一些错误检查来确保您可以处理所生成的点。

JTS.toGeometry被org.locationtech.jts.geom.GeometryFactory.toGeometry所取代。 - Ricardo Mayerhofer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接