Example #1
0
        /// <summary>
        /// 获取远程地理工作空间(SDE)
        /// </summary>
        /// <param name="server">远程服务地址</param>
        /// <param name="instance">数据库实例</param>
        /// <param name="user">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="database">数据库</param>
        /// <param name="version">版本</param>
        /// <returns>sde工作空间</returns>
        public static IWorkspace GetWorkspace(String server, String instance, String user, String password, String database, String version)
        {
            IWorkspace workspace = null;

            // Create and populate the property set
            ESRI.ArcGIS.esriSystem.IPropertySet propertySet = new ESRI.ArcGIS.esriSystem.PropertySetClass();
            propertySet.SetProperty("SERVER", server);
            propertySet.SetProperty("INSTANCE", instance);
            propertySet.SetProperty("DATABASE", database);
            propertySet.SetProperty("USER", user);
            propertySet.SetProperty("PASSWORD", password);
            propertySet.SetProperty("VERSION", version);
            ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2 workspaceFactory;
            workspaceFactory = (IWorkspaceFactory2) new ESRI.ArcGIS.DataSourcesGDB.SdeWorkspaceFactoryClass();

            try
            {
                workspace = workspaceFactory.Open(propertySet, 0);
            }
            catch (Exception Ex)
            {
                MessageBox.Show("连接到地理空间数据库失败:\r\n" + Ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            return(workspace);
        }
        public System.DateTime GetFileGeodatabaseFeatureClassLastUpdate(string FileGeoDBPath, string FeatureClassName)
        {
            ESRI.ArcGIS.esriSystem.IPropertySet fileGeodatabasePropertySet = null;
              try
              {
            //  Build a Property Set for the File Geodatabase.
            fileGeodatabasePropertySet = new ESRI.ArcGIS.esriSystem.PropertySetClass();
            fileGeodatabasePropertySet.SetProperty("DATABASE", FileGeoDBPath);

            //  Get the Last Modified Date of the File Geodatabase Feature Class.
            return GetFileGeodatabaseFeatureClassLastUpdate(fileGeodatabasePropertySet, FeatureClassName);

              }
              catch (System.Exception caught)
              {
            //  Determine the Line Number from which the exception was thrown.
            System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(caught, true);
            System.Diagnostics.StackFrame stackFrame = stackTrace.GetFrame(stackTrace.FrameCount - 1);
            int lineNumber = stackFrame.GetFileLineNumber();

            //  Let the user know that this process failed.
            if (ErrorMessage != null)
            {
              ErrorMessage("The Maintools.FeatureClassUtilities.GetFileGeodatabaseFeatureClassLastUpdate() Method failed with error message - " + caught.Message + " (Line:  " + lineNumber.ToString() + ")!");
            }

            //  Return January 1st, 1900 to the calling routine to indicate that this process failed.
            return new System.DateTime(1900, 1, 1);

              }
              finally
              {
            //  If the ESRI System File Geodatabase Property Set Object has been instantiated, close it.
            if (fileGeodatabasePropertySet == null)
            {
              fileGeodatabasePropertySet = null;
            }

              }
        }
        public bool ExportGeodatabaseAnnotationFeatureClassToFileGeoDB(ESRI.ArcGIS.Geodatabase.IFeatureWorkspace InputFeatureWorkspace,
                                          ESRI.ArcGIS.Geodatabase.IFeatureClass InputFeatureClass, string OutputFeatureClassName, string OutputFileGeoDBPath,
                                          string OutputFileGeoDBName)
        {
            ESRI.ArcGIS.Geodatabase.IWorkspace                  inputWorkspace           = null;
              ESRI.ArcGIS.esriSystem.IPropertySet                 inputPropertySet         = null;
              System.Type                                         inputFactoryType         = null;
              System.Object                                       inputFactoryObject       = null;
              ESRI.ArcGIS.Geodatabase.IWorkspaceFactory           inputWorkspaceFactory    = null;
              ESRI.ArcGIS.Geodatabase.IWorkspaceName              inputWorkspaceName       = null;
              ESRI.ArcGIS.Geodatabase.IDataset                    inputDataset             = null;
              ESRI.ArcGIS.esriSystem.VarArray                     geoprocessorVariantArray = null;
              PDX.BTS.DataMaintenance.MaintTools.GeneralUtilities generalUtilities         = null;
              ESRI.ArcGIS.Geoprocessing.GeoProcessor              geoProcessor             = null;

              try
              {
            //  QI to the Input Workspace.
            inputWorkspace = (ESRI.ArcGIS.Geodatabase.IWorkspace)InputFeatureWorkspace;

            //  Build a Property Set for the Current Workspace.
            inputPropertySet = new ESRI.ArcGIS.esriSystem.PropertySetClass();
            inputPropertySet = inputWorkspace.ConnectionProperties;

            //  The Folks at ESRI were not smart enough to handle a Property Set that did not include a Server Name so if there is not one in this propertyset dummy one in.
            if (inputPropertySet.GetProperty("Instance").ToString().ToUpper().IndexOf("SDE:SQLSERVER:") != -1)
            {
              //  Determine the server name from the "Instance" Property of the Property Set.
              string inputServerName = inputPropertySet.GetProperty("Instance").ToString();
              while (inputServerName.IndexOf(@":") != -1)
              {
            //  Strip the first character from the Input Server Name.
            inputServerName = inputServerName.Substring(1);
              }

              //  If the Server Name Includes an Instance Value, strip that from the server name.
              while (inputServerName.IndexOf(@"\") != -1)
              {
            //  Strip the last character from the Input Server Name.
            inputServerName = inputServerName.Substring(0, (inputServerName.Length - 1));
              }

              //  Add a Server Property to the Property Set.
              inputPropertySet.SetProperty("Server", inputServerName);

            }

            //  Determine which directory the Temporary SDE Connection File should be created in.
            string temporaryDirectory = null;
            if (System.IO.Directory.Exists(@"D:\Temp"))
            {
              //  Set the Temporary Directory to 'D:\TEMP\'.
              temporaryDirectory = @"D:\Temp\";

            }
            else
            {
              //  Check to see if there is a 'C:\TEMP' Directory.
              if (System.IO.Directory.Exists(@"C:\Temp"))
              {
            //  Set the Temporary Directory to 'C:\Temp\'
            temporaryDirectory = @"C:\Temp\";
              }
              else
              {
            //  Set the Temporary Directory to 'C:\'.
            temporaryDirectory = @"C:\";
              }

            }

            //  Make sure the Output Temporary Connection File does not already exist before attempting to create a new one.
            if (System.IO.File.Exists(temporaryDirectory + OutputFeatureClassName + "SDEConn.sde"))
            {
              //  Delete the existing File.
              System.IO.File.Delete(temporaryDirectory + OutputFeatureClassName + "SDEConn.sde");

            }

            //  Create the Temporary SDE Connection File that will be used to specify the Input Annotation Features for this export operation.
            inputFactoryType = System.Type.GetTypeFromProgID("esriDataSourcesGDB.SDEWorkspaceFactory");
            inputFactoryObject = System.Activator.CreateInstance(inputFactoryType);
            inputWorkspaceFactory = (ESRI.ArcGIS.Geodatabase.IWorkspaceFactory)inputFactoryObject;
            inputWorkspaceName = inputWorkspaceFactory.Create(temporaryDirectory, OutputFeatureClassName + "SDEConn.sde", inputPropertySet, 0);

            //  Specify the parameters for the export of this Feature Class to the Output File Geodatabase.
            inputDataset = (ESRI.ArcGIS.Geodatabase.IDataset)InputFeatureClass;
            string processDatasetName = null;
            if (inputDataset.Name.IndexOf(inputWorkspaceName.ConnectionProperties.GetProperty("Database").ToString()) > -1)
            {
              //  Drop the Server name from the Name before using it.
              processDatasetName = inputDataset.Name.Substring(inputWorkspaceName.ConnectionProperties.GetProperty("Database").ToString().Length + 1);
            }
            else
            {
              //  Use the Name as is.
              processDatasetName = inputDataset.Name.ToString();
            }

            //  Create a Field Mapping for this export.
            string inputAnnotationFeatures = inputWorkspaceName.PathName.ToString() + @"\" + processDatasetName;
            ESRI.ArcGIS.Geoprocessing.IGPUtilities geoprocessingUtilities = new ESRI.ArcGIS.Geoprocessing.GPUtilitiesClass();
            ESRI.ArcGIS.Geodatabase.IDETable inputTable = (ESRI.ArcGIS.Geodatabase.IDETable)geoprocessingUtilities.MakeDataElement(inputAnnotationFeatures, null, null);
            ESRI.ArcGIS.esriSystem.IArray inputTables = new ESRI.ArcGIS.esriSystem.ArrayClass();
            inputTables.Add(inputTable);
            ESRI.ArcGIS.Geoprocessing.IGPFieldMapping fieldMapping = new ESRI.ArcGIS.Geoprocessing.GPFieldMappingClass();

            //  Go through the fields in the Input Table and add them to the Field Mapping.
            object missing = Type.Missing;
            for (int i = 0; i < inputTable.Fields.FieldCount; i++)
            {
              if ((inputTable.Fields.get_Field(i).Type != ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeOID) &&
              (inputTable.Fields.get_Field(i).Type != ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeGeometry))
              {
            ESRI.ArcGIS.Geoprocessing.IGPFieldMap currentFieldMap = new ESRI.ArcGIS.Geoprocessing.GPFieldMapClass();
            currentFieldMap.AddInputField(inputTable, inputTable.Fields.get_Field(i), -1, -1);
            fieldMapping.AddFieldMap(currentFieldMap);
            currentFieldMap = null;

              }

            }

            //  Build the Variant Array that will be used to pass the parameters necessary for the export to the toolbox object.
            geoprocessorVariantArray = new ESRI.ArcGIS.esriSystem.VarArrayClass();
            geoprocessorVariantArray.Add(inputAnnotationFeatures);
            geoprocessorVariantArray.Add(System.IO.Path.Combine(OutputFileGeoDBPath, OutputFileGeoDBName + ".gdb"));
            geoprocessorVariantArray.Add(OutputFeatureClassName);
            geoprocessorVariantArray.Add(null);
            geoprocessorVariantArray.Add(fieldMapping);
            geoprocessorVariantArray.Add(null);

            //  Determine the ArcGIS Install Path so that the Projector Toolbox can be opened to be used.
            generalUtilities = new PDX.BTS.DataMaintenance.MaintTools.GeneralUtilities();
            string installPath = generalUtilities.DetermineArcGISDesktopInstallPath();

            //  Make sure the Install Path was determined successfully before moving on.
            if (System.String.IsNullOrEmpty(installPath))
            {
              //  Let the user know that the ArcGIS Desktop Install Path could not be determined.
              if (ErrorMessage != null)
              {
            ErrorMessage("Could not Determine the ArcGIS Desktop Install Path to Initialize the Projection Toolbox.  The MaintTools.FeatureClassUtilities.ProjectFeatureClassHARNtoWGS() Method failed!");
              }

              //  Return FALSE to the calling method to indicate that this method failed.
              return false;

            }

            //  Instantiate the Geoprocessing Object that will be used to export this Annotatioin Feature Class to the Output File Geodatabase.
            geoProcessor = new ESRI.ArcGIS.Geoprocessing.GeoProcessorClass();
            geoProcessor.AddToolbox(System.IO.Path.Combine(installPath, @"ArcToolbox\Toolboxes\Conversion Tools.tbx"));

            //  Perform the Export in a TRY Block so that any COM or IO Errors can be identified and handled.
            try
            {
              //  Perform the export.
              geoProcessor.Execute("FeatureClassToFeatureClass_Conversion", geoprocessorVariantArray, null);
              //  Write the messages from the Feature Class to Feature Class tool log file.
              int toolMessageCount = geoProcessor.MessageCount;
              int currentToolMessageIndex = 0;
              if (ProcessMessage != null)
              {
            ProcessMessage("         - Feature Class to Feature Class Operation Messages...");
              }
              while (currentToolMessageIndex < toolMessageCount)
              {
            //  Write the current message to the log file.
            if (ProcessMessage != null)
            {
              ProcessMessage("           + " + geoProcessor.GetMessage(currentToolMessageIndex));
            }
            //  Increment the Tool Message Index Counter.
            currentToolMessageIndex++;
              }

            }
            catch (System.IO.IOException ioException)
            {
              //  Determine the Line Number from which the exception was thrown.
              System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(ioException, true);
              System.Diagnostics.StackFrame stackFrame = stackTrace.GetFrame(stackTrace.FrameCount - 1);
              int lineNumber = stackFrame.GetFileLineNumber();

              //  Let the User know that the Dissolve Operation Failed.
              if (ErrorMessage != null)
              {
            ErrorMessage("The Feature Class to Feature Class Operation in the MaintTools.FeatureClassUtilities.ExportGeodatabaseAnnotationFeatureClassToFileGeoDB() Method Failed with error message - " + ioException.Message + " (" + ioException.Source + " Line:  " + lineNumber.ToString() + ")!");
              }
              int toolMessageCount = geoProcessor.MessageCount;
              int currentToolMessageIndex = 0;
              if (ProcessMessage != null)
              {
            ProcessMessage("The information from the Geoprocessor is:");
              }
              while (currentToolMessageIndex < toolMessageCount)
              {
            //  Write the current message to the log file.
            if (ProcessMessage != null)
            {
              ProcessMessage("   + " + geoProcessor.GetMessage(currentToolMessageIndex));
            }
            //  Increment to Toold Message Index Counter.
            currentToolMessageIndex++;
              }
              //  Return FALSE to the calling routine ito indicate that this process failed.
              return false;
            }
            catch (System.Runtime.InteropServices.COMException comException)
            {
              //  Determine the Line Number from which the exception was thrown.
              System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(comException, true);
              System.Diagnostics.StackFrame stackFrame = stackTrace.GetFrame(stackTrace.FrameCount - 1);
              int lineNumber = stackFrame.GetFileLineNumber();

              //  Let the User know that the Dissolve Operation Failed.
              if (ErrorMessage != null)
              {
            ErrorMessage("The Feature Class to Feature Class Operation in the MaintTools.FeatureClassUtilities.ExportGeodatabaseAnnotationFeatureClassToFileGeoDB() Method Failed with error message - " + comException.Message + " (" + comException.ErrorCode + " Line:  " + lineNumber.ToString() + ")!");
              }
              int toolMessageCount = geoProcessor.MessageCount;
              int currentToolMessageIndex = 0;
              if (ProcessMessage != null)
              {
            ProcessMessage("The information from the Geoprocessor is:");
              }
              while (currentToolMessageIndex < toolMessageCount)
              {
            //  Write the current message to the log file.
            if (ProcessMessage != null)
            {
              ProcessMessage("   + " + geoProcessor.GetMessage(currentToolMessageIndex));
            }
            //  Increment to Toold Message Index Counter.
            currentToolMessageIndex++;
              }
              //  Return FALSE to the calling routine ito indicate that this process failed.
              return false;
            }

            //  Delete the SDE Connection File since it is no longer needed.
            if (System.IO.File.Exists(temporaryDirectory + OutputFeatureClassName + "SDEConn.sde"))
            {
              //  Delete the existing File.
              System.IO.File.Delete(temporaryDirectory + OutputFeatureClassName + "SDEConn.sde");
            }

              }
              catch (System.Exception caught)
              {
            //  Determine the Line Number from which the exception was thrown.
            System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(caught, true);
            System.Diagnostics.StackFrame stackFrame = stackTrace.GetFrame(stackTrace.FrameCount - 1);
            int lineNumber = stackFrame.GetFileLineNumber();

            //  Let the User know that this process failed.
            if (ErrorMessage != null)
            {
              ErrorMessage("The MaintTools.FeatureClassUtilities.ExportGeodatabaseAnnotationFeatureClassToFileGeoDB() Method failed with error message:  " + caught.Message + " (Line:  " + lineNumber.ToString() + ")!");
            }

            //  Return FALSE to the calling routine to indicate that this process failed.
            return false;

              }
              finally
              {
            //  If the Input Workspace Object has been Instantiated, close it.
            if (inputWorkspace != null)
            {
              //  Close the Input Workspace Object.
              inputWorkspace = null;
            }
            //  If the Input Property Set Object was instantiated, close it.
            if (inputPropertySet != null)
            {
              inputPropertySet = null;
            }
            //  If the Input Factory Type Object was instantiated, close it.
            if (inputFactoryType != null)
            {
              inputFactoryType = null;
            }
            //  If the Input Factory Object was instantiated, close it.
            if (inputFactoryObject != null)
            {
              inputFactoryObject = null;
            }
            //  If the Input Workspace Factory Object was instantiated, close it.
            if (inputWorkspaceFactory != null)
            {
              inputWorkspaceFactory = null;
            }
            //  If the Input Workspace Name Object was instantiated, close it.
            if (inputWorkspaceName != null)
            {
              inputWorkspaceName = null;
            }
            //  If the Input Dataset name Object was instantiated, close it.
            if (inputDataset != null)
            {
              inputDataset = null;
            }
            //  If the Geoprocessor Variant Array Object was instantiated, close it.
            if (geoprocessorVariantArray != null)
            {
              geoprocessorVariantArray.RemoveAll();
              geoprocessorVariantArray = null;
            }
            //  If the General Utilities Object was instantiated, close it.
            if (generalUtilities != null)
            {
              generalUtilities = null;
            }
            //  If the Geoprocessor Object was instantiated, close it.
            if (geoProcessor != null)
            {
              geoProcessor = null;
            }

              }

              //  If the process made it to here it was successful so return TRUE to the calling method.
              return true;
        }
        public ESRI.ArcGIS.Geodatabase.IWorkspace EstablishFileGeodatabaseConnection(string FileGeodatabase)
        {
            ESRI.ArcGIS.esriSystem.IPropertySet fileGeodatabasePropertySet = null;

              try
              {
            //  Make sure the specified File Geodatabase Directory Exists.
            if (!System.IO.Directory.Exists(FileGeodatabase))
            {
              //  Let the User know that this method failed.
              if (ErrorMessage != null)
              {
            ErrorMessage("The specified File Geodatabase - " + FileGeodatabase + " - does not extist!  Aborting the MaintTools.FeatureClassUtilities.EstablishFileGeodatabaseConnection() Method!");
              }

              //  Return a NULL Pointer to the calling method to indicate that this methdo failed.
              return null;

            }

            //  Build the File Geodatabase PropertySet based on the File Geodatabase Path.
            fileGeodatabasePropertySet = new ESRI.ArcGIS.esriSystem.PropertySetClass();
            fileGeodatabasePropertySet.SetProperty("DATABASE", FileGeodatabase);

            //  Return the File Geodatabase Workspace pointer to the calling method.
            return EstablishFileGeodatabaseConnection(fileGeodatabasePropertySet);

              }
              catch (System.Runtime.InteropServices.COMException comException)
              {
            //  Determine the Line Number from which the exception was thrown.
            System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(comException, true);
            System.Diagnostics.StackFrame stackFrame = stackTrace.GetFrame(stackTrace.FrameCount - 1);
            int lineNumber = stackFrame.GetFileLineNumber();

            //  Let the User know that this method failed.
            if (ErrorMessage != null)
            {
              ErrorMessage("The MaintTools.FeatureClassUtilities.EstablishFileGeodatabaseConnection() Method failed while opening the File Geodatabase Workspace with COM Exception - " + comException.Message + " (" + comException.ErrorCode + " Line:  " + lineNumber.ToString() + ")!");
            }

            //  Return a NULL Pointer to the calling method to indicate that this methdo failed.
            return null;

              }
              catch (System.Exception caught)
              {
            //  Determine the Line Number from which the exception was thrown.
            System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(caught, true);
            System.Diagnostics.StackFrame stackFrame = stackTrace.GetFrame(stackTrace.FrameCount - 1);
            int lineNumber = stackFrame.GetFileLineNumber();

            //  Let the User know that this process failed.
            if (ErrorMessage != null)
            {
              ProcessMessage("The MaintTools.FeatureClassUtilities.EstablishFileGeodatabaseConnection() Method failed with error message:  " + caught.Message + " (Line:  " + lineNumber.ToString() + ")!");
            }

            //  Return NULL to the calling routine to indicate that this process failed.
            return null;

              }
              finally
              {
            //  If the ESRI System File Geodatabase Property Set Object was instantiated, close it.
            if (fileGeodatabasePropertySet != null)
            {
              fileGeodatabasePropertySet = null;
            }

              }
        }
        public ESRI.ArcGIS.Geodatabase.IWorkspace EstablishEnterpriseGeoDBConnection(string GeodatabaseInstance, string GeodatabaseName, string UserName = "******", string UserPassword = "******", string Version = "")
        {
            ESRI.ArcGIS.esriSystem.IPropertySet geodatabasePropertySet = null;

              try
              {
            //  Retrieve the Server Name from the Instance Value.
            string serverName = GeodatabaseInstance;
            while (serverName.IndexOf(":") != -1)
            {
              serverName = serverName.Substring(1);
            }
            if (serverName.IndexOf(@"\") != -1)
            {
              serverName = serverName.Substring(0, (serverName.IndexOf(@"\")));
            }

            //  If No User Name was passed to this method, use Operating System Authentication.  Otherwise, include the passed User Name and
            //  Password in the Property Set that will be used to connect to the Enterprise Geodatabase.
            if (UserName == "#")
            {
              //  Build the Property Set that will be used to connect to the Enterprise Geodatabase.
              geodatabasePropertySet = new ESRI.ArcGIS.esriSystem.PropertySetClass();
              geodatabasePropertySet.SetProperty("SERVER", serverName);
              geodatabasePropertySet.SetProperty("INSTANCE", GeodatabaseInstance);
              geodatabasePropertySet.SetProperty("DATABASE", GeodatabaseName);
              geodatabasePropertySet.SetProperty("AUTHENTICATION_MODE", "OSA");
              if (!System.String.IsNullOrEmpty(Version))
              {
            geodatabasePropertySet.SetProperty("VERSION", Version);
              }

            }
            else
            {
              //  Build the Property Set (including the passed User Name and Password) that will be used to connect to the Enterprise Geodatabase.
              geodatabasePropertySet = new ESRI.ArcGIS.esriSystem.PropertySetClass();
              geodatabasePropertySet.SetProperty("SERVER", serverName);
              geodatabasePropertySet.SetProperty("INSTANCE", GeodatabaseInstance);
              geodatabasePropertySet.SetProperty("DATABASE", GeodatabaseName);
              geodatabasePropertySet.SetProperty("USER", UserName);
              geodatabasePropertySet.SetProperty("PASSWORD", UserPassword);
              if (!System.String.IsNullOrEmpty(Version))
              {
            geodatabasePropertySet.SetProperty("VERSION", Version);
              }

            }

            //  Call the overload of this method that takes an ESRI Property Set as a parameter and return the result of the connection attempt
            //  to the calling method.
            return EstablishEnterpriseGeoDBConnection(geodatabasePropertySet);

              }
              catch (System.Exception caught)
              {
            //  Determine the Line Number from which the exception was thrown.
            System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(caught, true);
            System.Diagnostics.StackFrame stackFrame = stackTrace.GetFrame(stackTrace.FrameCount - 1);
            int lineNumber = stackFrame.GetFileLineNumber();

            //  Let the User know that this process failed.
            if (ErrorMessage != null)
            {
              ErrorMessage("The MaintTools.FeatureClassUtilities.EstablishEnterpriseGeoDBConnection() Method failed with error message:  " + caught.Message + " (Line:  " + lineNumber.ToString() + ")!");
            }

            //  Return NULL to the calling routine to indicate that this process failed.
            return null;

              }
              finally
              {
            //  If the ESRI System Geodatabase Property Set Object was instantiated, close it.
            if (geodatabasePropertySet != null)
            {
              System.Runtime.InteropServices.Marshal.ReleaseComObject(geodatabasePropertySet);
            }

              }
        }