GeoTools應用-JTS(Geometry之間的關係)

huayang183發表於2020-12-11

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

相等(Equals):

幾何形狀拓撲上相等。

脫節(Disjoint):

幾何形狀沒有共有的點。

相交(Intersects):

幾何形狀至少有一個共有點(區別於脫節)

接觸(Touches):

幾何形狀有至少一個公共的邊界點,但是沒有內部點。

交叉(Crosses):

幾何形狀共享一些但不是所有的內部點。

內含(Within):

幾何形狀A的線都在幾何形狀B內部。

包含(Contains):

幾何形狀B的線都在幾何形狀A內部(區別於內含)

重疊(Overlaps):

幾何形狀共享一部分但不是所有的公共點,而且相交處有他們自己相同的區域。

 

下面的例子介紹了 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: Chenll@mapbar.com

 * 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 疊加操作

 

緩衝區分析(Buffer)

包含所有的點在一個指定距離內的多邊形和多多邊形

凸殼分析(ConvexHull)

包含幾何形體的所有點的最小凸殼多邊形(外包多邊形)

交叉分析(Intersection)

交叉操作就是多邊形AB中所有共同點的集合。

聯合分析(Union)

AB的聯合操作就是AB所有點的集合。

差異分析(Difference)

AB形狀的差異分析就是A裡有B裡沒有的所有點的集合。

對稱差異分析(SymDifference)

AB形狀的對稱差異分析就是位於A中或者B中但不同時在AB中的所有點的集合

 

 

 

 

 

 

 

 

 

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

相關文章