public void gpProgressChanged(object sender, ESRI.ArcGIS.Geoprocessor.ProgressChangedEventArgs e) { //System.Windows.Forms.ProgressBar progressBar = progressBar1; IGeoProcessorResult2 gpResult = (IGeoProcessorResult2)e.GPResult; switch (e.ProgressChangedType) { case (ProgressChangedType.Show): //The tool that is running reports progress or has stopped reporting progress; make the // progress bar visible if appropriate. //progressBar.Visible = e.Show; break; case (ProgressChangedType.Message): //The application does not use these, since a tool being used reports percentage progress. break; case (ProgressChangedType.Percentage): //progressBar.Value = (int) //e.ProgressPercentage; break; default: throw new ApplicationException( "unexpected ProgressChangedEventsArgs.ProgressChangedType"); break; } }
public void gpToolExecuted(object sender, ToolExecutedEventArgs e) { IGeoProcessorResult2 result = e.GPResult as IGeoProcessorResult2; if (result.Status.Equals(esriJobStatus.esriJobSucceeded)) { //Check that there are no information or warning messages. if (result.MaxSeverity == 0) { //Get the return value. object returnValue = result.ReturnValue; //Application specific code, //for example, find the layer to which this return value corresponds. } else { //Application specific code. } } else { //Get all messages. IGPMessages msgs = result.GetResultMessages(); for (int i = 0; i < result.MessageCount; i++) { IGPMessage2 msg = msgs.GetMessage(i) as IGPMessage2; //Application specific code. } } }
private void ExecuteGP(IGPProcess GPProcess) { Geoprocessor gp = new Geoprocessor { OverwriteOutput = true }; try { IGeoProcessorResult2 result = gp.Execute(GPProcess, null) as IGeoProcessorResult2; } catch (Exception ex) { MessageBox.Show(ex.Message, "GP Error"); } finally { System.Text.StringBuilder sb = new System.Text.StringBuilder(); for (int i = 0; i < gp.MessageCount; i++) { sb.AppendLine(gp.GetMessage(i)); } if (sb.Capacity > 0) { MessageBox.Show(sb.ToString(), "GP Messages"); } } }
/// <summary> /// Handles the ToolExecuting event. All tools start by firing this event. /// </summary> void _gp_ToolExecuting(object sender, ToolExecutingEventArgs e) { IGeoProcessorResult2 gpResult = (IGeoProcessorResult2)e.GPResult; listView1.Items.Add(new ListViewItem(new string[2] { "ToolExecuting", gpResult.Process.Tool.Name + " " + gpResult.Status.ToString() }, "information")); }
public static void Messages(IGeoProcessorResult2 result) { if (result.MessageCount <= 0) { return; } for (var i = 0; i < result.MessageCount; i++) { Console.WriteLine(result.GetMessage(i)); } }
/// <summary> /// Handles the ProgressChanged event. /// </summary> void _gp_ProgressChanged(object sender, ESRI.ArcGIS.Geoprocessor.ProgressChangedEventArgs e) { IGeoProcessorResult2 gpResult = (IGeoProcessorResult2)e.GPResult; if (e.ProgressChangedType == ProgressChangedType.Message) { listView1.Items.Add(new ListViewItem(new string[2] { "ProgressChanged", e.Message }, "information")); } }
public void gpToolExecuting(object sender, ToolExecutingEventArgs e) { IGeoProcessorResult2 result = e.GPResult as IGeoProcessorResult2; //Determine if this is the tool to handle this event. if (result.Process.Tool.Name.Equals("CopyFeatures_management") && result.GetInput(0) .GetAsText().Equals(FileGDBPath + "\\testPoint_10w") && result.GetOutput(0) .GetAsText().Equals(FileGDBPath + "\\testPoint_10w_Copy")) { //Application specific code. } }
public static void Messages(IGeoProcessorResult2 result) { if (result.MessageCount <= 0) { return; } for (var i = 0; i < result.MessageCount; i++) { _msg.Items.Add(result.GetMessage(i)); } _msg.Items.Add(">>>>>>>>>>>>>>>"); _msg.Refresh(); }
/// <summary> /// Handles the ToolExecuted event. All tools end by firing this event. If the tool executed successfully /// the next tool in the queue is removed and submitted for execution. If unsuccessful, geoprocessing is terminated, /// an error message is written to the ListViewControl and the is queue cleared. After the final tool has executed /// the result layer is added to the Map. /// </summary> void _gp_ToolExecuted(object sender, ToolExecutedEventArgs e) { IGeoProcessorResult2 gpResult = (IGeoProcessorResult2)e.GPResult; try { //The first GP tool has completed, if it was successful process the others if (gpResult.Status == esriJobStatus.esriJobSucceeded) { listView1.Items.Add(new ListViewItem(new string[2] { "ToolExecuted", gpResult.Process.Tool.Name }, "success")); //Execute next tool in the queue if (_myGPToolsToExecute.Count > 0) { _gp.ExecuteAsync(_myGPToolsToExecute.Dequeue()); } //If last tool has executed add the output layer to the map else { IFeatureClass resultFClass = _gp.Open(gpResult.ReturnValue) as IFeatureClass; IFeatureLayer resultLayer = new FeatureLayerClass(); resultLayer.FeatureClass = resultFClass; resultLayer.Name = resultFClass.AliasName; //Add the result to the map axMapControl1.AddLayer((ILayer)resultLayer, 2); axTOCControl1.Update(); //add the result layer to the List of result layers _resultsList.Add(resultLayer); } } //If the GP process failed, do not try to process any more tools in the queue else if (gpResult.Status == esriJobStatus.esriJobFailed) { //The actual GP error message will be output by the MessagesCreated event handler listView1.Items.Add(new ListViewItem(new string[2] { "ToolExecuted", gpResult.Process.Tool.Name + " failed, any remaining processes will not be executed." }, "error")); //Empty the queue _myGPToolsToExecute.Clear(); } } catch (Exception ex) { listView1.Items.Add(new ListViewItem(new string[2] { "ToolExecuted", ex.Message }, "error")); } }
/// <summary> /// 输出结果的过程信息 /// </summary> /// <param name="gpResult"></param> /// <returns></returns> public static string GetMessages(IGeoProcessorResult2 gpResult) { StringBuilder msgBuilder = new StringBuilder(); if (gpResult != null) { for (int i = 0; i < gpResult.MessageCount; i++) { msgBuilder.Append(gpResult.GetMessage(i)); msgBuilder.Append(" "); } } return msgBuilder.ToString(); }
/// <param name="pFeatureClass1">相交要素1</param> /// <param name="pFeatureClass2">相交要素2</param> /// <param name="gdbPath">数据库路径</param> private IFeatureClass Intersect(IFeatureClass pFeatureClass1, IFeatureClass pFeatureClass2, string gdbPath) { IFeatureClass pOutFeatureClass = null; try { //调用GP ESRI.ArcGIS.Geoprocessor.Geoprocessor gp = new ESRI.ArcGIS.Geoprocessor.Geoprocessor(); gp.OverwriteOutput = true; //多个对象的输入:使用IGpValueTableObject接口,该接口可以设置 IGpValueTableObject vtobject = new GpValueTableObjectClass(); object pFeature1 = pFeatureClass1; object pFeature2 = pFeatureClass2; vtobject.SetColumns(2); vtobject.AddRow(ref pFeature1); vtobject.AddRow(ref pFeature2); ESRI.ArcGIS.AnalysisTools.Intersect pIntersect = new ESRI.ArcGIS.AnalysisTools.Intersect(); pIntersect.in_features = vtobject; pIntersect.out_feature_class = System.Environment.CurrentDirectory + @"\temp\" + "temp.shp"; pIntersect.output_type = "Input"; IGeoProcessorResult2 result = (IGeoProcessorResult2)gp.Execute(pIntersect, null); if (result.Status != ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded) { MessageBox.Show("操作失败!"); return(null); } else { //获取结果; IFeatureClass resultFClass = gp.Open(result.ReturnValue) as IFeatureClass; IDataset pDataset = resultFClass as IDataset; //目标数据库; IWorkspaceFactory factory = new FileGDBWorkspaceFactoryClass(); IWorkspace objectWorkspace = factory.OpenFromFile(gdbPath, 0); string fileName = pFeatureClass1.AliasName + "_" + pFeatureClass2.AliasName; pOutFeatureClass = IFeatureDataConverter_ConvertFeatureClass(pDataset.Workspace, objectWorkspace, pDataset.Name, fileName); } } catch (System.Exception ex) { MessageBox.Show("操作失败!"); return(null); } return(pOutFeatureClass); }
/// <summary> /// 输出结果的过程信息 /// </summary> /// <param name="gpResult"></param> /// <returns></returns> public static string GetMessages(IGeoProcessorResult2 gpResult) { StringBuilder msgBuilder = new StringBuilder(); if (gpResult != null) { for (int i = 0; i < gpResult.MessageCount; i++) { msgBuilder.Append(gpResult.GetMessage(i)); msgBuilder.Append(" "); } } return(msgBuilder.ToString()); }
private void ToolStripMenuItem_RepairGeometry_Click(object sender, EventArgs e) { if (this.lstBox_Files.Items.Count <= 0) { return; } Cursor.Current = Cursors.WaitCursor; this.ChangeStatus(false); try { string path = string.Empty; string logPath = Path.Combine(GlobalVar.LogPath, string.Format("Repair_{0}.txt", DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss.ss"))); using (StreamWriter sw = new StreamWriter(logPath, true)) { Geoprocessor gp = new Geoprocessor(); gp.OverwriteOutput = true; RepairGeometry rg = new RepairGeometry(); foreach (var item in this.lstBox_Files.Items) { if (item == null) { continue; } path = item.ToString(); if (!File.Exists(path)) { continue; } rg.in_features = path; IGeoProcessorResult2 result = SpatialAnalysisHelper.RunGeoProcessor(gp, rg, null); string resStr = SpatialAnalysisHelper.GetMessages(result); sw.WriteLine(resStr); sw.Flush(); this.txtbox_log.Text += resStr + "\r\n"; this.txtbox_log.SelectionStart = this.txtbox_log.Text.Length; this.txtbox_log.ScrollToCaret(); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { this.ChangeStatus(true); Cursor.Current = Cursors.Default; } }
/// <summary> /// 运行地理分析工具 /// </summary> /// <param name="gp"></param> /// <param name="process"></param> /// <param name="trackCancel"></param> /// <returns></returns> public static IGeoProcessorResult2 RunGeoProcessor(Geoprocessor gp, IGPProcess process, ITrackCancel trackCancel) { IGeoProcessorResult2 gpResult = null; try { gpResult = gp.Execute(process, trackCancel) as IGeoProcessorResult2; //执行分析 return(gpResult); } catch (Exception ex) { #if DEBUG System.Diagnostics.Debug.WriteLine(string.Format("#RunGP:{0};{1}", ex.Message, ex.StackTrace)); #endif return(gpResult); } }
/// <summary> /// Runs a Geoprocessing tool. /// </summary> /// <param name="geoprocessor">The ArcGIS Geoprocessor defining the environment the Geoprocessing Tool will run in.</param> /// <param name="process">The Geoprocessing tool to run.</param> /// <returns>The result object of the Geoprocessing tool.</returns> public static object RunGpTool(Geoprocessor geoprocessor, IGPProcess process) { object result = null; try { bool tryAgain = false; int retryCount = 0; int maxRetries = 1; do { retryCount++; try { geoprocessor.ProgressChanged += Geoprocessor_ProgressChanged; geoprocessor.MessagesCreated += Geoprocessor_MessagesCreated; IGeoProcessorResult2 geoprocessorResult = geoprocessor.Execute(process, null) as IGeoProcessorResult2; result = geoprocessorResult.ReturnValue; tryAgain = false; } catch (Exception ex) { if (retryCount <= maxRetries) { tryAgain = true; } else { throw new Exception("Geoprocessing tool " + process.ToolName + " failed after " + retryCount + " attempts.", ex); } } }while (tryAgain); return(result); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.GetType().FullName + ": " + ex.Message); throw; } }
private void 获取报错信息ToolStripMenuItem_Click(object sender, EventArgs e) { Geoprocessor GP = new Geoprocessor(); GP.OverwriteOutput = true; // Create the tool process object. ESRI.ArcGIS.AnalysisTools.Buffer bufferTool = new ESRI.ArcGIS.AnalysisTools.Buffer(); // Set parameter values. //bufferTool.in_features = FileGDBPath + "\\LotIds"; bufferTool.in_features = FileGDBPath + "\\line"; //bufferTool.out_feature_class = FileGDBPath + "\\LotIds_BufferSystem"; bufferTool.out_feature_class = FileGDBPath + "\\line_Buffer"; bufferTool.buffer_distance_or_field = "100 Feet"; bufferTool.line_side = "LEFT"; bufferTool.line_end_type = "FLAT"; try { IGeoProcessorResult2 result = GP.Execute(bufferTool, null) as IGeoProcessorResult2; } catch (Exception ex) { MessageBox.Show(ex.Message, "GP Error"); } finally { System.Text.StringBuilder sb = new System.Text.StringBuilder(); for (int i = 0; i < GP.MessageCount; i++) { sb.AppendLine(GP.GetMessage(i)); } if (sb.Capacity > 0) { MessageBox.Show(sb.ToString(), "GP Messages"); } } }
private void 后台GPToolStripMenuItem_Click(object sender, EventArgs e) { Geoprocessor gp = new Geoprocessor(); gp.OverwriteOutput = true; gp.ToolExecuting += new EventHandler <ESRI.ArcGIS.Geoprocessor.ToolExecutingEventArgs>(gpToolExecuting); gp.ProgressChanged += new EventHandler <ESRI.ArcGIS.Geoprocessor.ProgressChangedEventArgs>(gpProgressChanged); //Register to receive the geoprocessor event when the tools have completed execution. gp.ToolExecuted += new EventHandler <ESRI.ArcGIS.Geoprocessor.ToolExecutedEventArgs>(gpToolExecuted); // Create a variant array to hold the parameter values. IVariantArray parameters = new VarArrayClass(); object sev = null; // Populate the variant array with parameter values. parameters.Add(FileGDBPath + "\\testPoint_10w"); parameters.Add(FileGDBPath + "\\testPoint_10w_Copy"); IGeoProcessorResult2 gpResult = gp.Execute("CopyFeatures_management", parameters, null) as IGeoProcessorResult2; }
private void 前后台GP对比ToolStripMenuItem_Click(object sender, EventArgs e) { //前台执行GP Geoprocessor gp = new Geoprocessor(); gp.OverwriteOutput = true; // Create a variant array to hold the parameter values. IVariantArray parameters = new VarArrayClass(); // Populate the variant array with parameter values. parameters.Add(FileGDBPath + "\\testPoint_100w"); parameters.Add(FileGDBPath + "\\testPoint_100w_CopyPre"); try { IGeoProcessorResult2 result = gp.ExecuteAsync("CopyFeatures_management", parameters) as IGeoProcessorResult2; } catch (Exception ex) { MessageBox.Show(ex.Message, "GP Error"); } finally { System.Text.StringBuilder sb = new System.Text.StringBuilder(); for (int i = 0; i < gp.MessageCount; i++) { sb.AppendLine(gp.GetMessage(i)); } if (sb.Capacity > 0) { MessageBox.Show(sb.ToString(), "GP Messages"); } } //后台执行GP // Create a variant array to hold the parameter values. IVariantArray parameters1 = new VarArrayClass(); // Populate the variant array with parameter values. parameters1.Add(FileGDBPath + "\\testPoint_100w"); parameters1.Add(FileGDBPath + "\\testPoint_100w_CopyBack"); try { IGeoProcessorResult2 result = gp.Execute("CopyFeatures_management", parameters1, null) as IGeoProcessorResult2; } catch (Exception ex) { MessageBox.Show(ex.Message, "GP Error"); } finally { System.Text.StringBuilder sb = new System.Text.StringBuilder(); for (int i = 0; i < gp.MessageCount; i++) { sb.AppendLine(gp.GetMessage(i)); } if (sb.Capacity > 0) { MessageBox.Show(sb.ToString(), "GP Messages"); } } }