Esri geometry api java 学习文档 (2) 几何图形 (Geometry)与数据结构

Geometry是Esri geometry api中所有类的父类,它定义了所有几何数据的存储结构 与 基本方法。

无论派生出的任何点、线、面等要素都要遵循Geometry定下的基本规则,那我们就来看一下Geometry到底使用了什么思路?搭建了什么样的结构?

 

 上图是Esri Geometry的子类们,与下图OGC 对Geometry 的定义相对比,Esri 使用顶点Vertex(并非是Point)来构建多点的要素,先将“多点要素”、“域”、“线” 和 “单点要素”分开,再构成其他要素。

 

 

 

Geometry一般包含的数据:

  在Esri Geometry中每个几何实体都包含着:

 

  VertexDescription m_description;//属性描述
  volatile int m_touchFlag;//改动次数

 

  其中VertexDescription用于存储各顶点的数据与属性

      (点元素直接用它(m_description = VertexDescriptionDesignerImpl.getDefaultDescriptor2D();),

      其他类都自定义m_impl存储数据中也都会有类似语句 )

  而    m_touchFlag    则负责监视几何实体的改动操作

 

Geometry一般规定的操作:

  在Esri Geometry中每个几何实体都可以:

 

  对属性的操作(用的不多可跳过):

  addAttribute(int semantics)  加入属性

    派生函数:addID() addM()

  hasAttribute(int semantics)  查询属性

    派生函数:hasID() hasM() hasZ()


  dropAllAttributes()  删除所有附加属性(除了xy)

  dropAttribute(int semantics)  删除某个属性

 

  属性操作代码:

 1         geomPoint.addAttribute(0);//无效,因为x是固有属性
 2         geomPoint.addAttribute(1);//无效,因为y是固有属性
 3 
 4         geomPoint.addAttribute(2);//添加M属性
 5         geomPoint.addM();//等同于geomPoint.addAttribute(2);
 6         geomPoint.addAttribute(3);//添加ID属性
 7         geomPoint.addID();//等同于geomPoint.addAttribute(3);
 8 
 9         System.out.println(geomPoint.hasAttribute(3));//检查是否有ID属性true
10         
11         geomPoint.dropAttribute(3);//删除ID属性
12         System.out.println(geomPoint.hasAttribute(3));//检查是否有ID属性false
13         
14         geomPoint.dropAllAttributes();//删除所有属性(XY固有属性除外)
15         System.out.println(geomPoint.hasAttribute(2));//检查是否有M属性false
16         System.out.println(geomPoint.hasAttribute(0));//检查是否有X属性true 固有属性不可删除

 

  几何实体的几何判断*:

  

  calculateLength2D()  计算长度  点:0  线:总线长  面:总线长

  calculateArea2D()   计算面积  点:0  线:0     面:总面积

  getBoundary()       得到边界  点:无  线:端点  面:所有边缘线(不分内外)

  queryEnvelope(Envelope env)  得到外接矩形域

  vertex_count(Geometry geom)  计算顶点个数

  getDimension()    读取维度

  getType()      读取类型

 

  几何实体的几何判断代码: 

         System.out.println(geomPoint.calculateLength2D());
         System.out.println(geomLine.calculateLength2D());
         System.out.println(geomPolygon.calculateLength2D());
         //计算长度

         System.out.println(geomPoint.calculateArea2D());         
System.out.println(geomLine.calculateArea2D()); System.out.println(geomPolygon.calculateArea2D());
//计算面积 Geometry geomTemp = geomPoint.getBoundary(); geomTemp = geomLine.getBoundary(); geomTemp = geomPolygon.getBoundary(); //得到边界 Envelope env1 = new Envelope(); geomPoint.queryEnvelope(env1); System.out.println(env1.toString()); geomLine.queryEnvelope(env1); System.out.println(env1.toString()); geomPolygon.queryEnvelope(env1); System.out.println(env1.toString()); //得到外接矩形 System.out.println(Geometry.vertex_count(geomPoint)); System.out.println(Geometry.vertex_count(geomLine)); System.out.println(Geometry.vertex_count(geomPolygon)); //计算顶点个数 System.out.println(geomPoint.getDimension()); System.out.println(geomPoint.getType()); System.out.println(geomLine.getDimension()); System.out.println(geomLine.getType()); System.out.println(geomPolygon.getDimension()); System.out.println(geomPolygon.getType()); //读取维度/类型

 

   坐标转换(二维仿射变换):

     Transformation2D transformation = new Transformation2D();
     geomPoint.applyTransformation( transformation );

  

  其他操作:
  
  copyTo(Geometry dst)  复制到另一个

  createInstance()  新建一个同类型的空集

  copy()  复制一个新的( = copyTo + createInstance)


  estimateMemorySize()  估计内存

  setEmpty()  置空
  isEmpty()    判断为空


  toString()  输出string便于debug

 

  其他操作代码:

        wktString = "MULTIPOINT ((-106.4453583 41.11775),(-106.4453583 42.11775))";
        Geometry geomMulitPoint2 = OperatorImportFromWkt.local().execute(WktImportFlags.wktImportDefaults, Geometry.Type.MultiPoint, wktString, null);

        geomMulitPoint.copyTo(geomMulitPoint2);
        System.out.println(geomMulitPoint.toString());
        System.out.println(geomMulitPoint2.toString());
        
        System.out.println(geomMulitPoint.isEmpty());
        geomMulitPoint.setEmpty();
        System.out.println(geomMulitPoint.isEmpty());
        System.out.println(geomPoint.estimateMemorySize());

 

  

 

 

 

 

 

全部代码大家可以copy下来尝试:

  1 package learn;
  2 
  3 import java.io.IOException;
  4 
  5 import org.codehaus.jackson.JsonParseException;
  6 
  7 import com.esri.core.geometry.Geometry;
  8 import com.esri.core.geometry.OperatorImportFromWkt;
  9 import com.esri.core.geometry.Envelope;
 10 import com.esri.core.geometry.WktImportFlags;
 11 
 12 public class GeometryLearn {
 13 
 14     public static void main(String args[]) throws IOException
 15     {
 16         createFromWKT();
 17     }
 18     
 19     static void createFromWKT() throws JsonParseException, IOException {
 20 
 21 
 22         String wktString = "Point (-106.4453583 41.11775)";
 23         Geometry geomPoint = OperatorImportFromWkt.local().execute(WktImportFlags.wktImportDefaults, Geometry.Type.Point, wktString, null);
 24 
 25         wktString = "MULTIPOINT ((-106.4453583 39.11775),(-106.4453583 40.11775))";
 26         Geometry geomMulitPoint = OperatorImportFromWkt.local().execute(WktImportFlags.wktImportDefaults, Geometry.Type.MultiPoint, wktString, null);
 27 
 28         wktString = "MULTILINESTRING ((6.9 9.1,7 8.8),(6.8 8.8,7 9,7.2 8.9,7.4 9),(7.4 8.9,7.25 8.6,7.15 8.8))";
 29         Geometry geomLine = OperatorImportFromWkt.local().execute(WktImportFlags.wktImportDefaults, Geometry.Type.Polyline, wktString, null);
 30 
 31         wktString = "MULTIPOLYGON (((0.1 0.7, 0.1 0.4, 0.3 0.4, 0.3 0.7, 0.1 0.7)),"
 32                 + "((0 0, 0.5 0, 1 0.5, 0.5 1, 0 1, -0.5 0.5, 0 0),"
 33                 + "(0.5 0.2, 0.2 0.3, 0.1 0.2, -0.2 0.5, 0.2 0.9, 0.6 0.5, 0.5 0.2)))";
 34         Geometry geomPolygon = OperatorImportFromWkt.local().execute(WktImportFlags.wktImportDefaults, Geometry.Type.Polygon, wktString, null);
 35 
 36         geomPoint.addAttribute(0);
 37         geomPoint.addAttribute(1);
 38         geomPoint.addAttribute(2);
 39         geomPoint.addM();
 40         geomPoint.addAttribute(3);
 41         geomPoint.addID();
 42         
 43         System.out.println(geomPoint.hasAttribute(3));
 44         
 45         geomPoint.dropAttribute(3);
 46         System.out.println(geomPoint.hasAttribute(3));
 47         
 48         geomPoint.dropAllAttributes();
 49         System.out.println(geomPoint.hasAttribute(2));
 50         System.out.println(geomPoint.hasAttribute(0));
 51         
 52         
 53 //        Geometry geomMulitPoint2 = geomMulitPoint.copy();
 54 //        System.out.println(geomMulitPoint2.toString());
 55 //        
 56 //        geomMulitPoint2.assignVertexDescription(geomMulitPoint.getDescription());
 57 //        System.out.println(geomMulitPoint2.toString());
 58 //
 59 //        geomMulitPoint2.mergeVertexDescription(geomMulitPoint.getDescription());
 60 //        System.out.println(geomMulitPoint2.toString());
 61 
 62         
 63         System.out.println(geomPoint.calculateLength2D());
 64         System.out.println(geomLine.calculateLength2D());
 65         System.out.println(geomPolygon.calculateLength2D());
 66         
 67         System.out.println(geomPoint.calculateArea2D());
 68         System.out.println(geomLine.calculateArea2D());
 69         System.out.println(geomPolygon.calculateArea2D());
 70         
 71         
 72         Geometry geomTemp = geomPoint.getBoundary();
 73         geomTemp = geomLine.getBoundary();
 74         geomTemp = geomPolygon.getBoundary();
 75         
 76         Envelope env1 = new Envelope();
 77         geomPoint.queryEnvelope(env1);
 78         System.out.println(env1.toString());
 79 
 80         geomLine.queryEnvelope(env1);
 81         System.out.println(env1.toString());
 82         
 83         geomPolygon.queryEnvelope(env1);
 84         System.out.println(env1.toString());
 85         
 86         
 87         System.out.println(Geometry.vertex_count(geomPoint));
 88         System.out.println(Geometry.vertex_count(geomLine));
 89         System.out.println(Geometry.vertex_count(geomPolygon));
 90 
 91         System.out.println(geomPoint.getDimension());
 92         System.out.println(geomPoint.getType());
 93 
 94         System.out.println(geomLine.getDimension());
 95         System.out.println(geomLine.getType());
 96         
 97         System.out.println(geomPolygon.getDimension());
 98         System.out.println(geomPolygon.getType());
 99         
100         wktString = "MULTIPOINT ((-106.4453583 41.11775),(-106.4453583 42.11775))";
101         Geometry geomMulitPoint2 = OperatorImportFromWkt.local().execute(WktImportFlags.wktImportDefaults, Geometry.Type.MultiPoint, wktString, null);
102 
103         geomMulitPoint.copyTo(geomMulitPoint2);
104         System.out.println(geomMulitPoint.toString());
105         System.out.println(geomMulitPoint2.toString());
106         
107         System.out.println(geomMulitPoint.isEmpty());
108         geomMulitPoint.setEmpty();
109         System.out.println(geomMulitPoint.isEmpty());
110         System.out.println(geomPoint.estimateMemorySize());
111         
112     }
113 }

 

参考:

http://esri.github.io/geometry-api-java/javadoc/com/esri/core/geometry/Geometry.html

https://github.com/Esri/geometry-api-java/wiki

posted @ 2019-02-11 20:32 第17个巡道工 阅读(...) 评论(...) 编辑 收藏