如何分割JTS多边形

问题描述

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

我试图研究JTS方法,但无法使用它。

final List<Coordinate> coordinates = List.of(new Coordinate(0,0),new Coordinate(-1,1),new Coordinate(1,3),new Coordinate(2,new Coordinate(3,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,midY);
    Envelope ulEnv = new Envelope(minX,midY,maxY);
    Envelope urEnv = new Envelope(midX,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

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