JTS Geometry关系判断和分析

  • JTS Geometry关系判断和分析
    • 1.关系判断
      • 1.1实例
    • 2.关系分析
      • 2.1实例
  • JTS(Geometry)

JTS Geometry关系判断和分析

JTS Geometry关系判断和分析

1.关系判断

Geometry之间的关系有如下几种:

关系 说明
相等(Equals) 几何形状拓扑上相等
脱节(Disjoint) 几何形状没有共有的点
相交(Intersects) 几何形状至少有一个共有点(区别于脱节)
接触(Touches) 几何形状有至少一个公共的边界点,但是没有内部点
交叉(Crosses) 几何形状共享一些但不是所有的内部点
内含(Within) 几何形状A的线都在几何形状B内部
包含(Contains) 几何形状B的线都在几何形状A的内部(区别于内含)
重叠(Overlaps) 几何形状共享一部分但不是所有的公共点,而且相交处有他们自己相同的区域

1.1实例

package com.alibaba.autonavi;import com.vividsolutions.jts.geom.*;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;/*** gemotry之间的关系* @author xingxing.dxx**/
public class GeometryRelated {private GeometryFactory geometryFactory = new GeometryFactory();/***  两个几何对象是否是重叠的* @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.equals(geometry2);//true}/*** 几何对象没有交点(相邻)* @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);}/*** 至少一个公共点(相交)* @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);}/*** 判断以x,y为坐标的点point(x,y)是否在geometry表示的Polygon中* @param x* @param y* @param geometry wkt格式* @return*/public boolean withinGeo(double x,double y,String geometry) throws ParseException {Coordinate coord = new Coordinate(x,y);Point point = geometryFactory.createPoint( coord );WKTReader reader = new WKTReader( geometryFactory );Polygon polygon = (Polygon) reader.read(geometry);return point.within(polygon);}/*** @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());System.out.println(gr.withinGeo(5,5,"POLYGON((0 0, 10 0, 10 10, 0 10,0 0))"));}}

2.关系分析

关系分析有如下几种

关系 说明
缓冲区分析(Buffer) 包含所有的点在一个指定距离内的多边形和多多边形
凸壳分析(ConvexHull) 包含几何形体的所有点的最小凸壳多边形(外包多边形)
交叉分析(Intersection) A∩B 交叉操作就是多边形AB中所有共同点的集合
联合分析(Union) AUB AB的联合操作就是AB所有点的集合
差异分析(Difference) (A-A∩B) AB形状的差异分析就是A里有B里没有的所有点的集合
对称差异分析(SymDifference) (AUB-A∩B) AB形状的对称差异分析就是位于A中或者B中但不同时在AB中的所有点的集合

2.1实例

package com.alibaba.autonavi;import java.util.ArrayList;
import java.util.List;import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;/*** gemotry之间的关系分析** @author xingxing.dxx*/
public class Operation {private GeometryFactory geometryFactory = new GeometryFactory();/*** 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指定距离内的多边形和多多边形** @param a* @param distance* @return*/public Geometry bufferGeo(Geometry a, double distance) {return a.buffer(distance);}/*** 返回(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)}
}

JTS(Geometry)

JTS Geometry

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.GeometryCollection;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.LinearRing;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Polygon;
import com.vividsolutions.jts.geom.MultiPolygon;
import com.vividsolutions.jts.geom.MultiLineString;
import com.vividsolutions.jts.geom.MultiPoint;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;/**  * Class GeometryDemo.java * Description Geometry 几何实体的创建,读取操作* Company mapbar * author Chenll E-mail: Chenll@mapbar.com* Version 1.0 * Date 2012-2-17 上午11:08:50*/
public class GeometryDemo {private GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null );/*** create a point* @return*/public Point createPoint(){Coordinate coord = new Coordinate(109.013388, 32.715519);Point point = geometryFactory.createPoint( coord );return point;}/*** create a point by WKT* @return* @throws ParseException */public Point createPointByWKT() throws ParseException{WKTReader reader = new WKTReader( geometryFactory );Point point = (Point) reader.read("POINT (109.013388 32.715519)");return point;}/*** create multiPoint by wkt* @return*/public MultiPoint createMulPointByWKT()throws ParseException{WKTReader reader = new WKTReader( geometryFactory );MultiPoint mpoint = (MultiPoint) reader.read("MULTIPOINT(109.013388 32.715519,119.32488 31.435678)");return mpoint;}/*** * create a line* @return*/public LineString createLine(){Coordinate[] coords  = new Coordinate[] {new Coordinate(2, 2), new Coordinate(2, 2)};LineString line = geometryFactory.createLineString(coords);return line;}/*** create a line by WKT* @return* @throws ParseException*/public LineString createLineByWKT() throws ParseException{WKTReader reader = new WKTReader( geometryFactory );LineString line = (LineString) reader.read("LINESTRING(0 0, 2 0)");return line;}/*** create multiLine * @return*/public MultiLineString createMLine(){Coordinate[] coords1  = new Coordinate[] {new Coordinate(2, 2), new Coordinate(2, 2)};LineString line1 = geometryFactory.createLineString(coords1);Coordinate[] coords2  = new Coordinate[] {new Coordinate(2, 2), new Coordinate(2, 2)};LineString line2 = geometryFactory.createLineString(coords2);LineString[] lineStrings = new LineString[2];lineStrings[0]= line1;lineStrings[1] = line2;MultiLineString ms = geometryFactory.createMultiLineString(lineStrings);return ms;}/*** create multiLine by WKT* @return* @throws ParseException*/public MultiLineString createMLineByWKT()throws ParseException{WKTReader reader = new WKTReader( geometryFactory );MultiLineString line = (MultiLineString) reader.read("MULTILINESTRING((0 0, 2 0),(1 1,2 2))");return line;}/*** create a polygon(多边形) by WKT* @return* @throws ParseException*/public Polygon createPolygonByWKT() throws ParseException{WKTReader reader = new WKTReader( geometryFactory );Polygon polygon = (Polygon) reader.read("POLYGON((20 10, 30 0, 40 10, 30 20, 20 10))");return polygon;}/*** create multi polygon by wkt* @return* @throws ParseException*/public MultiPolygon createMulPolygonByWKT() throws ParseException{WKTReader reader = new WKTReader( geometryFactory );MultiPolygon mpolygon = (MultiPolygon) reader.read("MULTIPOLYGON(((40 10, 30 0, 40 10, 30 20, 40 10),(30 10, 30 0, 40 10, 30 20, 30 10)))");return mpolygon;}/*** create GeometryCollection  contain point or multiPoint or line or multiLine or polygon or multiPolygon* @return* @throws ParseException*/public GeometryCollection createGeoCollect() throws ParseException{LineString line = createLine();Polygon poly =  createPolygonByWKT();Geometry g1 = geometryFactory.createGeometry(line);Geometry g2 = geometryFactory.createGeometry(poly);Geometry[] garray = new Geometry[]{g1,g2};GeometryCollection gc = geometryFactory.createGeometryCollection(garray);return gc;}/*** create a Circle  创建一个圆,圆心(x,y) 半径RADIUS* @param x* @param y* @param RADIUS* @return*/public Polygon createCircle(double x, double y, final double RADIUS){final int SIDES = 32;//圆上面的点个数Coordinate coords[] = new Coordinate[SIDES+1];for( int i = 0; i < SIDES; i++){double angle = ((double) i / (double) SIDES) * Math.PI * 2.0;double dx = Math.cos( angle ) * RADIUS;double dy = Math.sin( angle ) * RADIUS;coords[i] = new Coordinate( (double) x + dx, (double) y + dy );}coords[SIDES] = coords[0];LinearRing ring = geometryFactory.createLinearRing( coords );Polygon polygon = geometryFactory.createPolygon( ring, null );return polygon;}/*** @param args* @throws ParseException */public static void main(String[] args) throws ParseException {GeometryDemo gt = new GeometryDemo();Polygon p = gt.createCircle(0, 1, 2);//圆上所有的坐标(32个)Coordinate coords[] = p.getCoordinates();for(Coordinate coord:coords){System.out.println(coord.x+","+coord.y);}}
}

JTS Geometry相关推荐

  1. JTS(Geometry)工具类

    空间数据模型 (1).JTS Geometry model  (2).ISO Geometry model (Geometry Plugin and JTS Wrapper Plugin) GeoTo ...

  2. JTS Geometry关系判断和分析

    关系判断 Geometry之间的关系有如下几种: 相等(Equals): 几何形状拓扑上相等. 脱节(Disjoint): 几何形状没有共有的点. 相交(Intersects): 几何形状至少有一个共 ...

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

    几何信息和拓扑关系是地理信息系统中描述地理要素的空间位置和空间关系的不可缺少的基本信息.其中几何信息主要涉及几何目标的坐标位置.方向.角度.距离和面积等信息,它通常用解析几何的方法来分析.而空间关系信 ...

  4. JTS Geometry用例分析

    微信搜索:"二十同学" 公众号,欢迎关注一条不一样的成长之路 拓扑关系        GeometryTest import com.vividsolutions.jts.geom ...

  5. JTS Java空间几何计算、距离、最近点、subLine等计算

    文章目录 前言 地理坐标系和投影坐标系 地理坐标系 投影坐标系 地图投影 墨卡托/Web墨卡托 常见坐标系 地理坐标系和投影坐标系互转 EPSG:3857和EPSG:4326 Java各坐标系之间的转 ...

  6. JTS Java空间几何计算、距离、最近点、subLine等 稳健的一比,持续更新中

    文章目录 前言 地理坐标系和投影坐标系 地理坐标系 投影坐标系 地图投影 墨卡托/Web墨卡托 常见坐标系 地理坐标系和投影坐标系互转 EPSG:3857和EPSG:4326 Java各坐标系之间的转 ...

  7. JTS 空间数据关系分析

    JTS Geometry关系分析: 分析类型 含义 缓冲区分析(Buffer) 包含所有的点在一个指定距离内的多边形和多多边形 凸壳分析(ConvexHull) 包含几何形体的所有点的最小凸壳多边形( ...

  8. JAVA使用JTS 判断坐标点是否在坐标多边形内部

    JAVA使用JTS 判断坐标点是否在坐标多边形内部 思路 Geometry之间的关系 API及参考博客 代码 依赖 工具类 测试类 思路 判断坐标点是否在坐标多边形内部,首先不能直接计算坐标点,是需要 ...

  9. Java JTS点线面分析、线以及多边形合并、面积计算

    参考文章: https://blog.csdn.net/kone0611/article/details/83781484 https://wenku.baidu.com/view/839cbdb1f ...

最新文章

  1. 在GNS3中模拟交换机和PC
  2. 设置超链接在新的窗口中打开,而不是在本窗口中打开
  3. 数论初步——同余与模算术
  4. jQuery的.live()和.die()
  5. php对象魔术方法,php学习之类与对象的魔术方法的使用
  6. Python开发中有可能遇到的套接字重复使用错误
  7. Lingo建模基础入门
  8. Windows下Xelatex的使用
  9. OFDM中的帧(frame)、符号(symbol)、子载波(subcarriers)、导频(Pilot)、保护间隔(guard)的关系图解以及代码详解--MATLAB
  10. 识别到硬盘 计算机不显示盘符,Win10系统下移动硬盘可以识别但是不显示盘符的解决方法...
  11. oop思想php,避免OOP的形式,POP的思想
  12. 毕业四年年薪200万是怎样的一种体验?
  13. 上偏续关系哈斯图_[离散]哈斯图偏序集--最好理解版本
  14. android espresso 教程,Espresso 设置说明
  15. 【数学】三角函数小题
  16. 怎样通过百度文库引流?使得你的网络业绩倍增
  17. [娱乐向]如何使用STM32播放篮球视频
  18. 曝iPhone15或换用USB-C接口;Google将下架第三方Android通话录音APP|极客头条
  19. 有些人,吵架都这么有理有据有节!
  20. 随身WiFi制作Linux服务器

热门文章

  1. SpringAOP实现多数据源切换
  2. WindowsXP/7/10 Python3.6.3开发环境配置图文教程
  3. SDCC 2016架构运维技术峰会(成都站)启动,首批讲师披露
  4. Rank-consistent Ordinal Regression for Neural Networks
  5. 电商网站商品模型之商品详情页设计方案
  6. 安卓开发笔记-UI设计的概念
  7. 黄仁勋:串行计算过时并行计算是未来
  8. 哪怕四处碰壁也要贯彻正道
  9. 大象做梦传媒写2022年公司大型年会主持稿完整版
  10. 海伦公式和鞋带公式求三角形的面积