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)的负缓冲。

猜你喜欢

转载自blog.csdn.net/kone0611/article/details/83781353