Ejemplo n.º 1
0
        public static void CopyPasteGeodatabaseData(IWorkspace sourceWorkspace, IWorkspace targetWorkspace, string objectName, esriDatasetType esriDataType)
        {
            // Validate input

            if ((sourceWorkspace.Type == esriWorkspaceType.esriFileSystemWorkspace) || (targetWorkspace.Type == esriWorkspaceType.esriFileSystemWorkspace))
            {
                return; // Should be a throw
            }
            //create source workspace name

            IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace;

            IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName;

            //create target workspace name

            IDataset targetWorkspaceDataset = (IDataset)targetWorkspace;

            IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName;

            //Create Name Object Based on data type

            IDatasetName datasetName;

            switch (esriDataType)
            {
            case esriDatasetType.esriDTFeatureDataset:

                IFeatureDatasetName inFeatureDatasetName = new FeatureDatasetNameClass();

                datasetName          = (IDatasetName)inFeatureDatasetName;
                inFeatureDatasetName = null;
                break;

            case esriDatasetType.esriDTFeatureClass:

                IFeatureClassName inFeatureClassName = new FeatureClassNameClass();

                datasetName        = (IDatasetName)inFeatureClassName;
                inFeatureClassName = null;

                break;

            case esriDatasetType.esriDTTable:

                ITableName inTableName = new TableNameClass();

                datasetName = (IDatasetName)inTableName;
                inTableName = null;

                break;

            case esriDatasetType.esriDTGeometricNetwork:

                IGeometricNetworkName inGeometricNetworkName = new GeometricNetworkNameClass();

                datasetName            = (IDatasetName)inGeometricNetworkName;
                inGeometricNetworkName = null;

                break;

            case esriDatasetType.esriDTRelationshipClass:

                IRelationshipClassName inRelationshipClassName = new RelationshipClassNameClass();

                datasetName             = (IDatasetName)inRelationshipClassName;
                inRelationshipClassName = null;
                break;

            case esriDatasetType.esriDTNetworkDataset:

                INetworkDatasetName inNetworkDatasetName = new NetworkDatasetNameClass();

                datasetName          = (IDatasetName)inNetworkDatasetName;
                inNetworkDatasetName = null;

                break;

            case esriDatasetType.esriDTTopology:

                ITopologyName inTopologyName = new TopologyNameClass();

                datasetName    = (IDatasetName)inTopologyName;
                inTopologyName = null;

                break;

            default:

                return;     // Should be a throw
            }

            // Set the name of the object to be copied

            datasetName.WorkspaceName = (IWorkspaceName)sourceWorkspaceName;

            datasetName.Name = objectName;

            //Setup mapping for copy/paste

            IEnumNameMapping fromNameMapping;

            ESRI.ArcGIS.esriSystem.IEnumNameEdit editFromName;

            ESRI.ArcGIS.esriSystem.IEnumName fromName = new NamesEnumerator();

            editFromName = (ESRI.ArcGIS.esriSystem.IEnumNameEdit)fromName;

            editFromName.Add((ESRI.ArcGIS.esriSystem.IName)datasetName);

            ESRI.ArcGIS.esriSystem.IName toName = (ESRI.ArcGIS.esriSystem.IName)targetWorkspaceName;

            // Generate name mapping


            ESRI.ArcGIS.Geodatabase.IGeoDBDataTransfer geoDBDataTransfer = new GeoDBDataTransferClass();

            bool targetWorkspaceExists;

            targetWorkspaceExists = geoDBDataTransfer.GenerateNameMapping(fromName, toName, out fromNameMapping);

            // Copy/Pate the data

            geoDBDataTransfer.Transfer(fromNameMapping, toName);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(geoDBDataTransfer);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(datasetName);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(fromName);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(editFromName);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(fromNameMapping);
            geoDBDataTransfer = null;
            datasetName       = null;
            editFromName      = null;
            fromName          = null;

            sourceWorkspaceDataset = null;
            sourceWorkspaceName    = null;
            targetWorkspaceDataset = null;
            targetWorkspaceName    = null;
            toName          = null;
            fromNameMapping = null;
        }
Ejemplo n.º 2
0
        public void TansferFileGDBToSDE(string fileGDBPath, string sourceFCName)
        {
            // Create workspace name objects.
            IWorkspaceName sourceWorkspaceName = getWorkspaceName("esriDataSourcesGDB.FileGDBWorkspaceFactory", fileGDBPath);

            // Create a name object for the target (SDE) workspace and open it.
            IWorkspaceName targetWorkspaceName = getWorkspaceName("esriDataSourcesGDB.SdeWorkspaceFactory", sdePath);

            IName targetName = (IName)targetWorkspaceName;

            // Create a name object for the source dataset.
            IFeatureClassName sourceFeatureClassName = getFeatureClassName(sourceWorkspaceName, sourceFCName);
            IName             sourceName             = (IName)sourceFeatureClassName;

            // Create an enumerator for source datasets.
            IEnumName     sourceEnumName     = new NamesEnumeratorClass();
            IEnumNameEdit sourceEnumNameEdit = (IEnumNameEdit)sourceEnumName;

            // Add the name object for the source class to the enumerator.
            sourceEnumNameEdit.Add(sourceName);

            // Create a GeoDBDataTransfer object and a null name mapping enumerator.
            IGeoDBDataTransfer geoDBDataTransfer = new GeoDBDataTransferClass();
            IEnumNameMapping   enumNameMapping   = null;

            // Use the data transfer object to create a name mapping enumerator.
            Boolean conflictsFound = geoDBDataTransfer.GenerateNameMapping(sourceEnumName,
                                                                           targetName, out enumNameMapping);

            enumNameMapping.Reset();

            // Check for conflicts.
            if (conflictsFound)
            {
                // Iterate through each name mapping.
                INameMapping nameMapping = null;
                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);
                            }
                        }
                    }
                }
            }

            // Start the transfer.
            geoDBDataTransfer.Transfer(enumNameMapping, targetName);
        }
Ejemplo n.º 3
0
        private void btnOK_Click(object sender, EventArgs e)
        {
            //首先进行坐标系x,y变换
            //string fileExpTran;//进行x,y做表转换后输出的tiff文件存储路径,用这一文件在进行后期的Z转换
            //fileExpTran = System.IO.Path.GetDirectoryName(LayerExpName) +"\\"+ System.IO.Path.GetFileNameWithoutExtension(LayerExpName)+"XY.tif";
            //try
            //{
            //    if (NorthEastToEastNorth(pRasterLayer, LayerExpName))
            //    {
            //        RasterLayerClass rasterlayer = new RasterLayerClass();
            //        rasterlayer.CreateFromFilePath(LayerExpName);
            //        IRaster2 pRaster2 = rasterlayer.Raster as IRaster2;
            //        IRasterDataset2 pRasterDataset = pRaster2.RasterDataset as IRasterDataset2;
            //        ChangeRasterValue(pRasterDataset, -1, 0);
            //        pMapControl.AddLayer(rasterlayer as ILayer);
            //        this.Close();
            //    }
            //}
            //catch (System.Exception ex)
            //{
            //    MessageBox.Show(ex.Message);
            //}

            try
            {
                IFeatureClass    pFC        = pFeatureLayer.FeatureClass;
                IDataset         pDS        = pFC as IDataset;
                IWorkspace       pWS        = pDS.Workspace;
                string           filedir    = pWS.PathName;
                string           fdestname  = System.IO.Path.GetFileNameWithoutExtension(txtFeatureName.Text);
                ClsGDBDataCommon CGD        = new ClsGDBDataCommon();
                IWorkspace       pTargetWS  = CGD.OpenFromShapefile(txtLayerExp.Text);
                IWorkspace2      workspace2 = pTargetWS as IWorkspace2;

                if (workspace2.get_NameExists(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTFeatureClass, fdestname))
                {
                    MessageBox.Show("目标文件已存在!");
                    return;
                }
                //shape文件直接拷贝后再修改
                if (pDS.CanCopy() == true)
                {
                    pDS.Copy(txtFeatureName.Text, pTargetWS);
                    IFeatureWorkspace pFW = pTargetWS as IFeatureWorkspace;
                    pTargetFeatureClass = pFW.OpenFeatureClass(txtFeatureName.Text);
                    TransCoordiante(pFW as IWorkspace, pTargetFeatureClass);
                }
                //表示为gdb的feature,只能往gdb拷贝
                else
                {
                    // Create workspace name objects.
                    IWorkspaceName sourceWorkspaceName = new WorkspaceNameClass();
                    IWorkspaceName targetWorkspaceName = new WorkspaceNameClass();
                    IName          targetName          = (IName)targetWorkspaceName;

                    // Set the workspace name properties.
                    sourceWorkspaceName.PathName = pWS.PathName;
                    sourceWorkspaceName.WorkspaceFactoryProgID =
                        "esriDataSourcesGDB.FileGDBWorkspaceFactory";
                    // targetWorkspaceName.PathName = @"PartialMontgomery.gdb";
                    targetWorkspaceName.PathName = txtLayerExp.Text;
                    targetWorkspaceName.WorkspaceFactoryProgID =
                        "esriDataSourcesGDB.FileGDBWorkspaceFactory";

                    //if (txtLayerExp.Text.Length>4 && txtLayerExp.Text.Substring(txtLayerExp.Text.Length-4,4) == ".gdb")
                    //{
                    //    targetWorkspaceName.WorkspaceFactoryProgID =
                    //      "esriDataSourcesGDB.FileGDBWorkspaceFactory";
                    //}
                    //else
                    //{
                    //    targetWorkspaceName.WorkspaceFactoryProgID =
                    //     "esriDataSourcesFile.ShapefileWorkspaceFactory";
                    //}

                    // Create a name object for the source feature class.
                    IFeatureClassName featureClassName = new FeatureClassNameClass();

                    // Set the featureClassName properties.
                    IDatasetName sourceDatasetName = (IDatasetName)featureClassName;
                    sourceDatasetName.WorkspaceName = sourceWorkspaceName;
                    sourceDatasetName.Name          = pDS.BrowseName;
                    IName sourceName = (IName)sourceDatasetName;


                    // Create an enumerator for source datasets.
                    IEnumName     sourceEnumName     = new NamesEnumeratorClass();
                    IEnumNameEdit sourceEnumNameEdit = (IEnumNameEdit)sourceEnumName;

                    // Add the name object for the source class to the enumerator.
                    sourceEnumNameEdit.Add(sourceName);

                    // Create a GeoDBDataTransfer object and a null name mapping enumerator.
                    IGeoDBDataTransfer geoDBDataTransfer = new GeoDBDataTransferClass();
                    IEnumNameMapping   enumNameMapping   = null;

                    // Use the data transfer object to create a name mapping enumerator.
                    Boolean conflictsFound = geoDBDataTransfer.GenerateNameMapping(sourceEnumName,
                                                                                   targetName, out enumNameMapping);
                    enumNameMapping.Reset();
                    //修改拷贝的文件名
                    INameMapping nameMapping = enumNameMapping.Next();
                    if ((nameMapping != null))
                    {
                        nameMapping.TargetName = txtFeatureName.Text;
                    }

                    // Check for conflicts.
                    //if (conflictsFound)
                    //{
                    //    // Iterate through each name mapping.
                    //    INameMapping nameMapping = null;
                    //    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);
                    //                }
                    //            }
                    //        }
                    //    }
                    //}

                    // Start the transfer.
                    geoDBDataTransfer.Transfer(enumNameMapping, targetName);
                    IFeatureWorkspace pFW = CGD.OpenFromFileGDB(txtLayerExp.Text) as IFeatureWorkspace;
                    pTargetFeatureClass = pFW.OpenFeatureClass(txtFeatureName.Text);
                    TransCoordiante(pFW as IWorkspace, pTargetFeatureClass);
                }

                if (pTargetFeatureClass != null)
                {
                    //添加到图层中
                    IFeatureLayer featureLayer = new FeatureLayerClass();
                    featureLayer.FeatureClass = pTargetFeatureClass;
                    featureLayer.Name         = fdestname;
                    pMapControl.AddLayer(featureLayer as ILayer);
                    pMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
                }

                this.DialogResult = DialogResult.OK;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
 /// <summary>
 /// copies the feature Class table from one database to another
 /// </summary>
 /// <param name="inWks">inWorkspace</param>
 /// <param name="inObjName">object name</param>
 /// <param name="outWks">out workspace</param>
 /// <param name="dType">data type</param>
 /// <returns>messages</returns>
 public string copyFeatureClassTable(IWorkspace inWks, string inObjName, IWorkspace outWks, esriDatasetType dType)
 {
     if (!(dType == esriDatasetType.esriDTFeatureClass || dType == esriDatasetType.esriDTTable))
     {
         return "Error: must specify a FeatureClass or Table";
     }
     if (!((IWorkspace2)inWks).get_NameExists(dType, inObjName))
     {
         return "Error: Parent object does not exist!";
     }
     Console.WriteLine(inObjName);
     string x = "successfully copied " + inObjName;
     try
     {
         IDataset outWksDset = (IDataset)outWks;
         IName targetWks = outWksDset.FullName;
         IEnumName eName = new NamesEnumeratorClass();
         IEnumNameEdit eNameEdit = (IEnumNameEdit)eName;
         //need to get name of table
         IFeatureWorkspace ftrWks = (IFeatureWorkspace)inWks;
         IObjectClass objCls;
         if (dType == esriDatasetType.esriDTTable)
         {
             ITable inTbl = ftrWks.OpenTable(inObjName);
             objCls = (IObjectClass)inTbl;
         }
         else
         {
             IFeatureClass inFtr = ftrWks.OpenFeatureClass(inObjName);
             objCls = (IObjectClass)inFtr;
         }
         IDataset tblDset = (IDataset)objCls;
         eNameEdit.Add(tblDset.FullName);
         IGeoDBDataTransfer geoTrans = new GeoDBDataTransferClass();
         IEnumNameMapping enumNameMapping = null;
         Boolean conflictsFound = geoTrans.GenerateNameMapping(eName, targetWks, out enumNameMapping);
         enumNameMapping.Reset();
         try
         {
             if (conflictsFound)
             {
                 Console.WriteLine("Conflicts found can't transfer");
             }
             else
             {
                 geoTrans.Transfer(enumNameMapping, targetWks);
             }
         }
         catch (Exception e)
         {
             x = "Error in trasfering data: " + e.ToString();
             MessageBox.Show(x);
         }
     }
     catch (Exception e)
     {
         x = "Error: " + e.ToString();
         Console.WriteLine(x);
     }
     return x;
 }
Ejemplo n.º 5
0
        public static void TranferBetweenGeodatabase(string featureClass, string targetPath, string sourcePath)
        {
            // Create new workspace name objects.
            IWorkspaceName sourceWorkspaceName = new WorkspaceNameClass();
            IWorkspaceName targetWorkspaceName = new WorkspaceNameClass();
            IName targetName = (IName)targetWorkspaceName;

            // Set the workspace name properties.
            sourceWorkspaceName.PathName = @sourcePath;//"C:\arcgis\ArcTutor\BuildingaGeodatabase\Montgomery.gdb";
            sourceWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesGDB.FileGDBWorkspaceFactory";
            targetWorkspaceName.PathName = @targetPath;//"PartialMontgomery.gdb";
            targetWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesGDB.FileGDBWorkspaceFactory";

            // Create a name object for the source feature class.
            IFeatureClassName featureClassName = new FeatureClassNameClass();

            // Set the featureClassName properties.
            IDatasetName sourceDatasetName = (IDatasetName)featureClassName;
            sourceDatasetName.WorkspaceName = sourceWorkspaceName;
            sourceDatasetName.Name = featureClass;//"Blocks";
            IName sourceName = (IName)sourceDatasetName;

            // Create an enumerator for source datasets.
            IEnumName sourceEnumName = new NamesEnumeratorClass();
            IEnumNameEdit sourceEnumNameEdit = (IEnumNameEdit)sourceEnumName;

            // Add the name object for the source class to the enumerator.
            sourceEnumNameEdit.Add(sourceName);

            // Create a GeoDBDataTransfer object and a null name mapping enumerator.
            IGeoDBDataTransfer geoDBDataTransfer = new GeoDBDataTransferClass();
            IEnumNameMapping enumNameMapping = null;

            // Use the data transfer object to create a name mapping enumerator.
            Boolean conflictsFound = geoDBDataTransfer.GenerateNameMapping(sourceEnumName,
              targetName, out enumNameMapping);
            enumNameMapping.Reset();

            // Check for conflicts.
            if (conflictsFound)
            {
                // Iterate through each name mapping.
                INameMapping nameMapping = null;
                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);
                            }
                        }
                    }
                }
            }

            // Start the transfer.
            geoDBDataTransfer.Transfer(enumNameMapping, targetName);
        }