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.0
System.out.println(op.intersectionGeo(line1, line2));//out GEOMETRYCOLLECTION EMPTY
System.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)(转)

    原文链接:http://blog.csdn.net/cdl2008sky/article/details/7268577 空间数据模型(1).JTS Geometry model (2).ISO Ge ...

  2. JTS Geometry关系判断和分析

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

  3. JTS相关资料和示例

    示例 JTS基本概念和使用 JTS Geometry之间的关系 JTS algorithm package

  4. UDF2

    问题 根据给定的gps点point(x,y)和北京的shape数据,关联出 AOI ID IO 输入 gps点表 create table gps ( x double, //经度 y double ...

  5. Geometry关系高级操作

    一些高级的操作 几何形状Geometry缓冲(buffer) 线段的融合(linemerge)是将Geometry A中相互连接的线段进行连接 多边形化操作(polygonize)对Geometry ...

  6. Java JTS & 空间数据模型

    空间数据模型 判断两个几何图形是否存在指定的空间关系.包括: 相等(equals).分离(disjoint).相交(intersect).相接(touches).交叉(crosses).包含于(wit ...

  7. JTS基本概念和使用

    简介 JTS是加拿大的 Vivid Solutions公司做的一套开放源码的 Java API.它提供了一套空间数据操作的核心算法.为在兼容OGC标准的空间对象模型中进行基础的几何操作提供2D空间谓词 ...

  8. JTS空间分析工具包(GIS开源)学习 JAVA

    JST空间分析工具包是一套JAVA API,提供一系列的空间数据分析操作.最近开发项目刚好需要用到,上网搜资料也少,就自己写下来记录一下.C++版本的拓扑分析开源工具叫:geos:.NET版本的拓扑分 ...

  9. 扩展mybatis和通用mapper,支持mysql的geometry类型字段

    因项目中需要用到地理位置信息的存储.查询.计算等,经过研究决定使用mysql(5.7版本)数据库的geometry类型字段来保存地理位置坐标,使用虚拟列(Virtual Generated Colum ...

随机推荐

  1. 超级电容(Supercapacitor) 和电池的比较

    之前看到同事在电路设计里使用了超级电容来进行供电,好奇为什么没有用到普通的电池,于是就是找了找两个的区别.有篇文章讲得挺好,所以就直接翻译一下. 超级电容有点像普通电池和一般电容的结合体,能比一般的电 ...

  2. R绘图(1): 在散点图边缘加上直方图/密度图/箱型图

    当我们在绘制散点图的时候,可能会遇到点特别多的情况,这时点与点之间过度重合,影响我们对图的认知.为了更好地反映特征,我们可以加上点的密度信息,比如在原来散点所在的位置将密度用热图的形式呈现出来,再比如 ...

  3. 当会打王者荣耀的AI学会踢足球,一不小心拿下世界冠军!

    难得的元旦小假期,没有什么比得上在慵懒的冬日艳阳下放松自己,拿起手机,叫上了许久未一起作战的小伙伴,到王者荣耀中激战了一番,仿佛又回到了当年那个年轻的自己. 厉害不,毕竟当年DD也是王者五十星的水平, ...

  4. 第七章节 BJROBOT 选择区域自主构建地图【ROS全开源阿克曼转向智能网联无人驾驶车】

    1.把小车平放在地板上,用资料里的虚拟机,打开一个终端 ssh 过去主控端启动roslaunch znjrobot bringup.launch 2.在虚拟机端再打开一个终端,ssh 过去主控端启动r ...

  5. SQL Server批量向表中插入多行数据语句

    因自己学习测试需要,需要两个有大量不重复行的表,表中行数越多越好.手动编写SQL语句,通过循环,批量向表中插入数据,考虑到避免一致问题,设置奇偶行不同.个人水平有限,如有错误,还望指正. 语句如下: ...

  6. ES6 对象拓展方法

    一,ES6 对象拓展方法 ES6为对象提供了一些拓展方法,下面列举几个比较常见的对象拓展方法.

  7. 通过写n本书的积累,我似乎找到了写好技术文章的方法(回复送我写的python股票电子书)

    我写的书不算少,写的博文就更多了,但大多数书的销量也就一般,而我写的技术文章里,虽然也有点击过万的,但不少点击量也就只有三位数. 通过不断反思,也通过对比了一些畅销书和顶流文章,我似乎找到了一些原因, ...

  8. linux常用命令--转载

    转载自: https://www.cnblogs.com/Qsunshine/p/10402179.html 常用指令 ls 显示文件或目录 -l列出文件详细信息l(list) -a列出当前目录下所有 ...

  9. 剑指offer之重建二叉树

    1.问题描述:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.        例如输入前序遍历序列pre {1,2,4,7,3,5,6, ...

  10. 数据库MySQL(带你零基础入门MySQL)

    (一)认识数据库 redis默认端口:6379 mysql默认端口:3306 什么是数据库? 数据库的英文单词:data base,简称DB. 数据库实际上就是一个文件集合,是一个存储数据的仓库,本质 ...