Flyingis

Talking and thinking freely !
Flying in the world of GIS !
随笔 - 156, 文章 - 16, 评论 - 589, 引用 - 0
数据加载中……

AE92 SDK for Java 最小示例学习

    作者:Flyingis

    初学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特征类:

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

  
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特定的功能与方法。

    得到特征类的质心位置:

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();
  
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方法:

public static void main(String[] args) {
  
if (args.length != 2{
    System.out.println(
"Usage: HelloCentroid shapefilePath shapefileName");
    System.exit(
1);
  }

  System.out.println(
"Hello, Centroid!");
  AoInitialize aoInitializer 
= null;
  
try {
    EngineInitializer.initializeEngine();
    aoInitializer 
= new AoInitialize();
    aoInitializer.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
    HelloCentroid thisApp 
= new HelloCentroid();
    
//
    
// Get the feature class for the path and name specified, 
    
// and get its first feature's centroid.
    
//
    FeatureClass featureClass = thisApp.getShapefileFeatureClass(args[0], args[1]);
      
if (featureClass != null{
        thisApp.printFirstFeatureCentroid(featureClass);
      }

  }

  
catch (IOException ex) {
    ex.printStackTrace();
    System.out.println(
"App failed.");
  }

  
finally {
    
try {
      aoInitializer.shutdown();
    }

    
catch (IOException ex) {
      ex.printStackTrace();
    }

  }

}

    从前面的四行代码可以看出,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参考下列表:

ESRI-License-Product-codes.gif

    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开发的大门,说的有点玄乎:)

posted on 2007-03-08 16:21 Flyingis 阅读(3618) 评论(0)  编辑  收藏 所属分类: ArcEngine


只有注册用户登录后才能发表评论。


网站导航: