2011-12-15 99 views
4

我有幾何對象的集合。現在我想從整個集合中計算最小的邊界矩形。 我使用java拓撲套件,但我無法弄清楚如何做到這一點?最小邊界矩形與JTS

回答

4

有在http://tsusiatsoftware.net/jts/javadoc/index.html

看看如果我假設你正在使用的GeometryCollection實例。如果這是真的,你可以直接調用

geometry.getEnvelope(); 

geometry.getEnvelopeInternal(); 

如果你想要一個信封實例

它將返回你的GeometryCollection的最小矩形。

如果你有幾何圖形的集合,你可以直接使用一個信封,並在每次處理您收集的新geometryc時間展開。

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()): 
} 
1

我從來沒有使用JTS,但Google搜索這樣的:

迭代通過集合併爲每個對象調用getBoundary().getEnvelopeInternal()

+0

HM,有什麼長期沒有你一派?你可以給我鏈接嗎? – ABLX 2011-12-15 13:23:32

0

我只是把一個這樣在一起。

幾何類有一個「getEnvelopeInternal()」返回的內切包絡,但「的getEnvelope()」只是返回另一個幾何。

看的Javadoc,看來返回的幾何對象可以是:

  1. 空點匹配空幾何對象。
  2. 單個點,與傳入的點匹配。
  3. 多邊形與4個座標指定封閉信封。

看着信封上的其它註釋,我看你能「擴大」信封....所以這裏的util的,我建轉換的靜態:

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