PIE SDK算法的自定义扩展

 

1.算法功能简介

    算法的自定义扩展允许用户自主开发新的算法。自定义的算法必须实现PIE.SystemAlgo.BaseSystemAlgo基础类,这样才能被PIE的算法管理器调用起来。

2.算法功能实现说明

2.1. 示例简介

    本算法用于实现栅格影像的拷贝。参数类“AlgoParams.cs”用于存放要拷贝的路径和拷贝到的路径,算法类“Algo.cs”用于执行算法,窗体类“FormTest.cs”用于接收用户输入的要拷贝的路径和拷贝到的路径,Command类“AlgoCommand.cs”用于实现算法调用。

    算法类“Algo.cs”继承自BaseSystemAlgo,该类实现了ISystemAlgo, ISystemAlgoEvents两个接口。以下对BaseSystemAlgo类的属性,方法和事件进行介绍:

属性介绍

Description

String

描述

Name

String

名称

Params

String

参数

方法介绍

Execute()

Bool

执行算法,返回结果:是否执行成功

GetErrorInfo(ref int errCode, ref string errMsg)

Void

获取错误信息,参数: errCode:错误信息编号,errMsg:错误信息描述

事件介绍

OnExecuteCompleted

OnExecuteCompletedEventHandler

执行完成事件

OnProgressChanged

OnProgressChangedEventHandler

进度变化事件

2.2. 实现步骤

第一步

编写参数类“AlgoParams.cs”

第二步

构造算法类“Algo.cs”

第三步

新建窗体类“FormTest.cs”

第四步

写Command类实现算法调用

2.3. 示例代码

项目路径

百度云盘地址下/PIE示例程序/10.算法调用/算法的调用方式/Algo.AlgoTest

数据路径

百度云盘地址下/PIE示例数据/栅格数据/04.World/ World.tif

视频路径

百度云盘地址下/PIE视频教程/10.算法调用/算法的调用方式/算法的自定义扩展.avi

示例代码

  1         //(一)编写参数类“AlgoParams.cs”
  2          public class AlgoParams
  3     {
  4         /// <summary>
  5         /// 要拷贝的路径
  6         /// </summary>
  7         public string OpenFilePath;
  8 
  9         /// <summary>
 10         /// 拷贝到的路径
 11         /// </summary>
 12         public string SaveFilePath;
 13     }
 14          //(二)构造算法类“Algo.cs”
 15 public class Algo : PIE.SystemAlgo.BaseSystemAlgo
 16     {
 17         /// <summary>
 18         /// 构造函数
 19         /// </summary>
 20         public Algo()
 21         {
 22 
 23         }
 24 
 25         /// <summary>
 26         /// 执行算法
 27         /// </summary>
 28         /// <returns>是否执行成功</returns> 
 29         public override bool Execute()
 30         {
 31             AlgoParams m_AlgoParams = (AlgoParams)this.Params;
 32             if (string.IsNullOrEmpty(m_AlgoParams.OpenFilePath) || string.IsNullOrEmpty(m_AlgoParams.SaveFilePath)) return false;
 33             File.Copy(m_AlgoParams.OpenFilePath, m_AlgoParams.SaveFilePath, true);
 34             return true;
 35         }
 36     }
 37          //(三)新建窗体类“FormTest.cs”
 38          public partial class FormTest : Form
 39     {
 40         /// <summary>
 41         /// 算法参数
 42         /// </summary>
 43         private AlgoTest.AlgoParams m_Parm = new AlgoParams();
 44 
 45         /// <summary>
 46         /// 构造函数
 47         /// </summary>
 48         public FormTest()
 49         {
 50             InitializeComponent();
 51         }
 52 
 53         /// <summary>
 54         /// 算法参数
 55         /// </summary>
 56         public AlgoParams Params
 57         {
 58             get { return m_Parm; }
 59             set { this.m_Parm = value; }
 60         }
 61 
 62         private void buttonEdit_save_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
 63         {
 64             SaveFileDialog save = new SaveFileDialog();
 65             save.Title = "另存为";
 66             save.Filter = "Raster Files|*.tif";
 67             save.FileName = "";
 68             if (save.ShowDialog() == DialogResult.OK)
 69             {
 70                 this.buttonEdit_save.Text = save.FileName;
 71             }
 72         }
 73 
 74         private void simpleButton_ok_Click(object sender, EventArgs e)
 75         {
 76             if (string.IsNullOrEmpty(this.buttonEdit_tif.Text) || string.IsNullOrEmpty(this.buttonEdit_save.Text))
 77             {
 78                 MessageBox.Show("请输入路径!");
 79                 return;
 80             }
 81             m_Parm.OpenFilePath = this.buttonEdit_tif.Text;
 82             m_Parm.SaveFilePath = this.buttonEdit_save.Text;
 83             this.DialogResult = DialogResult.OK;
 84         }
 85 
 86         private void simpleButton_cancel_Click(object sender, EventArgs e)
 87         {
 88             this.Close();
 89         }
 90 
 91         private void buttonEdit_tif_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
 92         {
 93             OpenFileDialog open = new OpenFileDialog();
 94             open.Filter = "Raster Files|*.tif";
 95             open.Title = "选择栅格文件";
 96             open.Multiselect = false;
 97             if (open.ShowDialog() != DialogResult.OK) return;
 98             string filePath = open.FileName;
 99 
100             IRasterDataset dataSet = DatasetFactory.OpenRasterDataset(filePath, OpenMode.ReadOnly);
101             if (dataSet == null)
102             {
103                 MessageBox.Show("文件打开失败!");
104                 return;
105             }
106             if (!dataSet.Open(filePath, OpenMode.ReadOnly))
107             {
108                 MessageBox.Show("文件打开失败!");
109                 return;
110             }
111             this.buttonEdit_tif.Text = filePath;
112         }
113 }
114          //(四)写Command类实现算法调用
115          public class AlgoCommand: DesktopCommand
116     {
117         /// <summary>
118         /// 构造函数
119         /// </summary>
120         public AlgoCommand()
121         {
122             this.Caption = "测试算法";
123             this.Name = "AlgoCommand";
124             this.ToolTip = "测试算法";
125             this.Checked = false;
126             this.Enabled = false;
127         }
128 
129         /// <summary>
130         /// 单击方法
131         /// </summary>
132         public override void OnClick()
133         {
134             #region 1、参数设置
135             if (!this.Enabled) return;
136             FormTest frmTest = new FormTest();
137             if (frmTest.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
138 
139             AlgoParams info = frmTest.Params;
140             PIE.SystemAlgo.ISystemAlgo algo = PIE.SystemAlgo.AlgoFactory.Instance().CreateAlgo("AlgoTest.dll", "AlgoTest.Algo");
141             if (algo == null) return;
142 
143             PIE.SystemAlgo.ISystemAlgoEvents algoEvents = algo as PIE.SystemAlgo.ISystemAlgoEvents;
144             algo.Name = "测试算法";
145             algo.Params = info;
146             algoEvents.OnExecuteCompleted += OnAlgoExcuteCompleted;
147             algoEvents.OnProgressChanged += OnAlgoProgressChanged;
148             #endregion
149 
150             //2、算法执行
151             PIE.AxControls.IStatusBar statusBar = m_Application.StatusBar;
152             statusBar.ShowProgress(0, 100, "");
153             PIE.SystemAlgo.AlgoFactory.Instance().AsynExecuteAlgo(algo);
154         }
155 
156         /// <summary>
157         /// 算法执行完成事件
158         /// </summary>
159         /// <param name="algo">算法</param>
160         private void OnAlgoExcuteCompleted(PIE.SystemAlgo.ISystemAlgo algo)
161         {
162             PIE.AxControls.IStatusBar statusBar = m_Application.StatusBar;
163             statusBar.UpdateProgress(100, "");
164             statusBar.HideProgress();
165 
166             //3、结果显示
167             AlgoParams info = (AlgoParams)algo.Params;
168             PIE.Carto.ILayer layer = PIE.Carto.LayerFactory.CreateDefaultLayer(info.SaveFilePath);
169             m_HookHelper.FocusMap.AddLayer(layer);
170             m_HookHelper.ActiveView.PartialRefresh(PIE.Carto.ViewDrawPhaseType.ViewAll);
171 
172             PIE.SystemAlgo.ISystemAlgoEvents algoEvents = algo as PIE.SystemAlgo.ISystemAlgoEvents;
173             algoEvents.OnExecuteCompleted -= OnAlgoExcuteCompleted;
174             algoEvents.OnProgressChanged -= OnAlgoProgressChanged;
175         }
176 
177         /// <summary>
178         /// 算法进度变化事件
179         /// </summary>
180         /// <param name="complete">进度</param>
181         /// <param name="msg">进度信息</param>
182         /// <param name="algo">算法</param>
183         /// <returns>int</returns>
184         private int OnAlgoProgressChanged(double complete, string msg, PIE.SystemAlgo.ISystemAlgo algo)
185         {
186             PIE.AxControls.IStatusBar statusBar = m_Application.StatusBar;
187             statusBar.UpdateProgress((int)complete, msg);
188             return 0;
189         }
190     }
View Code

2.4. 示例截图

posted @ 2019-01-07 10:22  PIESAT  阅读(1211)  评论(7编辑  收藏  举报