java for arcgis 之——将shapefile导入SDE

环境:1.安装arcgis客户端(百度有)

         2.在该路径下ArcGIS\Desktop10.2\java\lib 找到arcobject.jar  这是java对arcgis二次开发所需的jar包,添加到工程中(百度有)

          3.SDE本人采取的直连oracle的方式,也是查询资料后推荐的方式(需要本地安装oracle客户端,具体见百度)

以上环境准备好直接上代码

注意:!!!!!

            1.这里是很关键的地方,java在使用arcobject.jar时需要在VMOptions中添加一个java命令,目的是告诉jvm去寻找arcgis的dll组件,因为java不能像C#完美调用本地客户端服务

            -Djava.library.path="C:\Program Files (x86)\ArcGIS\Desktop10.2\bin"  -Dfile.encoding=UTF8 

            路径写自己的arcgis安装路径。

             2.java调用arcgis服务时必须先初始化ArcEngine环境,这会在代码中体现,个人猜测应该是调用组件的引擎

代码:

先讲下代码思路:shapefile-->shapefile的featureClass,SDE-->SDE的featureClass,shpFeatureClass插入到SDEFeatureClass中

初始化ArcEngine:

private static AoInitialize ao = null;

/**
 * 初始化ArcEngne
 * java操作arcgis必须先初始化ArcEngne环境  否则报错
 *
 */
public static void initializeArcGISLincenses(){
    /*所有AE10的与AE10.1以后版本的对应关系

    esriLicenseProductCodeArcInfo ==》esriLicenseProductCodeAdvanced
    esriLicenseProductCodeArcEditor==》esriLicenseProductCodeStandard
    esriLicenseProductCodeArcView ==》esriLicenseProductCodeBasic*/

    EngineInitializer.initializeVisualBeans();
    EngineInitializer.initializeEngine();
    try {
        ao = new AoInitialize();
        if (ao.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeAdvanced) == esriLicenseStatus.esriLicenseAvailable){
            ao.initialize(esriLicenseProductCode.esriLicenseProductCodeAdvanced);
        }else {
            System.out.print("lincense failed");
            ao.checkOutExtension(esriLicenseExtensionCode.esriLicenseExtensionCode3DAnalyst);
            ao.checkOutExtension(com.esri.arcgis.system.esriLicenseExtensionCode.esriLicenseExtensionCodeSpatialAnalyst);
            ao.checkOutExtension(com.esri.arcgis.system.esriLicenseExtensionCode.esriLicenseExtensionCodeSpatialAnalyst);
            ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
            ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB);
            ao.initialize(esriLicenseProductCode.esriLicenseProductCodeStandard);
            ao.checkOutExtension(esriLicenseExtensionCode.esriLicenseExtensionCodeSpatialAnalyst);
            System.out.println("arcgis>>>...");
        }
    }catch (Exception e){
        e.printStackTrace();
    }
}

连接SDE:

public class ConnectSDE {

    /**
     * 连接SDE  返回SDE工作空间
     * @return
     * @throws IOException
     */
    public static IWorkspace openSDE() throws IOException{
        IPropertySet iPropertySet = new PropertySet();
        iPropertySet.setProperty("AUTHENTICATION_MODE", "DBMS");
        iPropertySet.setProperty("DB_CLIENT", "ORCL");
        iPropertySet.setProperty("IS_GEODATABSE", "true");
        iPropertySet.setProperty("DB_CONNECTION_PROPERTIES", "ORCL");
        iPropertySet.setProperty("SERVER", "192.168.80.35");
        //1、oracle$后是客户端配置的实例名时 IP(SERVER)可以不设置 随便设置一个IP也可以
        //2、oracle$后是服务器IP【:端口(如1521)】/服务器实例名 时 IP(SERVER)一定要填且必须对
        // sde:oracle$orcl(客户端配置的实例名) 或者 sde:oracle$127.0.0.1[:1521]/orcl(默认端口不报错)
        iPropertySet.setProperty("INSTANCE", "sde:oracle:ORCL_192.168.80.35");
        iPropertySet.setProperty("USER", "dsnjgeo");
        iPropertySet.setProperty("PASSWORD", "gtis");
        //pPropertySet.SetProperty("VERSION", "SDE.DEFAULT");//默认版本可以不设置
        IWorkspaceFactory workspaceFactory = new SdeWorkspaceFactory();
        IWorkspace workspace = workspaceFactory.open(iPropertySet,0);
        if (workspace.exists()){
            return workspace;
        }else {
            return null;
        }
    }

}

获取shapefile要素集:

/**
 * 获取shapefile要素集
 * @param path //shapefile文件dizhi
 * @param name  featureClass的名称
 * @return
 * @throws IOException
 */
private static FeatureClass getFeatureClass(String path,String name) throws IOException{
    FeatureClass featureClass = null;
    try {
        ShapefileWorkspaceFactory swf = new ShapefileWorkspaceFactory();
        Workspace workspace = new Workspace(swf.openFromFile(path,0));
        featureClass = new FeatureClass(workspace.openFeatureClass(name));
    }catch (IOException e){
        System.out.println("Couldn't access feature class :" + name + " in " + path);
        throw e;
    }
    return featureClass;
}

开始导入:

/**
 * 批量导入
 * @param shapePath
 * @param featureClassName
 * @param targetFeatureName SDE中目标表名称
 * @throws IOException
 */
public static void shfToSDE(String shapePath,String featureClassName,String targetFeatureName) throws IOException{
    //初始化AE环境
    initializeArcGISLincenses();
    //获取shapefile要素集
    IFeatureClass shpFeature = getFeatureClass(shapePath,featureClassName);

    //获取SDE工作空间
    IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)ConnectSDE.openSDE();
    //指定SDE要素集
    IFeatureClass sdefeatureClass = featureWorkspace.openFeatureClass(targetFeatureName);

    //IFeatureCursor接口可用来访问要素类的一系列要素
    IFeatureCursor featureCursor = shpFeature.search(null,true);

    //NextFeature方法第一次调用时,实际指向第一条记录,之后每一次调用都是指向下一条记录
    //获取第一个feature
    IFeature feature = featureCursor.nextFeature();

    IFeatureCursor sdeFeatureCursor = sdefeatureClass.IFeatureClass_insert(true);

    IFeatureBuffer sdeFeatureBuffer = null;
    while (feature != null){
        sdeFeatureBuffer = sdefeatureClass.createFeatureBuffer();
        IField shpField = null;
        IFields shpFields = feature.getFields();
        for(int i=0; i; i++){
            shpField = shpFields.getField(i);
            int index = sdeFeatureBuffer.getFields().findField(shpField.getName());
            if (index != -1){
                sdeFeatureBuffer.setValue(index,feature.getValue(i));
            }
        }
        sdeFeatureCursor.insertFeature(sdeFeatureBuffer);
        sdeFeatureCursor.flush();
        feature = featureCursor.nextFeature();
    }
    featureWorkspace = null;
}

你可能感兴趣的:(工作随笔)