From: http://www.cnblogs.com/gisak/archive/2011/03/30/2000297.html
There are many methods to select an element or featureselection, such as IMAP: selectbyshape, ilayer: search, ifeaturesection: selectfeature, etc.
Mainly used methods:
Selectfeature (layer, feature) of the IMAP interface (method, select a feature from a layer );
The IMAP interface selectbyshape (shape, ENV, justone) (method, which depends on the range shape of a graph and the selected environment env in the layer to select elements, all layers are selected only from the ifeaturelayer layer)
Ifeatureselection interface selectfeatures (filter, method, justone) (method, select a factor based on the specified standard filter and method. The first parameter is a variable of the queryfilter type, the second parameter is a variable of the esriselectionresultenum type, and the third parameter is a Boolean variable, usually false)
Ifeaturelayer interface search (iqueryfilter, book) (method, create a cursor to query the corresponding filter settings)
1. Click to obtain elements
Let's talk about the code first:
View
Code
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. displaytransfo Rmation. 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 Pa Ctiveview = pmap as iactiveview; ifeaturelayer pfeaturelayer = pmap. get_layer (0) as ifeaturelayer; ifeatureclass pfeatureclass = pfeaturelayer. featureclass; // set the position of the click point. ipoint point = pactiveview. screendisplay. displaytransformation. tomappoint (E. x, E. y); itopologicaloperator ptopo = point as itopologicaloperator; double length; length = convertpixelstomapunits (pactiveview, 4); igeometry pbuffer = pt OPO. buffer (length); igeometry pgeomentry = pbuffer. envelope; // The space filter ispatialfilter pspatialfilter = new spatialfilterclass (); pspatialfilter. geometry = pgeomentry; // depending on the selected elements, set different spatial filtering relationships 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 temperature; ifeature pfeature = pfeatcursor. nextfeature (); While (pfeature! = NULL) {pmap. selectfeature (pfeaturelayer, pfeature); pfeature = pfeatcursor. nextfeature ();} pactiveview. partialrefresh (esriviewdrawphase. esriviewgraphicselection, null, null); // another rewrite: pspatialfilter. geometryfield = pfeatureclass. shapefieldname; iqueryfilter pfilter = pspatialfilter; ifeaturecursor pfeatcursor = pfeaturelayer. search (pfilter, false); ifeature pfeature = pfeatcursor. nextfeature (); While (PFE Ature! = NULL) {pmap. selectfeature (pfeaturelayer, pfeature); pfeature = pfeatcursor. nextfeature ();} pactiveview. partialrefresh (partition. esriviewgraphicselection, null, null );
There is also a simple method for clicking:
View
Code
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. Select
View
Code
IMap pMap = axMapControl1.Map; IActiveView pActiveView = pMap as IActiveView; IEnvelope pEnv = axMapControl1.TrackRectangle(); pMap.SelectByShape(pEnv, null, false); pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection,null, null);