【发布时间】: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在多边形中,它也没有选择它们中的任何一个。
请帮忙。谢谢你:)
【问题讨论】: