当前位置:   article > 正文

JTS(Geometry)_jts geometry

jts geometry

转载:JTS(Geometry)_忧郁王子的专栏-CSDN博客_geometry jts

空间数据模型 
(1)、JTS Geometry model  
(2)、ISO Geometry model (Geometry Plugin and JTS Wrapper Plugin) 
GeoTools has two implementations of these interfaces: 
Geometry Plugin a port of JTS 1.7 to the ISO Geometry interfaces 
JTS Wrapper Plugin an implementation that delegates all the work to JTS

JTS包结构

系(linearref包)、计算交点(noding包)、几何图形操作(operation包)、平面图(planargraph包)、多边形化(polygnize包)、精度(precision)、工具(util包)

重点理解JTS Geometry model 
(1) JTS提供了如下的空间数据类型 
     Point      
     MultiPoint 
     LineString      
     LinearRing  封闭的线条 
     MultiLineString    多条线 
     Polygon 
     MultiPolygon          
     GeometryCollection  包括点,线,面

(2) 支持接口
Coordinate
   Coordinate(坐标)是用来存储坐标的轻便的类。它不同于点,点是Geometry的子类。不像模范Point的对象(包含额外的信息,例如一个信包,一个精确度模型和空间参考系统信息),Coordinate只包含纵座标值和存取方法。
Envelope(矩形)
   一个具体的类,包含一个最大和最小的x 值和y 值。
GeometryFactory
   GeometryFactory提供一系列的有效方法用来构造来自Coordinate类的Geometry对象。支持接口

  1. package com.mapbar.geo.jts;
  2. import org.geotools.geometry.jts.JTSFactoryFinder;
  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. * Class GeometryDemo.java
  18. * Description Geometry 几何实体的创建,读取操作
  19. * Company mapbar
  20. * author Chenll E-mail: Chenll@mapbar.com
  21. * Version 1.0
  22. * Date 2012-2-17 上午11:08:50
  23. */
  24. public class GeometryDemo {
  25. private GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null );
  26. /**
  27. * create a point
  28. * @return
  29. */
  30. public Point createPoint(){
  31. Coordinate coord = new Coordinate(109.013388, 32.715519);
  32. Point point = geometryFactory.createPoint( coord );
  33. return point;
  34. }
  35. /**
  36. * create a point by WKT
  37. * @return
  38. * @throws ParseException
  39. */
  40. public Point createPointByWKT() throws ParseException{
  41. WKTReader reader = new WKTReader( geometryFactory );
  42. Point point = (Point) reader.read("POINT (109.013388 32.715519)");
  43. return point;
  44. }
  45. /**
  46. * create multiPoint by wkt
  47. * @return
  48. */
  49. public MultiPoint createMulPointByWKT()throws ParseException{
  50. WKTReader reader = new WKTReader( geometryFactory );
  51. MultiPoint mpoint = (MultiPoint) reader.read("MULTIPOINT(109.013388 32.715519,119.32488 31.435678)");
  52. return mpoint;
  53. }
  54. /**
  55. *
  56. * create a line
  57. * @return
  58. */
  59. public LineString createLine(){
  60. Coordinate[] coords = new Coordinate[] {new Coordinate(2, 2), new Coordinate(2, 2)};
  61. LineString line = geometryFactory.createLineString(coords);
  62. return line;
  63. }
  64. /**
  65. * create a line by WKT
  66. * @return
  67. * @throws ParseException
  68. */
  69. public LineString createLineByWKT() throws ParseException{
  70. WKTReader reader = new WKTReader( geometryFactory );
  71. LineString line = (LineString) reader.read("LINESTRING(0 0, 2 0)");
  72. return line;
  73. }
  74. /**
  75. * create multiLine
  76. * @return
  77. */
  78. public MultiLineString createMLine(){
  79. Coordinate[] coords1 = new Coordinate[] {new Coordinate(2, 2), new Coordinate(2, 2)};
  80. LineString line1 = geometryFactory.createLineString(coords1);
  81. Coordinate[] coords2 = new Coordinate[] {new Coordinate(2, 2), new Coordinate(2, 2)};
  82. LineString line2 = geometryFactory.createLineString(coords2);
  83. LineString[] lineStrings = new LineString[2];
  84. lineStrings[0]= line1;
  85. lineStrings[1] = line2;
  86. MultiLineString ms = geometryFactory.createMultiLineString(lineStrings);
  87. return ms;
  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. * create a polygon(多边形) by WKT
  101. * @return
  102. * @throws ParseException
  103. */
  104. public Polygon createPolygonByWKT() throws ParseException{
  105. WKTReader reader = new WKTReader( geometryFactory );
  106. Polygon polygon = (Polygon) reader.read("POLYGON((20 10, 30 0, 40 10, 30 20, 20 10))");
  107. return polygon;
  108. }
  109. /**
  110. * create multi polygon by wkt
  111. * @return
  112. * @throws ParseException
  113. */
  114. public MultiPolygon createMulPolygonByWKT() throws ParseException{
  115. WKTReader reader = new WKTReader( geometryFactory );
  116. 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)))");
  117. return mpolygon;
  118. }
  119. /**
  120. * create GeometryCollection contain point or multiPoint or line or multiLine or polygon or multiPolygon
  121. * @return
  122. * @throws ParseException
  123. */
  124. public GeometryCollection createGeoCollect() throws ParseException{
  125. LineString line = createLine();
  126. Polygon poly = createPolygonByWKT();
  127. Geometry g1 = geometryFactory.createGeometry(line);
  128. Geometry g2 = geometryFactory.createGeometry(poly);
  129. Geometry[] garray = new Geometry[]{g1,g2};
  130. GeometryCollection gc = geometryFactory.createGeometryCollection(garray);
  131. return gc;
  132. }
  133. /**
  134. * create a Circle 创建一个圆,圆心(x,y) 半径RADIUS
  135. * @param x
  136. * @param y
  137. * @param RADIUS
  138. * @return
  139. */
  140. public Polygon createCircle(double x, double y, final double RADIUS){
  141. final int SIDES = 32;//圆上面的点个数
  142. Coordinate coords[] = new Coordinate[SIDES+1];
  143. for( int i = 0; i < SIDES; i++){
  144. double angle = ((double) i / (double) SIDES) * Math.PI * 2.0;
  145. double dx = Math.cos( angle ) * RADIUS;
  146. double dy = Math.sin( angle ) * RADIUS;
  147. coords[i] = new Coordinate( (double) x + dx, (double) y + dy );
  148. }
  149. coords[SIDES] = coords[0];
  150. LinearRing ring = geometryFactory.createLinearRing( coords );
  151. Polygon polygon = geometryFactory.createPolygon( ring, null );
  152. return polygon;
  153. }
  154. /**
  155. * @param args
  156. * @throws ParseException
  157. */
  158. public static void main(String[] args) throws ParseException {
  159. GeometryDemo gt = new GeometryDemo();
  160. Polygon p = gt.createCircle(0, 1, 2);
  161. //圆上所有的坐标(32个)
  162. Coordinate coords[] = p.getCoordinates();
  163. for(Coordinate coord:coords){
  164. System.out.println(coord.x+","+coord.y);
  165. }
  166. }
  167. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/941026
推荐阅读
相关标签
  

闽ICP备14008679号