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對象。支持接口

[java] view plaincopy
  1. package com.mapbar.geo.jts;  
  2.   
  3. import org.geotools.geometry.jts.JTSFactoryFinder;  
  4.   
  5. import com.vividsolutions.jts.geom.Coordinate;  
  6. import com.vividsolutions.jts.geom.Geometry;  
  7. import com.vividsolutions.jts.geom.GeometryCollection;  
  8. import com.vividsolutions.jts.geom.GeometryFactory;  
  9. import com.vividsolutions.jts.geom.LineString;  
  10. import com.vividsolutions.jts.geom.LinearRing;  
  11. import com.vividsolutions.jts.geom.Point;  
  12. import com.vividsolutions.jts.geom.Polygon;  
  13. import com.vividsolutions.jts.geom.MultiPolygon;  
  14. import com.vividsolutions.jts.geom.MultiLineString;  
  15. import com.vividsolutions.jts.geom.MultiPoint;  
  16. import com.vividsolutions.jts.io.ParseException;  
  17. import com.vividsolutions.jts.io.WKTReader;  
  18.   
  19. /**   
  20.  
  21.  * Class GeometryDemo.java  
  22.  
  23.  * Description Geometry 幾何實體的創建,讀取操作 
  24.  
  25.  * Company mapbar  
  26.  
  27.  * author Chenll E-mail: [email protected] 
  28.  
  29.  * Version 1.0  
  30.  
  31.  * Date 2012-2-17 上午11:08:50 
  32.  
  33.  */  
  34. public class GeometryDemo {  
  35.   
  36.     private GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null );  
  37.       
  38.     /** 
  39.      * create a point 
  40.      * @return 
  41.      */  
  42.     public Point createPoint(){  
  43.         Coordinate coord = new Coordinate(109.01338832.715519);  
  44.         Point point = geometryFactory.createPoint( coord );  
  45.         return point;  
  46.     }  
  47.       
  48.     /** 
  49.      * create a point by WKT 
  50.      * @return 
  51.      * @throws ParseException  
  52.      */  
  53.     public Point createPointByWKT() throws ParseException{  
  54.         WKTReader reader = new WKTReader( geometryFactory );  
  55.         Point point = (Point) reader.read("POINT (109.013388 32.715519)");  
  56.         return point;  
  57.     }  
  58.       
  59.     /** 
  60.      * create multiPoint by wkt 
  61.      * @return 
  62.      */  
  63.     public MultiPoint createMulPointByWKT()throws ParseException{  
  64.         WKTReader reader = new WKTReader( geometryFactory );  
  65.         MultiPoint mpoint = (MultiPoint) reader.read("MULTIPOINT(109.013388 32.715519,119.32488 31.435678)");  
  66.         return mpoint;  
  67.     }  
  68.     /** 
  69.      *  
  70.      * create a line 
  71.      * @return 
  72.      */  
  73.     public LineString createLine(){  
  74.         Coordinate[] coords  = new Coordinate[] {new Coordinate(22), new Coordinate(22)};  
  75.         LineString line = geometryFactory.createLineString(coords);  
  76.         return line;  
  77.     }  
  78.       
  79.     /** 
  80.      * create a line by WKT 
  81.      * @return 
  82.      * @throws ParseException 
  83.      */  
  84.     public LineString createLineByWKT() throws ParseException{  
  85.         WKTReader reader = new WKTReader( geometryFactory );  
  86.         LineString line = (LineString) reader.read("LINESTRING(0 0, 2 0)");  
  87.         return line;  
  88.     }  
  89.       
  90.     /** 
  91.      * create multiLine  
  92.      * @return 
  93.      */  
  94.     public MultiLineString createMLine(){  
  95.         Coordinate[] coords1  = new Coordinate[] {new Coordinate(22), new Coordinate(22)};  
  96.         LineString line1 = geometryFactory.createLineString(coords1);  
  97.         Coordinate[] coords2  = new Coordinate[] {new Coordinate(22), new Coordinate(22)};  
  98.         LineString line2 = geometryFactory.createLineString(coords2);  
  99.         LineString[] lineStrings = new LineString[2];  
  100.         lineStrings[0]= line1;  
  101.         lineStrings[1] = line2;  
  102.         MultiLineString ms = geometryFactory.createMultiLineString(lineStrings);  
  103.         return ms;  
  104.     }  
  105.       
  106.     /** 
  107.      * create multiLine by WKT 
  108.      * @return 
  109.      * @throws ParseException 
  110.      */  
  111.     public MultiLineString createMLineByWKT()throws ParseException{  
  112.         WKTReader reader = new WKTReader( geometryFactory );  
  113.         MultiLineString line = (MultiLineString) reader.read("MULTILINESTRING((0 0, 2 0),(1 1,2 2))");  
  114.         return line;  
  115.     }  
  116.       
  117.     /** 
  118.      * create a polygon(多邊形) by WKT 
  119.      * @return 
  120.      * @throws ParseException 
  121.      */  
  122.     public Polygon createPolygonByWKT() throws ParseException{  
  123.         WKTReader reader = new WKTReader( geometryFactory );  
  124.         Polygon polygon = (Polygon) reader.read("POLYGON((20 10, 30 0, 40 10, 30 20, 20 10))");  
  125.         return polygon;  
  126.     }  
  127.       
  128.     /** 
  129.      * create multi polygon by wkt 
  130.      * @return 
  131.      * @throws ParseException 
  132.      */  
  133.     public MultiPolygon createMulPolygonByWKT() throws ParseException{  
  134.         WKTReader reader = new WKTReader( geometryFactory );  
  135.         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)))");  
  136.         return mpolygon;  
  137.     }  
  138.       
  139.     /** 
  140.      * create GeometryCollection  contain point or multiPoint or line or multiLine or polygon or multiPolygon 
  141.      * @return 
  142.      * @throws ParseException 
  143.      */  
  144.     public GeometryCollection createGeoCollect() throws ParseException{  
  145.         LineString line = createLine();  
  146.         Polygon poly =  createPolygonByWKT();  
  147.         Geometry g1 = geometryFactory.createGeometry(line);  
  148.         Geometry g2 = geometryFactory.createGeometry(poly);  
  149.         Geometry[] garray = new Geometry[]{g1,g2};  
  150.         GeometryCollection gc = geometryFactory.createGeometryCollection(garray);  
  151.         return gc;  
  152.     }  
  153.       
  154.     /** 
  155.      * create a Circle  創建一個圓,圓心(x,y) 半徑RADIUS 
  156.      * @param x 
  157.      * @param y 
  158.      * @param RADIUS 
  159.      * @return 
  160.      */  
  161.     public Polygon createCircle(double x, double y, final double RADIUS){  
  162.         final int SIDES = 32;//圓上面的點個數  
  163.         Coordinate coords[] = new Coordinate[SIDES+1];  
  164.         forint i = 0; i < SIDES; i++){  
  165.             double angle = ((double) i / (double) SIDES) * Math.PI * 2.0;  
  166.             double dx = Math.cos( angle ) * RADIUS;  
  167.             double dy = Math.sin( angle ) * RADIUS;  
  168.             coords[i] = new Coordinate( (double) x + dx, (double) y + dy );  
  169.         }  
  170.         coords[SIDES] = coords[0];  
  171.         LinearRing ring = geometryFactory.createLinearRing( coords );  
  172.         Polygon polygon = geometryFactory.createPolygon( ring, null );  
  173.         return polygon;  
  174.     }  
  175.       
  176.     /** 
  177.      * @param args 
  178.      * @throws ParseException  
  179.      */  
  180.     public static void main(String[] args) throws ParseException {  
  181.         GeometryDemo gt = new GeometryDemo();  
  182.         Polygon p = gt.createCircle(012);  
  183.         //圓上所有的座標(32個)  
  184.         Coordinate coords[] = p.getCoordinates();  
  185.         for(Coordinate coord:coords){  
  186.             System.out.println(coord.x+","+coord.y);  
  187.         }  
  188.     }  
  189. }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章