JTS基本概念和使用

深碍√TFBOYSˉ_ 2023-09-30 13:44 56阅读 0赞

简介

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

操作

  1. 表示Geometry对象

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

    1. 支持的空间操作包括





































相等(Equals): 几何形状拓扑上相等。
脱节(Disjoint): 几何形状没有共有的点。
相交(Intersects): 几何形状至少有一个共有点(区别于脱节)
接触(Touches): 几何形状有至少一个公共的边界点,但是没有内部点。
交叉(Crosses): 几何形状共享一些但不是所有的内部点。
内含(Within): 几何形状A的线都在几何形状B内部。
包含(Contains): 几何形状B的线都在几何形状A内部(区别于内含)
重叠(Overlaps): 几何形状共享一部分但不是所有的公共点,而且相交处有他们自己相同的区域。
  1. package com.alibaba.autonavi;
  2. import com.vividsolutions.jts.geom.*;
  3. import com.vividsolutions.jts.io.ParseException;
  4. import com.vividsolutions.jts.io.WKTReader;
  5. /**
  6. * gemotry之间的关系
  7. *
  8. */
  9. public class GeometryRelated {
  10. private GeometryFactory geometryFactory = new GeometryFactory();
  11. /**
  12. * 两个几何对象是否是重叠的
  13. * @return
  14. * @throws ParseException
  15. */
  16. public boolean equalsGeo() throws ParseException{
  17. WKTReader reader = new WKTReader( geometryFactory );
  18. LineString geometry1 = (LineString) reader.read("LINESTRING(0 0, 2 0, 5 0)");
  19. LineString geometry2 = (LineString) reader.read("LINESTRING(5 0, 0 0)");
  20. return geometry1.equals(geometry2);//true
  21. }
  22. /**
  23. * 几何对象没有交点(相邻)
  24. * @return
  25. * @throws ParseException
  26. */
  27. public boolean disjointGeo() throws ParseException{
  28. WKTReader reader = new WKTReader( geometryFactory );
  29. LineString geometry1 = (LineString) reader.read("LINESTRING(0 0, 2 0, 5 0)");
  30. LineString geometry2 = (LineString) reader.read("LINESTRING(0 1, 0 2)");
  31. return geometry1.disjoint(geometry2);
  32. }
  33. /**
  34. * 至少一个公共点(相交)
  35. * @return
  36. * @throws ParseException
  37. */
  38. public boolean intersectsGeo() throws ParseException{
  39. WKTReader reader = new WKTReader( geometryFactory );
  40. LineString geometry1 = (LineString) reader.read("LINESTRING(0 0, 2 0, 5 0)");
  41. LineString geometry2 = (LineString) reader.read("LINESTRING(0 0, 0 2)");
  42. Geometry interPoint = geometry1.intersection(geometry2);//相交点
  43. System.out.println(interPoint.toText());//输出 POINT (0 0)
  44. return geometry1.intersects(geometry2);
  45. }
  46. /**
  47. * 判断以x,y为坐标的点point(x,y)是否在geometry表示的Polygon中
  48. * @param x
  49. * @param y
  50. * @param geometry wkt格式
  51. * @return
  52. */
  53. public boolean withinGeo(double x,double y,String geometry) throws ParseException {
  54. Coordinate coord = new Coordinate(x,y);
  55. Point point = geometryFactory.createPoint( coord );
  56. WKTReader reader = new WKTReader( geometryFactory );
  57. Polygon polygon = (Polygon) reader.read(geometry);
  58. return point.within(polygon);
  59. }
  60. /**
  61. * @param args
  62. * @throws ParseException
  63. */
  64. public static void main(String[] args) throws ParseException {
  65. GeometryRelated gr = new GeometryRelated();
  66. System.out.println(gr.equalsGeo());
  67. System.out.println(gr.disjointGeo());
  68. System.out.println(gr.intersectsGeo());
  69. System.out.println(gr.withinGeo(5,5,"POLYGON((0 0, 10 0, 10 10, 0 10,0 0))"));
  70. }
  71. }

发表评论

表情:
评论列表 (有 0 条评论,56人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Git 的基本概念使用方式

    一、Git基本概念 Git是一种分布式版本控制系统,它可以追踪文件的变化,并允许多人同时对同一文件进行更新和合并。以下是Git的基本概念和使用方式: 1. 仓库(Re

    相关 jcs-基本概念使用

    前言 如果一个计算非常耗时,每次获取都重新计算,会消耗很多额外的计算资源。对于这些耗时的计算结果,可以存储在缓存中,下次需要的时候,直接访问缓存中的内容,而不是再重新计算

    相关 jts

    JTS基本概念和使用 简介 1. JTS是加拿大的 Vivid Solutions公司做的一套开放源码的 Java API。它提供了一套空间数据操作的核心算法。