1. 程式人生 > >ArcGIS Engine 空間查詢

ArcGIS Engine 空間查詢

實現利用圖層進行空間查詢的功能,其功能介面如下: 
空間查詢面板 
其功能模組程式碼如下:

//從地圖中讀取目標圖層和源圖層
 private void checkedListBoxControl_FeatureLayer_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e)
        {
            comboBox_SourceLayer.Properties.Items.Clear();                                        //目標圖層勾選之前先清空一下源圖層ComboBox
            for (int i = 0; i < checkedListBoxControl_FeatureLayer.Items.Count; i++)              //遍歷所有的目標圖層
            {
                if (checkedListBoxControl_FeatureLayer.Items[i].CheckState == CheckState.Unchecked)
                {
                    ComboInfo pComboInfo = checkedListBoxControl_FeatureLayer.Items[i].Value as ComboInfo;
                    comboBox_SourceLayer.Properties.Items.Add(pComboInfo);
                }
            }
            comboBox_SourceLayer.SelectedIndex = 0;
        }

//目標圖層要素的空間選擇方法
private void comboBox_SpatialSelectMethod_SelectedIndexChanged(object sender, EventArgs e)    //空間選擇方法切換
        {
            switch (comboBox_SpatialSelectMethod.SelectedIndex)
            {
                case 0: pesriSpatialRelEnum = esriSpatialRelEnum.esriSpatialRelIntersects;            //與源圖層要素相交
                    checkEdit_SearchDistance.Checked = false;
                    checkEdit_SearchDistance.Enabled = true;
                    break;
                case 1: pesriSpatialRelEnum = esriSpatialRelEnum.esriSpatialRelContains;               //包含源圖層要素
                    checkEdit_SearchDistance.Checked = false;
                    checkEdit_SearchDistance.Enabled = true;
                    break;
                case 2: pesriSpatialRelEnum = esriSpatialRelEnum.esriSpatialRelRelation;               //與源圖層要素完全相同
                    checkEdit_SearchDistance.Checked = false;
                    checkEdit_SearchDistance.Enabled = true;
                    break;
                case 3: pesriSpatialRelEnum = esriSpatialRelEnum.esriSpatialRelEnvelopeIntersects;     //在源圖層要素的某一距離範圍內
                    checkEdit_SearchDistance.Checked = true;
                    checkEdit_SearchDistance.Enabled = false;
                    break;                   
                case 4: pesriSpatialRelEnum = esriSpatialRelEnum.esriSpatialRelIndexIntersects;        //在源圖層要素範圍內    
                    checkEdit_SearchDistance.Checked = false;
                    checkEdit_SearchDistance.Enabled = true;
                    break;
            }
        }

//空間查詢方法
  private void select()
        {
            ComboInfo pSourceLayerComboInfo = comboBox_SourceLayer.SelectedItem as ComboInfo;                     //從comboBox中拿到選定的圖層
            if (pSourceLayerComboInfo == null) return;
            IFeatureLayer pSourceLayer = pSourceLayerComboInfo.ObjectValue as IFeatureLayer;

            for (int i = 0; i < checkedListBoxControl_FeatureLayer.CheckedItems.Count; i++)
            {
                ComboInfo pTargetLayerComboInfo = checkedListBoxControl_FeatureLayer.CheckedItems[i] as ComboInfo;     //從checkedListBox中拿到選定的圖層
                if (pTargetLayerComboInfo == null) return;
                IFeatureLayer pTargetLayer = pTargetLayerComboInfo.ObjectValue as IFeatureLayer;
                var pFeatureSelection = pTargetLayer as IFeatureSelection;                                          //將選中的目標圖層作為待查尋遍歷Feature選擇集

                pFeatureSelection.CombinationMethod = esriSelectionResultEnum.esriSelectionResultNew;
                var pSelectionSet = pFeatureSelection.SelectionSet;

                if (pSourceLayer.FeatureClass != null)
                {
                    IFeatureCursor pFeatureCursor = pSourceLayer.FeatureClass.Search(null, false);  //定義一個FeatureCursor,將SourceLayer的FeatureClass全放在裡面
                    IFeature pFeature = pFeatureCursor.NextFeature();                               //從FeatureCursor中拿Feature
                    while (pFeature != null)
                    {
                        IGeometry pGeometry = pFeature.Shape;
                        if (checkEdit_SearchDistance.Checked == true)
                        {
                            ITopologicalOperator TopologicalOperator = pFeature.ShapeCopy as ITopologicalOperator;    //緩衝區
                            pGeometry = TopologicalOperator.Union(pGeometry);
                            double distance = GetDistance();
                            TopologicalOperator = pGeometry as ITopologicalOperator;
                            pGeometry = TopologicalOperator.Buffer(distance);
                        }
                        ISpatialFilter pSpatialFilter = new SpatialFilterClass();                   //定義一個空間過濾器
                        pSpatialFilter.Geometry = pGeometry;
                        pSpatialFilter.SpatialRel = pesriSpatialRelEnum;                            //方法                        
                        pSpatialFilter.GeometryField = pTargetLayer.FeatureClass.ShapeFieldName;
                        IFeatureCursor pNewCursor = pTargetLayer.FeatureClass.Search(pSpatialFilter, false);
                        IFeature pSelectFeature = pNewCursor.NextFeature();

                        while (pSelectFeature != null)
                        {
                            pSelectionSet.Add(pSelectFeature.OID);
                            pSelectFeature = pNewCursor.NextFeature();
                        }
                        pFeature = pFeatureCursor.NextFeature(); 
                    }
                }
            }

            axMapControl1.Refresh();
        }

執行結果如下圖,查詢到的要素高量顯示: 
空間查詢結果