简介

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

操作

  1. 表示Geometry对象
    1. Geometry类型介绍见另一篇文章:WKT WKB和GeoJSON
      1. package com.alibaba.autonavi;
      2.  
      3. import com.vividsolutions.jts.geom.Coordinate;
      4. import com.vividsolutions.jts.geom.Geometry;
      5. import com.vividsolutions.jts.geom.GeometryCollection;
      6. import com.vividsolutions.jts.geom.GeometryFactory;
      7. import com.vividsolutions.jts.geom.LineString;
      8. import com.vividsolutions.jts.geom.LinearRing;
      9. import com.vividsolutions.jts.geom.Point;
      10. import com.vividsolutions.jts.geom.Polygon;
      11. import com.vividsolutions.jts.geom.MultiPolygon;
      12. import com.vividsolutions.jts.geom.MultiLineString;
      13. import com.vividsolutions.jts.geom.MultiPoint;
      14. import com.vividsolutions.jts.io.ParseException;
      15. import com.vividsolutions.jts.io.WKTReader;
      16.  
      17. public class GeometryDemo {
      18.  
      19. private GeometryFactory geometryFactory = new GeometryFactory();
      20.  
      21. /**
      22. * create a point
      23. * @return
      24. */
      25. public Point createPoint(){
      26. Coordinate coord = new Coordinate(109.013388, 32.715519);
      27. Point point = geometryFactory.createPoint( coord );
      28. return point;
      29. }
      30.  
      31. /**
      32. * create a point by WKT
      33. * @return
      34. * @throws ParseException
      35. */
      36. public Point createPointByWKT() throws ParseException{
      37. WKTReader reader = new WKTReader( geometryFactory );
      38. Point point = (Point) reader.read("POINT (109.013388 32.715519)");
      39. return point;
      40. }
      41.  
      42. /**
      43. * create multiPoint by wkt
      44. * @return
      45. */
      46. public MultiPoint createMulPointByWKT()throws ParseException{
      47. WKTReader reader = new WKTReader( geometryFactory );
      48. MultiPoint mpoint = (MultiPoint) reader.read("MULTIPOINT(109.013388 32.715519,119.32488 31.435678)");
      49. return mpoint;
      50. }
      51. /**
      52. *
      53. * create a line
      54. * @return
      55. */
      56. public LineString createLine(){
      57. Coordinate[] coords = new Coordinate[] {new Coordinate(2, 2), new Coordinate(2, 2)};
      58. LineString line = geometryFactory.createLineString(coords);
      59. return line;
      60. }
      61.  
      62. /**
      63. * create a line by WKT
      64. * @return
      65. * @throws ParseException
      66. */
      67. public LineString createLineByWKT() throws ParseException{
      68. WKTReader reader = new WKTReader( geometryFactory );
      69. LineString line = (LineString) reader.read("LINESTRING(0 0, 2 0)");
      70. return line;
      71. }
      72.  
      73. /**
      74. * create multiLine
      75. * @return
      76. */
      77. public MultiLineString createMLine(){
      78. Coordinate[] coords1 = new Coordinate[] {new Coordinate(2, 2), new Coordinate(2, 2)};
      79. LineString line1 = geometryFactory.createLineString(coords1);
      80. Coordinate[] coords2 = new Coordinate[] {new Coordinate(2, 2), new Coordinate(2, 2)};
      81. LineString line2 = geometryFactory.createLineString(coords2);
      82. LineString[] lineStrings = new LineString[2];
      83. lineStrings[0]= line1;
      84. lineStrings[1] = line2;
      85. MultiLineString ms = geometryFactory.createMultiLineString(lineStrings);
      86. return ms;
      87. }
      88.  
      89. /**
      90. * create multiLine by WKT
      91. * @return
      92. * @throws ParseException
      93. */
      94. public MultiLineString createMLineByWKT()throws ParseException{
      95. WKTReader reader = new WKTReader( geometryFactory );
      96. MultiLineString line = (MultiLineString) reader.read("MULTILINESTRING((0 0, 2 0),(1 1,2 2))");
      97. return line;
      98. }
      99.  
      100. /**
      101. * create a polygon(多边形) by WKT
      102. * @return
      103. * @throws ParseException
      104. */
      105. public Polygon createPolygonByWKT() throws ParseException{
      106. WKTReader reader = new WKTReader( geometryFactory );
      107. Polygon polygon = (Polygon) reader.read("POLYGON((20 10, 30 0, 40 10, 30 20, 20 10))");
      108. return polygon;
      109. }
      110.  
      111. /**
      112. * create multi polygon by wkt
      113. * @return
      114. * @throws ParseException
      115. */
      116. public MultiPolygon createMulPolygonByWKT() throws ParseException{
      117. WKTReader reader = new WKTReader( geometryFactory );
      118. 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)))");
      119. return mpolygon;
      120. }
      121.  
      122. /**
      123. * create GeometryCollection contain point or multiPoint or line or multiLine or polygon or multiPolygon
      124. * @return
      125. * @throws ParseException
      126. */
      127. public GeometryCollection createGeoCollect() throws ParseException{
      128. LineString line = createLine();
      129. Polygon poly = createPolygonByWKT();
      130. Geometry g1 = geometryFactory.createGeometry(line);
      131. Geometry g2 = geometryFactory.createGeometry(poly);
      132. Geometry[] garray = new Geometry[]{g1,g2};
      133. GeometryCollection gc = geometryFactory.createGeometryCollection(garray);
      134. return gc;
      135. }
      136.  
      137. /**
      138. * create a Circle 创建一个圆,圆心(x,y) 半径RADIUS
      139. * @param x
      140. * @param y
      141. * @param RADIUS
      142. * @return
      143. */
      144. public Polygon createCircle(double x, double y, final double RADIUS){
      145. final int SIDES = 32;//圆上面的点个数
      146. Coordinate coords[] = new Coordinate[SIDES+1];
      147. for( int i = 0; i < SIDES; i++){
      148. double angle = ((double) i / (double) SIDES) * Math.PI * 2.0;
      149. double dx = Math.cos( angle ) * RADIUS;
      150. double dy = Math.sin( angle ) * RADIUS;
      151. coords[i] = new Coordinate( (double) x + dx, (double) y + dy );
      152. }
      153. coords[SIDES] = coords[0];
      154. LinearRing ring = geometryFactory.createLinearRing( coords );
      155. Polygon polygon = geometryFactory.createPolygon( ring, null );
      156. return polygon;
      157. }
      158.  
      159. /**
      160. * @param args
      161. * @throws ParseException
      162. */
      163. public static void main(String[] args) throws ParseException {
      164. GeometryDemo gt = new GeometryDemo();
      165. Polygon p = gt.createCircle(0, 1, 2);
      166. //圆上所有的坐标(32个)
      167. Coordinate coords[] = p.getCoordinates();
      168. for(Coordinate coord:coords){
      169. System.out.println(coord.x+","+coord.y);
      170. }
      171. }
      172. }

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. 使用webpack和react搭建项目

    看了N多博客,日志,一边迷茫一边摸索.本文记录流程.我怕自己忘了...并且修复了博客园首页推荐那个日志中遇到的bug 1.webstorm新建一个空白项目,比如webpack_demo 2.因为要用r ...

  2. centos6 Linux安装redis 2.6.14

    1.获取安装文件 wget http://download.redis.io/redis-stable.tar.gz 2.解压文件 tar xzvf redis-stable.tar.gz 3.进入目 ...

  3. JavaScript -- Window-Focus

    -----034-Window-Focus.html----- <!DOCTYPE html> <html> <head> <meta http-equiv= ...

  4. vue数组检测更新问题

    由于 JavaScript 的限制, Vue 不能检测以下变动的数组: 当你利用索引直接设置一个项时,例如: vm.items[indexOfItem] = newValue 当你修改数组的长度时,例 ...

  5. Redis+Jedis封装工具类

    package com.liying.monkey.core.util; import java.io.IOException; import java.util.ArrayList; import ...

  6. mysql命令行查看建表语句

    命令如下: SHOW CREATE TABLE tbl_name 例子: mysql> SHOW CREATE TABLE t\G . row ************************* ...

  7. SpringMVC之类型转换

    在数据绑定上,SpringMVC提供了到各种基本类型的转换,由前端到后台时,SpringMVC将字符串参数自动转换为各种基本类型.而对于其他,则需要自己编写代码进行转换.本随笔以转换时间类型为例,使用 ...

  8. mysql dbcp Caused By: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy ...

  9. Pro * c Oracle 12c

    #include<stdio.h> #include<string.h> #include<stdlib.h> #include<sqlca.h> vo ...

  10. LVS+keepalived+nginx+tomcat部署实现

    拓扑如下所示 # 节点分布情况 LVS-dr-master eth0: 192.168.146.141 LVS-dr-slave eth0: 192.168.146.142 nginx1: eth0: ...