ArcGISEngine二次開發(4):屬性查詢(2)
阿新 • • 發佈:2019-02-04
屬性查詢(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();
}