GeoTools应用-JTS(Geometry之间的关系)

几何信息和拓扑关系是地理信息系统中描述地理要素的空间位置和空间关系的不可缺少的基本信息。其中几何信息主要涉及几何目标的坐标位置、方向、角度、距离和面积等信息,它通常用解析几何的方法来分析。而空间关系信息主要涉及几何关系的“相连”、“相邻”、“包含”等信息,它通常用拓扑关系或拓扑结构的方法来分析。拓扑关系是明确定的

相等(Equals):

几何形状拓扑上相等。

脱节(Disjoint):

几何形状没有共有的点。

相交(Intersects):

几何形状至少有一个共有点(区别于脱节)

接触(Touches):

几何形状有至少一个公共的边界点,但是没有内部点。

交叉(Crosses):

几何形状共享一些但不是所有的内部点。

内含(Within):

几何形状A的线都在几何形状B内部。

包含(Contains):

几何形状B的线都在几何形状A内部(区别于内含)

重叠(Overlaps):

几何形状共享一部分但不是所有的公共点,而且相交处有他们自己相同的区域。

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

  1. package com.mapbar.geo.jts; 
  2.  
  3. import org.geotools.geometry.jts.JTSFactoryFinder; 
  4.  
  5. import com.vividsolutions.jts.geom.Coordinate; 
  6. import com.vividsolutions.jts.geom.Geometry; 
  7. import com.vividsolutions.jts.geom.GeometryFactory; 
  8. import com.vividsolutions.jts.geom.LineString; 
  9. import com.vividsolutions.jts.geom.Point; 
  10. import com.vividsolutions.jts.io.ParseException; 
  11. import com.vividsolutions.jts.io.WKTReader; 
  12.  
  13. /** 
  14. * Class GeometryRelated.java
  15. * Description 二元比较集合。二元比较以两个几何对象作为参数,返回一个Boolean类型的值,
  16. * 来指明这两个几何对象是否具有指定的空间关系。支持的空间关系包括:
  17. * equals、disjoint、intersects, touches, crosses, within, contains, overlaps
  18. * Company mapbar
  19. * author Chenll E-mail: Chenll@mapbar.com
  20. * Version 1.0
  21. * Date 2012-2-17 下午06:17:01
  22. */ 
  23. public class GeometryRelated { 
  24.      
  25.     private GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(
    null ); 
  26.      
  27.     public Point createPoint(String lon,String lat){ 
  28.         Coordinate coord = new Coordinate(Double.parseDouble(lon), Double.parseDouble(lat)); 
  29.         Point point = geometryFactory.createPoint( coord ); 
  30.         return point; 
  31.     } 
  32.      
  33.     /**
  34.      *  will return true as the two line strings define exactly the same shape.
  35.      *  两个几何对象是否是重叠的
  36.      * @return
  37.      * @throws ParseException
  38.      */ 
  39.     public boolean equalsGeo()
    throws ParseException{ 
  40.         WKTReader reader = new WKTReader( geometryFactory ); 
  41.         LineString geometry1 = (LineString) reader.read(“LINESTRING(0 0, 2 0, 5 0)”); 
  42.         LineString geometry2 = (LineString) reader.read(“LINESTRING(5 0, 0 0)”); 
  43.         // return geometry1 ==geometry2;  false 
  44.         //check if two geometries are exactly equal; right down to the coordinate level. 
  45.         // return geometry1.equalsExact(geometry2);   false 
  46.         return geometry1.equals(geometry2);//true 
  47.     } 
  48.      
  49.     /**
  50.      * The geometries have no points in common
  51.      * 几何对象没有交点(相邻)
  52.      * @return
  53.      * @throws ParseException
  54.      */ 
  55.     public boolean disjointGeo()
    throws ParseException{ 
  56.         WKTReader reader = new WKTReader( geometryFactory ); 
  57.         LineString geometry1 = (LineString) reader.read(“LINESTRING(0 0, 2 0, 5 0)”); 
  58.         LineString geometry2 = (LineString) reader.read(“LINESTRING(0 1, 0 2)”); 
  59.         return geometry1.disjoint(geometry2); 
  60.     } 
  61.      
  62.     /**
  63.      * The geometries have at least one point in common.
  64.      * 至少一个公共点(相交)
  65.      * @return
  66.      * @throws ParseException
  67.      */ 
  68.     public boolean intersectsGeo()
    throws ParseException{ 
  69.         WKTReader reader = new WKTReader( geometryFactory ); 
  70.         LineString geometry1 = (LineString) reader.read(“LINESTRING(0 0, 2 0, 5 0)”); 
  71.         LineString geometry2 = (LineString) reader.read(“LINESTRING(0 0, 0 2)”); 
  72.         Geometry interPoint = geometry1.intersection(geometry2);//相交点 
  73.         System.out.println(interPoint.toText());//输出 POINT (0 0) 
  74.         return geometry1.intersects(geometry2); 
  75.     } 
  76.     /**
  77.      * @param args
  78.      * @throws ParseException
  79.      */ 
  80.     public static
    void main(String[] args) throws ParseException { 
  81.         GeometryRelated gr = new GeometryRelated(); 
  82.         System.out.println(gr.equalsGeo()); 
  83.         System.out.println(gr.disjointGeo()); 
  84.         System.out.println(gr.intersectsGeo()); 
  85.     } 
  86.  
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)的负缓冲。]


转载自:https://blog.csdn.net/sxausgyy/article/details/8151003

You may also like...