AE開發—利用IQueryFilter接口進行屬性查詢

在ArcGis Engine二次開發過程中,經常會需要用到查詢統計的功能,而IQueryFilter是最常見的屬性字段查詢接口,可以用來做一些簡單的查詢工作。

現在有一些公交站點和公交路線的數據,可視化效果如下:

其中站點數據的屬性信息中記錄了站點名稱和經過的路線,如下圖所示:

功能需求爲:用戶輸入一條公交路線,程序運行並返回這條路線經過的所有站點。

在AE程序中先建立查詢的窗體,如下圖所示:

這個功能需要從MapControl中獲取圖層,因此調用此窗體的時候需要將MapControl作爲參數傳入,另外還需要用到ArrayList接口,在代碼中添加如下引用:

1 using System.Collections;
2 using ESRI.ArcGIS.Controls;
3 using ESRI.ArcGIS.Carto;
4 using ESRI.ArcGIS.Geodatabase;

構建一個用於屬性查詢的函數,返回結果爲ArrayList數組,具體代碼如下:

 1 //查詢函數
 2        private ArrayList Query(AxMapControl axMapControl1, int layerIndex, string inputField, string outputField, string inputTxt)
 3         {
 4             IFeatureLayer pFeatureLayer = axMapControl1.Map.get_Layer(layerIndex) as IFeatureLayer;   //獲取查詢的圖層
 5             IFeatureClass pFeatCls = pFeatureLayer.FeatureClass;
 6             IQueryFilter pQueryfilter = new QueryFilterClass();
 7             pQueryfilter.WhereClause = inputField + "='" + inputTxt + "'";  //設置屬性查詢條件
 8             IFeatureCursor pFeatCur = pFeatCls.Search(pQueryfilter, false);
 9             IFields pFields = pFeatCls.Fields;  //獲取圖層的字段
10             int iField = pFields.FindField(outputField);  //找出輸出字段的位置
11             IFeature pFeat = pFeatCur.NextFeature();
12             ArrayList OutputList = new ArrayList(); //新建輸出列表
13             while (pFeat != null)
14             {
15                 OutputList.Add(pFeat.get_Value(iField).ToString());
16                 pFeat = pFeatCur.NextFeature();
17             }
18             return OutputList;
19         }

在“查詢”按鈕的點擊響應函數下添加如下代碼:

 1     private void button1_Click(object sender, EventArgs e)
 2         {
 3             string inputTxt = textBox1.Text;   //獲取線路值
 4             if (inputTxt != null && inputTxt != "")
 5             {
 6                 ArrayList outListBus = Query(mapControl, 1, "lineName", "name", inputTxt);
 7                 ArrayList outListSubway = Query(mapControl, 3, "lineName", "name", inputTxt);
 8                 if (outListBus.Count != 0 || outListSubway.Count != 0)
 9                 {
10                     string outList = null;
11                     if (outListBus.Count > 0)  //若查詢路線爲公交路線
12                     {
13                         for (int i = 0; i < outListBus.Count - 1;i++ )
14                         {
15                             outList += outListBus[i].ToString() + "-->";
16                         }
17                         richTextBox1.Text = "經過站點:" + outList + outListBus[outListBus.Count - 1].ToString();
18                     }
19                     else       //若查詢路線爲地鐵路線
20                     {
21                         for (int i = 0; i < outListSubway.Count - 1; i++)
22                         {
23                             outList += outListSubway[i].ToString() + "-->";
24                         }
25                         richTextBox1.Text = "經過站點:" + outList + outListSubway[outListSubway.Count - 1].ToString();
26                     }
27                 }
28                 else
29                 {
30                     MessageBox.Show("查無此線路,請輸入正確的線路值!");
31                 }
32 
33             }
34             else
35             {
36                 MessageBox.Show("請輸入正確的線路值!");
37             }
38 
39         }

最終運行結果如下:

至此,一個簡單的屬性查詢窗體已經完成!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章