AE92 SDK for Java 最小示例学习

简介:

初学ArcEngine开发,看到这么多的类和接口有点让人望而生畏,不好好整理思路容易让人发晕,其实熟悉了ArcGIS各组件的功能,了解了各种操作的设计思路,拿着各种文档顺藤摸瓜,还是可以迅速进入开发角色的。整个SDK结构组成的分析留到后面,我们先学习一个最小的开发示例,例子来源于ArcEngine开发文档(HelloCentroid)。

    例子的功能:

    返回某shapefile文件第一个feature质心的坐标。

    引用的包:

import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory;
import com.esri.arcgis.geodatabase.*;
import com.esri.arcgis.geometry.*;
import com.esri.arcgis.system.*;

    每个包的具体用途与功能先不管,以后的学习中会慢慢涉及到并加以分析。

    从文件路径中捕获shapefile特征类:

ExpandedBlockStart.gif private FeatureClass getShapefileFeatureClass(String path, String name)  throws IOException  {
  FeatureClass featureClass = null;
ExpandedSubBlockStart.gif  try {
    ShapefileWorkspaceFactory shapefileWorkspaceFactory = new ShapefileWorkspaceFactory();
    Workspace workspace = (Workspace) shapefileWorkspaceFactory.openFromFile(path, 0);
    featureClass = new FeatureClass(workspace.openFeatureClass(name));
  }

ExpandedSubBlockStart.gif  catch (IOException ex) {
    System.out.println("Could not open shapefile: " + name);
    throw ex;
  }

  return featureClass;
}

    调用ShapefileWorkspaceFactory工厂类的对象的方法openFromFile,将指定的路径列为工作空间,然后打开工作空间中指定名称的特征类,参数传入FeatureClass的构造方法中,返回FeatureClass对象。

    ShapefileWorkspaceFactory工厂类除了可以返回指定的工作空间外,还可以创建、移动、复制工作空间,以及得到工作空间相关的基本信息。

    在com.esri.arcgis.geodatabase包中可以找到Workspace类,它的方法有许多,涉及和工作空间相关的许多功能,例如连接到工作空间的数据库名称、用户名称,开始/停止编辑工作空间,创建和删除注记类、特征类、特征数据集、关联类,判断工作空间中某种操作能否执行,工作空间的基本信息,判断对象是否注册为版本等等。代码中所用到的openFeatureClass用于打开已存在的特征类并返回为IFeatureClass。

    FeatureClass类的构造方法接收workspace.openFeatureClass返回的参数,将对象的引用赋给featureClass对象并返回。

    这个方法的核心应该关注Workspace类,它把握着Geodatabase数据的整体框架与功能导向,FeatureClass是Workspace组成部分,包含了FeatureClass特定的功能与方法。

    得到特征类的质心位置:

ExpandedBlockStart.gif private  void printFirstFeatureCentroid(FeatureClass featureClass)  throws IOException  {
  //
  
// Get the first feature in the feature class.
  
//
  IFeature feature = featureClass.getFeature(0);
  //
  
// Get the shape of the feature, and if the shape is a polygon or ring, 
  
// get its centroid by casting it to the interface common to both of them (IArea),
  
// which interface defines the getCentroid method.
  
//
  IGeometry shape = feature.getShape();
ExpandedSubBlockStart.gif  if (!(shape instanceof Polygon || shape instanceof Ring)) {
    System.out.println("Feature's shape is neither a polygon nor a ring.  No centroid available.");
    return;
  }

  IArea area = (IArea) shape;
  IPoint centroid = area.getCentroid();
  System.out.println("Centroid: " + centroid.getX() + ", " + centroid.getY());
}

    featureClass对象的getFeature(0)方法得到特征类中第一个feature,通过判断确定该feature为区或环,将该feature所对应的shape uppercast为IArea类型,由getCentroid方法得到area对象的质心点,getX()和getY()输出该点的坐标。IGeometry、IArea、IPoint都是com.esri.arcgis.geometry包中的接口,指定了不同的几何类型。由printFirstFeatureCentroid方法,我们可以扩展学习com.esri.arcgis.geometry包中典型接口的使用,例如示例中用到的接口,其包含的方法都很简单。

    main方法:

ExpandedBlockStart.gif private  void printFirstFeatureCentroid(FeatureClass featureClass)  throws IOException  {
  //
  
// Get the first feature in the feature class.
  
//
  IFeature feature = featureClass.getFeature(0);
  //
  
// Get the shape of the feature, and if the shape is a polygon or ring, 
  
// get its centroid by casting it to the interface common to both of them (IArea),
  
// which interface defines the getCentroid method.
  
//
  IGeometry shape = feature.getShape();
ExpandedSubBlockStart.gif  if (!(shape instanceof Polygon || shape instanceof Ring)) {
    System.out.println("Feature's shape is neither a polygon nor a ring.  No centroid available.");
    return;
  }

  IArea area = (IArea) shape;
  IPoint centroid = area.getCentroid();
  System.out.println("Centroid: " + centroid.getX() + ", " + centroid.getY());
}

    从前面的四行代码可以看出,java解释器运行该类文件编译后的字节码需要两个参数,一个是featureclass所在的路径,一个是该路径下featureclass名称。需要注意的是这三行代码:

EngineInitializer.initializeEngine();
aoInitializer =  new AoInitialize();
aoInitializer.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);

    com.esri.arcgis.system.EngineInitializer.initializeEngine(),在原始AO组件和Java Class之间建立联系,如果要使用ArcGIS Visual JavaBeans进行图形操作,则应使用initializeVisualBeans静态方法进行初始化。aoInitializer对象决定不同的授权和扩展,ESRI License Product codes参考下列表:

 

    Eclipse运行测试,需要在"运行"中输入两个"自变量"作为参数,采用ArcGIS自带的数据,分别为

    "ArcGISHome\ArcTutor\Getting_Started\project\City_share\land"、"parcel_1"

    测试的结果,控制台输出为:

    Hello, Centroid!
    Centroid: 479049.62060511723, 3771922.345004217

    这个例子描述了一个最简单AE开发的整个过程,从初始化、授权,到Workspace类、FeatureClass类方法,到com.esri.arcgis.geometry包中典型接口的使用,最后得到我们需要的结果,过程清晰明了,初学者可以通过这个例子顺藤摸瓜,敲开AE开发的大门,说的有点玄乎:)


本文转自Flyingis博客园博客,原文链接:http://www.cnblogs.com/flyingis/archive/2007/03/08/668275.html,如需转载请自行联系原作者

相关文章
|
9天前
|
消息中间件 前端开发 Java
java学习路径
【4月更文挑战第9天】java学习路径
17 1
|
24天前
|
Java 数据处理 调度
Dataphin常见问题之离线管道同步数据datax就报连接超时如何解决
Dataphin是阿里云提供的一站式数据处理服务,旨在帮助企业构建一体化的智能数据处理平台。Dataphin整合了数据建模、数据处理、数据开发、数据服务等多个功能,支持企业更高效地进行数据治理和分析。
|
29天前
|
安全 Java 程序员
学习Java类加载机制
在Java的世界里,每一个类或者接口,在经历编译器后,都会生成一个个.class文件。
18 0
|
29天前
|
Java 开发工具
【GDAL-java的四个常用代码示例】
【GDAL-java的四个常用代码示例】
32 0
|
1月前
|
Java 关系型数据库 MySQL
37、一篇文章学习 Java 中的日期相关类(Date 和 Calendar),非常常用
37、一篇文章学习 Java 中的日期相关类(Date 和 Calendar),非常常用
27 0
|
1月前
|
存储 安全 Java
24、使用 Java 官方教程学习:① 类变量和类方法详解;② 深入介绍 main() 方法
24、使用 Java 官方教程学习:① 类变量和类方法详解;② 深入介绍 main() 方法
37 1
|
9天前
|
设计模式 前端开发 安全
Java是一种广泛使用的编程语言,其学习路径可以大致分为以下几个阶段
【4月更文挑战第9天】Java是一种广泛使用的编程语言,其学习路径可以大致分为以下几个阶段
12 1
|
1月前
|
设计模式 Java 数据库连接
【重温设计模式】代理模式及其Java示例
【重温设计模式】代理模式及其Java示例
21 2
|
1月前
|
Java 索引
Java中String方法学习总结_kaic
Java中String方法学习总结_kaic
|
5天前
|
Java 存储
键值之道:深入学习Java中强大的HashMap(二)
键值之道:深入学习Java中强大的HashMap
9 0
键值之道:深入学习Java中强大的HashMap(二)