Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm currently using ArcGIS and I'm trying to create a basic application where I can click on a country and then get back information such as the shape, size, etc.

I've looked at the code snippets and the documentation and up to date this is what I've come up with.

IPoint pt = new PointClass();
pt.X = e.X; 
pt.Y = e.Y;

IGeoFeatureLayer layer = myMapControl.get_Layer(0) as IGeoFeatureLayer;
ESRI.ArcGIS.Geodatabase.IFeatureCursor cursor = GetAllFeaturesFromPointSearchInGeoFeatureLayer(1.0 , IPoint , layer , myMapControl.ActiveView)

So at this point I can click on the map, get the X,Y coords from the mouse - get the base layer from the map control and then get the FeatureCursor. Looking at the cursor info I do see things like CNTRY_NAME but nothing else of use. Am I going about this in the totally wrong direction? I've looked at http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html quite a bit lately in addition to this site, but I'm coming up blank.

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

It may not the fastest way to get the requested feature using the IIdentify-Interface, but it works quite well:

IMxDocument doc = m_application.Document as IMxDocument;
IIdentify2 ident = featLayer as IIdentify2;
int tol = doc.SearchTolerancePixels;

// consruct a small rectangle out of the x,y coord and the document's pixel tolerance
tagRECT r;
r.left = x - tol;
r.top = y - tol;
r.right = x + tol;
r.bottom = y + tol;

// Tranform the device rectange into a geographic rectangle via the display transformation
IEnvelope env = new EnvelopeClass();
int opt = esriDisplayTransformationEnum.esriTransformPosition.GetHashCode()+
          esriDisplayTransformationEnum.esriTransformToMap.GetHashCode();
app.Display.DisplayTransformation.TransformRect(env, ref r, opt);

// setup the spatial reference on the newly hydrated envelope
env.SpatialReference = doc.FocusMap.SpatialReference;

// identify with the envelope
ident.Scale = m_application.Display.DisplayTransformation.ScaleRatio;
IArray ids = ident.Identify(env, null);

if(ids == null || ids.Count == 0) {
    return null;
}

IIdentifyObj obj = ids.get_Element(0) as IIdentifyObj;
IRowIdentifyObject row = obj as IRowIdentifyObject;
return row.Row;

m_application is of type IApplication - a reference to your ArcMap instance. And have a look at the example in ArcObjects.

share|improve this answer
    
Change IIdentify2 to IIdentify and a few small changes and it works. THANKS! –  PSU_Kardi Oct 27 '10 at 18:13
    
Though not strictly an error, GetHashCode() is not intended to be used for this purposes. Instead, use direct cast to an int, for example (int)esriDisplayTransformationEnum.esriTransformPosition. –  Petr Krebs Oct 28 '10 at 22:50
add comment

You're almost there. You just have to add the following code :

    int countryNameIndex = featureClass.Fields.FindField("CNTRY_NAME");

    IFeature feature;
    while((feature = cursor.NextFeature()) != null)
    {
        object countryName = feature.get_Value(countryNameIndex);
        Console.WriteLine(countryName);
    }

If you don't like the while loop you can add the following extension method :

    public static IEnumerable<IFeature> ToEnumerable(this IFeatureCursor cursor)
    {
        IFeature feat;
        while ((feat = cursor.NextFeature()) != null)
        {
            yield return feat;
        }
        yield break;
    }

And replace the while loop with :

    foreach(var feature in cursor.ToEnumerable())
    {
         // add your code here
    }
share|improve this answer
    
Where did featureClass come from? –  PSU_Kardi Oct 27 '10 at 14:48
    
You can get at that by using layer.FeatureClass in your code. See here. –  Michael Todd Oct 27 '10 at 15:12
    
I tried the code you suggested; however, it never steps into the cursor.NextFeature. HOWEVER, the countryNameIndex did return a value –  PSU_Kardi Oct 27 '10 at 15:21
    
That probably means that your cursor didn't have any features in it, i.e. nothing was returned from the query. Are you certain that there should be some features at that location or via that where clause? –  Michael Todd Oct 27 '10 at 15:29
    
I would think so. I'm clicking right on top of North America. If I use the pre-defined toolbar control "Identify" it selects North America and flashes briefly when selected. –  PSU_Kardi Oct 27 '10 at 16:19
add comment

Look at the sample: Tabbed feature inspector

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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