如何分割JTS多边形

huangapple go评论63阅读模式
英文:

How to split a JTS polygon

问题

我有一个大多边形,想要找出与该多边形相交的特征,但由于多边形太大,我得到了超时异常。

我尝试查阅了 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();

有人能否给我一些关于如何分割多边形对象的指导?

英文:

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.

final List&lt;Coordinate&gt; 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?

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

这会为您的多边形产生以下结果:

(插入图片描述)

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

(插入图片描述)

在生产环境中,您会希望进行一些错误检查,以确保您能够处理生成的点。

英文:

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&lt;Geometry&gt; split(Polygon p) {
    List&lt;Geometry&gt; ret = new ArrayList&lt;&gt;();
    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:

如何分割JTS多边形

And if you call it again on that output:

如何分割JTS多边形

In a production setting you'd want some error checking to make sure you can handle the point that's generated.

huangapple
  • 本文由 发表于 2020年10月8日 05:34:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64252638.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定