当前位置:   article > 正文

arcengine for java 将gdb文件里的图层数据存入sde_arcpy批量导入gdb数据库到sde库中

arcpy批量导入gdb数据库到sde库中

一、相关环境以及准备

参见我上一篇文章

二、arcengine连接sde

1、可以现在自己电脑上的arcgis上连接下sde试试看能不能连上,如果自己的arcgis都连不上sde,代码里肯定也连不上。
2、arcengine连接sde的相关代码如下:

 /**
     * 连接sde 并返回连接sde后的工作空间
     *
     * @return
     * @throws IOException
     */
    public static Workspace connectSde() throws IOException {
        initializeArcGISLicenses();
        Workspace workspace = null;

        IPropertySet pPropset = new PropertySet();
//        IWorkspaceFactory pWorkspaceFact = new SdeWorkspaceFactory();
        SdeWorkspaceFactory pWorkspaceFact = new SdeWorkspaceFactory();
        // Populate the property set with the connection parameters
        pPropset.setProperty("SERVER", "10.10.1.161");
//        propSet.setProperty("INSTANCE", "sde:postgresql:10.10.1.178,5432");
        pPropset.setProperty("INSTANCE", "sde:oracle11g:10.10.1.258/ORCL");
        pPropset.setProperty("DATABASE", "orcl");
        pPropset.setProperty("USER", "data_check");
        pPropset.setProperty("PASSWORD", "123456");
        pPropset.setProperty("VERSION", "sde.DEFAULT");
        log.info("数据源数据库为:10.10.1.258,1521");

        // Open the ArcSDE workspace using the connection PropertySet
//        workspace = pWorkspaceFact.open(pPropset, 0);
        workspace = new Workspace(pWorkspaceFact.open(pPropset, 0));

        return workspace;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

三、先在sde里创建图层数据表(sde里已有图层数据表的可以忽略)

/**
     * 在 sde 里创建 table
     *
     * @param gdbPath
     * @param FcName      图层名
     * @param pWorkspace  连接sde的工作空间
     * @param tableName   生成的表明
     */
    public static void createTable(String gdbPath, String FcName, IWorkspace pWorkspace, String tableName) throws IOException {
        initializeArcGISLicenses();

        try {
            //获取gdb里的featureClass
            //创建 gdb工作空间
            FileGDBWorkspaceFactory pFileGDBWorkspaceFactoryClass = new FileGDBWorkspaceFactory();
            IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace) pFileGDBWorkspaceFactoryClass.openFromFile(gdbPath, 0);
            //获取 gdb中某个图层的 FeatureClass对象
            IFeatureClass pFeatureClass = pFeatureWorkspace.openFeatureClass(FcName);

//            IGeoDataset pGeoDataset = (IGeoDataset) pFeatureClass;
            //featureClass的字段
            IFields fields = pFeatureClass.getFields();


            //连接sde库
            if (pWorkspace != null) {
                    IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)pWorkspace;
                    //创建新featureclass
                IFeatureClass pFCls = featureWorkspace.createFeatureClass(tableName, fields, null, null,
                        pFeatureClass.getFeatureType(), pFeatureClass.getShapeFieldName(), "");
                
                }
            } catch (Exception ex) {
            ex.printStackTrace();
            }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

四、将数据从gdb文件写入sde

/**
     * 将gdb里某个图层的数据全部存入sde的某个table中
     * @param gdbPath
     * @param layerCode  gdb里图层名称
     * @param tableName  sde里的表名称
     * @param startId    sde表开始存的id
     * @param primaryKey sde表的主键
     * @throws IOException
     */
    public static void dataToTable(String gdbPath, String layerCode, String tableName , Integer startId ,
                                   String primaryKey) throws IOException {
        initializeArcGISLicenses();
        //获取 连接sde的 IFeatureClass 对象
        Workspace workspace = connectSde();
        IFeatureClass iFeatureClass = workspace.openFeatureClass("data_check." + tableName);

        //获取 gdb里图层的 featureClass 对象
        FeatureVo fcAndF = getFCAndF(gdbPath, layerCode);
        IFeatureClass featureClass = fcAndF.getFeatureClass();
//        IFeatureCursor featureCursor = featureClass.IFeatureClass_insert(true);
        IQueryFilter queryFilter = new QueryFilter();
        IFeatureCursor search = featureClass.search(queryFilter, true);

        //获取 IFeature对象
        IFeature pFeature = null;
        while ((pFeature = search.nextFeature()) != null) {
            //将数据一条一条存进sde
            gdbToSde(iFeatureClass, pFeature , startId , primaryKey);
        }

        search.flush();
    }


    /**
     * 将 gdb文件里的一条数据写进sde的table里(sde中的table已存在)
     *
     * @param pFeatureClass 连接数据库里的table对象
     * @param pFeature      单条数据,也就是单个图版
     * @param startId       sde表开始存的id
     * @param primaryKey    sde表的主键
     * @return
     * @throws IOException
     */
    public static void gdbToSde(IFeatureClass pFeatureClass, IFeature pFeature , Integer startId ,
                                String primaryKey) throws IOException {
        initializeArcGISLicenses();
//        IFeatureClass rFeatureClass = Erase(pFeatureClass, pFeature);

        IFeatureCursor featureCursor = pFeatureClass.IFeatureClass_insert(true);
        IFeatureBuffer feaBuffer = pFeatureClass.createFeatureBuffer();

        IField fld = new Field();
        IFields flds = pFeature.getFields();
        if(startId == 0){
            for (int i = 0; i < flds.getFieldCount(); i++) {
                fld = flds.getField(i);
                String fldname = fld.getName();
                int index = feaBuffer.getFields().findField(fldname);
                if (index != -1 && index != 0) {
                    feaBuffer.setValue(index, pFeature.getValue(i));
                }

            }
            feaBuffer.setShapeByRef(pFeature.getShape());
            featureCursor.insertFeature(feaBuffer);
            featureCursor.flush();
        }else {
            for (int i = 0; i < flds.getFieldCount(); i++) {
                fld = flds.getField(i);
                String fldname = fld.getName();
                int index = feaBuffer.getFields().findField(fldname);
                if (index != -1 && index != 0) {
                    if(fldname.equals(primaryKey)){
                        feaBuffer.setValue(index, ++startId);
                    }else {
                        feaBuffer.setValue(index, pFeature.getValue(i));
                    }
                }

            }
            feaBuffer.setShapeByRef(pFeature.getShape());
            featureCursor.insertFeature(feaBuffer);
//            featureCursor.flush();
        }
    }

/**
     * 获取 IFeatureWorkspace 和 IFeatureClass 和 IFeature 对象
     *
     * @param gdbPath
     * @param layerCode
     * @return
     */
    public static FeatureVo getFCAndF(String gdbPath, String layerCode) throws IOException {
        initializeArcGISLicenses();
        //featureVo 就是个实体类,封装了IFeatureWorkspace 与 IFeatureClass
        FeatureVo featureVo = new FeatureVo();

        //创建 gdb工作空间
        FileGDBWorkspaceFactory pFileGDBWorkspaceFactoryClass = new FileGDBWorkspaceFactory();
        IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace) pFileGDBWorkspaceFactoryClass.openFromFile(gdbPath, 0);
        //获取 gdb中某个图层的 FeatureClass对象
        IFeatureClass pFeatureClass = pFeatureWorkspace.openFeatureClass(layerCode);

        featureVo.setFeatureWorkspace(pFeatureWorkspace);
        featureVo.setFeatureClass(pFeatureClass);


        return featureVo;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111

dataToTable这个方法里有个参数是gdb图层名称,我上一篇文章有如何获取gdb里图层名称的方法
https://blog.csdn.net/qq_45697944/article/details/107243806

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/341091
推荐阅读
相关标签
  

闽ICP备14008679号