zoukankan      html  css  js  c++  java
  • C#+AO中调用ArcToolbox中自定义的模型

    我们知道,可以通过ArcGIS提供的Geoprocessing工具调用ArcToolbox中提供的各类工具,然而,如果是调用自定义的模型该如何设置?

    示例:根据高程点插值为面,然后进行坡度分析,最后以15度为界限进行重分类。

    在ArcGIS中ArcToolbox中新建模型,然后使用C#调用该模型。

    开发环境:VS2010,ArcGIS10.1,ArcObject10.1

    1.在ArcGIS中新建模型

    首先,在ArcToolbox中新建工具箱,然后在该工具新建模型,如图1所示。

    图1 新建模型

     然后,添加工具到该模型中,搜索IDW,Slope,Reclassify到该模型中,然后进行连接及参数设置,IDW右键,Make Variable,From Parammeter,Input point features,即完成添加输入参数。双击Reclassify,在Reclassification中设置重分类的属性,如图2所示。

    图2 Reclassify中Reclassification的设置

    需要注意的事,在输入Old values时,”-”前后需添加空格,而且最后一行NoData也是要添加的,否则会出现如图3所示错误。

    图3 输入Old values时未输入空格显示的错误提示

    最后,在Input point features和Output raster(3)分别右键选择Model Parameter命令,右上角出现“p”即为设置成功。

    至此,自定义建模完成,在ArcGIS中运行成功无误后便可进入下一步,调用自定义模型。

     

    2.调用自定义模型

    VS2010中,设计界面如图4所示。

    图4 设计界面图

    引用命名空间:

    ESRI.ArcGIS.AnalysisTools

    ESRI.ArcGIS.Geoprocessor

    ESRI.ArcGIS.System

    ESRI.ArcGIS.Version

    代码中加入:

    using ESRI.ArcGIS.Geoprocessor;
    using ESRI.ArcGIS.esriSystem;

     

    图5 错误一

    当出现如图5所示错误时,解决办法如下:

     1     static class Program
     2     {
     3         /// <summary>
     4         /// 应用程序的主入口点。
     5         /// </summary>
     6         [STAThread]
     7         static void Main()
     8         {
     9             Application.EnableVisualStyles();
    10             Application.SetCompatibleTextRenderingDefault(false);
    11             ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);
    12             Application.Run(new Form1());
    13         }
    14     }

    即在Program.cs中添加ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);

    图6 错误二

    解决办法为:Form1中添加License授权代码

    1 IAoInitialize pAoInitialize = new AoInitializeClass();
    2 esriLicenseStatus licenseStatus = esriLicenseStatus.esriLicenseUnavailable;
    3 licenseStatus = pAoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeAdvanced);
    4  pAoInitialize.CheckOutExtension(esriLicenseExtensionCode.esriLicenseExtensionCode3DAnalyst);

    至此,整个应用程序运行成功!效果如图7所示。

    图7 运行结果图

    最后,附上整个应用程序的代码:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Windows.Forms;
     9 
    10 using ESRI.ArcGIS.Geoprocessor;
    11 using ESRI.ArcGIS.esriSystem;
    12 
    13 namespace CallModel
    14 {
    15     public partial class Form1 : Form
    16     {
    17         public Form1()
    18         {
    19             IAoInitialize pAoInitialize = new AoInitializeClass();
    20             esriLicenseStatus licenseStatus = esriLicenseStatus.esriLicenseUnavailable;
    21             licenseStatus = pAoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeAdvanced);
    22             pAoInitialize.CheckOutExtension(esriLicenseExtensionCode.esriLicenseExtensionCode3DAnalyst);
    23 
    24             InitializeComponent();
    25         }
    26 
    27         private void Form1_Load(object sender, EventArgs e)
    28         {
    29 
    30         }
    31 
    32         private void button1_Click(object sender, EventArgs e)
    33         {
    34             OpenFileDialog openFileDialog = new OpenFileDialog();
    35             openFileDialog.Title = "打开文件";
    36             openFileDialog.Filter = "(shp文件)*.shp|*.shp";
    37             if (openFileDialog.ShowDialog() == DialogResult.OK)
    38             {
    39                 textBox1.Text = openFileDialog.FileName;
    40             }
    41         }
    42 
    43         private void button2_Click(object sender, EventArgs e)
    44         {
    45             SaveFileDialog saveDialog = new SaveFileDialog();
    46             saveDialog.Title = "保存文件";
    47             saveDialog.Filter = "(tif文件)*.tif|*.tif|(img文件)*.img|*.img";
    48             if (saveDialog.ShowDialog() == DialogResult.OK)
    49             {
    50                 textBox2.Text = saveDialog.FileName;
    51             }
    52         }
    53 
    54         private void button3_Click(object sender, EventArgs e)
    55         {
    56             Geoprocessor gp = new Geoprocessor();
    57             gp.OverwriteOutput = true;
    58 
    59             gp.AddToolbox(@"D:\Program Files\ArcGIS\Desktop10.1\ArcToolbox\Toolboxes\MyToolbar.tbx");
    60             IVariantArray parameters = new VarArrayClass();
    61             parameters.Add(textBox1.Text.ToString().Trim());
    62             parameters.Add(textBox2.Text.ToString().Trim());
    63             gp.Execute("Model", parameters, null);
    64             MessageBox.Show("调用成功!", "提示");
    65         }
    66 
    67         private void button4_Click(object sender, EventArgs e)
    68         {
    69             this.Close();
    70             //System.Environment.Exit(System.Environment.ExitCode);终止程序方式二
    71             //Application.Exit();终止程序方式三
    72         }
    73     }
    74 }

     

     

     

  • 相关阅读:
    app分析报告试验
    NABCD模式试验
    课堂练习
    学生管理系统测试计划及测试矩阵
    图书管理系统活动,时序图
    图书借阅测试用例UML图
    风险分析
    需求规格说明说
    PM的定义跟功能说明模块
    需求分析-NABCD
  • 原文地址:https://www.cnblogs.com/zhzhx/p/3061232.html
Copyright © 2011-2022 走看看