Arcengine 实现要素选取的方法

选择一个要素或者一个要素集(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 点选法获取要素

   废话少说先看代码:

  1. private double ConvertPixelsToMapUnits(IActiveView pActiveView, double pixelUnits)  
  2. {  
  3.     // Uses the ratio of the size of the map in pixels to map units to do the conversion  
  4.     IPoint p1 = pActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds.UpperLeft;  
  5.     IPoint p2 = pActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds.UpperRight;  
  6.     int x1, x2, y1, y2;  
  7.     pActiveView.ScreenDisplay.DisplayTransformation.FromMapPoint(p1, out x1, out y1);  
  8.     pActiveView.ScreenDisplay.DisplayTransformation.FromMapPoint(p2, out x2, out y2);  
  9.     double pixelExtent = x2 – x1;  
  10.     double realWorldDisplayExtent = pActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds.Width;  
  11.     double sizeOfOnePixel = realWorldDisplayExtent / pixelExtent;  
  12.     return pixelUnits * sizeOfOnePixel;  
  13. }  
  14.      IMap pMap = axMapControl1.Map;  
  15.         IActiveView pActiveView = pMap as IActiveView;  
  16.         IFeatureLayer pFeatureLayer = pMap.get_Layer(0) as IFeatureLayer;  
  17.         IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;  
  18.   
  19.         //设置点击点的位置  
  20.         IPoint point = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(e.x, e.y);  
  21.         ITopologicalOperator pTOpo = point as ITopologicalOperator;  
  22.         double length;  
  23.         length = ConvertPixelsToMapUnits(pActiveView, 4);  
  24.         IGeometry pBuffer = pTOpo.Buffer(length);  
  25.         IGeometry pGeomentry = pBuffer.Envelope;  
  26.         //空间滤过器  
  27.         ISpatialFilter pSpatialFilter = new SpatialFilterClass();  
  28.         pSpatialFilter.Geometry = pGeomentry;  
  29.         //根据被选择要素的不同,设置不同的空间滤过关系  
  30.         switch (pFeatureClass.ShapeType)  
  31.         {  
  32.             case esriGeometryType.esriGeometryPoint:  
  33.                 pSpatialFilter.SpatialRel=esriSpatialRelEnum.esriSpatialRelContains;  
  34.                 break;  
  35.             case esriGeometryType.esriGeometryPolyline:  
  36.                 pSpatialFilter.SpatialRel=esriSpatialRelEnum.esriSpatialRelCrosses;  
  37.                 break;  
  38.             case esriGeometryType.esriGeometryPolygon :  
  39.                 pSpatialFilter.SpatialRel=esriSpatialRelEnum.esriSpatialRelIntersects;  
  40.                 break;  
  41.   
  42.         }  
  43.         IFeatureSelection pFSelection=pFeatureLayer as IFeatureSelection;  
  44.         pFSelection.SelectFeatures(pSpatialFilter,esriSelectionResultEnum.esriSelectionResultNew,false);  
  45.         ISelectionSet pSelectionset=pFSelection.SelectionSet;  
  46.         ICursor pCursor;  
  47.         pSelectionset.Search(null,true,out pCursor);  
  48.         IFeatureCursor pFeatCursor=pCursor as IFeatureCursor;  
  49.         IFeature pFeature=pFeatCursor.NextFeature();  
  50.          while(pFeature!=null)  
  51.          {  
  52.              pMap.SelectFeature(pFeatureLayer,pFeature);  
  53.              pFeature=pFeatCursor.NextFeature();  
  54.          }  
  55.    pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphicSelection,null,null);  
  56.   
  57.      pSpatialFilter.GeometryField = pFeatureClass.ShapeFieldName;  
  58.         IQueryFilter pFilter = pSpatialFilter;  
  59.         IFeatureCursor pFeatCursor = pFeatureLayer.Search(pFilter,false);  
  60.         IFeature pFeature=pFeatCursor.NextFeature();  
  61.          while(pFeature!=null)  
  62.          {  
  63.              pMap.SelectFeature(pFeatureLayer,pFeature);  
  64.              pFeature=pFeatCursor.NextFeature();  
  65.          }  
  66.    pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphicSelection,null,null);  

另外还有一种比较简单的点选方法:

[csharp] view
plain
 copy

  1. IGeometry g = null;  
  2. IEnvelope pEnv;  
  3. IActiveView pActiveView = axMapControl1.ActiveView;  
  4. IMap pMap = axMapControl1.Map;  
  5. pEnv = axMapControl1.TrackRectangle();  
  6. if (pEnv.IsEmpty == true)  
  7. {  
  8.     ESRI.ArcGIS.Display.tagRECT r;  
  9.     r.bottom = e.y + 5;  
  10.     r.top = e.y – 5;  
  11.     r.left = e.x – 5;  
  12.     r.right = e.x + 5;  
  13.     pActiveView.ScreenDisplay.DisplayTransformation.TransformRect(pEnv, ref r, 4);  
  14.     pEnv.SpatialReference = pActiveView.FocusMap.SpatialReference;  
  15. }  
  16. g = pEnv as IGeometry;  
  17. axMapControl1.Map.SelectByShape(g, nullfalse);  
  18. axMapControl1.Refresh(esriViewDrawPhase.esriViewGeoSelection, nullnull);  

2 拉框选择


  1. IMap pMap = axMapControl1.Map;  
  2.                 IActiveView pActiveView = pMap as IActiveView;  
  3.                 IEnvelope pEnv = axMapControl1.TrackRectangle();  
  4. pMap.SelectByShape(pEnv, nullfalse);  
  5.                 pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection,nullnull); 

转载自:https://blog.csdn.net/kone0611/article/details/73733233

You may also like...