不同gdb,相同數據集合並


眾所周知,數據處理是GIS中一項重要且繁瑣的工作,處理數據的工具和方法也太多了,在做數據處理的時候,經常會遇到這樣的問題:對存儲在不同gdb中、並且數據集名稱相同的數據進行合並處理:

如圖:數據組織如下,每個gdb中都存儲了一些列FeatureClass,(但gdb中的FeatureClass數量並不相同)

思路是:

1.先對每個gdb中的數據進行處理,使得每個gdb中的featureclass數量和名稱相同。由於對Engine比較熟悉,這里我是用Engine進行處理的,具體代碼如下:

private function Execute(){  //初始執行函數:
     string templatePath = @"F:\testout";
     DirectoryInfo directoryInfo = new DirectoryInfo(templatePath);
     DirectoryInfo[] dirInfo = directoryInfo.GetDirectories();
     string yy = dirInfo[0].Name;
     string FeatureClassName = "ROALK_arc";  //FeatureClass名稱,這里可以設置一個數組,存儲所有的FeatureClass
     for (int i = 0; i < dirInfo.Length; i++)
     {
         string gdbName = dirInfo[i].Name;
         //打開filegdb
         bool value = oper(@"F:\testout\" + gdbName, FeatureClassName);//判斷FeatureClass是否存在
         string path = @"F:\testout\" + gdbName;
         if (value == false)
         {
             copyFeatureClass(path, FeatureClassName);
          }
               
    }
}
public bool oper(string filename,string featureClassName) //判斷FeatureClass是否存在

        {

            IWorkspace2 workspace = null;
            IWorkspaceFactory2 workspaceFactory = new FileGDBWorkspaceFactoryClass();
            workspace = workspaceFactory.OpenFromFile(filename, 1) as IWorkspace2;

            IFeatureWorkspace featureWorkspace = workspace as IFeatureWorkspace;

            bool flag = workspace.get_NameExists(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTFeatureClass, featureClassName);

            return flag;
            

        }
public bool oper(string filename,string featureClassName) //判斷是gdb中是否存在某個FeatureClass

        {

            IWorkspace2 workspace = null;
            IWorkspaceFactory2 workspaceFactory = new FileGDBWorkspaceFactoryClass();
            workspace = workspaceFactory.OpenFromFile(filename, 1) as IWorkspace2;

            IFeatureWorkspace featureWorkspace = workspace as IFeatureWorkspace;

            bool flag = workspace.get_NameExists(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTFeatureClass, featureClassName);

            return flag;
            

        }

//拷貝所有的FeatureClass到gdb,並刪除里面的數據,保證每個featureclass為空,注:D:\Data\Shapefiles存儲了所有的要合並的FeatureClass的空圖層,便於拷貝。

 private void convert()
        {
                 IWorkspaceName sourceWorkspaceName = new WorkspaceNameClass
                 {
                     WorkspaceFactoryProgID = "esriDataSourcesFile.ShapefileWorkspaceFactory",
                     PathName = @"D:\Data\Shapefiles"
                 };
                 IName sourceWorkspaceIName = (IName)sourceWorkspaceName;
                 IWorkspace sourceWorkspace = (IWorkspace)sourceWorkspaceIName.Open();


                 // Create a name object for the target (file GDB) workspace and open it.
                 IWorkspaceName targetWorkspaceName = new WorkspaceNameClass
                 {
                     WorkspaceFactoryProgID = "esriDataSourcesGDB.FileGDBWorkspaceFactory",
                     PathName = @"D:\Data\Public.gdb"
                 };
                 IName targetWorkspaceIName = (IName)targetWorkspaceName;
                 IWorkspace targetWorkspace = (IWorkspace)targetWorkspaceIName.Open();



                 // Create a name object for the source dataset.
                 IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass();
                 IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName;
                 sourceDatasetName.Name = "BOUNT_arc";
                 sourceDatasetName.WorkspaceName = sourceWorkspaceName;


                 // Create a name object for the target dataset.
                 IFeatureClassName targetFeatureClassName = new FeatureClassNameClass();
                 IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName;
                 targetDatasetName.Name = "BOUNT_arc";
                 targetDatasetName.WorkspaceName = targetWorkspaceName;


                 // Open source feature class to get field definitions.
                 IName sourceName = (IName)sourceFeatureClassName;
                 IFeatureClass sourceFeatureClass = (IFeatureClass)sourceName.Open();


                 // Create the objects and references necessary for field validation.
                 IFieldChecker fieldChecker = new FieldCheckerClass();
                 IFields sourceFields = sourceFeatureClass.Fields;
                 IFields targetFields = null; 
                 IEnumFieldError enumFieldError = null;

                 // Set the required properties for the IFieldChecker interface.
                 fieldChecker.InputWorkspace = sourceWorkspace;
                 fieldChecker.ValidateWorkspace = targetWorkspace;


                 // Validate the fields and check for errors.
                 fieldChecker.Validate(sourceFields, out enumFieldError, out targetFields);
                 if (enumFieldError != null)
                 {
                     // Handle the errors in a way appropriate to your application.
                     MessageBox.Show("Errors were encountered during field validation.");
                 }    // Find the shape field.

                 String shapeFieldName = sourceFeatureClass.ShapeFieldName;
                 int shapeFieldIndex = sourceFeatureClass.FindField(shapeFieldName);
                 IField shapeField = sourceFields.get_Field(shapeFieldIndex);


                 // Get the geometry definition from the shape field and clone it.
                 IGeometryDef geometryDef = shapeField.GeometryDef;
                 IClone geometryDefClone = (IClone)geometryDef;
                 IClone targetGeometryDefClone = geometryDefClone.Clone();
                 IGeometryDef targetGeometryDef = (IGeometryDef)targetGeometryDefClone;

                 // Cast the IGeometryDef to the IGeometryDefEdit interface.
                 IGeometryDefEdit targetGeometryDefEdit = (IGeometryDefEdit)targetGeometryDef;
                 // Set the IGeometryDefEdit properties.
                 targetGeometryDefEdit.GridCount_2 = 1;
                 targetGeometryDefEdit.set_GridSize(0, 0.75);

                 IFeatureDataConverter featureDataConverter = new FeatureDataConverterClass();
                 IEnumInvalidObject enumInvalidObject = featureDataConverter.ConvertFeatureClass
                     (sourceFeatureClassName, null, null, targetFeatureClassName,
                     targetGeometryDef, targetFields, "", 1000, 0);    // Check for errors.
                 IInvalidObjectInfo invalidObjectInfo = null; enumInvalidObject.Reset();


        }

2.合並,在ArcGIS中采用Python:

   可以參考http://blog.csdn.net/esrichinacd/article/details/14146653

最后需要注意的地方是:在10.2的ArcMap中執行時會如下錯誤

 

    我也是檢查了好長時間,原因是10.2的ArcMap中執行結果會自動添加到ArcMap中,即使右鍵取消“添加至結果”也不行。(導致了第二次循環的時候合並的數據是結果集相同的數據的合並,所以會報上面錯誤) 

 

所以這里,我們執行的時候可以到ArcCatalog中執行python腳本:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM