作者:
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参考下列表:
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开发的大门,说的有点玄乎:)
分享到:
相关推荐
ArcGIS_Engine_SDK_for_Java_最小示例学习
使用JAVA对AE10.4进行二次开发的小demo,功能有空间查询、距离量算、数据库连接等。包含详细源代码。运行需要安装ArcGIS套件切拥有许可。
东方时代份额为人万恶天天软件浏览可就苦了开户局号
ArcEngine for java开发demo源码(个人开发),本人使用的eclipse环境开发的AE demo。提供下载学习
jpct-ae jar包和文档、示例,希望对有这方面学习兴趣朋友有所帮助!
ArcObjects SDK(AE)10.1安装时提示需要VS2010错误提示的方法
最近学习Java进行AE的二次开发,自己写了一个鹰眼的小程序(用的是Eclipse),拿来与大家分享!
用matlab从底层实现深度学习包,打好了架构。实现了自动编码器。
adobe-sdk.zip,adobe的sdk开发包adobe-sdk.zip
AE Linux 配置 ArcGIS Engine for Linux 环境配置说明
所谓的鹰眼,就是一个缩略地图,上面有一个矩形框,矩形框区域就是当前显示的地图区域,拖动矩形框可以改变当前地图显示的位置,改变矩形框的大小,可以改变当前地图的显示区域大小,从起到导航的作用。...
Trapcode Particular插件是最火爆的一款三维粒子插件,每位AE软件使用者都装有此插件。Trapcode Particular是视频处理必备的一款后期插件,用于制作真实的3D特效,非常强大专业。此版Trapcode Particular插件支持AE ...
red giant for AE 3.0
AE学习资料
Red Giant Trapcode 合集 For AE CS5!用AE的都懂~
RED GIANT TRAPCODE 3D STROKE V2.5 FOR AE CS3 RED GIANT TRAPCODE ECHOSPACE V1.01 FOR AE CS3 RED GIANT TRAPCODE LUX V1.01 FOR AE CS3 RED GIANT TRAPCODE PARTICULAR V1.5 FOR AE CS3 RED GIANT TRAPCODE ...
AE2018 For Mac破解补丁,完美好用,安装完成之后直接替换就可以了,如果有不知道怎么替换的,请留言
新版E3D V2.2.3插件具有多帧渲染功能,可以让AE同时渲染多帧,加速渲染!在4K显示器上使用UI时,增加了对更大缩放的支持! MFR多帧渲染性能优势:由于AE可以开始在您的所有处理器内核上渲染多个帧,因此依赖于您的...
AE的开发案例,学习AE开发用的到的,可以试试看看。
AE光工厂FOR mac 适合5.5 6.0