GeoTools应用-(JTS Geometry Operations)

来源:互联网 发布:做问卷的软件 编辑:程序博客网 时间:2024/05/18 03:01

GeoTools应用-(JTS Geometry Operations)

转载自:http://blog.csdn.net/cdl2008sky/article/details/7278873

 

Geometry 空间分析方法几何图形操作包
在operation包内,包含buffer、distance、linemerge、overlap、polygonize、predicate、relate、valide八个子包;
分别对应着计算图形的缓冲、距离、线段融合、图形覆盖、多边形化、断言、关联、有效性等的操作。所有的操作针对的都是在geom包中定义的Geometry对象。
由于在计算机中,所有的图形都是离散的点组成,所以所有的操作都是在组成图形的点的集合上进行的.
一个图形(Geometry)的缓冲(buffer)
距离操作(distance)是个二元操作,操作对象Geometry A、B,返回(A)与(B)中距离最近的两个点的距离。
线段的融合(linemerge)是将Geometry A中相互连接的线段进行连接。
图形的覆盖(overlap),在这里不想多说了,自己稀里糊涂的。
多边形化操作(polygonize)对Geometry A进行计算,返回一个多边形(Polygon)。将由许多个点表示的图形,用少量的点来表示,减少图形的信息,即对图形进行降维。
断言(predicate)是一个二维的操作,对Geometry之间的关系进行判断的操作。

关联(relate) 根据DE-9IM(The Dimensionally Extended Nine-Intersection Model),该方法返回两个Geometry A与B的相交矩阵IM(Intersections Matrix)。这个矩阵在计算图形关系上用到。

列举了distance,intersection,union,difference 操作

 

代码段:

package com.mapbar.geo.jts.operation;import java.util.ArrayList;import java.util.List;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;/**   * Class Operation.java  * Description 几何对象操作 * Company mapbar  * author Chenll E-mail: Chenll@mapbar.com * Version 1.0  * Date 2012-2-21 上午10:47:47 */public class Operation {private GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null );/** * create a Point * @param x * @param y * @return */public Coordinate point(double x,double y){return new Coordinate(x,y);}/** * create a line * @return */public LineString createLine(List<Coordinate> points){Coordinate[] coords  = (Coordinate[]) points.toArray(new Coordinate[points.size()]);LineString line = geometryFactory.createLineString(coords);return line;}/** * 返回(A)与(B)中距离最近的两个点的距离 * @param a * @param b * @return */public double distanceGeo(Geometry a,Geometry b){return a.distance(b);}/** * 两个几何对象的交集 * @param a * @param b * @return */public Geometry intersectionGeo(Geometry a,Geometry b){return a.intersection(b);}/** * 几何对象合并 * @param a * @param b * @return */public Geometry unionGeo(Geometry a,Geometry b){return a.union(b);}/** * 在A几何对象中有的,但是B几何对象中没有 * @param a * @param b * @return */public Geometry differenceGeo(Geometry a,Geometry b){return a.difference(b);}public static void main(String[] args){Operation op = new Operation();//创建一条线List<Coordinate> points1 = new ArrayList<Coordinate>();points1.add(op.point(0,0));points1.add(op.point(1,3));points1.add(op.point(2,3));LineString line1 = op.createLine(points1);//创建第二条线List<Coordinate> points2 = new ArrayList<Coordinate>();points2.add(op.point(3,0));points2.add(op.point(3,3));points2.add(op.point(5,6));LineString line2 = op.createLine(points2);System.out.println(op.distanceGeo(line1,line2));//out 1.0System.out.println(op.intersectionGeo(line1,line2));//out GEOMETRYCOLLECTION EMPTYSystem.out.println(op.unionGeo(line1,line2)); //out MULTILINESTRING ((0 0, 1 3, 2 3), (3 0, 3 3, 5 6))System.out.println(op.differenceGeo(line1,line2));//out LINESTRING (0 0, 1 3, 2 3)}}


 

原创粉丝点击