不一樣gdb,相同數據集合並

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

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

思路是:app

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:ui

   能夠參考http://blog.csdn.net/esrichinacd/article/details/14146653spa

最後須要注意的地方是:在10.2的ArcMap中執行時會以下錯誤.net

 

    我也是檢查了好長時間,緣由是10.2的ArcMap中執行結果會自動添加到ArcMap中,即便右鍵取消「添加至結果」也不行。(致使了第二次循環的時候合併的數據是結果集相同的數據的合併,因此會報上面錯誤) code

 

因此這裏,咱們執行的時候能夠到ArcCatalog中執行python腳本:blog

相關文章
相關標籤/搜索