简介

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

操作

  1. 表示Geometry对象
    1. Geometry类型介绍见另一篇文章:WKT WKB和GeoJSON
    2. package com.alibaba.autonavi;
      
      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 GeometryDemo { 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);
      }
      }
      }

JTS基本概念和使用的更多相关文章

  1. JTS相关资料和示例

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

  2. java各种概念 Core Java总结

    Base: OOA是什么?OOD是什么?OOP是什么?{ oo(object-oriented):基于对象概念,以对象为中心,以类和继承为构造机制,来认识,理解,刻画客观世界和设计,构建相应的软件系统 ...

  3. JTS

    在这个系列的 第 1 部分,我们讨论了事务并研究了它们的基本属性 ― 原子性(atomicity).一致性(consistency).孤立性(isolation)和持久性(durability).事务 ...

  4. JTS(Geometry)(转)

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

  5. J2EE之13个规范标准概念

    主要是关于j2EE十三个规范的总结. java基础知识 首先java分为三类:J2ME.J2SE.J2EE. 依据开发软件的大小和量级他们的作用分别不同,J2ME是开发为机顶盒.移动电话和PDA之类嵌 ...

  6. 如何一步一步用DDD设计一个电商网站(一)—— 先理解核心概念

    一.前言     DDD(领域驱动设计)的一些介绍网上资料很多,这里就不继续描述了.自己使用领域驱动设计摸滚打爬也有2年多的时间,出于对知识的总结和分享,也是对自我理解的一个公开检验,介于博客园这个平 ...

  7. 【Machine Learning】机器学习及其基础概念简介

    机器学习及其基础概念简介 作者:白宁超 2016年12月23日21:24:51 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本系列文章是作者结 ...

  8. 【Oracle 集群】ORACLE DATABASE 11G RAC 知识图文详细教程之集群概念介绍(一)

    集群概念介绍(一)) 白宁超 2015年7月16日 概述:写下本文档的初衷和动力,来源于上篇的<oracle基本操作手册>.oracle基本操作手册是作者研一假期对oracle基础知识学习 ...

  9. 声音分贝的概念,dBSPL.dBm,dBu,dBV,dBFS

    需要做个音频的PPM表,看着一堆的音频术语真是懵了,苦苦在网上扒了几天的文档,终于有了点收获,下面关于声音的分贝做个总结. 分贝 Decibel 分贝(dB)是一个对数单位(logarithmic u ...

随机推荐

  1. 调用的执行器“executor://mstestadapter/v2”时发生异常: 无法找到程序集“log4net, Version=1.2.15.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a”

    加上下面的一句就好了 [TestCleanup]      public void cleanup()      {          CallContext.FreeNamedDataSlot(&q ...

  2. ajex 相关参数

    1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如 ...

  3. (转)使用 db2pd 命令进行监视和故障诊断

    原文:https://www.ibm.com/support/knowledgecenter/zh/SSEPGG_9.7.0/com.ibm.db2.luw.admin.trb.doc/doc/c00 ...

  4. Git使用(3)

    1.查看本地和远程分支 git branch -a 删除本地分支 git branch -D branchName(D要大写) 删除远程分支 git push origin :branchName 2 ...

  5. StreamSets学习系列之StreamSets支持多种安装方式【Core Tarball、Cloudera Parcel 、Full Tarball 、Full RPM 、Docker Image和Source Code 】(图文详解)

    不多说,直接上干货! Streamsets的官网 https://streamsets.com/ 得到 https://streamsets.com/opensource/ StreamSets支持多 ...

  6. 获取Javascript 滚动条距离顶部的距离(兼容IE6+,火狐,谷歌,其它没测)

    document.body.scrollTop || document.documentElement.scrollTop

  7. java命令行指定log4j2

    java -Dlog4j.configurationFile=directory/file.xml

  8. AI---训练集(train set) 验证集(validation set) 测试集(test set)

    在有监督(supervise)的机器学习中,数据集常被分成2~3个即: 训练集(train set) 验证集(validation set) 测试集(test set) 一般需要将样本分成独立的三部分 ...

  9. HDU 1242 Rescue(BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...

  10. 25. k个一组翻转链表

    题目描述 给出一个链表,每 k 个节点一组进行翻转,并返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度.如果节点总数不是 k 的整数倍,那么将最后剩余节点保持原有顺序. 示例 : 给定 ...