英文:
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<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?
答案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<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:
In a production setting you'd want some error checking to make sure you can handle the point that's generated.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论