public bool? CompressFileGeoDB(string FileGeodatabase) { System.Collections.Generic.IEnumerable<ESRI.ArcGIS.RuntimeInfo> runtimeInfo = null; ESRI.ArcGIS.esriSystem.IVariantArray geoprocessorVariantArray = null; ESRI.ArcGIS.Geoprocessing.GeoProcessor compressGeoprocessor = null; try { // Make sure the File Geodatabase exists before attempting to uncompress it. if (!System.IO.Directory.Exists(FileGeodatabase)) { // Let the user know that the File Geodatabase does not exist. if (ErrorMessage != null) { ErrorMessage("The File Geodatabase - " + FileGeodatabase.ToUpper() + " does not exist and thus was not compressed..."); } // Return FALSE to the calling routine to indicate that this process failed. return false; } // Let the user know what is happening. if (ProcessMessage != null) { ProcessMessage(" - Attempting to compress the File Geodatabase - " + FileGeodatabase.ToUpper() + "..."); } // Determine the Install Path for the ArcGIS Software. runtimeInfo = ESRI.ArcGIS.RuntimeManager.InstalledRuntimes; string arcToolboxPath = null; foreach (ESRI.ArcGIS.RuntimeInfo currentRuntimeItem in runtimeInfo) { // Retrieve the Install Path for the ArcGIS Desktop Runtime (ArcGIS Toolbox Items // are found in this path). if (currentRuntimeItem.Product == ESRI.ArcGIS.ProductCode.Desktop) { arcToolboxPath = currentRuntimeItem.Path; } } // If the ArcToolBox Directory Exists, add it to the Path. if (System.IO.Directory.Exists(System.IO.Path.Combine(arcToolboxPath, @"ArcToolbox"))) { arcToolboxPath = System.IO.Path.Combine(arcToolboxPath, @"ArcToolbox"); } // If the Toolboxes Directory Exists, add it to the Path. if (System.IO.Directory.Exists(System.IO.Path.Combine(arcToolboxPath, @"Toolboxes"))) { arcToolboxPath = System.IO.Path.Combine(arcToolboxPath, @"Toolboxes"); } // If the Toolbox File Exists, add it to the Toolbox path. if (System.IO.File.Exists(System.IO.Path.Combine(arcToolboxPath, "Data Management Tools.tbx"))) { // Add the File Name to the path. arcToolboxPath = System.IO.Path.Combine(arcToolboxPath, "Data Management Tools.tbx"); } // Build the Geoprocessor Variant Array that will pass the arguments to the Geoprocessing Tool. geoprocessorVariantArray = new ESRI.ArcGIS.esriSystem.VarArrayClass(); geoprocessorVariantArray.Add(FileGeodatabase); // Initialize the Geoprocessing Object that will be used to compress the Geodatabase. compressGeoprocessor = new ESRI.ArcGIS.Geoprocessing.GeoProcessor(); compressGeoprocessor.AddToolbox(arcToolboxPath); try { // Perform the export. compressGeoprocessor.Execute("CompressFileGeodatabaseData_management", geoprocessorVariantArray, null); // Write the messages from the Compress Tool to the log file. if (ProcessMessage != null) { int toolMessageCount = compressGeoprocessor.MessageCount; int currentToolMessageIndex = 0; ProcessMessage(""); ProcessMessage(" - Successfully Compressed the File Geodatabase..."); ProcessMessage(" - Compress File Geodatabase Operation Messages..."); while (currentToolMessageIndex < toolMessageCount) { // Write the current message to the log file. ProcessMessage(" + " + compressGeoprocessor.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 Compress Operation Failed. if (ErrorMessage != null) { ErrorMessage("The Compress File Geodatabase Operation in the FeatureClassUtilities.CompressFileGeoDB() Method Failed with error message - " + ioException.Message + " (" + ioException.Source + " Line: " + lineNumber.ToString() + ")!"); if (ProcessMessage != null) { int toolMessageCount = compressGeoprocessor.MessageCount; int currentToolMessageIndex = 0; ProcessMessage("The information from the Geoprocessor is:"); while (currentToolMessageIndex < toolMessageCount) { // Write the current message to the log file. ProcessMessage(" + " + compressGeoprocessor.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 Compress Operation Failed. if (ErrorMessage != null) { ErrorMessage("The Compress File Geodatabase Operation in the CompressFileGeoDB() Method Failed with error message - " + comException.Message + " (" + comException.ErrorCode + " Line: " + lineNumber.ToString() + ")!"); if (ProcessMessage != null) { int toolMessageCount = compressGeoprocessor.MessageCount; int currentToolMessageIndex = 0; ProcessMessage("The information from the Geoprocessor is:"); while (currentToolMessageIndex < toolMessageCount) { // Write the current message to the log file. ProcessMessage(" + " + compressGeoprocessor.GetMessage(currentToolMessageIndex)); // Increment to Toold Message Index Counter. currentToolMessageIndex++; } } } // Return FALSE to the calling routine ito indicate that this process failed. return false; } // If the process made it to here, it was successful so return TRUE to the calling // method. return true; } 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.CompressFileGeoDB() 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 Installed Runtimes List has been instantiated, close it. if (runtimeInfo != null) { runtimeInfo = null; } // If the Geoprocessor Parameters Array has been instantiated, close it. if (geoprocessorVariantArray != null) { geoprocessorVariantArray = null; } // If the Uncompress Geoprocessor Object has been instantiated, close it. if (compressGeoprocessor != null) { compressGeoprocessor = null; } } }
/// <summary> /// Indexes the specified Field in the Feature Class. /// </summary> /// <param name="IndexFeatureClass"> /// An ESRI Geodatabase Feature Class Object for the Feature Class that contains the Field that is to be indexed. /// </param> /// <param name="IndexFieldName"> /// The Name of the Field in the Class that is to be indexed. /// </param> /// <returns> /// TRUE if the Field was successfully indexed. /// FALSE if the Field was not successfully indexed. /// </returns> public bool IndexedFieldInFeatureClass(ESRI.ArcGIS.Geodatabase.IFeatureClass IndexFeatureClass, string IndexFieldName) { PDX.BTS.DataMaintenance.MaintTools.GeneralUtilities generalUtilities = null; ESRI.ArcGIS.Geodatabase.ITable inputTable = null; ESRI.ArcGIS.esriSystem.VarArray geoprocessorVariantArray = null; ESRI.ArcGIS.Geoprocessing.IGeoProcessor geoprocessor = null; try { // If the specified Field exists in the Feature Class, index it. if (IndexFeatureClass.Fields.FindField(IndexFieldName) != -1) { // Instantiate the CGIS General Utilities Object that will be used to determine the ArcGIS Install Path on the server. generalUtilities = new PDX.BTS.DataMaintenance.MaintTools.GeneralUtilities(); // 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.IndexedFieldInFeatureClass() Method failed!"); } // Return FALSE to the calling method to indicate that this method failed. return false; } //QI to the Input Table. inputTable = (ESRI.ArcGIS.Geodatabase.ITable)IndexFeatureClass; // Build the Variant Array that will hold the parameters to be passed to the Geoprocessing Tool. geoprocessorVariantArray = new ESRI.ArcGIS.esriSystem.VarArrayClass(); geoprocessorVariantArray.Add(inputTable); geoprocessorVariantArray.Add(IndexFieldName); //geoprocessorVariantArray.Add(indexFeatureClassIndexField); geoprocessorVariantArray.Add(IndexFieldName + @"_idx"); geoprocessorVariantArray.Add("NON_UNIQUE"); geoprocessorVariantArray.Add("NON_ASCENDING"); // Instantiate the Geoprocessing Object that will be used to Index the Specified Field in the Feature Class. geoprocessor = new ESRI.ArcGIS.Geoprocessing.GeoProcessorClass(); geoprocessor.AddToolbox(System.IO.Path.Combine(installPath, @"ArcToolbox\Toolboxes\Data Management Tools.tbx")); // Perform the Index Field Operation in a TRY Block so that any COM or IO Errors can be identified and handled. try { // Perform the Add Index Operation. geoprocessor.Execute("AddIndex_Management", geoprocessorVariantArray, null); // Write the messages from the Add Index tool log file. int toolMessageCount = geoprocessor.MessageCount; int currentToolMessageIndex = 0; if (ProcessMessage != null) { ProcessMessage(" - Add Index 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 Add Index Operation Failed. if (ErrorMessage != null) { ErrorMessage("The Add Attribute Index Operation in the MaintTools.FeatureClassUtilities.IndexedFieldInFeatureClass() 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 Tool 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 Add Attribute Index Operation in the MaintTools.FeatureClassUtilities.IndexedFieldInFeatureClass() 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 Tool Message Index Counter. currentToolMessageIndex++; } // Return FALSE to the calling routine ito indicate that this process failed. return false; } } else { // Let the user know that the Field does not exist in the Feature Class and will not be indexed. if (ProcessMessage != null) { ProcessMessage(" - The field - " + IndexFieldName.ToUpper() + " could not be found in the " + IndexFeatureClass.AliasName + " Feature Class and will not be indexed..."); } } } 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 method failed. if (ErrorMessage != null) { ErrorMessage("The MaintTools.FeatureClassUtilities.IndexedFieldInFeatureClass() Method failed with error message - " + caught.Message + " (Line: " + lineNumber.ToString() + ")!"); } // Return FALSE to the calling Method to indicate that this Method failed. return false; } finally { // If the Geoprocessor Object was instantiated, close it. if (geoprocessor != null) { geoprocessor = null; } // If the Geoprocessor Variant Array Object was instantiated, close it. if (geoprocessorVariantArray != null) { geoprocessorVariantArray = null; } // If the Input Table Object was instantiated, close it. if (inputTable != null) { inputTable = null; } // If the CGIS Data Maintenance General Utilities Object was instantiated, close it. if (generalUtilities != null) { generalUtilities = null; } } // If the process made it to here, it was successful so return a "TRUE" to the calling method. return true; }
private bool ProjectFeatureClassHARNtoWGS(string SourceFeatures, string OutputFeatures, string OutputProjectionDefinitionFile) { ESRI.ArcGIS.esriSystem.IVariantArray geoprocessorVariantArray = null; PDX.BTS.DataMaintenance.MaintTools.GeneralUtilities generalUtilities = null; ESRI.ArcGIS.Geoprocessing.GeoProcessor projectDataGeoProcessor = null; try { // Let the user know what is happening. if (ProcessMessage != null) { ProcessMessage(" + Initializing the Projection Operation..."); } // Specify the parameters for the export of this Feature Class to the Output File Geodatabase. geoprocessorVariantArray = new ESRI.ArcGIS.esriSystem.VarArrayClass(); geoprocessorVariantArray.Add(SourceFeatures); geoprocessorVariantArray.Add(OutputFeatures); geoprocessorVariantArray.Add(OutputProjectionDefinitionFile); geoprocessorVariantArray.Add("NAD_1983_HARN_To_WGS_1984"); // 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. projectDataGeoProcessor = new ESRI.ArcGIS.Geoprocessing.GeoProcessorClass(); projectDataGeoProcessor.AddToolbox(System.IO.Path.Combine(installPath, @"ArcToolbox\Toolboxes\Data Management Tools.tbx")); // Let the user know what is happening. if (ProcessMessage != null) { ProcessMessage(" + Performing the Projection Operation..."); } // Perform the Projection in a TRY Block so that any COM or IO Errors can be identified and handled. try { // Perform the export. projectDataGeoProcessor.Execute("Project_Management", geoprocessorVariantArray, null); // Write the messages from the Projection Tool to the process log file. if (ProcessMessage != null) { int toolMessageCount = projectDataGeoProcessor.MessageCount; int currentToolMessageIndex = 0; ProcessMessage(""); ProcessMessage(" + Project Feature Class Operation Messages..."); while (currentToolMessageIndex < toolMessageCount) { // Write the current message to the log file. ProcessMessage(" >> " + projectDataGeoProcessor.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 Project Feature Class Operation in the ProjectFeatureClassHARNtoWGS() Method Failed with error message - " + ioException.Message + " (" + ioException.Source + " Line: " + lineNumber.ToString() + ")!"); } // 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 Project Feature Class Operation in the ProjectFeatureClassHARNtoWGS() Method Failed with error message - " + comException.Message + " (" + comException.ErrorCode + " Line: " + lineNumber.ToString() + ")!"); } // Return FALSE to the calling routine ito indicate that this process failed. return false; } } 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.ProjectFeatureClassHARNtoWGS() 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 process made it to here it was successful so return TRUE to the calling method. return true; }
/// <summary> /// Deletes all features from the specified feature class. /// </summary> /// <param name="DeleteFeatureClass"> /// An Object Pointer to the Feature Class that is to be emptied. /// </param> /// <returns> /// TRUE if the features were successfully deleted. /// FALSE if the process failed to delete the features from the dataset. /// </returns> public bool EmptyGeodatabaseFeatureClass(ESRI.ArcGIS.Geodatabase.IFeatureClass DeleteFeatureClass) { PDX.BTS.DataMaintenance.MaintTools.GeneralUtilities generalUtilities = null; ESRI.ArcGIS.esriSystem.IVariantArray geoprocessorVariantArray = null; ESRI.ArcGIS.Geoprocessing.IGeoProcessor geoProcessor = null; try { // Determine the ArcGIS Install Path so that the Data Management 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 Data Management Toolbox. The MaintTools.FeatureClassUtilities.EmptyGeodatabaseFeatureClass() Method failed!"); } // Return FALSE to the calling method to indicate that this method failed. return false; } // Build the Variant Array that will be used to pass the parameters necessary for the delete to the toolbox object. geoprocessorVariantArray = new ESRI.ArcGIS.esriSystem.VarArrayClass(); geoprocessorVariantArray.Add(DeleteFeatureClass); // Instantiate the Geoprocessing Object that will be used to delete the features from the feature class. geoProcessor = new ESRI.ArcGIS.Geoprocessing.GeoProcessorClass(); geoProcessor.AddToolbox(System.IO.Path.Combine(installPath, @"ArcToolbox\Toolboxes\Data Management 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("DeleteFeatures_management", geoprocessorVariantArray, null); // Write the messages from the Delete Features tool log file. int toolMessageCount = geoProcessor.MessageCount; int currentToolMessageIndex = 0; if (ProcessMessage != null) { ProcessMessage(" - Delete Features 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 Delete Features Operation Failed. if (ErrorMessage != null) { ErrorMessage("The Delete Features Operation in the MaintTools.FeatureClassUtilities.EmptyGeodatabaseFeatureClass() 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 Delete Features Operation Failed. if (ErrorMessage != null) { ErrorMessage("The Delete Features Operation in the MaintTools.FeatureClassUtilities.EmptyGeodatabaseFeatureClass() 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; } } 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.EmptyGeodatabaseFeatureClass() 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 Geoprocessor Object was instantiated, close it. if (geoProcessor != null) { geoProcessor = null; } // If the Geoprocessor Variant Array Object was instantiated, close it. if (geoprocessorVariantArray != null) { geoprocessorVariantArray = null; } // If the BTS General Utilities Object was instantiated, close it. if (generalUtilities != null) { generalUtilities = null; } } // If the process made it to here, it was successful so return TRUE to the calling method. return true; }
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; }
/// <summary> /// create a new feature class with buffer polygons of the input feature class /// </summary> public static IFeatureClass GP_Union_analysis(IFeatureClass FirstFC, IFeatureClass SecondFC, string OutFCPathAndName, ref string ErrorMessage) { ESRI.ArcGIS.esriSystem.IVariantArray GPParams; ESRI.ArcGIS.Geoprocessing.IGeoProcessor ThisGeoProcessor = null; ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult GeoResult = null; IFeatureClass ResultsFC = null; NPSGlobal NPS; string FCName, FCList; NPS = NPSGlobal.Instance; try { ThisGeoProcessor = new ESRI.ArcGIS.Geoprocessing.GeoProcessor(); GPParams = new ESRI.ArcGIS.esriSystem.VarArrayClass(); FCList = ((IDataset)FirstFC).FullName + ";" + ((IDataset)SecondFC).FullName; FCList = System.IO.Path.Combine(((IDataset)FirstFC).Workspace.PathName, ((IDataset)FirstFC).Name) + ";" + System.IO.Path.Combine(((IDataset)FirstFC).Workspace.PathName, ((IDataset)SecondFC).Name); GPParams.Add(FCList); GPParams.Add(OutFCPathAndName); GPParams.Add("ALL"); GeoResult = ThisGeoProcessor.Execute("Union_analysis", GPParams, null); FCName = OutFCPathAndName.Substring(OutFCPathAndName.LastIndexOf("\\") + 1); ResultsFC = Util.GetFeatureClass(FCName, ref ErrorMessage); Util.DeleteLayerFromMap(FCName); } catch (Exception ex) { ErrorMessage = string.Format("Geoprocessor error:\r\nTask:Union_analysis\r\n" + "Params:{0},{1},{2},{3}\r\nException:{4}", ((IDataset)FirstFC).Name, ((IDataset)SecondFC).Name, OutFCPathAndName, "ALL", ex.Message); ThisGeoProcessor = null; return null; } ThisGeoProcessor = null; return ResultsFC; }
public static void AddZValuesToPoints(int SurveyID, int NewBatchID, int TempBatchID, IGeoDataset ThisRasterDS, string ThisDEMUnits, ref string ErrorMessage) { ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult GeoResult = null; ESRI.ArcGIS.Geoprocessing.IGeoProcessor ThisGeoProcessor = null; ESRI.ArcGIS.esriSystem.IVariantArray GPParams; IFeatureClass TempFCHolderFC = null, PointFC = null; string CopyRandFilter = "", TempElePoints, TempFCHolder, InternalError = ""; NPSGlobal NPS; int GPLIndex = 0; NPS = NPSGlobal.Instance; TempFCHolder = "TempFCHolder"; TempElePoints = "TempElePoints"; ThisDEMUnits = ThisDEMUnits.ToLower(); if (string.IsNullOrEmpty(ErrorMessage)) PointFC = Util.GetFeatureClass(NPS.LYR_RANDOMPOINTS, ref ErrorMessage); if (string.IsNullOrEmpty(ErrorMessage) == false) { Util.SetProgressMessage("Deleting old temp files"); //delete the temporary table if it already exists Util.DeleteDataset(TempElePoints, esriDatasetType.esriDTFeatureClass, ref InternalError); Util.DeleteDataset(TempElePoints + "_Tbl", esriDatasetType.esriDTTable, ref InternalError); Util.DeleteDataset(TempFCHolder, esriDatasetType.esriDTFeatureClass, ref InternalError); Util.DeleteDataset(TempFCHolder + "_Tbl", esriDatasetType.esriDTTable, ref InternalError); } if (string.IsNullOrEmpty(ErrorMessage)) { try { Util.SetProgressMessage("Copying random points to temp FeatureClass"); ThisGeoProcessor = new ESRI.ArcGIS.Geoprocessing.GeoProcessor(); GPParams = new ESRI.ArcGIS.esriSystem.VarArrayClass(); GPParams.Add(NPS.Workspace.PathName); GPParams.Add(TempFCHolder); GPParams.Add("POINT"); GPParams.Add(System.IO.Path.Combine(NPS.Workspace.PathName, ((IDataset)PointFC).Name)); GeoResult = ThisGeoProcessor.Execute("CreateFeatureClass_management", GPParams, null); } catch (Exception ex) { ErrorMessage = string.Format("Geoprocessor error:\r\nTask:CreateFeatureClass_management\r\n" + "Params:{0},{1},{2},{3}\r\nException:{4}", NPS.Workspace.PathName, TempFCHolder, "POINT", System.IO.Path.Combine(NPS.Workspace.PathName, ((IDataset)PointFC).Name), ex.Message); } ThisGeoProcessor = null; Util.DeleteLayerFromMap(TempFCHolder); } if (string.IsNullOrEmpty(ErrorMessage)) { CopyRandFilter = " SurveyID=" + SurveyID; if (TempBatchID != -1) CopyRandFilter += " And BATCH_ID=" + TempBatchID; //get the temp feature class and copy only surveyid points to it TempFCHolderFC = Util.GetFeatureClass(TempFCHolder, ref ErrorMessage); if (string.IsNullOrEmpty(ErrorMessage)) Util.CopyFeatures(PointFC, CopyRandFilter, TempFCHolderFC, null, ref ErrorMessage); int TestCount = FeatureCount(TempFCHolderFC, ""); } if (string.IsNullOrEmpty(ErrorMessage)) { try { Util.SetProgressMessage("Extracting point elevation from DEM"); ThisGeoProcessor = new ESRI.ArcGIS.Geoprocessing.GeoProcessor(); GPParams = new ESRI.ArcGIS.esriSystem.VarArrayClass(); //GPParams.Add(TempFCHolderFC); //GPParams.Add(ThisRasterDS); //GPParams.Add(System.IO.Path.Combine(NPS.Workspace.PathName, TempElePoints)); //GeoResult = ThisGeoProcessor.Execute("ExtractValuesToPoints_sa", GPParams, null); GPParams.Add(ThisRasterDS); GPParams.Add(TempFCHolderFC); GPParams.Add("Spot"); // field name to add GPParams.Add(1); // Z-Factor GeoResult = ThisGeoProcessor.Execute("SurfaceSpot_3D", GPParams, null); } catch (Exception ex) { ErrorMessage = string.Format("Geoprocessor error:\r\nTask:SurfaceSpot_3D\r\n" + "Params:{0},{1},{2},{3}\r\nException:{4}", ((IDataset)ThisRasterDS).Name, ((IDataset)TempFCHolderFC).Name, "Spot", 1, ex.Message); } ThisGeoProcessor = null; //delete GPL[index] layers created from SurfaceSpot geoprocessor call while (GPLIndex < 10) { Util.DeleteLayerFromMap("GPL" + GPLIndex); GPLIndex++; } } if (string.IsNullOrEmpty(ErrorMessage)) { Util.SetProgressMessage("Importing updated random points"); //delete all points for that survey Util.DeleteFeatures(PointFC, CopyRandFilter, ref ErrorMessage); //replace the points of that survey with these points Util.CopyFeaturesForPoints(TempFCHolderFC, "", PointFC, NewBatchID, ThisDEMUnits, ref ErrorMessage); } Util.SetProgressMessage("Cleaning up temp files"); Util.DeleteDataset(TempElePoints, esriDatasetType.esriDTFeatureClass, ref InternalError); Util.DeleteDataset(TempElePoints + "_Tbl", esriDatasetType.esriDTTable, ref InternalError); Util.DeleteDataset(TempFCHolder, esriDatasetType.esriDTFeatureClass, ref InternalError); Util.DeleteDataset(TempFCHolder + "_Tbl", esriDatasetType.esriDTTable, ref InternalError); }
/// <summary> /// clips one feature class by another and returns the resulting feature class. the resulting featureclass /// is a temp feature class and will need to be deleted after it's features have been processed /// </summary> public static IFeatureClass GP_Clip_analysis(string TempClipFCName, IFeatureClass InputFC, IFeatureClass ClipFC, ref string ErrorMessage) { ESRI.ArcGIS.esriSystem.IVariantArray GPParams; ESRI.ArcGIS.Geoprocessing.IGeoProcessor ThisGeoProcessor = null; ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult GeoResult = null; IFeatureClass TempClipFC = null; NPSGlobal NPS; NPS = NPSGlobal.Instance; try { ThisGeoProcessor = new ESRI.ArcGIS.Geoprocessing.GeoProcessor(); GPParams = new ESRI.ArcGIS.esriSystem.VarArrayClass(); GPParams.Add(InputFC); GPParams.Add(ClipFC); GPParams.Add(System.IO.Path.Combine(NPS.DatabasePath, TempClipFCName)); GeoResult = ThisGeoProcessor.Execute("Clip_analysis", GPParams, null); Util.DeleteLayerFromMap(TempClipFCName); TempClipFC = Util.GetFeatureClass(TempClipFCName, ref ErrorMessage); } catch (Exception ex) { ErrorMessage = string.Format("Geoprocessor error:\r\nTask:Clip_analysis\r\n" + "Params:{0},{1},{2}\r\nException:{3}", ((IDataset)InputFC).Name, ((IDataset)ClipFC).Name, System.IO.Path.Combine(NPS.DatabasePath, TempClipFCName), ex.Message); ThisGeoProcessor = null; return null; } ThisGeoProcessor = null; return TempClipFC; }
/// <summary> /// export a featureclass as a shapefile /// </summary> public static void GP_FeatureclassToShapefile_conversion(IFeatureClass ThisFC, string ShapeFileFolderPath, string WhereClause, ref string ErrorMessage) { ESRI.ArcGIS.esriSystem.IVariantArray GPParams; ESRI.ArcGIS.Geoprocessing.IGeoProcessor ThisGeoProcessor = null; ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult GeoResult = null; IFeatureLayer TempLayer = null; NPSGlobal NPS; NPS = NPSGlobal.Instance; try { ThisGeoProcessor = new ESRI.ArcGIS.Geoprocessing.GeoProcessor(); GPParams = new ESRI.ArcGIS.esriSystem.VarArrayClass(); TempLayer = new FeatureLayerClass(); TempLayer.FeatureClass = ThisFC; ((ITableDefinition)TempLayer).DefinitionExpression = WhereClause; TempLayer.Name = ((IDataset)ThisFC).Name; GPParams.Add(TempLayer); GPParams.Add(ShapeFileFolderPath); ThisGeoProcessor.OverwriteOutput = true; GeoResult = ThisGeoProcessor.Execute("FeatureclassToShapefile_conversion", GPParams, null); } catch (Exception ex) { ErrorMessage = string.Format("Geoprocessor error:\r\nTask:Clip_analysis\r\n" + "Params:{0},{1}\r\nException:{2}", ((IDataset)ThisFC).Name, ShapeFileFolderPath, ex.Message); } ThisGeoProcessor = null; }
/// <summary> /// create a new feature class with buffer polygons of the input feature class /// </summary> public static IFeatureClass GP_Buffer_analysis(IFeatureClass FCToBuffer, string OutFCPathAndName, double BufferDistance, string BufferSide, ref string ErrorMessage) { ESRI.ArcGIS.esriSystem.IVariantArray GPParams; ESRI.ArcGIS.Geoprocessing.IGeoProcessor ThisGeoProcessor = null; ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult GeoResult = null; IFeatureClass ResultsFC = null; NPSGlobal NPS; string FCName; NPS = NPSGlobal.Instance; try { ThisGeoProcessor = new ESRI.ArcGIS.Geoprocessing.GeoProcessor(); GPParams = new ESRI.ArcGIS.esriSystem.VarArrayClass(); GPParams.Add(FCToBuffer); GPParams.Add(OutFCPathAndName); GPParams.Add(BufferDistance); GPParams.Add(BufferSide.ToUpper()); GPParams.Add("FLAT"); GPParams.Add("NONE"); GeoResult = ThisGeoProcessor.Execute("Buffer_analysis", GPParams, null); FCName = OutFCPathAndName.Substring(OutFCPathAndName.LastIndexOf("\\") + 1); ResultsFC = Util.GetFeatureClass(FCName, ref ErrorMessage); Util.DeleteLayerFromMap(FCName); } catch (Exception ex) { ErrorMessage = string.Format("Geoprocessor error:\r\nTask:Buffer_analysis\r\n" + "Params:{0},{1},{2},{3},{4}\r\nException:{5}", ((IDataset)FCToBuffer).Name, OutFCPathAndName, BufferDistance, BufferSide, "NONE", ex.Message); ThisGeoProcessor = null; return null; } ThisGeoProcessor = null; return ResultsFC; }
/// <summary> /// generate flatarea poylgons for the specified survey using the specified raster /// </summary> public static void GenerateFlatAreaPolygons(int SurveyID, double MinSlope, double MaxSlope, IGeoDataset ClippedRasterDS, ref string ErrorMessage) { ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult GeoResult = null; ESRI.ArcGIS.Geoprocessing.IGeoProcessor ThisGeoProcessor = null; ESRI.ArcGIS.GeoAnalyst.IRasterDescriptor ThisRasterDescriptor; ESRI.ArcGIS.GeoAnalyst.IConversionOp ThisConversionOp; IGeoDataset FloatRasterDS = null, IntRasterDS = null; ESRI.ArcGIS.esriSystem.IVariantArray GPParams; ESRI.ArcGIS.GeoAnalyst.ISurfaceOp ThisSurfaceOp; ESRI.ArcGIS.SpatialAnalyst.IMathOp ThisMathOp; IFeatureClass TempFlatAreasPolyFC = null, TempAggregateFC = null, TempAggregateClipFC = null, BoundaryFC = null, FlatAreasFC = null; IQueryFilter ThisQueryFilter; IFeatureCursor FlatAreasCursor, TempAggregateClipCursor; IFeatureBuffer FlatAreasFBuffer; IFeature TempAggregateClipFeature; string TempFlatAreasPolyFCName, TempAggregateFCName, InternalErrors = "", TempClipFCName; object zFactor; NPSGlobal NPS; int SurveyIDIndex; TempClipFCName = "NPS_TEMP_ClippedFC"; TempAggregateFCName = "NPS_TEMP_FlatAreasAggrFC"; TempFlatAreasPolyFCName = "NPS_TEMP_FlatAreasFC"; NPS = NPSGlobal.Instance; zFactor = 1; BoundaryFC = Util.GetFeatureClass(NPS.LYR_SURVEY_BOUNDARY, ref ErrorMessage); if (string.IsNullOrEmpty(ErrorMessage) == false) return; FlatAreasFC = Util.GetFeatureClass(NPS.LYR_FLAT_AREAS, ref ErrorMessage); if (string.IsNullOrEmpty(ErrorMessage) == false) return; SurveyIDIndex = FlatAreasFC.FindField("SurveyID"); if (SurveyIDIndex == -1) { ErrorMessage = "Could not find a SurveyID field on the " + ((IDataset)FlatAreasFC).Name + " FeatureClass."; return; } Util.SetProgressMessage("Deleting old temp files"); Util.DeleteDataset(TempFlatAreasPolyFCName, esriDatasetType.esriDTFeatureClass, ref InternalErrors); Util.DeleteDataset(TempAggregateFCName, esriDatasetType.esriDTFeatureClass, ref InternalErrors); Util.DeleteDataset(TempClipFCName, esriDatasetType.esriDTFeatureClass, ref InternalErrors); Util.DeleteDataset(TempAggregateFCName + "_Tbl", esriDatasetType.esriDTTable, ref InternalErrors); Util.DeleteDataset(TempClipFCName + "_Tbl", esriDatasetType.esriDTTable, ref InternalErrors); ThisSurfaceOp = new ESRI.ArcGIS.GeoAnalyst.RasterSurfaceOpClass(); try { Util.SetProgressMessage("Computing slope values from DEM"); FloatRasterDS = ThisSurfaceOp.Slope(ClippedRasterDS, ESRI.ArcGIS.GeoAnalyst.esriGeoAnalysisSlopeEnum.esriGeoAnalysisSlopeDegrees, ref zFactor); } catch (Exception ex) { ErrorMessage = "Error occured while converting DEM evelation values to float slope values. " + ex.Message; return; } try { Util.SetProgressMessage("Converting DEM values to integers"); ThisMathOp = new ESRI.ArcGIS.SpatialAnalyst.RasterMathOpsClass(); IntRasterDS = ThisMathOp.Int(FloatRasterDS); } catch (Exception ex) { ErrorMessage = "Error occured while converting DEM float slope values to slope integer values. " + ex.Message; return; } try { Util.SetProgressMessage("Building flat area polygons from DEM"); ThisQueryFilter = new QueryFilterClass(); ThisQueryFilter.WhereClause = " Value >= " + MinSlope + " AND Value <= " + MaxSlope; ThisRasterDescriptor = new ESRI.ArcGIS.GeoAnalyst.RasterDescriptorClass(); ThisRasterDescriptor.Create(IntRasterDS as IRaster, ThisQueryFilter, "Value"); ThisConversionOp = new ESRI.ArcGIS.GeoAnalyst.RasterConversionOpClass(); TempFlatAreasPolyFC = ThisConversionOp.RasterDataToPolygonFeatureData(ThisRasterDescriptor as IGeoDataset, NPS.Workspace, TempFlatAreasPolyFCName, true) as IFeatureClass; } catch (Exception ex) { ErrorMessage = "Error occured while generating a FeatureClass of flat " + "areas from the default DEM file. " + ex.Message; return; } try { Util.SetProgressMessage("Aggregating flat area polygons"); ThisGeoProcessor = new ESRI.ArcGIS.Geoprocessing.GeoProcessor(); GPParams = new ESRI.ArcGIS.esriSystem.VarArrayClass(); GPParams.Add(System.IO.Path.Combine(NPS.DatabasePath, TempFlatAreasPolyFCName)); GPParams.Add(System.IO.Path.Combine(NPS.DatabasePath, TempAggregateFCName)); GPParams.Add("1 Meters"); GeoResult = ThisGeoProcessor.Execute("AggregatePolygons_management", GPParams, null); } catch (Exception ex) { ErrorMessage = string.Format("Geoprocessor error:\r\nTask:AggregatePolygons_management\r\n" + "Params:{0},{1},{2}\r\nException:{3}", System.IO.Path.Combine(NPS.DatabasePath, TempFlatAreasPolyFCName), System.IO.Path.Combine(NPS.DatabasePath, TempAggregateFCName), "1 Meters", ex.Message); ThisGeoProcessor = null; return; } ThisGeoProcessor = null; Util.DeleteLayerFromMap(TempAggregateFCName); TempAggregateFC = Util.GetFeatureClass(TempAggregateFCName, ref ErrorMessage); if (string.IsNullOrEmpty(ErrorMessage) == false) { ErrorMessage = "Could not find temporary FeatureClass " + TempAggregateFCName + ". " + ErrorMessage; return; } Util.SetProgressMessage("Validating flat area polygons"); TempAggregateClipFC = Util.GP_Clip_analysis(TempClipFCName, TempAggregateFC, BoundaryFC, ref ErrorMessage); if (string.IsNullOrEmpty(ErrorMessage) == false) return; FlatAreasFBuffer = FlatAreasFC.CreateFeatureBuffer(); FlatAreasCursor = FlatAreasFC.Insert(true); ThisQueryFilter = null; ThisQueryFilter = new QueryFilterClass(); ThisQueryFilter.WhereClause = "SHAPE_AREA > 25000"; TempAggregateClipCursor = TempAggregateClipFC.Search(ThisQueryFilter, false); Util.SetProgressMessage("Importing flat area polygons to Survey"); while ((TempAggregateClipFeature = TempAggregateClipCursor.NextFeature()) != null) { if (TempAggregateClipFeature.Shape == null) continue; FlatAreasFBuffer.Shape = TempAggregateClipFeature.ShapeCopy; FlatAreasFBuffer.set_Value(SurveyIDIndex, SurveyID); FlatAreasCursor.InsertFeature(FlatAreasFBuffer); } TempAggregateClipCursor = null; FlatAreasCursor = null; Util.SetProgressMessage("Cleaning up temp files"); Util.DeleteDataset(TempFlatAreasPolyFCName, esriDatasetType.esriDTFeatureClass, ref InternalErrors); Util.DeleteDataset(TempAggregateFCName, esriDatasetType.esriDTFeatureClass, ref InternalErrors); Util.DeleteDataset(TempClipFCName, esriDatasetType.esriDTFeatureClass, ref InternalErrors); Util.DeleteDataset(TempAggregateFCName + "_Tbl", esriDatasetType.esriDTTable, ref InternalErrors); Util.DeleteDataset(TempClipFCName + "_Tbl", esriDatasetType.esriDTTable, ref InternalErrors); Util.CreateFillerPolygon(FlatAreasFC, SurveyID, ref InternalErrors); }
/// <summary> /// generate exlcuded area polygons for the specified survey and elevation /// </summary> public static void GenerateExcludedAreasPolygons(int SurveyID, int MaximumElevInFeet, bool IsAboveElevation, IGeoDataset ClippedRasterDS, ref string ErrorMessage) { ESRI.ArcGIS.SpatialAnalyst.IMathOp ThisMathOp; IGeoDataset IntRasterDS = null; IQueryFilter ThisQueryFilter; ESRI.ArcGIS.GeoAnalyst.IRasterDescriptor ThisRasterDescriptor; ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult GeoResult = null; ESRI.ArcGIS.Geoprocessing.IGeoProcessor ThisGeoProcessor = null; ESRI.ArcGIS.esriSystem.IVariantArray GPParams; IFeatureClass NewElvPolyFC, BoundaryFC, ElvPolyFC; ESRI.ArcGIS.GeoAnalyst.IConversionOp ThisConversionOp; string TempElvFCName, TempAggFCName, InternalErrors = "", TempClipFCName; IFeatureBuffer InsertBuffer; IFeatureCursor InsertCursor, TempCursor; IFeature TempFeature; int ShapeAreaIndex; double CurrentShapeArea, MaxShapeArea; int SurveyIDIndex; NPSGlobal NPS; NPS = NPSGlobal.Instance; TempElvFCName = "NPS_TEMP_TempElvPolyFCName"; TempAggFCName = "NPS_TEMP_TempElevationFC"; TempClipFCName = "NPS_TEMP_TempClipFCName"; ElvPolyFC = Util.GetFeatureClass(NPS.LYR_EXCLUDED_AREAS, ref ErrorMessage); if (string.IsNullOrEmpty(ErrorMessage) == false) return; BoundaryFC = Util.GetFeatureClass(NPS.LYR_SURVEY_BOUNDARY, ref ErrorMessage); if (string.IsNullOrEmpty(ErrorMessage) == false) return; SurveyIDIndex = ElvPolyFC.FindField("SurveyID"); if (SurveyIDIndex == -1) { ErrorMessage = "Could not find the SurveyID field on the Excluded Areas FeatureClass."; return; } Util.SetProgressMessage("Deleting old temp files"); Util.DeleteDataset(TempElvFCName, esriDatasetType.esriDTFeatureClass, ref InternalErrors); Util.DeleteDataset(TempClipFCName, esriDatasetType.esriDTFeatureClass, ref InternalErrors); Util.DeleteDataset(TempAggFCName, esriDatasetType.esriDTFeatureClass, ref InternalErrors); Util.DeleteDataset(TempClipFCName + "_Tbl", esriDatasetType.esriDTTable, ref InternalErrors); Util.DeleteDataset(TempAggFCName + "_Tbl", esriDatasetType.esriDTTable, ref InternalErrors); try { Util.SetProgressMessage("Converting DEM values to integers"); ThisMathOp = new ESRI.ArcGIS.SpatialAnalyst.RasterMathOpsClass(); IntRasterDS = ThisMathOp.Int(ClippedRasterDS); } catch (Exception ex) { ErrorMessage = "Error occured while converting DEM float slope values to slope integer values. " + ex.Message; return; } try { Util.SetProgressMessage("Building excluded area polygons from DEM"); //determine which values to exclude ThisQueryFilter = new QueryFilterClass(); if (IsAboveElevation) ThisQueryFilter.WhereClause = " Value >= " + MaximumElevInFeet; else ThisQueryFilter.WhereClause = " Value <= " + MaximumElevInFeet; ThisRasterDescriptor = new ESRI.ArcGIS.GeoAnalyst.RasterDescriptorClass(); ThisRasterDescriptor.Create(IntRasterDS as IRaster, ThisQueryFilter, "Value"); ThisConversionOp = new ESRI.ArcGIS.GeoAnalyst.RasterConversionOpClass(); NewElvPolyFC = ThisConversionOp.RasterDataToPolygonFeatureData(ThisRasterDescriptor as IGeoDataset, NPS.Workspace, TempElvFCName, true) as IFeatureClass; } catch (Exception ex) { ErrorMessage = "Error occured while generating a FeatureClass of flat " + "areas from the default DEM file. " + ex.Message; return; } try { Util.SetProgressMessage("Aggregating excluded area polygons"); ThisGeoProcessor = new ESRI.ArcGIS.Geoprocessing.GeoProcessor(); GPParams = new ESRI.ArcGIS.esriSystem.VarArrayClass(); GPParams.Add(System.IO.Path.Combine(NPS.DatabasePath, TempElvFCName)); GPParams.Add(System.IO.Path.Combine(NPS.DatabasePath, TempAggFCName)); GPParams.Add("1 Meters"); GeoResult = ThisGeoProcessor.Execute("AggregatePolygons_management", GPParams, null); } catch (Exception ex) { ErrorMessage = string.Format("Geoprocessor error:\r\nTask:AggregatePolygons_management\r\n" + "Params:{0},{1},{2}\r\nException:{3}", System.IO.Path.Combine(NPS.DatabasePath, TempElvFCName), System.IO.Path.Combine(NPS.DatabasePath, TempAggFCName), "1 Meters", ex.Message); ThisGeoProcessor = null; return; } ThisGeoProcessor = null; Util.DeleteLayerFromMap(TempAggFCName); NewElvPolyFC = Util.GetFeatureClass(TempAggFCName, ref ErrorMessage); if (string.IsNullOrEmpty(ErrorMessage) == false) { ErrorMessage = "Could not find temporary FeatureClass " + TempAggFCName + ". " + ErrorMessage; return; } Util.SetProgressMessage("Validating excluded area polygons"); NewElvPolyFC = Util.GP_Clip_analysis(TempClipFCName, NewElvPolyFC, BoundaryFC, ref ErrorMessage); if (string.IsNullOrEmpty(ErrorMessage) == false) return; InsertBuffer = ElvPolyFC.CreateFeatureBuffer(); InsertCursor = ElvPolyFC.Insert(true); ThisQueryFilter = null; ThisQueryFilter = new QueryFilterClass(); TempCursor = NewElvPolyFC.Search(ThisQueryFilter, false); ShapeAreaIndex = NewElvPolyFC.FindField(NewElvPolyFC.AreaField.Name); MaxShapeArea = (double)Util.SafeConvert(Util.GetFirstRecordValue(BoundaryFC, BoundaryFC.AreaField.Name, "SurveyID=" + SurveyID), typeof(double)); Util.SetProgressMessage("Importing excluded area polygons to Survey"); while ((TempFeature = TempCursor.NextFeature()) != null) { if (TempFeature.Shape == null) continue; CurrentShapeArea = (double)Util.SafeConvert(TempFeature.get_Value(ShapeAreaIndex), typeof(double)); if ((CurrentShapeArea / MaxShapeArea) >= 0.95) continue; InsertBuffer.Shape = TempFeature.ShapeCopy; InsertBuffer.set_Value(SurveyIDIndex, SurveyID); InsertCursor.InsertFeature(InsertBuffer); } TempCursor = null; InsertCursor = null; Util.SetProgressMessage("Cleaning up temp files"); Util.DeleteDataset(TempElvFCName, esriDatasetType.esriDTFeatureClass, ref InternalErrors); Util.DeleteDataset(TempClipFCName, esriDatasetType.esriDTFeatureClass, ref InternalErrors); Util.DeleteDataset(TempAggFCName, esriDatasetType.esriDTFeatureClass, ref InternalErrors); Util.DeleteDataset(TempClipFCName + "_Tbl", esriDatasetType.esriDTTable, ref InternalErrors); Util.DeleteDataset(TempAggFCName + "_Tbl", esriDatasetType.esriDTTable, ref InternalErrors); Util.CreateFillerPolygon(ElvPolyFC, SurveyID, ref InternalErrors); }
/// <summary> /// export default values table to a standonly dbase file at the specified file path /// </summary> public static void ExportDefaultValuesTable(string ExportTablePath, ref string ErrorMessage) { ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult GeoResult = null; ESRI.ArcGIS.Geoprocessing.IGeoProcessor ThisGeoProcessor = null; ESRI.ArcGIS.esriSystem.IVariantArray GPParams; ITable DefaultValuesTable; IWorkspace ShapeFileWorkspace; try { DefaultValuesTable = Util.GetDefaultValuesTable(ref ErrorMessage); if (string.IsNullOrEmpty(ErrorMessage) == false) return; ShapeFileWorkspace = Util.OpenShapeFileWorkspace(ExportTablePath, ref ErrorMessage); if (string.IsNullOrEmpty(ErrorMessage) == false) return; ThisGeoProcessor = new ESRI.ArcGIS.Geoprocessing.GeoProcessor(); GPParams = new ESRI.ArcGIS.esriSystem.VarArrayClass(); GPParams.Add(DefaultValuesTable); GPParams.Add(ShapeFileWorkspace); GPParams.Add("defaultvalues.dbf"); GeoResult = ThisGeoProcessor.Execute("TableToTable_conversion", GPParams, null); } catch (Exception ex) { ErrorMessage = "Error occured while exporting defaultvalues table. " + ex.Message; } }
public static void ExecuteGP(string GPToolName, List<object> Params, ref string ErrorMessage) { ESRI.ArcGIS.esriSystem.IVariantArray GPParams; ESRI.ArcGIS.Geoprocessing.IGeoProcessor ThisGeoProcessor = null; ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult GeoResult = null; NPSGlobal NPS; NPS = NPSGlobal.Instance; try { ThisGeoProcessor = new ESRI.ArcGIS.Geoprocessing.GeoProcessor(); GPParams = new ESRI.ArcGIS.esriSystem.VarArrayClass(); foreach (object ThisParam in Params) GPParams.Add(ThisParam); GeoResult = ThisGeoProcessor.Execute(GPToolName, GPParams, null); } catch { } ThisGeoProcessor = null; }