arcengine突击3——空间查询之 要素选择和属性查询

分为要素选择和属性查询(在同一窗口下实现,不涉及新窗口)

要素选择:

实现按矩形面的方式选择要素,实现方法都在主视图的onMouseDown中实现。

定义全局变量state,这里用单选按钮实现要素选择方式:

       如果按面选择且事件源为鼠标右键,定义map1的矩形轨迹为包络线,使用map1.map.selectbyshape传入包络线,实现选择矩形框内元素,map1刷新

        bool state = false;



private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)

        {

            if (state == true && e.button == 2)

            {

                IEnvelope pEnv = axMapControl1.TrackRectangle();

                axMapControl1.Map.SelectByShape(pEnv, null, false);

                axMapControl1.Refresh();

            }

        }

 

属性查询:

所需控件:comboBox(选中图层),Listbox1(显示图层字段),listbox2(显示字段值,配合Button使用),textbox(输入查询条件),=button(在TextBox添加=),查询button(实现查询)

OnMapReplaced

地图更新,comboBox清除原有items,遍历图层,将map中各图层name添加到items中。

comboBox1_selectedIndexChanged

       选框内容改变,清除原有items,转换选择的图层为要素图层,要素图层游标查找,根据游标获取第一个要素。遍历要素字段,如果不是字段类型不是系统几何类型,添加要素字段名到listbox1中。

Button1_click

       获取字段值,转换选择的图层为要素图层,要素图层游标查找,根据游标获取第一个要素。使用自定义方法,传入要素图层的要素类型和listbox1.text,获得字段值集合。新建字段类对象,遍历要素字段将其name与listbox       1.text比较,如果相等字段对象赋值。清除listbox2原有items,遍历字段值集合添加到listbox.items中,如果字段类型是字符串,需要添加 ’ 。

       自定义方法,传入要素类型和文本。从要素类型search方法获取新游标,新建数据统计对象,对象的字段为传入文本,游标为新建的游标。新建枚举类型为数据统计对象的独特值。根据数据统计对象独特值个数新建字符串数组。枚举对象指针重置。遍历枚举对象内容并添加到字符串数组中,返回字符串数组。

Button2_click

       转换选择的图层为要素图层,如果图层为空,结束。新建属性查询对象,设置条件为textbox.text。从要素图层获取游标,新建要素对象,如果根据游标获取要素对象且不为空,使用map1.map.selectFeature方法,传入要素图层和要素,选择要素。Map活动窗口刷新。

Listbox的双击事件、=button的单击事件,向textbox添加文本

private void axMapControl1_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e)
        {
            layerComboBox.Items.Clear();
            for (int i = 0; i < axMapControl1.LayerCount; i++)
            {
                layerComboBox.Items.Add(axMapControl1.get_Layer(i).Name);
            }
        }

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            IFeatureLayer pFeatureLayer = axMapControl.get_Layer(comboBox1.SelectedIndex) as IFeatureLayer;
            IFeatureCursor pFeatureCursor = pFeatureLayer.Search(null, true);
            IFeature pFeature = pFeatureCursor.NextFeature();
            for (int i = 0; i < pFeature.Fields.FieldCount; i++)
            {
                if (pFeature.Fields.get_Field(i).Type != esriFieldType.esriFieldTypeGeometry)
                {
                    listBox1.Items.Add(pFeature.Fields.get_Field(i).Name);
                }
            }
        }

        private string[] getValue(IFeatureClass pFeatureClass, string strField)
        {
            IFeatureCursor lFeatureCursor = pFeatureClass.Search(null, false);
            IDataStatistics lData = new DataStatisticsClass();
            lData.Field = strField;
            lData.Cursor = lFeatureCursor as ICursor;

            IEnumerator pEnumVar = lData.UniqueValues;
            string[] strValue = new string[lData.UniqueValueCount];
            pEnumVar.Reset();
            int i = 0;
            while (pEnumVar.MoveNext())
            {
                strValue[i++] = pEnumVar.Current.ToString();
            }
            return strValue;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            IFeatureLayer pFeatureLayer = axMapControl.get_Layer(comboBox1.SelectedIndex) as IFeatureLayer;        
            IFeatureCursor pFeatureCursor = pFeatureLayer.Search(null, true);
            IFeature pFeature = pFeatureCursor.NextFeature();

            string[] values = getValue(pFeatureLayer.FeatureClass, listBox1.Text);
            IField pField = new FieldClass();
            for (int i = 0; i < pFeature.Fields.FieldCount; i++)
            {
                if (listBox1.Text == pFeature.Fields.get_Field(i).Name)
                {
                    pField = pFeature.Fields.get_Field(i);
                }
            }
            
            listBox2.Items.Clear();
            for (int i = 0; i < values.Length; i++)
            {
                if (pField.Type == esriFieldType.esriFieldTypeString)
                {
                    listBox2.Items.Add("\'" + values[i] + "\'");
                }
                else
                {
                    listBox2.Items.Add(values[i]);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            IFeatureLayer pFeatureLayer = axMapControl.get_Layer(comboBox1.SelectedIndex) as IFeatureLayer;
            if (pFeatureLayer == null)
            {
                MessageBox.Show("wrong");
                return;
            }

            IQueryFilter pQueryFilter = new QueryFilterClass();
            pQueryFilter.WhereClause = textBox1.Text;

            try
            {
                IFeatureCursor pFeatureCursor = pFeatureLayer.Search(pQueryFilter, false);
                IFeature pFeature;
                while ((pFeature = pFeatureCursor.NextFeature()) != null)
                {
                    axMapControl.Map.SelectFeature(pFeatureLayer, pFeature);
                }
                axMapControl.ActiveView.Refresh();
            }
            catch (Exception pException)
            {
                MessageBox.Show(pException.Message);
                return;
            }
        }

        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            textBox1.Text += listBox1.Text;
        }

        private void listBox2_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            textBox1.Text += listBox2.Text;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Text += " = ";
        }

转载自:https://blog.csdn.net/nominior/article/details/84636029

You may also like...

退出移动版