GeoTools应用-JTS(Geometry)

空间数据模型
(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.  
  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. * Class GeometryDemo.java
  21. * Description Geometry 几何实体的创建,读取操作
  22. * Company mapbar
  23. * author Chenll E-mail: Chenll@mapbar.com
  24. * Version 1.0
  25. * Date 2012-2-17 上午11:08:50
  26. */ 
  27. public class GeometryDemo { 
  28.  
  29.     private GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null ); 
  30.      
  31.     /**
  32.      * create a point
  33.      * @return
  34.      */ 
  35.     public Point createPoint(){ 
  36.         Coordinate coord = new Coordinate(109.013388, 32.715519); 
  37.         Point point = geometryFactory.createPoint( coord ); 
  38.         return point; 
  39.     } 
  40.      
  41.     /**
  42.      * create a point by WKT
  43.      * @return
  44.      * @throws ParseException
  45.      */ 
  46.     public Point createPointByWKT() throws ParseException{ 
  47.         WKTReader reader = new WKTReader( geometryFactory ); 
  48.         Point point = (Point) reader.read("POINT (109.013388 32.715519)"); 
  49.         return point; 
  50.     } 
  51.      
  52.     /**
  53.      * create multiPoint by wkt
  54.      * @return
  55.      */ 
  56.     public MultiPoint createMulPointByWKT()throws ParseException{ 
  57.         WKTReader reader = new WKTReader( geometryFactory ); 
  58.         MultiPoint mpoint = (MultiPoint) reader.read("MULTIPOINT(109.013388 32.715519,119.32488 31.435678)"); 
  59.         return mpoint; 
  60.     } 
  61.     /**
  62.      *
  63.      * create a line
  64.      * @return
  65.      */ 
  66.     public LineString createLine(){ 
  67.         Coordinate[] coords  = new Coordinate[] {new Coordinate(2, 2), new Coordinate(2, 2)}; 
  68.         LineString line = geometryFactory.createLineString(coords); 
  69.         return line; 
  70.     } 
  71.      
  72.     /**
  73.      * create a line by WKT
  74.      * @return
  75.      * @throws ParseException
  76.      */ 
  77.     public LineString createLineByWKT() throws ParseException{ 
  78.         WKTReader reader = new WKTReader( geometryFactory ); 
  79.         LineString line = (LineString) reader.read("LINESTRING(0 0, 2 0)"); 
  80.         return line; 
  81.     } 
  82.      
  83.     /**
  84.      * create multiLine
  85.      * @return
  86.      */ 
  87.     public MultiLineString createMLine(){ 
  88.         Coordinate[] coords1  = new Coordinate[] {new Coordinate(2, 2), new Coordinate(2, 2)}; 
  89.         LineString line1 = geometryFactory.createLineString(coords1); 
  90.         Coordinate[] coords2  = new Coordinate[] {new Coordinate(2, 2), new Coordinate(2, 2)}; 
  91.         LineString line2 = geometryFactory.createLineString(coords2); 
  92.         LineString[] lineStrings = new LineString[2]; 
  93.         lineStrings[0]= line1; 
  94.         lineStrings[1] = line2; 
  95.         MultiLineString ms = geometryFactory.createMultiLineString(lineStrings); 
  96.         return ms; 
  97.     } 
  98.      
  99.     /**
  100.      * create multiLine by WKT
  101.      * @return
  102.      * @throws ParseException
  103.      */ 
  104.     public MultiLineString createMLineByWKT()throws ParseException{ 
  105.         WKTReader reader = new WKTReader( geometryFactory ); 
  106.         MultiLineString line = (MultiLineString) reader.read("MULTILINESTRING((0 0, 2 0),(1 1,2 2))"); 
  107.         return line; 
  108.     } 
  109.      
  110.     /**
  111.      * create a polygon(多边形) by WKT
  112.      * @return
  113.      * @throws ParseException
  114.      */ 
  115.     public Polygon createPolygonByWKT() throws ParseException{ 
  116.         WKTReader reader = new WKTReader( geometryFactory ); 
  117.         Polygon polygon = (Polygon) reader.read("POLYGON((20 10, 30 0, 40 10, 30 20, 20 10))"); 
  118.         return polygon; 
  119.     } 
  120.      
  121.     /**
  122.      * create multi polygon by wkt
  123.      * @return
  124.      * @throws ParseException
  125.      */ 
  126.     public MultiPolygon createMulPolygonByWKT() throws ParseException{ 
  127.         WKTReader reader = new WKTReader( geometryFactory ); 
  128.         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)))"); 
  129.         return mpolygon; 
  130.     } 
  131.      
  132.     /**
  133.      * create GeometryCollection  contain point or multiPoint or line or multiLine or polygon or multiPolygon
  134.      * @return
  135.      * @throws ParseException
  136.      */ 
  137.     public GeometryCollection createGeoCollect() throws ParseException{ 
  138.         LineString line = createLine(); 
  139.         Polygon poly =  createPolygonByWKT(); 
  140.         Geometry g1 = geometryFactory.createGeometry(line); 
  141.         Geometry g2 = geometryFactory.createGeometry(poly); 
  142.         Geometry[] garray = new Geometry[]{g1,g2}; 
  143.         GeometryCollection gc = geometryFactory.createGeometryCollection(garray); 
  144.         return gc; 
  145.     } 
  146.      
  147.     /**
  148.      * create a Circle  创建一个圆,圆心(x,y) 半径RADIUS
  149.      * @param x
  150.      * @param y
  151.      * @param RADIUS
  152.      * @return
  153.      */ 
  154.     public Polygon createCircle(double x, double y, final double RADIUS){ 
  155.         final int SIDES = 32;//圆上面的点个数 
  156.         Coordinate coords[] = new Coordinate[SIDES+1]; 
  157.         for( int i = 0; i < SIDES; i++){ 
  158.             double angle = ((double) i / (double) SIDES) * Math.PI * 2.0
  159.             double dx = Math.cos( angle ) * RADIUS; 
  160.             double dy = Math.sin( angle ) * RADIUS; 
  161.             coords[i] = new Coordinate( (double) x + dx, (double) y + dy ); 
  162.         } 
  163.         coords[SIDES] = coords[0]; 
  164.         LinearRing ring = geometryFactory.createLinearRing( coords ); 
  165.         Polygon polygon = geometryFactory.createPolygon( ring, null ); 
  166.         return polygon; 
  167.     } 
  168.      
  169.     /**
  170.      * @param args
  171.      * @throws ParseException
  172.      */ 
  173.     public static void main(String[] args) throws ParseException { 
  174.         GeometryDemo gt = new GeometryDemo(); 
  175.         Polygon p = gt.createCircle(0, 1, 2); 
  176.         //圆上所有的坐标(32个) 
  177.         Coordinate coords[] = p.getCoordinates(); 
  178.         for(Coordinate coord:coords){ 
  179.             System.out.println(coord.x+","+coord.y); 
  180.         } 
  181.     } 
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值