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();
        }

执行结果如下图,查找到的要素高量显示: 
空间查询结果

猜你喜欢

转载自blog.csdn.net/kone0611/article/details/79129238