JTS基本概念和使用

x33g5p2x  于2022-05-20 转载在 其他  
字(6.9k)|赞(0)|评价(0)|浏览(586)

简介

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

操作

  1. 表示Geometry对象

  2. Geometry类型介绍见另一篇文章:WKT WKB和GeoJSON

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

  2. 支持的空间操作包括

相等(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. }

相关文章