不同gdb,相同数据集合并

摘要:
众所周知,数据处理是GIS中一项重要而乏味的工作,处理数据的工具和方法太多。在进行数据处理时,我们经常会遇到以下问题:合并和处理存储在不同gdb中的数据,数据集名称相同:如图所示:数据组织如下,每个gdb存储多个FeatureClass列。想法是:1。首先处理每个gdb中的数据,使每个gdb的featureclass的数量和名称相同。

众所周知,数据处理是GIS中一项重要且繁琐的工作,处理数据的工具和方法也太多了,在做数据处理的时候,经常会遇到这样的问题:对存储在不同gdb中、并且数据集名称相同的数据进行合并处理:

如图:数据组织如下,每个gdb中都存储了一些列FeatureClass,(但gdb中的FeatureClass数量并不相同)

不同gdb,相同数据集合并第1张

思路是:

1.先对每个gdb中的数据进行处理,使得每个gdb中的featureclass数量和名称相同。由于对Engine比较熟悉,这里我是用Engine进行处理的,具体代码如下:

private function Execute(){  //初始执行函数:
     string templatePath = @"F:	estout";
     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:	estout" + gdbName, FeatureClassName);//判断FeatureClass是否存在
         string path = @"F:	estout" + 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:DataShapefiles存储了所有的要合并的FeatureClass的空图层,便于拷贝。

 private void convert()
        {
                 IWorkspaceName sourceWorkspaceName = new WorkspaceNameClass
                 {
                     WorkspaceFactoryProgID = "esriDataSourcesFile.ShapefileWorkspaceFactory",
                     PathName = @"D:DataShapefiles"
                 };
                 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:DataPublic.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中执行时会如下错误

  不同gdb,相同数据集合并第2张

    我也是检查了好长时间,原因是10.2的ArcMap中执行结果会自动添加到ArcMap中,即使右键取消“添加至结果”也不行。(导致了第二次循环的时候合并的数据是结果集相同的数据的合并,所以会报上面错误) 

  不同gdb,相同数据集合并第3张

所以这里,我们执行的时候可以到ArcCatalog中执行python脚本:

不同gdb,相同数据集合并第4张

免责声明:文章转载自《不同gdb,相同数据集合并》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇linux运行jar、nohup: ignoring input and redirecting stderr to stdout阿里云服务器完整修改主机名教程下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

第3课-CoreDump程序故障分析

1.定义在程序运行过程中,突然出现错误。然后Linux会把程序出错时的内存内容存放到一个Core文件中,这个过程就叫Core Dump。就叫核心转存。2.主要用途:段错误,产生段错误的地方主要有(1)数组访问越界(2)访问空指针(3)栈溢出(4)修改制度内存3.使能CoreDump打开 ulimit -c unlimited关闭ulimit -c 04.在...

嵌入式Linux应用程序开发

1.Linux成员命令的使用。 切换超级用户:su 例:su - root 用户管理:useradd:添加用户账号 passwd:设置账号密码 例:useradd liu;passwd liu 显示进程:ps:显示当前用户运行进程列表 例:ps -ef 杀死进程:kill:输出特定的信号给特定的PID(进程号) 例:kill -9 7421(kill -9...

GDB调试方法

一、多线程调试 多线程调试可能是问得最多的。其实,重要就是下面几个命令: info thread 查看当前进程的线程。 thread <ID> 切换调试的线程为指定ID的线程。 break file.c:100 thread all  在file.c文件第100行处为所有经过这里的线程设置断点。 set scheduler-locki...

GDB下查看内存命令(x命令)

http://blog.csdn.net/allenlinrui/article/details/5964046 可以使用examine命令(简写是x)来查看内存地址中的值。x命令的语法如下所示: x/<n/f/u> <addr> n、f、u是可选的参数。 n是一个正整数,表示需要显示的内存单元的个数,也就是说从当前地址向后显示几个...

GDB调试的layout使用

https://blog.csdn.net/zhangjs0322/article/details/10152279 layout:用于分割窗口,可以一边查看代码,一边测试。主要有以下几种用法:layout src:显示源代码窗口layout asm:显示汇编窗口layout regs:显示源代码/汇编和寄存器窗口layout split:显示源代码和汇编...

解析IOS控制台利用GDB命令查看报错堆栈

解析IOS控制台利用GDB命令查看报错堆栈是本文要介绍的内容,在ios开发中,如果提前释放一个指针的内存,在以后还继续使用这个指针,那么程序会立刻crash掉,而且很难有报错信息,我以前都是靠猜测去判断错误的原因,我们应该利用工具去找到错误的地方,然后快速准确的定位到错误地方,及其错误原因,最后进行改进。 其实ios控制台,提供这种机制,如果你选择debu...