ArcGISEngine二次开发(4):属性查询(2)

来源:互联网 发布:mac adb unauthorized 编辑:程序博客网 时间:2024/06/08 11:49

属性查询(2)

使用IGeometry接口TrackPolygon方法建立对象实现属性查询
使用ISpatialFilter接口SpatialRel属性定义Intersects取交集为查询对象
之后将查询到的(FindField方法)属性显示在新的windowsform中的listbox中显示属性字段
单击Listbox中的属性字段,地图高亮显示对应多边形


添加引用:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using ESRI.ArcGIS.Carto;using ESRI.ArcGIS.SystemUI;using ESRI.ArcGIS.esriSystem;using ESRI.ArcGIS.Controls;using ESRI.ArcGIS.Geometry;using ESRI.ArcGIS.Geodatabase;using ESRI.ArcGIS.DataSourcesFile;

添加ToolTripButton,添加Click事件代码如下

ICommand spatialqueryintersect = new spatialquery();spatialqueryintersect.OnCreate(axMapControl1.Object);spatialqueryintersect.OnClick();ITool ptool = spatialqueryintersect as ITool;axMapControl1.CurrentTool = ptool;

这里写图片描述


这里写图片描述
新建windowsform窗体,并在里边添加Listbox,Modifiers属性Public,否则在其他窗体的事件代码中不能检测到该控件,事件代码如下

//定义全局变量public IMapControlDefault spaqury;//必须添加public关键字,因为要从新建的类中传来Imapcontrol对象// private void listBox1_Click(object sender, EventArgs e)        {            IFeatureLayer pFeatureLayer = spaqury.get_Layer(0) as     IFeatureLayer;            IQueryFilter pqueryfilter = new QueryFilterClass();            pqueryfilter.SubFields = "*";            pqueryfilter.WhereClause = "NAME" + "=" + "'" + this.listBox1.SelectedItem.ToString() + "'";            IFeatureCursor pfeaturecursor = pFeatureLayer.Search(pqueryfilter, false);            IFeature pfeature = pfeaturecursor.NextFeature();            spaqury.FlashShape(pfeature.Shape, 3, 500, null);            return;         }

新建类库,手动实现Icommand,ITool 接口,定义全局变量:

IMapControlDefault m_app;
//Onclick函数设为空,因为单击Button没有立刻开始画多边形,而是在单击axmapcontrol对象以后开始画public void OnClick()        {        }
//传入ImapControl对象public void OnCreate(object Hook)        {            m_app = Hook as IMapControlDefault;        }
//其他接口置为空public void OnMouseDown(int button, int shift, int x, int y)        {            IGeometry pGeometry = m_app.TrackPolygon();            IFeatureLayer pfeatureLayer = m_app.get_Layer(0) as IFeatureLayer;            ISpatialFilter pSpatialfilter =new SpatialFilterClass();            pSpatialfilter.SubFields="*";            pSpatialfilter.GeometryField="shape";//所画图形类型            pSpatialfilter.Geometry=pGeometry;            pSpatialfilter.SpatialRel=esriSpatialRelEnum.esriSpatialRelIntersects;//空间交集            IFeatureCursor pfeaturecursor = pfeatureLayer.Search(pSpatialfilter, false);            IFeature pfeature = pfeaturecursor.NextFeature();            int ifname = pfeaturecursor.FindField("NAME");//属性查询字段            SpatialQueryListBox listboxwindows = new SpatialQueryListBox();//SpatialQueryListBox为新建的windowsform窗体            listboxwindows.spaqury = m_app;//该窗体中的全局变量            while (pfeature != null)            {                listboxwindows.listBox1.Items.Add(pfeature.get_Value(ifname).ToString());                m_app.FlashShape(pfeature.Shape, 3, 500, null);                pfeature = pfeaturecursor.NextFeature();            }            listboxwindows.Show();        }
原创粉丝点击