private void iToolStripMenuItem_Click(object sender, EventArgs e) { // Initialize the geoprocessor. IGeoProcessor2 gp = new GeoProcessorClass(); gp.OverwriteOutput = true; // Add the BestPath toolbox. gp.AddToolbox(tbxPath); // Generate the array of parameters. IVariantArray parameters = new VarArrayClass(); parameters.Add(FileGDBPath + "\\LotIds"); parameters.Add("100 Feet"); parameters.Add(FileGDBPath + "\\LotIds_BufferCustomArray1"); object sev = null; try { // Execute the model tool by name. gp.Execute("Model2", parameters, null); Console.WriteLine(gp.GetMessages(ref sev)); } catch (Exception ex) { // Print geoprocessing execution error messages. Console.WriteLine(gp.GetMessages(ref sev)); } }
private void simpleButton4_Click(object sender, EventArgs e) { if (this.comboBoxEditInputRaster2.Text.ToString() == "" || this.comboBoxEditOutputRaster.Text.ToString() == "") { MessageBox.Show("请输入参数!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } string strResultsDBPath = m_pGDBHelper.GetResultsDBPath(); if (Utilities.GDBUtilites.CheckNameExist(strResultsDBPath, this.comboBoxEditOutputRaster.Text.ToString()) == false) { //执行R2V分析 ILayer pInputLayer1 = null; for (int i = 0; i < m_pMapCtrl.LayerCount; i++) { if (m_pMapCtrl.get_Layer(i).Name == this.comboBoxEditInputRaster2.Text.ToString()) { pInputLayer1 = m_pMapCtrl.get_Layer(i); } } if (pInputLayer1 != null) { IRasterLayer pRasterLayer1 = pInputLayer1 as IRasterLayer; IGeoProcessor2 pGP = new GeoProcessorClass(); pGP.AddToolbox(m_pGDBHelper.GetToolboxPath()); IVariantArray gpParameters = new VarArrayClass(); gpParameters.Add(pRasterLayer1.Raster); gpParameters.Add(m_pGDBHelper.GetResultsDBPath() + "\\" + this.comboBoxEditOutputRaster.Text.ToString()); IGeoProcessorResult pGeoProcessorResult = pGP.Execute("Raster2Vector", gpParameters, null); if (pGeoProcessorResult.Status == esriJobStatus.esriJobSucceeded) { if (this.checkBox1.Checked) { //IWorkspaceFactory2 pWKF = new FileGDBWorkspaceFactoryClass(); //IFeatureL pFW = (IFeatureWorkspace)pWKF.OpenFromFile(m_pGDBHelper.GetResultsDBPath() + "\\" + this.comboBoxEditOutputRaster.Text.ToString(), 0); //IRasterWorkspaceEx pRasterWKEx = (IRasterWorkspaceEx)pWKF.OpenFromFile(m_pGDBHelper.GetResultsDBPath(), 0); //IRasterDataset3 pRasterDataset = pRasterWKEx.OpenRasterDataset(this.comboBoxEditOutputRaster.Text.ToString()) as IRasterDataset3; //Utilities.MapUtilites.AddRasterLayer(m_pMapCtrl.ActiveView, pRasterDataset, null); } MessageBox.Show("转换完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } this.DialogResult = DialogResult.OK; this.Close(); } else { MessageBox.Show("已存在同名数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } }
static void Main(string[] args) { // Load the product code and version to the version manager ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop); //ESRI License Initializer generated code. m_AOLicenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeAdvanced }, new esriLicenseExtensionCode[] { }); // Create geoprocessor. Overwrite true will replace existing output IGeoProcessor2 gp = new GeoProcessorClass(); gp.OverwriteOutput = true; // Get the workspace from the user Console.WriteLine("Enter the path to folder where you copied the data folder."); Console.WriteLine("Example: C:\\AirportsAndGolf\\data"); Console.Write(">"); string wks = Console.ReadLine(); // set the workspace to the value user entered gp.SetEnvironmentValue("workspace", wks + "\\" + "golf.gdb"); // Add the custom toolbox to geoprocessor gp.AddToolbox(wks + "\\" + "Find Golf Courses.tbx"); // Create a variant - data are in the workspace IVariantArray parameters = new VarArrayClass(); parameters.Add("Airports"); parameters.Add("8 Miles"); parameters.Add("Golf"); parameters.Add("GolfNearAirports"); object sev = null; try { gp.Execute("GolfFinder", parameters, null); Console.WriteLine(gp.GetMessages(ref sev)); } catch (Exception ex) { Console.WriteLine(ex.Message); string errorMsgs = gp.GetMessages(ref sev); Console.WriteLine(errorMsgs); } finally { Console.WriteLine("Hit Enter to quit"); Console.ReadLine(); // pause the console to see messages } //ESRI License Initializer generated code. //Do not make any call to ArcObjects after ShutDownApplication() m_AOLicenseInitializer.ShutdownApplication(); }
public bool RebuildMosaicDataset(string mosaicDatasetName) { IMosaicDataset pMosaicDataset = OpenMosaicDataset(GetRastersDBPath(), mosaicDatasetName); IGeoProcessor2 pGP = new GeoProcessorClass(); pGP.AddToolbox(GetToolboxPath()); IVariantArray gpParameters = new VarArrayClass(); gpParameters.Add(pMosaicDataset); IGeoProcessorResult pGeoProcessorResult = pGP.Execute("ReseeBuildMosaicDataset", gpParameters, null); if (pGeoProcessorResult.Status == esriJobStatus.esriJobSucceeded) { return(true); } return(false); }
private void simpleButton4_Click(object sender, EventArgs e) { if (this.comboBoxInputRaster.Text.ToString() == "" || this.comboBoxContrastRaster.Text.ToString() == "" || this.textBoxOutputRaster.Text.ToString() == "") { MessageBox.Show("请输入参数!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; }//有任意一个空为空则弹出对话框提示:请输入参数。 string strResultsDBPath = m_pGDBHelper.GetResultsDBPath();//声明变量获取数据库结果路径 string ToolName = "ToolName"; if (comboBoxMethod.Text == "主成分差异法") { ToolName = "NewPCADiff"; } if (comboBoxMethod.Text == "多波段主成分变换法") { ToolName = "NewBandPCA"; } if (comboBoxMethod.Text == "差异主成分法") { ToolName = "NewDiffPCA"; } if (Utilities.GDBUtilites.CheckNameExist(strResultsDBPath, this.textBoxOutputRaster.Text.ToString()) == false)//如果没有同名文件 { //执行PCA分析 ILayer pInputLayer1 = null; ILayer pInputLayer2 = null; for (int i = 0; i < m_pMapCtrl.LayerCount; i++) { if (m_pMapCtrl.get_Layer(i).Name == this.comboBoxInputRaster.Text.ToString()) { pInputLayer1 = m_pMapCtrl.get_Layer(i); } else if (m_pMapCtrl.get_Layer(i).Name == this.comboBoxContrastRaster.Text.ToString()) { pInputLayer2 = m_pMapCtrl.get_Layer(i); } } if (pInputLayer1 != null && pInputLayer2 != null) { IRasterLayer pRasterLayer1 = pInputLayer1 as IRasterLayer; IRasterLayer pRasterLayer2 = pInputLayer2 as IRasterLayer; string rasterpath1 = pRasterLayer1.FilePath; string rasterpath2 = pRasterLayer2.FilePath; IGeoProcessor2 pGP = new GeoProcessorClass(); string GPPath = m_pGDBHelper.GetToolboxPath(); pGP.AddToolbox(GPPath); IVariantArray gpParameters = new VarArrayClass(); //gpParameters.Add(pRasterLayer1.Raster); //gpParameters.Add(pRasterLayer2.Raster); gpParameters.Add(rasterpath1); gpParameters.Add(rasterpath2); gpParameters.Add(strResultsDBPath + "\\" + this.textBoxOutputRaster.Text); IGeoProcessorResult pGeoProcessorResult = null; try { pGeoProcessorResult = pGP.Execute(ToolName, gpParameters, null); } catch (Exception ex) { if (ex is System.Runtime.InteropServices.COMException) { int errorCode = (ex as System.Runtime.InteropServices.COMException).ErrorCode; MessageBox.Show(errorCode.ToString()); } } if (pGeoProcessorResult.Status == esriJobStatus.esriJobSucceeded) { if (this.checkBoxAdd.Checked) { IWorkspaceFactory2 pWKF = new FileGDBWorkspaceFactoryClass(); IRasterWorkspaceEx pRasterWKEx = (IRasterWorkspaceEx)pWKF.OpenFromFile(m_pGDBHelper.GetResultsDBPath(), 0); IRasterDataset3 pRasterDataset = pRasterWKEx.OpenRasterDataset(this.textBoxOutputRaster.Text) as IRasterDataset3; Utilities.MapUtilites.AddRasterLayer(m_pMapCtrl.ActiveView, pRasterDataset, null); } MessageBox.Show("PCA分析完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } this.DialogResult = DialogResult.OK; this.Close(); } else { MessageBox.Show("已存在同名数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } }
private void simpleButton4_Click(object sender, EventArgs e) { if (this.comboBoxEditInputRaster1.Text.ToString() == "" || this.comboBoxEditInputRaster2.Text.ToString() == "" || this.comboBoxEditOutputRaster.Text.ToString() == "") { MessageBox.Show("请输入参数!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } string strResultsDBPath = m_pGDBHelper.GetResultsDBPath(); if (Utilities.GDBUtilites.CheckNameExist(strResultsDBPath, this.comboBoxEditOutputRaster.Text.ToString()) == false) { //执行PCA分析 ILayer pInputLayer1 = null; ILayer pInputLayer2 = null; for (int i = 0; i < m_pMapCtrl.LayerCount; i++) { if (m_pMapCtrl.get_Layer(i).Name == this.comboBoxEditInputRaster1.Text.ToString()) { pInputLayer1 = m_pMapCtrl.get_Layer(i); } else if (m_pMapCtrl.get_Layer(i).Name == this.comboBoxEditInputRaster2.Text.ToString()) { pInputLayer2 = m_pMapCtrl.get_Layer(i); } } if (pInputLayer1 != null && pInputLayer2 != null) { IRasterLayer pRasterLayer1 = pInputLayer1 as IRasterLayer; IRasterLayer pRasterLayer2 = pInputLayer2 as IRasterLayer; string RasterPath1 = pRasterLayer1.FilePath; string RasterPath2 = pRasterLayer2.FilePath; IGeoProcessor2 pGP = new GeoProcessorClass(); pGP.AddToolbox(m_pGDBHelper.GetToolboxPath()); IVariantArray gpParameters = new VarArrayClass(); //gpParameters.Add(pRasterLayer1.Raster); //gpParameters.Add(pRasterLayer2.Raster); gpParameters.Add(RasterPath1); gpParameters.Add(RasterPath2); gpParameters.Add(String.Format("{0}\\{1}", m_pGDBHelper.GetResultsDBPath(), this.comboBoxEditOutputRaster.Text)); string ToolName = "ReseePACBandMath"; ToolName = "NewDiffPCA"; IGeoProcessorResult pGeoProcessorResult = pGP.Execute(ToolName, gpParameters, null); if (pGeoProcessorResult.Status == esriJobStatus.esriJobSucceeded) { if (this.checkBox1.Checked) { IWorkspaceFactory2 pWKF = new FileGDBWorkspaceFactoryClass(); IRasterWorkspaceEx pRasterWKEx = (IRasterWorkspaceEx)pWKF.OpenFromFile(m_pGDBHelper.GetResultsDBPath(), 0); IRasterDataset3 pRasterDataset = pRasterWKEx.OpenRasterDataset(this.comboBoxEditOutputRaster.Text.ToString()) as IRasterDataset3; Utilities.MapUtilites.AddRasterLayer(m_pMapCtrl.ActiveView, pRasterDataset, null); } MessageBox.Show("PCA分析完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } this.DialogResult = DialogResult.OK; this.Close(); } else { MessageBox.Show("已存在同名数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } }
/// <summary> /// Called when a step of this type is executed in the workflow. /// </summary> /// <param name="JobID">ID of the job being executed</param> /// <param name="StepID">ID of the step being executed</param> /// <param name="argv">Array of arguments passed into the step's execution</param> /// <param name="ipFeedback">Feedback object to return status messages and files</param> /// <returns>Return code of execution for workflow path traversal</returns> public int Execute(int jobID, int stepID, ref object[] argv, ref IJTXCustomStepFeedback ipFeedback) { #if (!SERVER) StatusForm sfd = null; #endif bool success = true; // Reset the message logging this.ClearCachedLogMessages(); try { Log("ExecuteGPTool.Execute beginning.."); // Define variables string strToolboxPath = ""; string strTool = ""; string strToolboxRoot = ""; string strToolboxName = ""; // Get the toolbox name (check for errors) if (!StepUtilities.GetArgument(ref argv, "toolboxpath", true, out strToolboxPath) || strToolboxPath.Equals(String.Empty)) { success = false; Log("Error getting 'toolboxpath' argument"); #if (!SERVER) MessageBox.Show("Missing toolbox argument."); #endif } // Get the tool name (check for errors) else if (!StepUtilities.GetArgument(ref argv, "tool", true, out strTool) || strTool.Equals(String.Empty)) { success = false; Log("Error getting 'tool' argument"); #if (!SERVER) MessageBox.Show("Missing tool argument."); #endif } else { #if (!SERVER) // Display status dialog sfd = new StatusForm(); sfd.ToolName = strTool; sfd.Show(); sfd.Refresh(); #endif // Get directory of toolbox strToolboxRoot = Path.GetDirectoryName(strToolboxPath); // Get toolbox name without .tbx extension strToolboxName = Path.GetFileNameWithoutExtension(strToolboxPath); // Open toolbox and tool IWorkspaceFactory pToolboxWorkspaceFactory = new ToolboxWorkspaceFactoryClass(); IToolboxWorkspace pToolboxWorkspace = pToolboxWorkspaceFactory.OpenFromFile(strToolboxRoot, 0) as IToolboxWorkspace; IGPToolbox pGPToolbox = pToolboxWorkspace.OpenToolbox(strToolboxName); IGPTool pGPTool = pGPToolbox.OpenTool(strTool); Log("ExecuteGPTool.Execute successfully opened Tool " + strTool + " in Toolbox " + strToolboxPath + ".."); // Generate the arrays for parameters IVariantArray parameters = new VarArrayClass(); // For Execute method ESRI.ArcGIS.esriSystem.IArray pParameterArray = pGPTool.ParameterInfo; // For Validate and InvokeModal methods // Get parameter "pairs"; "GetDoubleArguments" supports the GP-style, // two-colon argument strings, like "/param:tool_param_name:tool_param_value" string[] argNames; string[] argValues; if (StepUtilities.GetDoubleArguments(ref argv, "param", true, out argNames, out argValues)) { // Stash away the variables that were passed in Dictionary<string, string> argTable = new Dictionary<string, string>(); for (int i = 0; i < argNames.Length; i++) { string uppercaseName = argNames[i].ToUpper(); argTable[uppercaseName] = argValues[i]; } // The GP tool's parameter list may include parameters that weren't specified in the // JTX step arguments. So iterate through the tool's parameters, setting the values // from the input where you can. Where no value was passed in, just skip the // parameter and go with the default. for (int i = 0; i < pParameterArray.Count; i++) { IGPParameter pGPParam = (IGPParameter)pParameterArray.get_Element(i); IGPParameterEdit pGPParamEdit = (IGPParameterEdit)pGPParam; string uppercaseName = pGPParam.Name.ToUpper(); // Override the default value, if something was passed in if (argTable.ContainsKey(uppercaseName)) { IGPDataType pGPType = pGPParam.DataType; string strValue = argTable[uppercaseName]; pGPParamEdit.Value = pGPType.CreateValue(strValue); Log("ExecuteGPTool.Execute Tool Parameter = " + uppercaseName + ", Value = " + strValue + ".."); } // Always stash away the current parameter value, since we need the complete list // for "Execute" below. parameters.Add(pGPParam.Value); } } Log("ExecuteGPTool.Execute successfully setup parameter values.."); // Initialize the geoprocessor IGeoProcessor pGP = new GeoProcessorClass(); // Create callback object for GP messages JTXGPCallback pCallback = new JTXGPCallback(); // Set up the geoprocessor pGP.AddToolbox(strToolboxPath); pGP.RegisterGeoProcessorEvents(pCallback); Log("ExecuteGPTool.Execute created and registered GeoProcessor.."); // Create the messages object and a bool to pass to InvokeModal method IGPMessages pGPMessages = pGPTool.Validate(pParameterArray, true, null); IGPMessages pInvokeMessages = new GPMessagesClass(); string strMessage = ""; // Check for error messages if (pGPMessages.MaxSeverity == esriGPMessageSeverity.esriGPMessageSeverityError) { #if (!SERVER) // Only want to invoke a modal dialog if we're running on a workstation // Set a reference to IGPCommandHelper2 interface IGPToolCommandHelper2 pToolHelper = new GPToolCommandHelperClass() as IGPToolCommandHelper2; pToolHelper.SetTool(pGPTool); bool pOK = true; // Open tool GUI pToolHelper.InvokeModal(0, pParameterArray, out pOK, out pInvokeMessages); if (pOK == true) { bool bFailureMessages; strMessage = ConvertGPMessagesToString(pInvokeMessages, out bFailureMessages); success = !bFailureMessages; } else { success = false; } #else Log("ExecuteGPTool.Execute Tool Validate failed.."); // If we're running on a server, then just indicate a failure. (Someone will // have to use the JTX application to fix the step arguments, if they can be // fixed.) success = false; #endif } else // If there are no error messages, execute the tool { Log("ExecuteGPTool.Execute successfully validated parameter values, About to Execute.."); IGPMessages ipMessages = new GPMessagesClass(); try { pGPTool.Execute(pParameterArray, null, new GPEnvironmentManagerClass(), ipMessages); Log("ExecuteGPTool.Execute completed call to pGPTool.Execute()"); } catch (System.Runtime.InteropServices.COMException ex) { success = false; Log("ExecuteGPTool.Execute Tool Execute failed, Message = " + ex.Message + ", DataCode = " + ex.ErrorCode + ", Data = " + ex.StackTrace); } // Get Messages bool bFailureMessages = false; strMessage += this.ConvertGPMessagesToString(ipMessages, out bFailureMessages); Log("ExecuteGPTool.Execute got messages from tool"); Log("*** GP MESSAGES ***" + System.Environment.NewLine + strMessage + System.Environment.NewLine); Log("*** END GP MESSAGES ***"); // If tool failed during execution, indicate a failure if (pGP.MaxSeverity == (int)esriGPMessageSeverity.esriGPMessageSeverityError) { success = false; Log("ExecuteGPTool.Execute Found Error messages from the tool.."); } } // Call AttachMsg try { IJTXJobManager ipJobManager = m_ipDatabase.JobManager; IJTXJob ipJob = ipJobManager.GetJob(jobID); AttachMsg(ipJob, strTool, this.GetCachedLogMessages()); this.ClearCachedLogMessages(); } catch (Exception ex) { string strEx = ex.Message; Log("Caught exception: " + ex.Message); } pGP.UnRegisterGeoProcessorEvents(pCallback); } } catch (Exception ex2) { success = false; Log("Caught exception: " + ex2.Message); #if (!SERVER) string msg = ""; if (ex2.InnerException == null) { msg = "Inner Exception is null"; } else { msg = "Inner Exception is not null"; } MessageBox.Show("Stack Trace: " + ex2.StackTrace + Environment.NewLine + "Message: " + ex2.Message + Environment.NewLine + "Source: " + ex2.Source + Environment.NewLine + msg); #endif } // Clean up #if (!SERVER) if (sfd != null) { sfd.Close(); } #endif // Indicate success or failure if (success == true) { return 1; } else { return 0; } }
private void simpleButton4_Click(object sender, EventArgs e) { if (this.comboBoxInputRaster.Text.ToString() == "" || this.comboBoxContrastRaster.Text.ToString() == "" || this.comboBoxOutputRaster.Text.ToString() == "") { MessageBox.Show("请输入参数!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; }//有任意一个空为空则弹出对话框提示:请输入参数。 string strResultsDBPath = m_pGDBHelper.GetResultsDBPath();//声明变量获取数据库结果路径 //string ToolName = "变化向量角5"; if (Utilities.GDBUtilites.CheckNameExist(strResultsDBPath, this.comboBoxOutputRaster.Text.ToString()) == false)//如果没有同名文件 { //执行光谱角分析 ILayer pInputLayer1 = null; ILayer pInputLayer2 = null; for (int i = 0; i < m_pMapCtrl.LayerCount; i++) { if (m_pMapCtrl.get_Layer(i).Name == this.comboBoxInputRaster.Text.ToString()) { pInputLayer1 = m_pMapCtrl.get_Layer(i); } else if (m_pMapCtrl.get_Layer(i).Name == this.comboBoxContrastRaster.Text.ToString()) { pInputLayer2 = m_pMapCtrl.get_Layer(i); } } if (pInputLayer1 != null && pInputLayer2 != null) { IRasterLayer pRasterLayer1 = pInputLayer1 as IRasterLayer; IRasterLayer pRasterLayer2 = pInputLayer2 as IRasterLayer; string rasterpath1 = pRasterLayer1.FilePath; string rasterpath2 = pRasterLayer2.FilePath; IGeoProcessor2 pGP = new GeoProcessorClass(); string GPPath = m_pGDBHelper.GetToolboxPath(); pGP.AddToolbox(GPPath); IVariantArray gpParameters = new VarArrayClass(); //gpParameters.Add(pRasterLayer1.Raster); //gpParameters.Add(pRasterLayer2.Raster); gpParameters.Add(rasterpath1); gpParameters.Add(rasterpath2); gpParameters.Add(strResultsDBPath + "\\" + this.comboBoxOutputRaster.Text); IGeoProcessorResult pGeoProcessorResult = null; pGeoProcessorResult = pGP.Execute("changevector", gpParameters, null); if (pGeoProcessorResult.Status == esriJobStatus.esriJobSucceeded) { if (this.checkBox1.Checked) { IWorkspaceFactory2 pWKF = new FileGDBWorkspaceFactoryClass(); IRasterWorkspaceEx pRasterWKEx = (IRasterWorkspaceEx)pWKF.OpenFromFile(m_pGDBHelper.GetResultsDBPath(), 0); IRasterDataset3 pRasterDataset = pRasterWKEx.OpenRasterDataset(this.comboBoxOutputRaster.Text.ToString()) as IRasterDataset3; Utilities.MapUtilites.AddRasterLayer(m_pMapCtrl.ActiveView, pRasterDataset, null); } MessageBox.Show("变化向量分析完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } this.DialogResult = DialogResult.OK; this.Close(); } else { MessageBox.Show("已存在同名数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } }