選擇一個要素或者一個要素集(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 ) (方法,創建一個游標去查詢相應設置的過濾器的查詢)
方法五:
IGraphicsContainer 可以m_GraphicsContainer = m_MapControl.ActiveView.GraphicsContainer;然后重置遍歷循環IElement element = m_GraphicsContainer.Next();
具體代碼
方法二:
IMap pMap = axMapControl1.Map;
IActiveView pActiveView = pMap as IActiveView; IFeatureLayer pFeatureLayer = pMap.get_Layer(0) as IFeatureLayer; IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass; IFeatureSelection pFSelection = pFeatureLayer as IFeatureSelection; //設置點擊點的位置 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; ISelectionEnvironment pSelEnv = new SelectionEnvironment();//新建選擇環境 IRgbColor pColor = new RgbColor(); pColor.Red = 255; pSelEnv.DefaultColor = pColor;//設置高亮顏色 pMap.SelectByShape(pBuffer, pSelEnv, false);//選擇圖形 _map.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null);?
----------------------------------------------- 分割線 --------------------------------------------------------------
另一種使用方式
看到的一篇文章轉載過來:【原文傳送門】
要素選擇包括點選和拉框選擇,拉框選擇比較簡單,直接用IMap.SelectByShape就可以了;
點選則是根據鼠標點創建一個緩沖區,然后再SelectByShape方法。如果不想建緩沖區的話,直接根據鼠標點擴展成一個正方形框也可以。其實兩者差別不是很大,但后者更簡便。
public override void OnMouseDown(int Button, int Shift, int X, int Y)
{
IEnvelope pEnv;
IActiveView pActiveView = m_MapCtl.ActiveView; IMap pMap = m_MapCtl.Map; pEnv = m_MapCtl.TrackRectangle(); //ISelectionEnvironment pSelectionEnv = new SelectionEnvironment(); //新建選擇環境 補充 //pSelectionEnv.DefaultColor = new RgbColor() { Red = 255, Green = 0, Blue = 0 }; if ( pEnv.IsEmpty == true ) //點選 { tagRECT r; r.bottom = Y + 5; r.top = Y - 5; r.left = X - 5; r.right = X + 5; pActiveView.ScreenDisplay.DisplayTransformation.TransformRect(pEnv, ref r, 4); pEnv.SpatialReference = pActiveView.FocusMap.SpatialReference; } pMap.SelectByShape(pEnv, m_SelectEnvir, false); pActiveView.Refresh(); pEnv = null; }
其實主要利用了DisplayTransformation.TransformRect方法,將屏幕范圍轉換成地圖范圍。
上面這個方法可以用於點選和框選。
方法三:
可以用這個方法來放大圖層。
IFeatureCursor featureCursor = pFeatureLayer.Search(pQueryFilter, false); IFeature pFeature; IFeatureSelection pFeatureSelection = pFeatureLayer as IFeatureSelection; pFeatureSelection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, false); axMapControl.ActiveView.Refresh(); if ((pFeature = featureCursor.NextFeature()) != null) { ... }
方法五:
m_GraphicsContainer.Reset(); IElement element = m_GraphicsContainer.Next(); while (element != null) { //If the polygon contains the point if (relationalOperator.Contains(element.Geometry) == true) { //QI for IMarkerElement interface through IElement interface IMarkerElement markerElement = (IMarkerElement) element; markerElement.Symbol = GetMarkerSymbol(true); //QI for the IElementProperties interface through IElement interface IElementProperties elementProperties = (IElementProperties) element; elementProperties.Name = true.ToString(); } element = m_GraphicsContainer.Next(); } if (chkTracking.CheckState == CheckState.Unchecked) //Refresh the graphics m_MapControl.Refresh(esriViewDrawPhase.esriViewGraphics, Type.Missing, Type.Missing);
---------------------------------------------------- 沒錯,我就是分割線~~ ------------------------------------------
下面附上點選框選之“經典”的博客,嗬!【傳送門啊啊啊】【開吧!遠古....咳咳】
PS.
不論是 pMap.SelectByShape(pEnv, pSelectionEnv, false);
還是 pMap.SelectByShape(g, null, false); (PEnv轉Geometry)
在多層要素重疊情況下,點選似乎都會遇到pEnv.depth引發異常,暫時不知道如何處理,但是可以忽略