JTS中的最小边界矩形

9

我有一个几何对象的集合。现在我想从整个集合中计算最小边界矩形。 我正在使用Java拓扑套件,但我不知道该如何实现这一点?

3个回答

11

请查看http://tsusiatsoftware.net/jts/javadoc/index.html

如果我假设您正在使用GeometryCollection实例。如果是这样,您可以直接调用:

geometry.getEnvelope();
或者
geometry.getEnvelopeInternal();

如果您想要一个Envelope实例

它会返回GeometryCollection的最小矩形。

如果您有一组Geometries,可以直接使用Envelope,并在处理集合中的每个新几何体时扩展它。

Envelope env = new Envelope();
for(Geometry g : mySet){
  env.expandToInclude(g.getEnvelopeInternal()):
}
或者
Envelope env = new Envelope();
for(Geometry g : mySet){
  env.expandToInclude(g.getBoundary().getEnvelopeInternal()):
}

getEnvelopeInternal()会忽略您几何图形的精度模型。 - Campa

2

我刚刚像这样组合了一个。

Geometry类有一个“getEnvelopeInternal()”方法,返回内部的包络线,但是“getEnvelope()”只返回另一个Geometry。

从javadoc中可以看出,返回的Geometry对象可能是:

  1. 与空Geometry对象匹配的空点。
  2. 与传入点匹配的单个点。
  3. 具有4个坐标的多边形,指定封闭包络线。

查看其他关于Envelope的注释,我发现您可以“扩展”包络线...因此,这是我构建的用于转换的静态工具:

public static Envelope enclosingEnvelopFromGeometry(Geometry geometry) {
    final Envelope envelope = new Envelope();
    final Geometry enclosingGeometry = geometry.getEnvelope();
    final Coordinate[] enclosingCoordinates = enclosingGeometry.getCoordinates();
    for (Coordinate c : enclosingCoordinates) {
        envelope.expandToInclude(c);
    }
    return envelope;
} 

1

我从未使用 jts,但是搜索到了以下内容:

遍历集合并对每个对象调用getBoundary().getEnvelopeInternal()


嗯,你搜了什么术语?能给我链接吗? - ABLX
在官方文档中:http://www.vividsolutions.com/jts/JTSHome.htm,包含几何和包络类。 - red1ynx
这不是官方文档。vividsolution不是最新JTS版本的维护者。 - Agemen
请注意,getEnvelopeInternal()将忽略您几何图形的精度模型。 - Campa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接