• AE二次开发中几个功能速成归纳(符号设计器、创建要素、图形编辑、属性表编辑、缓冲区分析)


    /*
     * 实习课上讲进阶功能所用文档,因为赶时间从网上抄抄改改,凑合能用,记录一下以备个人后用。
     *
     * -------------------------------------------------------------------
     *
     * 使用前提:已搭建好AE的GIS基本框架,包括TOC、mapcontrol、toolbar拖控件,mxd、shp文件载入显示,查看图层属性表等
     *
     * -------------------------------------------------------------------
     */
    /* Form1中的using */
    
    using System;
    
    using System.Windows.Forms;
    
    using ESRI.ArcGIS.Carto;
    
    using ESRI.ArcGIS.Controls;
    
    using ESRI.ArcGIS.Display;
    
    using ESRI.ArcGIS.Geometry;
    
    using ESRI.ArcGIS.Geodatabase;
    
    using ESRI.ArcGIS.SystemUI;
    
    using ESRI.ArcGIS.esriSystem;
    
    using System.Drawing;
    
    using System.Runtime.InteropServices;
    
    using ESRI.ArcGIS.DataSourcesFile;
    
    using System.Collections;
    
    
    /*
     * 一、符号设计
     *
     * 在TOC的doubleclick事件中写入:
     */
    
    esriTOCControlItem toccItem = esriTOCControlItem.esriTOCControlItemNone;
    
    ILayer iLayer = null;
    
    IBasicMap iBasicMap = null;
    
    object unk = null;
    
    object data = null;
    
    axTOCControl1.HitTest( e.x, e.y, ref toccItem, ref iBasicMap, ref iLayer, ref unk, ref data ); /* 获取所点击图例及其图层 */
    
    if ( e.button == 1 )
    
    {
        if ( toccItem == esriTOCControlItem.esriTOCControlItemLegendClass )
    
        {
            ILegendClass pLC = new LegendClassClass();
    
            pLC = ( (ILegendGroup) unk).get_Class( (int) data ); /* 获取图例 */
    
            ISymbol pSym = pLC.Symbol; /* 获取图例的符号 */
    
            ESRI.ArcGIS.DisplayUI.ISymbolSelector pSS = new ESRI.ArcGIS.DisplayUI.SymbolSelectorClass(); /* 创建符号选择器 */
    
            bool a = false;
    
            pSS.AddSymbol( pSym );
    
            a = pSS.SelectSymbol( 0 ); /* 打开符号选择器 */
    
            if ( a )
    
            {
                pLC.Symbol = pSS.GetSymbolAt( 0 );
            }
    
            this.axMapControl1.ActiveView.Refresh();
    
            this.axTOCControl1.Refresh();
        }
    }
    
    
    /*
     * 二、创建要素
     *
     * 1.创建并添加shp新图层
     */
    
    /* 点shp的创建并添加 */
    
    private void 点ToolStripMenuItem_Click( object sender, EventArgs e )
    
    {
        string pointshppath = "";
    
        SaveFileDialog spointdlg = new SaveFileDialog(); /* 打开保存文件对话框,设置保存路径和shp文件名 */
    
        if ( spointdlg.ShowDialog() == DialogResult.OK )
    
        {
            pointshppath = spointdlg.FileName;
        }else {
            return;
        }
    
    /* 准备好要素类空对象 */
    
        IFeatureClass m_pointfeatureclass = null;
    
    /* 从文件路径中分解出文件夹路径和文件名称 */
    
        int count = pointshppath.LastIndexOf ("");
    
    string folder = pointshppath.Substring(0, count);
    
    string name = pointshppath.Substring(count + 1, pointshppath.Length - count - 1);
    
    //根据文件夹路径创建工作空间工厂和工作空间
    
    IWorkspace ipws;
    
    IWorkspaceFactory ipwsf = new ShapefileWorkspaceFactoryClass();
    
    ipws = ipwsf.OpenFromFile(folder, 0);
    
    //转为要素工作空间
    
    IFeatureWorkspace ifeatws;
    
    ifeatws = ipws as IFeatureWorkspace;
    
    //对shp文件的一些必要设置,除了红字部分外都不用改
    
    IFields pFields = new FieldsClass();
    
    IField pField = new FieldClass();
    
    IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
    
    IFieldEdit pFieldEdit = pField as IFieldEdit;
    
    IGeometryDef ipGeodef = new GeometryDefClass();
    
    IGeometryDefEdit ipGeodefEdit = ipGeodef as IGeometryDefEdit;
    
    ISpatialReference ipSpatialRef;
    
    IUnknownCoordinateSystem iunknowncoord = new UnknownCoordinateSystemClass();
    
    ipSpatialRef = iunknowncoord;
    
    ipGeodefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;//确定你要生成的shp的几何类型(点线面)
    
    ipSpatialRef.SetMDomain(-10000, 10000);
    
    ipGeodefEdit.HasM_2 = false;
    
    ipGeodefEdit.HasZ_2 = false;
    
    ipGeodefEdit.SpatialReference_2 = ipSpatialRef;
    
    pFieldEdit.Name_2 = "Shape ";
    
    pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
    
    pFieldEdit.GeometryDef_2 = ipGeodef;
    
    pFieldsEdit.AddField(pField);
    
    //////////////////////////////////////////
    
    //创建要素类并导出shp文件于预设文件路径
    
    m_pointfeatureclass = ifeatws.CreateFeatureClass(name, pFields, null, null, esriFeatureType.esriFTSimple, "Shape ", " ");
    
    //加载新创建的shp文件并调到图层显示顺序的最顶层
    
    axMapControl1.AddShapeFile(folder,name);
    
    axMapControl1.MoveLayerTo(0, axMapControl1.LayerCount - 1);
    
    return;
    
    }
    
    //线shp的创建并添加
    
    private void 线ToolStripMenuItem_Click(object sender, EventArgs e)
    
    {
    
    string lineshppath = " ";
    
    SaveFileDialog slinedlg = new SaveFileDialog();//打开保存文件对话框,设置保存路径和shp文件名
    
    if (slinedlg.ShowDialog() == DialogResult.OK)
    
    {
    
    lineshppath = slinedlg.FileName;
    
    }
    
    else
    
    {
    
    return;
    
    }
    
    //准备好要素类空对象
    
    IFeatureClass m_linefeatureclass = null;
    
    //从文件路径中分解出文件夹路径和文件名称
    
    int count = lineshppath.LastIndexOf("  ");
    
    string folder = lineshppath.Substring(0, count);
    
    string name = lineshppath.Substring(count + 1, lineshppath.Length - count - 1);
    
    //根据文件夹路径创建工作空间工厂和工作空间
    
    IWorkspace ipws;
    
    IWorkspaceFactory ipwsf = new ShapefileWorkspaceFactoryClass();
    
    ipws = ipwsf.OpenFromFile(folder, 0);
    
    //转为要素工作空间
    
    IFeatureWorkspace ifeatws;
    
    ifeatws = ipws as IFeatureWorkspace;
    
    //对shp文件的一些必要设置,除了红字部分外都不用改
    
    IFields pFields = new FieldsClass();
    
    IField pField = new FieldClass();
    
    IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
    
    IFieldEdit pFieldEdit = pField as IFieldEdit;
    
    IGeometryDef ipGeodef = new GeometryDefClass();
    
    IGeometryDefEdit ipGeodefEdit = ipGeodef as IGeometryDefEdit;
    
    ISpatialReference ipSpatialRef;
    
    IUnknownCoordinateSystem iunknowncoord = new UnknownCoordinateSystemClass();
    
    ipSpatialRef = iunknowncoord;
    
    ipGeodefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolyline;//确定你要生成的shp的几何类型(点线面)
    
    ipSpatialRef.SetMDomain(-10000, 10000);
    
    ipGeodefEdit.HasM_2 = false;
    
    ipGeodefEdit.HasZ_2 = false;
    
    ipGeodefEdit.SpatialReference_2 = ipSpatialRef;
    
    pFieldEdit.Name_2 = "Shape ";
    
    pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
    
    pFieldEdit.GeometryDef_2 = ipGeodef;
    
    pFieldsEdit.AddField(pField);
    
    //////////////////////////////////////////
    
    //创建要素类并导出shp文件于预设文件路径
    
    m_linefeatureclass = ifeatws.CreateFeatureClass(name, pFields, null, null, esriFeatureType.esriFTSimple, "Shape ", " ");
    
    //加载新创建的shp文件并调到图层显示顺序的最顶层
    
    axMapControl1.AddShapeFile(folder,name);
    
    axMapControl1.MoveLayerTo(0, axMapControl1.LayerCount - 1);
    
    return;
    
    }
    
    //面shp的创建并添加
    
    private void 面ToolStripMenuItem_Click(object sender, EventArgs e)
    
    {
    
    string polygonshppath = " ";
    
    SaveFileDialog spolygondlg = new SaveFileDialog();//打开保存文件对话框,设置保存路径和shp文件名
    
    if (spolygondlg.ShowDialog() == DialogResult.OK)
    
    {
    
    polygonshppath = spolygondlg.FileName;
    
    }
    
    else
    
    {
    
    return;
    
    }
    
    //准备好要素类空对象
    
    IFeatureClass m_polygonfeatureclass = null;
    
    //从文件路径中分解出文件夹路径和文件名称
    
    int count = polygonshppath.LastIndexOf("  ");
    
    string folder = polygonshppath.Substring(0, count);
    
    string name = polygonshppath.Substring(count + 1, polygonshppath.Length - count - 1);
    
    //根据文件夹路径创建工作空间工厂和工作空间
    
    IWorkspace ipws;
    
    IWorkspaceFactory ipwsf = new ShapefileWorkspaceFactoryClass();
    
    ipws = ipwsf.OpenFromFile(folder, 0);
    
    //转为要素工作空间
    
    IFeatureWorkspace ifeatws;
    
    ifeatws = ipws as IFeatureWorkspace;
    
    //对shp文件的一些必要设置,除了红字部分外都不用改
    
    IFields pFields = new FieldsClass();
    
    IField pField = new FieldClass();
    
    IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
    
    IFieldEdit pFieldEdit = pField as IFieldEdit;
    
    IGeometryDef ipGeodef = new GeometryDefClass();
    
    IGeometryDefEdit ipGeodefEdit = ipGeodef as IGeometryDefEdit;
    
    ISpatialReference ipSpatialRef;
    
    IUnknownCoordinateSystem iunknowncoord = new UnknownCoordinateSystemClass();
    
    ipSpatialRef = iunknowncoord;
    
    ipGeodefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;//确定你要生成的shp的几何类型(点线面)
    
    ipSpatialRef.SetMDomain(-10000, 10000);
    
    ipGeodefEdit.HasM_2 = false;
    
    ipGeodefEdit.HasZ_2 = false;
    
    ipGeodefEdit.SpatialReference_2 = ipSpatialRef;
    
    pFieldEdit.Name_2 = "Shape ";
    
    pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
    
    pFieldEdit.GeometryDef_2 = ipGeodef;
    
    pFieldsEdit.AddField(pField);
    
    //////////////////////////////////////////
    
    //创建要素类并导出shp文件于预设文件路径
    
    m_polygonfeatureclass = ifeatws.CreateFeatureClass(name, pFields, null, null, esriFeatureType.esriFTSimple, "Shape ", " ");
    
    //加载新创建的shp文件并调到图层显示顺序的最顶层
    
    axMapControl1.AddShapeFile(folder,name);
    
    axMapControl1.MoveLayerTo(0, axMapControl1.LayerCount - 1);
    
    return;
    
    }
    
    
    /*
    2.在shp中绘制点线面要素并储存
    
    课上只提到调用Toolbar里的工具:
    
    使用流程:
    
    开始编辑——>选择目标图层——>开启草图工具——>绘制新图形——>保存并停止编辑
    
    而写代码方式的主要思路如下(没做撤销和双缓冲):
    */
    //获取MapControl中的全部图层名称,并加入ComboBox
    
     //图层
    
     toolStripComboBox1.Visible = true;
    
     ILayer pLayer;
    
     //图层名称
    
     string strLayerName;
    
     for (int i = 0; i < this.axMapControl1.LayerCount; i++)
    
                {
    
                    pLayer = this.axMapControl1.get_Layer(i);
    
                    strLayerName = pLayer.Name;
    
                    //图层名称加入ComboBox
    
                    this.toolStripComboBox1.Items.Add(strLayerName);
    
                }
    
                //默认显示第一个选项
    
    
    
                this.toolStripComboBox1.SelectedIndex = 0;
    
    
    
    //用三个int成员变量drawpoint、drawline、drawregion指示添加的是点、线还是面
    
    //用三个IFeatureClass成员变量startpointshp、startlineshp、startpolygonshp来取出所选图层的要素类
    
    //用一个点集数列IPointArray pts存储画线、面时连续产生的节点
    
    //当combobox中选项变化时判断所选图层的要素类的几何类型并取出
    
    IFeatureLayer layer = axMapControl1.get_Layer(this.toolStripComboBox1.SelectedIndex) as IFeatureLayer;
    
                if (layer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPoint)
    
                {
    
                    startpointshp = layer.FeatureClass;
    
                    drawpoint = 1;
    
                    drawline = 0;
    
                    drawregion = 0;
    
                }
    
                if (layer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolyline)
    
                {
    
                    startlineshp = layer.FeatureClass;
    
                    drawpoint = 0;
    
                    drawline = 1;
    
                    drawregion = 0;
    
                }
    
                if (layer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon)
    
                {
    
                    startpolygonshp = layer.FeatureClass;
    
                    drawpoint = 0;
    
                    drawline = 0;
    
                    drawregion = 1;
    
    
    
                }
    //在onmousedown(onmouseup也行)中进行判断并创建新要素图形
    if (e.button == 2 &&drawline ==0 &&drawpoint==0 && drawregion==0)
    
                {
    
                    m_menuMap.PopupMenu(e.x, e.y, m_mapControl.hWnd);//没开启添加要素功能,则正常弹菜单项
    
                }
    
                if (e.button == 1 && drawpoint == 1)
    
                {
    
    IPoint pt;
    
                    pt = axMapControl1.ToMapPoint(e.x, e.y);
    
                    IFeature pFeature = startpointshp.CreateFeature();
    
                    pFeature.Shape = pt;
    
                    pFeature.Store();
    
                    this.axMapControl1.ActiveView.Refresh();
    
                    return;
    
    
    
                }
    
                if (e.button == 1 && drawline == 1)//左键创建线的节点
    
                {
    
                    IPoint pt;
    
                    pt = axMapControl1.ToMapPoint(e.x, e.y);
    
                    pts.Add(pt);
    
                    return;
    
                }
    
                if (e.button == 2 && drawline == 1)//右键根据节点创建线
    
                {
    
                    ESRI.ArcGIS.Geometry.IPolyline ipPolyline = new PolylineClass();
    
                    ESRI.ArcGIS.Geometry.IPointCollection ipPointCol = (IPointCollection)ipPolyline;
    
                    object missing = Type.Missing;
    
                    for (int i = 0; i < pts.Count; i++)
    
                    {
    
                        object t = pts.get_Element(i);
    
                        ESRI.ArcGIS.Geometry.Point p = (ESRI.ArcGIS.Geometry.Point)t;
    
                        if (p != null)
    
                        {
    
                            ipPointCol.AddPoint(p, ref missing, ref missing);
    
                        }
    
                    }
    
                    IPolyline polyline = ipPolyline;
    
                    IFeature pFeature = startlineshp.CreateFeature();
    
                    pFeature.Shape = polyline;
    
                    pFeature.Store();
    
                    this.axMapControl1.ActiveView.Refresh();
    
                    pts.RemoveAll();
    
                    return;
    
                }
    
                if (e.button == 1 && drawregion == 1)//左键创建面的节点
    
                {
    
                    IPoint pt;
    
                    pt = axMapControl1.ToMapPoint(e.x, e.y);
    
                    pts.Add(pt);
    
                    return;
    
                }
    
                if (e.button == 2 && drawregion == 1)//右键根据节点创建面
    
                {
    
                    ESRI.ArcGIS.Geometry.IPolygon ipPolyGon = new PolygonClass();
    
                    ESRI.ArcGIS.Geometry.IPointCollection ipPointCol = (IPointCollection)ipPolyGon;
    
                    object missing = Type.Missing;
    
                    for (int i = 0; i < pts.Count; i++)
    
                    {
    
                        object t = pts.get_Element(i);
    
                        ESRI.ArcGIS.Geometry.Point p = (ESRI.ArcGIS.Geometry.Point)t;
    
                        if (p != null)
    
                        {
    
                            ipPointCol.AddPoint(p, ref missing, ref missing);
    
                        }
    
                    }
    
                    ipPointCol.AddPoint(pts.get_Element(0), ref missing, ref missing);//面的坐标串首尾坐标应一致(如P1-P2-P3-P4-P1)
    
                    IPolygon polygon = ipPolyGon;
    
                    IFeature pFeature = startpolygonshp.CreateFeature();
    
                    pFeature.Shape = polygon;
    
                    pFeature.Store();
    
                    this.axMapControl1.ActiveView.Refresh();
    
                    pts.RemoveAll();
    
                    return;
    
    
    
                }
    
    //结束创建时执行清理、重置
    
       drawpoint = 0;
    
                drawline = 0;
    
                drawregion = 0;
    
                pts.RemoveAll();
    
                startpointshp = null;
    
                startlineshp = null;
    
                startpolygonshp = null;
    
                this.toolStripComboBox1.Visible = false;
    
    
    
                this.toolStripComboBox1.Items.Clear();
    
     /*
    
    3.shp中点线面要素的图形编辑
    
    使用Toolbar
    
    使用流程:
    
    开始编辑——>选择目标图层——>开启编辑工具——>图形编辑——>保存并停止编辑
    
    
    
    三、属性表编辑
    
    
    1.在属性表窗体设计中加一个按钮用于更新数据
    
    
    
    
    
    2.属性表类中至少应有如下成员对象,在表开启后这些值应都已经赋值或初始化
      * */
    
    public DataTable attributeTable;//你的表
    
    string tableName;//你的表的名字
    
    public List array = new List();//你用来记录哪些行的数据被改变了的数列
    
    public ILayer currentlayer;//你用来获取当前图层的对象其中,比如,attributeTable和tableName可在Load函数中赋值,currentlayer可在构造函数中赋值
    
    /*
    3.添加如下函数
     */
    
    //在按钮的点击事件中添加如下代码
    
    private void button1_Click(object sender, EventArgs e)
    
    {
    
    if (array.Count < 1)//没有记录到任何数据可能改变的行
    
    {
    
    MessageBox.Show(" 未 修改任何数据 ! ");
    
    return;
    
    }
    
    array.Sort();
    
    ILayer player = this.currentlayer;
    
    UpdateFTOnDV(player, attributeTable, array.ToArray());
    
    dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;//dataGridView1是你属性表中显示数据的视图
    
    dataGridView1.Refresh();
    
    }
    
    //在表的CellValueChanged事件中添加如下代码
    
    private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    
    {
    
    array.Add(e.RowIndex);//将有值改变的那个行的行号记录下来
    
    }
    
    //在属性表类中添加如下函数
    
    public void UpdateFTOnDV(ILayer player, DataTable pdatatable, int[] array)
    
    {
    
    IFeatureLayer pFTClass = player as IFeatureLayer;
    
    ITable pTable = pFTClass as ITable;
    
    IRow pRow;
    
    ICursor pCursor = pTable.GetRows(array, false);
    
    for (int i = 0; i < array.Length; i++)
    
    {
    
    pRow = pCursor.NextRow();
    
    int k = array[i];
    
    for (int j = 2; j < pdatatable.Columns.Count; j++)
    
    {
    
    object pgridview = pdatatable.Rows[k][j];
    
    object prow = pRow.get_Value(j);
    
    if (prow.ToString() != pgridview.ToString())//当表格中值与shp文件属性表中值不同时发生替换
    
    {
    
    if (pgridview == System.DBNull.Value)
    
    {
    
    string skipinfo = "" + (k+1).ToString() + "行第 " + (j+1).ToString() + " 列 的数据 可 为 空 自动跳过修改 ";
    
    MessageBox.Show(skipinfo);
    
    continue;
    
    }
    
    pRow.set_Value(j, pgridview);
    
    pRow.Store();
    
    }
    
    }
    
    }
    
    MessageBox.Show("数据保存 成 功 ! ");
    
    }
    
    /*
    四、空间分析
    
    以缓冲区分析为例,实现对某类地物周边一定范围内其他地物的统计与显示。
     */
    
    //buffer类中的using
    
    using System;
    
    using System.Windows.Forms;
    
    using ESRI.ArcGIS.Carto;
    
    using ESRI.ArcGIS.Controls;
    
    using ESRI.ArcGIS.Geoprocessing;
    
    using ESRI.ArcGIS.Geoprocessor;
    
    using ESRI.ArcGIS.Geodatabase;
    
    using ESRI.ArcGIS.AnalysisTools;
    
    using System.Collections;
    /*
    
    1.创建一个windows窗体类buffer.cs
    
    
    
    2.对窗体进行设计
    
    
    目标地物(上面的)下拉框的Name设为comboBox2
    
    被统计地物(下面的)下拉框Name设为comboBox3
    
    分析距离textBox的Name设为textBox1
    
    按钮Name设为button1
    
    
    
    3.添加相关代码
    */
    //1>Buffer类中添加三个成员
    
    
    private static int counter = 1;//用来对生成图层的计数
    
    private AxMapControl axMapControl1;//用来获取主框架传进来的AxMapControl
    
    private ArrayList flyr = new ArrayList();//用来存储生成的图层,便于结束分析后删除
    
    
    //2>修改构造函数为
    
    public buffer(AxMapControl mapControl)
    
    {
    
    InitializeComponent();
    
    this.axMapControl1 = mapControl;
    
    }
    
    //3>在窗体设计布局中,双击Buffer的对话框,进入buffer_Load函数,添加以下代码
    
    //MapControl中没有图层时返回
    
    if (axMapControl1.LayerCount <= 0)
    
    return;
    
    ILayer pLayer;//图层
    
    string strLayerName;//图层名称
    
    //获取MapControl中的全部图层名称,并加入ComboBox
    
    for (int i = 0; i < axMapControl1.LayerCount; i++)
    
    {
    
    pLayer = axMapControl1.get_Layer(i);
    
    strLayerName = pLayer.Name;
    
    comboBox2.Items.Add(strLayerName);
    
    comboBox3.Items.Add(strLayerName);
    
    }
    
    //默认显示第一个选项
    
    comboBox2.SelectedIndex = 0;
    
    comboBox3.SelectedIndex = 0;
    
    //3>在窗体设计布局中,双击按钮,进入click事件,添加以下代码
    
    //获取所设置的缓冲区距离
    
    string distance = this.textBox1.Text.ToString();
    
    //给距离加上单位,默认为米
    
    string para = distance + " Meters ";
    
    //根据所选图层确定其数据源位置,即shp文件所在的文件夹路径
    
    IDataLayer combo2 = (IDataLayer)axMapControl1.get_Layer(comboBox2.SelectedIndex);
    
    IWorkspaceName ws = ((IDatasetName)(combo2.DataSourceName)).WorkspaceName;
    
    string featurefolder = ws.PathName;
    
    //使用gp处理工具
    
    Geoprocessor gp = new Geoprocessor();
    
    //允许覆盖同名文件
    
    gp.OverwriteOutput = true;
    
    //调用缓冲区工具
    
    ESRI.ArcGIS.AnalysisTools.Buffer buffertool = new ESRI.ArcGIS.AnalysisTools.Buffer();
    
    //设置输入图层路径和输出图层路径
    
    buffertool.in_features = featurefolder+ "  " + comboBox2.SelectedItem + ".shp ";
    
    buffertool.out_feature_class = featurefolder + "  " + comboBox2.SelectedItem + "_buffer " + counter.ToString() + ".shp ";
    
    //设置缓冲区相关参数
    
    buffertool.buffer_distance_or_field = para;
    
    buffertool.dissolve_option = "ALL ";
    
    //执行
    
    try
    
    { gp.Execute(buffertool, null); }
    
    catch (Exception ex)
    
    {
    
    MessageBox.Show("ERROR ");
    
    return;
    
    }
    
    //对生成图层的计数
    
    counter++;
    
    //用生成的缓冲区与被统计地物进行叠置分析求交集
    
    Intersect pIntersect = new Intersect();
    
    int chooselayer = counter - 1;
    
    Geoprocessor gp2 = new Geoprocessor();
    
    gp2.OverwriteOutput = true;    //允许覆盖同名文件
    
    FeatureLayer pFeatureLayer = new FeatureLayerClass();
    
    //设置相关参数
    
    object obj = gp2.GetEnvironmentValue("Extent ");
    
    gp2.SetEnvironmentValue("Extent ", "MAXOF ");
    
    obj = gp2.GetEnvironmentValue("OutputZFlag ");
    
    gp2.SetEnvironmentValue("OutputZFlag ", "DEFAULT ");
    
    obj = gp2.GetEnvironmentValue("OutputMFlag ");
    
    gp2.SetEnvironmentValue("OutputMFlag ", "DEFAULT ");
    
    obj = gp2.GetEnvironmentValue("QualifiedFieldNames ");
    
    gp2.SetEnvironmentValue("QualifiedFieldNames ", "QUALIFIED ");
    
    //把要求交的两个要素放到一个IGpValueTableObject中作为参数
    
    IGpValueTableObject pObject = new GpValueTableObjectClass();
    
    pObject.SetColumns(2);
    
    object inputfeature = featurefolder + "  " + comboBox3.SelectedItem + ".shp ";
    
    pObject.AddRow(ref inputfeature);
    
    object inputfeature2 = featurefolder + "  " + comboBox2.SelectedItem + "_buffer " + chooselayer.ToString() + ".shp ";
    
    pObject.AddRow(ref inputfeature2);
    
    //设置输入图层路径和输出图层路径
    
    pIntersect.in_features = pObject;
    
    pIntersect.out_feature_class = featurefolder + "  " + comboBox2.SelectedItem + "_insert " + chooselayer.ToString() + ".shp ";
    
    pIntersect.join_attributes = "All ";
    
    //执行
    
    IGeoProcessorResult pResult = (IGeoProcessorResult)gp2.Execute(pIntersect, null);
    
    //从求交的结果中提取Feature并做相关统计
    
    IGPUtilities pGPUtil = new GPUtilitiesClass();
    
    IFeatureClass pFC;
    
    IQueryFilter pQF;
    
    pGPUtil.DecodeFeatureLayer(pResult.GetOutput(0), out pFC, out pQF);
    
    int count = pFC.FeatureCount(null);
    
    IFeatureCursor pCursor = pFC.Insert(true);
    
    pFeatureLayer.FeatureClass = pFC;
    
    //将缓冲区载入地图中显示
    
    axMapControl1.AddShapeFile(featurefolder + "  ", comboBox2.SelectedItem + "_buffer " + chooselayer.ToString() + ".shp ");
    
    axMapControl1.MoveLayerTo(0, axMapControl1.LayerCount - 1);
    
    //获取生成的缓冲区对象
    
    FeatureLayer bufferlayer = axMapControl1.get_Layer(axMapControl1.LayerCount - 1) as FeatureLayer;
    
    //将求交得到的对象载入地图中显示
    
    pFeatureLayer.Name = comboBox2.SelectedItem + " 周边 " + textBox1.Text + "米内的 " + comboBox3.SelectedItem;
    
    axMapControl1.Map.AddLayer(pFeatureLayer);
    
    //将生成的缓冲区和求交对象放到一个图层数组中,在关闭缓冲区分析工具后统一移出系统
    
    flyr.Add(bufferlayer);
    
    flyr.Add(pFeatureLayer);
    
    //将缓冲区分析的结果放到属性表中并显示
    
    ILayer layer = pFeatureLayer as ILayer;
    
    FrmAttribute attributeTable = new FrmAttribute(layer, axMapControl1);
    
    attributeTable.Show();
    
    //4>在窗体的FormClosing事件中,添加以下代码
    
    //删除所有生成的缓冲区和求交对象
    
    foreach (FeatureLayer pFeatureLayer in flyr)
    
    {
    
    IDataLayer2 OnOff = pFeatureLayer as IDataLayer2;
    
    OnOff.Disconnect();
    
    axMapControl1.Map.DeleteLayer(pFeatureLayer);
    
    }
    /*
    4.在主窗体中调用此模块
    
    首先在菜单栏中新建一个选项如“周边设施分析”
    
    之后双击该选项,添加如下代码
    */
    buffer b = new buffer(axMapControl1);
    
    b.Show();
    /*
    五、esriAddIn扩展项在ArcEngine中的添加
    
    对原代码中接口适当修改,使其可用于ArcEngine二次开发工程中
    
    1.在服务中引用“AE开发用”文件夹中的MappingTools.dll
    
    2.菜单栏中创建对应菜单,在单击事件中加入调用代码,并在Form1类的顶端填写using MappingTools;
    */
    
    
    //调用创建直方图时,是在mapcontrol1的onmousedown事件中触发创建直方图的函数
    
    //首先到onmousedown中加入如下代码
    
    if (zhifangtu == 1 && e.button==1)//开启了直方图功能且在地图上单击了鼠标左键时
    
    {
    
    Createzft (e.x,e.y);
    
    }
    
    
    
    //之后在Form1主类中加入如下函数
    
    Public void Createzft(int x,int y)
    
    {
    
    MappingTools.CreateGraph a = new MappingTools.CreateGraph();
    
    frmGraph frm = new frmGraph(this.axMapControl1);
    
    frm.BasePoint = axMapControl1.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y);
    
    DialogResult re = frm.ShowDialog();
    
    }
  • 相关阅读:
    sql月,年,统计报表sql报表
    Sql server在cmd下的使用
    c# 批量处理数据录入
    vmware安装64位系统“此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态”的问题
    以太坊去中心化淘宝智能合约案例
    智能合约入门
    智能资产构建去中心化的资产管理系统
    以太坊搭建联盟链
    web 前端
    EasyUI+bootsrtap混合前端框架
  • 原文地址:https://www.cnblogs.com/gisext/p/7098542.html
Copyright © 2020-2023  润新知