微信搜索:“二十同学” 公众号,欢迎关注一条不一样的成长之路

拓扑关系


      
GeometryTest

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;public class GeometryTest {private GeometryFactory geometryFactory = new GeometryFactory();/*** 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);}}
}

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

import com.vividsolutions.jts.geom.*;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;/*** gemotry之间的关系使用*/
public class GeometryTest {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))"));}}

关系类型

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;public class GeometryTest {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用例分析相关推荐

  1. chtMultiRegionFoam求解器及算例分析

    1. 算例分析 1.1. 算例结构 算例目录heatTransfer/chtMultiRegionFoam/heatedDuct 0 fluid p p_rgh T U heater T metal ...

  2. JTS Geometry

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

  3. stella forum v1.2 用例分析

    最近一直在看<uml与模式应用>,想把里面的ooa/d思想应用到下一版的stella forum中,按照里面统一过程的思想,先要做用例分析,嘿嘿,于是就有了下面这个昨天写的用户的帖子操作的 ...

  4. Makefile 实际用例分析(一) ------- 比较通用的一种架构

    这里不再说Makefile的基本知识,如果需要学习,那么请参考: 下载:makefile 中文手册 或者 点击打开链接 或者 跟我一起写Makefile( 陈皓 ) 这里说的是一般的实际的一个工程应该 ...

  5. 需求用例分析之九:序列图

    作者:张克强    作者微博:张克强-敏捷307 序列图,也称时序图.顺序图,英文名Sequence Diagram.在雅各布森用例分析方法中鼓励使用各类图形来表达,但恰恰没有明确提到序列图.而科伯恩 ...

  6. 苍狼敏捷需求用例分析方法简介并讲义下载

    作者:张克强    作者微博:张克强-敏捷307 用例分析方法已经有不短的历史,发展出了多种用例分析方法.笔者花费了大量时间,对用例分析的各个方面进行实践和分析,得到如下系列文章: 需求用例分析之一: ...

  7. 需求用例分析之七:业务用例之小结

    作者:张克强    作者微博:张克强-敏捷307 RUP虽然对于业务对象建模进行了详细的说明,但其本身并没有把业务对象建模(领域模型).业务用例作为必须的工件.Rational系方法把业务用例作为需求 ...

  8. 需求用例分析之八:用例颗粒度

    作者:张克强    作者微博:张克强-敏捷307 RUP系的考虑 在RUP中,没有对用例的颗粒度给出清晰的指导.2004年Rational 中国区技术销售经理 傅纯一发表一文<用例建模指南> ...

  9. 需求用例分析之四:业务规则

    作者:张克强 作者微博:张克强-敏捷307 在雅各布森用例分析方法和科伯恩用例分析方法中用例本身其实都没有"业务规则"的属性.但是业界使用中常常会给用例加上这个属性,这是为什么呢? ...

最新文章

  1. CentOS7.5下yum安装MySQL8图文教程
  2. 0510JS基础:定义、输出、变量
  3. nginx实现tomcat的负载均衡及企业内部应用的代理
  4. 用property声明属性时,strong,copy,weak的一般用法
  5. JDK环境变量配置(一次性成功)
  6. SequoiaDB 系列之六 :源码分析之coord节点
  7. nsga2代码解读python_代码资料
  8. Win7 局域网内简单共享的设置
  9. 恭主驾到:最新的汽车年审新规,都了解了吗?
  10. SAP中税码、税率、税务科目的几个表及其中的勾稽关系
  11. 黄一老师:什么是个人信用融资?
  12. Web漏洞扫描工具(批量脱壳、反序列化、CMS)
  13. 部落冲突-家乡防御建筑-箭塔(1级至20级)
  14. php 图片印章_PHP制作中文圆形印章示例
  15. MTK6226-DS-PHB-SIMB-Load
  16. Python编程思想(18):哇!原来Python参数还可以这样用
  17. gcc-c++安装—使用系统自带的源yum install gcc-c++和本地源进行相应的安装
  18. Python画糖葫芦和奥运五环
  19. double类型的输出方式
  20. puppy linux 版本,Puppy Linux 8.0 发布,轻量级发行版

热门文章

  1. runTime runLoop
  2. netty入门前置知识-NIO
  3. 判断给定序列是否为BST后序遍历序列
  4. JS 59 筋斗云案例
  5. 步华为后尘?三星S8被爆也混用内存!
  6. 自学软件测试的学习方法
  7. Windows下使用phpstudy安装edusoho
  8. Git 一次性 pull push 所有的分支
  9. java中的继承详解
  10. Effective C++ 之《构造/析构/赋值运算》