简体   繁体   English

如何分割 JTS 多边形

[英]How to split a JTS polygon

I have a big polygon and I want to find intersecting features with the polygon but since polygon is too big, I am getting timeout exception.我有一个大多边形,我想找到与多边形相交的特征,但由于多边形太大,我遇到超时异常。

I was trying to look into JTS methods but couldn't get how to use it.我试图研究 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();

Can someone give me pointers on how to split the polygon object?有人可以指导我如何拆分多边形 object 吗?

There are many (an infinite number) ways to split a polygon, but this is how I would do it, by calculating the mid lines of the envelope and intersecting the new envelopes against the polygon.有许多(无数种)方法可以分割多边形,但我会这样做,通过计算包络线的中线并将新包络线与多边形相交。

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;
  }

This gives this for your polygon:这为您的多边形提供了这个:

在此处输入图像描述

And if you call it again on that output:如果您在 output 上再次调用它:

在此处输入图像描述

In a production setting you'd want some error checking to make sure you can handle the point that's generated.在生产设置中,您需要进行一些错误检查以确保您可以处理生成的点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM