# 如何分割JTS多边形

go评论64阅读模式

How to split a JTS polygon

# 问题

``````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

``````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);

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

return ret;
}
``````

This gives this for your polygon:

And if you call it again on that output:

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

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

go 70

go 52

go 70

go 63