選擇一個要素或者一個要素集(FeatureSelection)的方法很多,如IMap::SelectByShape、ILayer::search、IFeatureSection::SelectFeature等方法。
主要用到的方法:
IMap接口的SelectFeature(Layer, Feature) (方法,從一個Layer中選擇一個Feature);
IMap接口SelectByShape(Shape, env, justOne) (方法,從Layer中依靠一個圖形的范圍shape和一個選擇的環境env來選擇要素,而在所有圖層中只從IFeatureLayer的圖層中進行選擇)
IFeatureSelection接口SelectFeatures (Filter, Method, justOne ) (方法,根據指定的標准過濾器filter和方法,選擇要素,第一個參數為QueryFilter類型的變量,第二個參數為esriSelectionResultEnum類型的變量,第三個參數為布爾型變量,通常為false)
IFeatureLayer接口Search (IqueryFilter, book ) (方法,創建一個游標去查詢相應設置的過濾器的查詢)
1 點選法獲取要素
private double ConvertPixelsToMapUnits(IActiveView pActiveView, double pixelUnits) { // Uses the ratio of the size of the map in pixels to map units to do the conversion IPoint p1 = pActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds.UpperLeft; IPoint p2 = pActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds.UpperRight; int x1, x2, y1, y2; pActiveView.ScreenDisplay.DisplayTransformation.FromMapPoint(p1, out x1, out y1); pActiveView.ScreenDisplay.DisplayTransformation.FromMapPoint(p2, out x2, out y2); double pixelExtent = x2 - x1; double realWorldDisplayExtent = pActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds.Width; double sizeOfOnePixel = realWorldDisplayExtent / pixelExtent; return pixelUnits * sizeOfOnePixel; } IMap pMap = axMapControl1.Map; IActiveView pActiveView = pMap as IActiveView; IFeatureLayer pFeatureLayer = pMap.get_Layer(0) as IFeatureLayer; IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass; //設置點擊點的位置 IPoint point = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(e.x, e.y); ITopologicalOperator pTOpo = point as ITopologicalOperator; double length; length = ConvertPixelsToMapUnits(pActiveView, 4); IGeometry pBuffer = pTOpo.Buffer(length); IGeometry pGeomentry = pBuffer.Envelope; //空間濾過器 ISpatialFilter pSpatialFilter = new SpatialFilterClass(); pSpatialFilter.Geometry = pGeomentry; //根據被選擇要素的不同,設置不同的空間濾過關系 switch (pFeatureClass.ShapeType) { case esriGeometryType.esriGeometryPoint: pSpatialFilter.SpatialRel=esriSpatialRelEnum.esriSpatialRelContains; break; case esriGeometryType.esriGeometryPolyline: pSpatialFilter.SpatialRel=esriSpatialRelEnum.esriSpatialRelCrosses; break; case esriGeometryType.esriGeometryPolygon : pSpatialFilter.SpatialRel=esriSpatialRelEnum.esriSpatialRelIntersects; break; } IFeatureSelection pFSelection=pFeatureLayer as IFeatureSelection; pFSelection.SelectFeatures(pSpatialFilter,esriSelectionResultEnum.esriSelectionResultNew,false); ISelectionSet pSelectionset=pFSelection.SelectionSet; ICursor pCursor; pSelectionset.Search(null,true,out pCursor); IFeatureCursor pFeatCursor=pCursor as IFeatureCursor; IFeature pFeature=pFeatCursor.NextFeature(); while(pFeature!=null) { pMap.SelectFeature(pFeatureLayer,pFeature); pFeature=pFeatCursor.NextFeature(); } pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphicSelection,null,null); //另外的改寫: pSpatialFilter.GeometryField = pFeatureClass.ShapeFieldName; IQueryFilter pFilter = pSpatialFilter; IFeatureCursor pFeatCursor = pFeatureLayer.Search(pFilter,false); IFeature pFeature=pFeatCursor.NextFeature(); while(pFeature!=null) { pMap.SelectFeature(pFeatureLayer,pFeature); pFeature=pFeatCursor.NextFeature(); } pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphicSelection,null,null);
另外還有一種較簡單的點選方法:
IGeometry g = null; IEnvelope pEnv; IActiveView pActiveView = axMapControl1.ActiveView; IMap pMap = axMapControl1.Map; pEnv = axMapControl1.TrackRectangle(); if (pEnv.IsEmpty == true) { ESRI.ArcGIS.Display.tagRECT r; r.bottom = e.y + 5; r.top = e.y - 5; r.left = e.x - 5; r.right = e.x + 5; pActiveView.ScreenDisplay.DisplayTransformation.TransformRect(pEnv, ref r, 4); pEnv.SpatialReference = pActiveView.FocusMap.SpatialReference; } g = pEnv as IGeometry; axMapControl1.Map.SelectByShape(g, null, false); axMapControl1.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
2 拉框選擇
IMap pMap = axMapControl1.Map; IActiveView pActiveView = pMap as IActiveView; IEnvelope pEnv = axMapControl1.TrackRectangle(); pMap.SelectByShape(pEnv, null, false); pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection,null, null);