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

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数据的整体框架与功能导向,FeatureClassWorkspace组成部分,包含了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 uppercastIArea类型,由getCentroid方法得到area对象的质心点,getX()getY()输出该点的坐标。IGeometryIAreaIPoint都是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参考下列表:

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

 

转载于:https://www.cnblogs.com/cherry316/archive/2009/12/16/1625407.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值