5

I want to get the feature at where mouse was clicked in map. So I was wondering how to get features which intersects with that point. Plus I was wondering how can I check what type of object it is (point, poly-line, polygon) ?

3
  • Where are you currently at? The first step is to acquire the shape of the clicked map point. Is your project an Add-in or a COM tool? Commented Nov 3, 2014 at 23:07
  • I don't know exactly difference, but I think it's COM tool. Well at this point I only managed get point object which represents clicked space. I thought that I didn't need to get shape as there should be function on some object which would get all objects which intersects with clicked location.
    – Vilius
    Commented Nov 3, 2014 at 23:12
  • Were abouts are you having the problem? Please post some code of where you're up to... you should be in the OnMouseDown event, have you done DisplayTransformation yet? Commented Nov 3, 2014 at 23:45

1 Answer 1

4

To select from the map using the current selection environment (you can turn off selectability and visibility of layers then this wont pick them up.

Firstly you need to be creating a tool:

using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geodatabase;

namespace RED
{
    public class REd : ESRI.ArcGIS.Desktop.AddIns.Tool

Then in the OnMouseDown event of the tool:

protected override void OnMouseDown(MouseEventArgs arg)
{
    //if (arg.Button == System.Windows.Forms.MouseButtons.Left) // check for left mouse button if you like
    // convert point from 'screen' to 'map' coordinates
    IPoint MouseMapPoint = (ArcMap.Document.FocusMap as IActiveView).ScreenDisplay.DisplayTransformation.ToMapPoint(arg.X, arg.Y);
    // clear selection first
    ArcMap.Document.FocusMap.ClearSelection();
    // Select using the shape (point) to
    // select the feature(s) - false to select any intersecting, true to select just the first
    ArcMap.Document.FocusMap.SelectByShape(MouseMapPoint,(ArcMap.Application as IMxApplication).SelectionEnvironment,false);
    // get the (now) selected features
    IEnumFeature EnumFeatures = (IEnumFeature)ArcMap.Document.FocusMap.FeatureSelection;
    IFeature ThisFeature = EnumFeatures.Next();
    do
    {
        // get the geometry
        IGeometry ThisGeom = ThisFeature.ShapeCopy; // IMPORTANT! USE SHAPECOPY not SHAPE
        // do something different for each geometry type
        if (ThisGeom.GeometryType == esriGeometryType.esriGeometryPoint)
        { 
            // something for points (note: does not include multipoint)
        }
        else if (ThisGeom.GeometryType == esriGeometryType.esriGeometryPolygon)
        { 
            // something for polygons
        }
        else if (ThisGeom.GeometryType == esriGeometryType.esriGeometryPolyline)
        { 
            // something for polylines (note: polylines are different to lines)
        }
        else
        { 
            // unrecognized geometry type
        }
        ThisFeature = EnumFeatures.Next();
    } while (ThisFeature != null);  
}

Which works fine for selecting using a point... this will have problems when trying to select a point or line so to counter that use an increased search buffer (polygon) to make the selection:

// to increase the search

    IGeometry selectionGeometry = (MouseMapPoint as ITopologicalOperator).Buffer(SmallDistance); // make a buffer
    ArcMap.Document.FocusMap.SelectByShape(selectionGeometry, (ArcMap.Application as IMxApplication).SelectionEnvironment, false);

Read about IMap.SelectByShape, IEnumFeature, ITopologicalOperator and Esri Geometry Type Constants

Normally I don't do it that way - the selection environment gets in the way so I use IMap.Layers to iterate through each layer then with an ISpatialFilter make a selection against each layer... that works even if the layer is invisible and/or not selectable.

2
  • Thanks, I wouldn't have thought of the idea to use selections like that
    – Vilius
    Commented Nov 4, 2014 at 9:17
  • No worries Vilius. I usually create objects for the focus map and others (IMap thisMap = ArcMap.Document.FocusMap;) to make it less wordy (then it's thisMap.ClearSelection(); instead of ArcMap.Document.FocusMap.ClearSelection();); I have left it as the default objects to show the relationship/inheritance. Commented Nov 4, 2014 at 22:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.