1. 程式人生 > >JTS Geometry空間關係的判斷

JTS Geometry空間關係的判斷

幾何資訊和拓撲關係是地理資訊系統中描述地理要素的空間位置和空間關係的不可缺少的基本資訊。其中幾何資訊主要涉及幾何目標的座標位置、方向、角度、距離和麵積等資訊,它通常用解析幾何的方法來分析。而空間關係資訊主要涉及幾何關係的“相連”、“相鄰”、“包含”等資訊,它通常用拓撲關係或拓撲結構的方法來分析。拓撲關係是明確定的


下面的例子介紹了 equals、disjoint、intersects 的用法:

package com.mapbar.geo.jts;
 
import org.geotools.geometry.jts.JTSFactoryFinder;
 
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;
 
/**  
 * Class GeometryRelated.java 
 * Description 二元比較集合。二元比較以兩個幾何物件作為引數,返回一個Boolean型別的值,
 * 來指明這兩個幾何物件是否具有指定的空間關係。支援的空間關係包括:
 * equals、disjoint、intersects, touches, crosses, within, contains, overlaps
 * Company mapbar 
 * author Chenll E-mail: 
[email protected]
* Version 1.0 * Date 2012-2-17 下午06:17:01 */ public class GeometryRelated { private GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null ); public Point createPoint(String lon,String lat){ Coordinate coord = new Coordinate(Double.parseDouble(lon), Double.parseDouble(lat)); Point point = geometryFactory.createPoint( coord ); return point; } /** * will return true as the two line strings define exactly the same shape. * 兩個幾何物件是否是重疊的 * @return * @throws ParseException */ public boolean equalsGeo() throws ParseException{ WKTReader reader = new WKTReader( geometryFactory ); LineString geometry1 = (LineString) reader.read("LINESTRING(0 0, 2 0, 5 0)"); LineString geometry2 = (LineString) reader.read("LINESTRING(5 0, 0 0)"); // return geometry1 ==geometry2; false //check if two geometries are exactly equal; right down to the coordinate level. // return geometry1.equalsExact(geometry2); false return geometry1.equals(geometry2);//true } /** * The geometries have no points in common * 幾何物件沒有交點(相鄰) * @return * @throws ParseException */ public boolean disjointGeo() throws ParseException{ WKTReader reader = new WKTReader( geometryFactory ); LineString geometry1 = (LineString) reader.read("LINESTRING(0 0, 2 0, 5 0)"); LineString geometry2 = (LineString) reader.read("LINESTRING(0 1, 0 2)"); return geometry1.disjoint(geometry2); } /** * The geometries have at least one point in common. * 至少一個公共點(相交) * @return * @throws ParseException */ public boolean intersectsGeo() throws ParseException{ WKTReader reader = new WKTReader( geometryFactory ); LineString geometry1 = (LineString) reader.read("LINESTRING(0 0, 2 0, 5 0)"); LineString geometry2 = (LineString) reader.read("LINESTRING(0 0, 0 2)"); Geometry interPoint = geometry1.intersection(geometry2);//相交點 System.out.println(interPoint.toText());//輸出 POINT (0 0) return geometry1.intersects(geometry2); } /** * @param args * @throws ParseException */ public static void main(String[] args) throws ParseException { GeometryRelated gr = new GeometryRelated(); System.out.println(gr.equalsGeo()); System.out.println(gr.disjointGeo()); System.out.println(gr.intersectsGeo()); } }

Geometry 疊加操作

在GIS中,緩衝(buffering)是一種用於計算包含在一個幾何圖形(Geometry)特定距離區域內所有點的的操作。在數學術語中,這被稱為通過一個與緩衝區相等的圓的半徑去計算幾何圖形的閔可夫斯基(Minkowski)總和。發現正的(positive)和負的(negative)緩衝,有時與操作的腐蝕(erosion)和膨脹(dilation)有關。在CAD/CAM,緩衝曲線被稱為偏移曲線(offset curves)。你可以使用JTS,通過Geometry buffer方法或者Bufferop類,去計算一個圖形的緩衝區。緩衝操作所輸入的Geometry可以是任何類別(包括任意的Geometry集合)。緩衝操作的結果通常是一種區域型別(area type)(多邊形或者多多邊形)。結果也可能為空[例如,一條線(linestring)的負緩衝。