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參考下列表:
??? 初學ArcEngine開發,看到這么多的類和接口有點讓人望而生畏,不好好整理思路容易讓人發暈,其實熟悉了ArcGIS各組件的功能,了解了各種操作的設計思路,拿著各種文檔順藤摸瓜,還是可以迅速進入開發角色的。整個SDK結構組成的分析留到后面,我們先學習一個最小的開發示例,例子來源于ArcEngine開發文檔(HelloCentroid)。
??? 例子的功能:
??? 返回某shapefile文件第一個feature質心的坐標。
??? 引用的包:




??? 每個包的具體用途與功能先不管,以后的學習中會慢慢涉及到并加以分析。
??? 從文件路徑中捕獲shapefile特征類:



















??? 調用ShapefileWorkspaceFactory工廠類的對象的方法openFromFile,將指定的路徑列為工作空間,然后打開工作空間中指定名稱的特征類,參數傳入FeatureClass的構造方法中,返回FeatureClass對象。
??? ShapefileWorkspaceFactory工廠類除了可以返回指定的工作空間外,還可以創建、移動、復制工作空間,以及得到工作空間相關的基本信息。
??? 在com.esri.arcgis.geodatabase包中可以找到Workspace類,它的方法有許多,涉及和工作空間相關的許多功能,例如連接到工作空間的數據庫名稱、用戶名稱,開始/停止編輯工作空間,創建和刪除注記類、特征類、特征數據集、關聯類,判斷工作空間中某種操作能否執行,工作空間的基本信息,判斷對象是否注冊為版本等等。代碼中所用到的openFeatureClass用于打開已存在的特征類并返回為IFeatureClass。
??? FeatureClass類的構造方法接收workspace.openFeatureClass返回的參數,將對象的引用賦給featureClass對象并返回。
??? 這個方法的核心應該關注Workspace類,它把握著Geodatabase數據的整體框架與功能導向,FeatureClass是Workspace組成部分,包含了FeatureClass特定的功能與方法。
??? 得到特征類的質心位置:























??? 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方法:


















































??? 從前面的四行代碼可以看出,java解釋器運行該類文件編譯后的字節碼需要兩個參數,一個是featureclass所在的路徑,一個是該路徑下featureclass名稱。需要注意的是這三行代碼:



??? 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開發的大門,說的有點玄乎:)
posted on 2007-03-08 16:21 Flyingis 閱讀(3643) 評論(0) 編輯 收藏 所屬分類: ArcEngine