【问题标题】:How to count and select points within a polygon ArcGIS?如何计算和选择多边形 ArcGIS 中的点?
【发布时间】:2019-01-02 00:56:57
【问题描述】:

我正在尝试创建一个按钮来计算和选择多边形内的所有点。

protected override void OnClick()
    {   IActiveView activeView = ArcMap.Document.ActiveView;
        IEnumLayer featureClasses = activeView.FocusMap.Layers;

        if (featureClasses != null){
            ILayer layer ;
            while((layer = featureClasses.Next()) != null){

                if (layer as IFeatureLayer == null)
                    continue;
                if (layer.Name == "EX_BOUNDARY")//Name of polygon layer.{
                    var ftrLayer = layer as IFeatureLayer;
                    var ftrClass = ftrLayer.FeatureClass;

                    if (ftrClass == null)
                        continue;
                    GetSelectedPolygon(activeView, ftrClass, layer);
                }

            }
        }
    }

获取选定多边形的方法,选择并计算其中的点。

public void GetSelectedPolygon(IActiveView activeView, IFeatureClass featureClass, ILayer a)
    {

        //Check that the selected geometry is a polygon.
        IMap map = activeView.FocusMap;
        var selectedFeatures1 = (IEnumFeature)map.FeatureSelection;
        IFeature selectedFeature = selectedFeatures1.Next();
        IGeometry selectedGeometry = selectedFeature.ShapeCopy;


        if (selectedGeometry != null && selectedGeometry.GeometryType == esriGeometryType.esriGeometryPolygon)
        {
            //execute query for all points inside polygon.
            ISpatialFilter spatialFilter = new SpatialFilterClass();
            spatialFilter.Geometry = selectedGeometry;
            spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
            FeatureCursor featureCursor = null;
            var srchResult = featureClass.Search(spatialFilter, false);
            featureCursor = (FeatureCursor) featureClass.Search(spatialFilter, false);


            //select outcoming queries and their count.
            //map.FeatureSelection = srchResult as ISelection;
            IFeature singleSearchFeature = null;
            while ((singleSearchFeature = srchResult.NextFeature()) != null){
                //only looped once.
                map.SelectFeature(a,singleSearchFeature);
            }
            var count = srchResult.Fields.FieldCount;
            MessageBox.Show(count.ToString());
        }
    }

我对整个 GIS 文化非常陌生,而且我才刚刚开始我的实际培训。这是我需要完成的主要任务之一,我从概念上理解事物是如何工作的,但这对我来说都是全新的。

我怀疑我用于搜索的查询有问题,因为发生的事情是我尝试选择一个多边形然后单击我的按钮,无论那里有多少点,消息框都会显示 4在多边形中,它也没有选择它们中的任何一个。

请帮忙。谢谢你:)

【问题讨论】:

    标签: c# arcgis


    【解决方案1】:

    “srchResult.Fields.FieldCount”是错误的。 FieldCount 是特征字段的数量。

    你可以用这个:

        protected override void OnClick()
        {
            try
            {
                IMxDocument document = (IMxDocument)ArcMap.Document;
                IMap map = document.FocusMap;
                IActiveView activeView = document.ActiveView;
    
                if (map.SelectionCount != 1)
                {
                    MessageBox.Show("Select one polygon.");
                    return;
                }
    
                // Get the polygon for selecting features
                IEnumFeature features = (IEnumFeature)map.FeatureSelection;
                IFeature feature = features.Next();
                IGeometry geometry = feature.ShapeCopy;
    
                if (geometry.GeometryType != esriGeometryType.esriGeometryPolygon)
                {
                    MessageBox.Show("Select one polygon.");
                    return;
                }
    
                // Find layer to select features
                UID uid = new UIDClass() { Value = typeof(IFeatureLayer).GUID.ToString("B") };
                IEnumLayer layers = map.get_Layers(uid);
    
                string layerName = "Name of your points layer to select";
    
                ILayer layer = null;
                IFeatureLayer featureLayer = null;
                while ((layer = layers.Next()) != null)
                {
                    if (layer.Name == layerName)
                    {
                        featureLayer = (IFeatureLayer)layer;
                        break;
                    }
                }
    
                map.ClearSelection();
    
                SelectByPolygon(featureLayer, geometry);
    
                activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, activeView.Extent);
                ISelectionEvents selectionEvents = (ISelectionEvents)map;
                selectionEvents.SelectionChanged();
    
                MessageBox.Show(string.Format("Number of selected features is {0}", map.SelectionCount));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    
    
        /// <summary>
        /// Select features.
        /// </summary>
        /// <param name="featurelayer">Layers to select features.</param>
        /// <param name="polygon">Polygon geometry for select features.</param>
        public void SelectByPolygon(IFeatureLayer featurelayer, IGeometry polygon)
        {
            ISpatialFilter filter = new SpatialFilterClass();
            filter.Geometry = polygon;
            filter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;
    
            var featureSelection = (IFeatureSelection)featurelayer;
            featureSelection.SelectFeatures(filter, esriSelectionResultEnum.esriSelectionResultNew, false);
            featureSelection.SelectionChanged();
        }
    

    【讨论】:

    • 所以我应该用我写的GetSelectedPolygon方法替换它,然后在按钮的onClick()事件中调用它?
    • 是的,你可以在 onClick 事件中写这个。您可以将参数“多边形”的几何类型检查到 esriGeometryType.esriGeometryPolygon。有不明白的可以问,或者看:ArcObjects
    • 我现在试试,如果我想排除存在或与多边形边缘相交的点怎么办?
    • 我试过你的代码但它不起作用,它一直说我选择的任何多边形中有 2 个点:/
    • 在 OnClick 事件中将变量 'layerName' 设置为点图层的名称。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-08
    • 1970-01-01
    • 2015-03-26
    • 1970-01-01
    • 2017-02-21
    • 2023-03-25
    • 2020-09-10
    相关资源
    最近更新 更多