Ejemplo n.º 1
0
        private void ImportXmlWorkspaceDocument(string inputXmlFile)
        {
            IWorkspaceName targetWorkspaceName = getWorkspaceName("esriDataSourcesGDB.SdeWorkspaceFactory", sdePath);
            IName          targetName          = (IName)targetWorkspaceName;
            IWorkspace     targetWorkspace     = (IWorkspace)targetName.Open();

            IGdbXmlImport    gdbXmlImport    = new GdbImporterClass();
            IEnumNameMapping enumNameMapping = null;

            Boolean conflictsFound = gdbXmlImport.GenerateNameMapping(inputXmlFile, targetWorkspace, out enumNameMapping);

            // Check for conflicts.
            if (conflictsFound)
            {
                // Iterate through each name mapping.
                INameMapping nameMapping = null;
                enumNameMapping.Reset();
                while ((nameMapping = enumNameMapping.Next()) != null)
                {
                    // Resolve the mapping's conflict (if there is one).
                    if (nameMapping.NameConflicts)
                    {
                        nameMapping.TargetName = nameMapping.GetSuggestedName(targetName);
                    }

                    // See if the mapping's children have conflicts.
                    IEnumNameMapping childEnumNameMapping = nameMapping.Children;
                    if (childEnumNameMapping != null)
                    {
                        childEnumNameMapping.Reset();

                        // Iterate through each child mapping.
                        INameMapping childNameMapping = null;
                        while ((childNameMapping = childEnumNameMapping.Next()) != null)
                        {
                            if (childNameMapping.NameConflicts)
                            {
                                childNameMapping.TargetName =
                                    childNameMapping.GetSuggestedName(targetName);
                            }
                        }
                    }
                }
            }

            // Import the workspace document, including both schema and data.
            gdbXmlImport.ImportWorkspace(inputXmlFile, enumNameMapping, targetWorkspace, false);
        }
Ejemplo n.º 2
0
        public static ESRI.ArcGIS.Geodatabase.IWorkspace GetWorkspace(string inXMLFile)
        {
            IWorkspaceFactory inMemoryWorkspaceFactory = new InMemoryWorkspaceFactoryClass();
            IWorkspaceName    workspaceName            = inMemoryWorkspaceFactory.Create("", "InMemory", null, 0);
            IName             name      = workspaceName as IName;
            IWorkspace        workspace = (IWorkspace)name.Open();

            //Create a GDB importer
            IGdbXmlImport xmlImport = new GdbImporterClass();

            // Read schema section from the XML file.
            // Note: enumNameMapping is initialized to nothing and its value is set 'by ref' with a call to the GenerateNameMapping method.
            IEnumNameMapping eNameMapping = null;

            xmlImport.GenerateNameMapping(inXMLFile, workspace, out eNameMapping);

            //Create the workspace schema and load the data
            xmlImport.ImportWorkspace(inXMLFile, eNameMapping, workspace, false);

            return(workspace);
        }
Ejemplo n.º 3
0
        public List <string> CheckDataSchema(string template, string dbpath)
        {
            List <string> msgs = new List <string>();

            try
            {
                Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.AccessWorkspaceFactory");
                IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance
                                                         (factoryType);

                IFeatureWorkspace source_fws = workspaceFactory.OpenFromFile(dbpath, 0) as IFeatureWorkspace;

                IScratchWorkspaceFactory target_wsf = new FileGDBScratchWorkspaceFactoryClass();
                IWorkspace target_ws = target_wsf.CreateNewScratchWorkspace();

                IGdbXmlImport    importer   = new GdbImporterClass();
                IEnumNameMapping mapping    = null;
                bool             isConflict = false;
                isConflict = importer.GenerateNameMapping(template, target_ws, out mapping);
                importer.ImportWorkspace(template, mapping, target_ws, true);

                IDataset      ds      = target_ws as IDataset;
                List <String> fcNames = new List <string>();
                Util.GetAllFeatureClassNames(ds, ref fcNames);

                IFeatureWorkspace target_fws = target_ws as IFeatureWorkspace;
                IWorkspace2       source_ws  = source_fws as IWorkspace2;
                foreach (string fcName in fcNames)
                {
                    if (!source_ws.get_NameExists(esriDatasetType.esriDTFeatureClass, fcName))
                    {
                        msgs.Add(string.Format("数据源中丢失图层:{0}", fcName));
                        continue;
                    }
                    IFeatureClass source_fc = source_fws.OpenFeatureClass(fcName);
                    IFeatureClass target_fc = target_fws.OpenFeatureClass(fcName);
                    for (int i = 0; i < target_fc.Fields.FieldCount; i++)
                    {
                        IField fld = target_fc.Fields.get_Field(i);
                        if (fcName == target_fc.OIDFieldName)
                        {
                            continue;
                        }
                        int source_fld_idx = source_fc.FindField(fld.Name);
                        if (source_fld_idx == -1)
                        {
                            msgs.Add(string.Format("数据源中丢失字段{0}", fld.Name));
                        }
                        else
                        {
                            IField source_fld = source_fc.Fields.get_Field(source_fld_idx);
                            if (source_fld.Type != fld.Type)
                            {
                                msgs.Add(string.Format("数据源中字段{0}类型不正确", fld.Name));
                            }
                            if (source_fld.Length != fld.Length)
                            {
                                msgs.Add(string.Format("数据源中字段{0}长度不正确", fld.Name));
                            }
                            if (source_fld.Precision != fld.Precision)
                            {
                                msgs.Add(string.Format("数据源中字段{0}精度不正确", fld.Name));
                            }
                        }
                    }
                    if (source_fc.FindField("SyncID") == -1)
                    {
                        msgs.Add(@"数据源中丢失系统字段SyncID");
                    }
                    if (source_fc.FindField("SyncTimeStamp") == -1)
                    {
                        msgs.Add(@"数据源中丢失系统字段SyncTimeStamp");
                    }
                    if (source_fc.FindField("SyncStatus") == -1)
                    {
                        msgs.Add(@"数据源中丢失系统字段SyncStatus");
                    }
                    if (source_fc.FindField("SyncEditable") == -1)
                    {
                        msgs.Add(@"数据源中丢失系统字段SyncEditable");
                    }
                }
            }
            catch
            { }
            return(msgs);
        }
Ejemplo n.º 4
0
        public bool CheckOut(IWorkspace source, string dir, string dbname, string template, IPolygon area, string taskName, string dept)
        {
            m_message = "";
            //检查文件是否已经存在
            string mdbpath = dir + @"\" + dbname + ".mdb";
            bool   isExist = File.Exists(mdbpath);

            if (isExist)
            {
                File.Delete(mdbpath);
            }

            Type factoryType = Type.GetTypeFromProgID(
                "esriDataSourcesGDB.AccessWorkspaceFactory");
            IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance
                                                     (factoryType);

            IWorkspaceName workspaceName = workspaceFactory.Create(dir, dbname,
                                                                   null, 0);

            IWorkspace workspace = workspaceFactory.OpenFromFile(mdbpath, 0);

            //导入库结构
            IGdbXmlImport    importer   = new GdbImporterClass();
            IEnumNameMapping mapping    = null;
            bool             isConflict = false;

            isConflict = importer.GenerateNameMapping(template, workspace, out mapping);
            importer.ImportWorkspace(template, mapping, workspace, true);


            IDataset      ds      = workspace as IDataset;
            List <String> fcNames = new List <string>();

            Util.GetAllFeatureClassNames(ds, ref fcNames);

            IFeatureWorkspace source_ws = source as IFeatureWorkspace;
            IFeatureWorkspace target_ws = workspace as IFeatureWorkspace;
            IWorkspaceEdit    wse       = target_ws as IWorkspaceEdit;

            foreach (string fcname in fcNames)
            {
                IWorkspace2 source_ws2 = source_ws as IWorkspace2;
                if (!source_ws2.get_NameExists(esriDatasetType.esriDTFeatureClass, fcname))
                {
                    continue;
                }
                IFeatureClass source_fc = source_ws.OpenFeatureClass(fcname);
                IFeatureClass target_fc = target_ws.OpenFeatureClass(fcname);
                AddSyncFields(target_fc);

                ISpatialFilter filter = new SpatialFilterClass();
                filter.Geometry   = area as IGeometry;
                filter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;
                wse.StartEditing(false);
                IFeatureCursor target_cur = target_fc.Insert(true);
                IFeatureBuffer buffer     = target_fc.CreateFeatureBuffer();
                IFeatureCursor source_cur = source_fc.Search(filter, true);
                IFeature       source_fea = source_cur.NextFeature();
                while (source_fea != null)
                {
                    buffer.set_Value(target_fc.FindField("SyncID"), source_fea.OID);
                    for (int i = 0; i < source_fc.Fields.FieldCount; i++)
                    {
                        IField source_field = source_fc.Fields.get_Field(i);
                        if (source_field.Name != source_fc.OIDFieldName)
                        {
                            int target_field_index = target_fc.FindField(source_field.Name);
                            if (target_field_index != -1)
                            {
                                object source_value = source_fea.get_Value(source_fc.FindField(source_field.Name));
                                buffer.set_Value(target_field_index, source_value);
                            }
                        }
                    }
                    target_cur.InsertFeature(buffer);
                    source_fea = source_cur.NextFeature();
                }
                target_cur.Flush();
                wse.StopEditing(true);
            }

            IFeatureClass log_fc = source_ws.OpenFeatureClass("TaskLog");

            IWorkspaceEdit source_wse = source_ws as IWorkspaceEdit;

            source_wse.StartEditing(false);
            IFeature log_fea = log_fc.CreateFeature();

            log_fea.set_Value(log_fc.FindField("CheckOutDate"), DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
            log_fea.set_Value(log_fc.FindField("TaskName"), taskName);
            log_fea.set_Value(log_fc.FindField("Dept"), dept);
            log_fea.set_Value(log_fc.FindField("Status"), Task.TaskManager.CHECKOUT_STATUS);
            log_fea.Shape = area;
            log_fea.Store();
            source_wse.StopEditing(true);

            return(true);
        }
Ejemplo n.º 5
0
        private void ImportXmlWorkspaceDocument(String fileGdbPath, String workspaceDocPath)
        {
            // Open the target file geodatabase and create a name object for it.

            Type factoryType = Type.GetTypeFromProgID(
                "esriDataSourcesGDB.FileGDBWorkspaceFactory");
            IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance
                (factoryType);
            IWorkspace workspace = workspaceFactory.OpenFromFile(fileGdbPath, 0);
            IDataset workspaceDataset = (IDataset)workspace;
            IName workspaceName = workspaceDataset.FullName;

            // Create a GdbImporter and use it to generate name mappings.
            IGdbXmlImport gdbXmlImport = new GdbImporterClass();
            IEnumNameMapping enumNameMapping = null;
            Boolean conflictsFound = gdbXmlImport.GenerateNameMapping(workspaceDocPath,
                workspace, out enumNameMapping);

            // Check for conflicts.
            if (conflictsFound)
            {
                // Iterate through each name mapping.
                INameMapping nameMapping = null;
                enumNameMapping.Reset();
                while ((nameMapping = enumNameMapping.Next()) != null)
                {
                    // Resolve the mapping's conflict (if there is one).
                    if (nameMapping.NameConflicts)
                    {
                        nameMapping.TargetName = nameMapping.GetSuggestedName(workspaceName);
                    }

                    // See if the mapping's children have conflicts.
                    IEnumNameMapping childEnumNameMapping = nameMapping.Children;
                    if (childEnumNameMapping != null)
                    {
                        childEnumNameMapping.Reset();

                        // Iterate through each child mapping.
                        INameMapping childNameMapping = null;
                        while ((childNameMapping = childEnumNameMapping.Next()) != null)
                        {
                            if (childNameMapping.NameConflicts)
                            {
                                childNameMapping.TargetName =
                                    childNameMapping.GetSuggestedName(workspaceName);
                            }
                        }
                    }
                }
            }

            // Import the workspace document, including both schema and data.
            gdbXmlImport.ImportWorkspace(workspaceDocPath, enumNameMapping, workspace, false);
        }
Ejemplo n.º 6
0
        private void ImportXMLToGDB(string xmlFileName, string gdbName)
        {
            this.textBox3.Text += "xmlFileName:" + System.IO.Path.GetFileName( xmlFileName)+"\n";
            this.textBox3.SelectionStart = this.textBox3.Text.Length;
            this.textBox3.ScrollToCaret();
            IWorkspaceFactory pWSF = new FileGDBWorkspaceFactoryClass();
            IWorkspace pWS = pWSF.OpenFromFile(gdbName,0);

            IGdbXmlImport pImport = new GdbImporterClass();

            IEnumNameMapping pEnumNM = null;
            bool bHasConflicts = pImport.GenerateNameMapping(xmlFileName, pWS, out pEnumNM);
            pImport.ImportWorkspace(xmlFileName, pEnumNM, pWS, false);
        }
Ejemplo n.º 7
0
        public static void ExportDatabase(IWorkspace theWorkspace)
        {
            // Try and make sure the user understands what they're doing
            MessageBox.Show("You must first browse to a previously generated, empty geodatabase.", "NCGMP Tools");

            // Browse for a file, personal or SDE geodatabase
            IWorkspaceFactory wsFact          = null;
            IWorkspace        openedWorkspace = null;

            IGxObjectFilter objectFilter = new GxFilterWorkspaces();
            IGxObject       openedObject = commonFunctions.OpenArcFile(objectFilter, "Please select an empty database");

            if (openedObject == null)
            {
                return;
            }

            // Check to see if it is a File, Personal or SDE database, create appropriate workspace factory
            string pathToOpen = null;

            switch (openedObject.Category)
            {
            case "Personal Geodatabase":
                wsFact     = new AccessWorkspaceFactoryClass();
                pathToOpen = openedObject.FullName;
                break;

            case "File Geodatabase":
                wsFact     = new FileGDBWorkspaceFactoryClass();
                pathToOpen = openedObject.FullName;
                break;

            case "Spatial Database Connection":
                wsFact = new SdeWorkspaceFactoryClass();
                IGxRemoteDatabaseFolder remoteDatabaseFolder = (IGxRemoteDatabaseFolder)openedObject.Parent;
                pathToOpen = remoteDatabaseFolder.Path + openedObject.Name;
                break;

            default:
                break;
            }
            openedWorkspace = wsFact.OpenFromFile(pathToOpen, 0);

            // Okay, now export the current database to an XML doc
            string tempFilePath = System.IO.Path.GetTempFileName();

            tempFilePath += ".xml";

            IGdbXmlExport xmlExporter = new GdbExporterClass();

            xmlExporter.ExportWorkspace(theWorkspace, tempFilePath, true, false, false);

            // Import to the new database
            // Use the temp file to perform the import
            IGdbXmlImport    xmlImporter     = new GdbImporterClass();
            IEnumNameMapping enumNameMapping = null;
            bool             conflictsFound  = xmlImporter.GenerateNameMapping(tempFilePath, openedWorkspace, out enumNameMapping);

            try
            {
                // Deal with conflicts (lifted wholesale from http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/d/00010000011m000000.htm)
                if (conflictsFound)
                {
                    IName workspaceName = ((IDataset)openedWorkspace).FullName;

                    // Iterate through each name mapping.
                    INameMapping nameMapping = null;
                    enumNameMapping.Reset();
                    while ((nameMapping = enumNameMapping.Next()) != null)
                    {
                        // Resolve the mapping's conflict (if there is one).
                        if (nameMapping.NameConflicts)
                        {
                            nameMapping.TargetName = nameMapping.GetSuggestedName(workspaceName);
                        }
                        // See if the mapping's children have conflicts.
                        IEnumNameMapping childEnumNameMapping = nameMapping.Children;
                        if (childEnumNameMapping != null)
                        {
                            childEnumNameMapping.Reset();
                            // Iterate through each child mapping.
                            INameMapping childNameMapping = null;
                            while ((childNameMapping = childEnumNameMapping.Next()) != null)
                            {
                                if (childNameMapping.NameConflicts)
                                {
                                    childNameMapping.TargetName = nameMapping.GetSuggestedName
                                                                      (workspaceName);
                                }
                            }
                        }
                    }
                }

                // Perform the import
                xmlImporter.ImportWorkspace(tempFilePath, enumNameMapping, openedWorkspace, false);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace + Environment.NewLine + Environment.NewLine + "XML Workspace Doc written to: " + tempFilePath);
            }


            // Perhaps, strip the SysInfo table
        }
Ejemplo n.º 8
0
        public static void ExportDatabase(IWorkspace theWorkspace)
        {
            // Try and make sure the user understands what they're doing
            MessageBox.Show("You must first browse to a previously generated, empty geodatabase.", "NCGMP Tools");

            // Browse for a file, personal or SDE geodatabase
            IWorkspaceFactory wsFact = null;
            IWorkspace openedWorkspace = null;

            IGxObjectFilter objectFilter = new GxFilterWorkspaces();
            IGxObject openedObject = commonFunctions.OpenArcFile(objectFilter, "Please select an empty database");
            if (openedObject == null) { return; }

            // Check to see if it is a File, Personal or SDE database, create appropriate workspace factory
            string pathToOpen = null;

            switch (openedObject.Category)
            {
                case "Personal Geodatabase":
                    wsFact = new AccessWorkspaceFactoryClass();
                    pathToOpen = openedObject.FullName;
                    break;
                case "File Geodatabase":
                    wsFact = new FileGDBWorkspaceFactoryClass();
                    pathToOpen = openedObject.FullName;
                    break;
                case "Spatial Database Connection":
                    wsFact = new SdeWorkspaceFactoryClass();
                    IGxRemoteDatabaseFolder remoteDatabaseFolder = (IGxRemoteDatabaseFolder)openedObject.Parent;
                    pathToOpen = remoteDatabaseFolder.Path + openedObject.Name;
                    break;
                default:
                    break;
            }
            openedWorkspace = wsFact.OpenFromFile(pathToOpen, 0);

            // Okay, now export the current database to an XML doc
            string tempFilePath = System.IO.Path.GetTempFileName();
            tempFilePath += ".xml";

            IGdbXmlExport xmlExporter = new GdbExporterClass();
            xmlExporter.ExportWorkspace(theWorkspace, tempFilePath, true, false, false);

            // Import to the new database
            // Use the temp file to perform the import
            IGdbXmlImport xmlImporter = new GdbImporterClass();
            IEnumNameMapping enumNameMapping = null;
            bool conflictsFound = xmlImporter.GenerateNameMapping(tempFilePath, openedWorkspace, out enumNameMapping);

            try
            {
                // Deal with conflicts (lifted wholesale from http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/d/00010000011m000000.htm)
                if (conflictsFound)
                {
                    IName workspaceName = ((IDataset)openedWorkspace).FullName;

                    // Iterate through each name mapping.
                    INameMapping nameMapping = null;
                    enumNameMapping.Reset();
                    while ((nameMapping = enumNameMapping.Next()) != null)
                    {
                        // Resolve the mapping's conflict (if there is one).
                        if (nameMapping.NameConflicts)
                        {
                            nameMapping.TargetName = nameMapping.GetSuggestedName(workspaceName);
                        }
                        // See if the mapping's children have conflicts.
                        IEnumNameMapping childEnumNameMapping = nameMapping.Children;
                        if (childEnumNameMapping != null)
                        {
                            childEnumNameMapping.Reset();
                            // Iterate through each child mapping.
                            INameMapping childNameMapping = null;
                            while ((childNameMapping = childEnumNameMapping.Next()) != null)
                            {
                                if (childNameMapping.NameConflicts)
                                {
                                    childNameMapping.TargetName = nameMapping.GetSuggestedName
                                        (workspaceName);
                                }
                            }
                        }
                    }
                }

                // Perform the import
                xmlImporter.ImportWorkspace(tempFilePath, enumNameMapping, openedWorkspace, false);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace + Environment.NewLine + Environment.NewLine + "XML Workspace Doc written to: " + tempFilePath);
            }

            // Perhaps, strip the SysInfo table
        }
Ejemplo n.º 9
0
        public static void ImportFromXml(IWorkspace targetWorkspace, string sourcePath, bool schemaOnly)
        {
            // Open the target File geodatabase and create a name object for it.
            IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactoryClass();
            //IWorkspace workspace = workspaceFactory.OpenFromFile("ImportXmlDoc.gdb", 0);
            IDataset workspaceDataset = (IDataset)targetWorkspace;
            IName workspaceName = workspaceDataset.FullName;

            // Create a GdbImporter and use it to generate name mappings.
            IGdbXmlImport gdbXmlImport = new GdbImporterClass();
            IEnumNameMapping enumNameMapping = null;
            Boolean conflictsFound = gdbXmlImport.GenerateNameMapping(sourcePath,
              targetWorkspace, out enumNameMapping);

            // Check for conflicts.
            if (conflictsFound)
            {
                // Iterate through each name mapping.
                INameMapping nameMapping = null;
                enumNameMapping.Reset();
                while ((nameMapping = enumNameMapping.Next()) != null)
                {
                    // Resolve the mapping's conflict (if there is one).
                    if (nameMapping.NameConflicts)
                    {
                        nameMapping.TargetName = nameMapping.GetSuggestedName(workspaceName);
                    }

                    // See if the mapping's children have conflicts.
                    IEnumNameMapping childEnumNameMapping = nameMapping.Children;
                    if (childEnumNameMapping != null)
                    {
                        childEnumNameMapping.Reset();

                        // Iterate through each child mapping.
                        INameMapping childNameMapping = null;
                        while ((childNameMapping = childEnumNameMapping.Next()) != null)
                        {
                            if (childNameMapping.NameConflicts)
                            {
                                childNameMapping.TargetName = childNameMapping.GetSuggestedName
                                  (workspaceName);
                            }
                        }
                    }
                }
            }

            // Import the workspace document, including both schema and data.
            gdbXmlImport.ImportWorkspace(sourcePath, enumNameMapping, targetWorkspace, schemaOnly);
        }