我正在尝试创建一个按钮,该按钮计算并选择多边形内的所有点。
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,无论那里有多少点在多边形中,也不会选择任何一个。
请帮助。谢谢:)
答案 0 :(得分:0)
“ 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();
}