0

I have a list of IFeature (List lst) and I want to select and highlight and show in the map. All I am getting is like the IFeatureSelection , ISelectionSet and they work with Feature Layer not the feature. Can anyone show me some light in that direction. Regarding the context, I am getting the features from a method after validations and I am trying to select those/ or highlight those.

public void HighlightInvalidFeatures(List<IFeature> featureList, IActiveView activeView, IGeometry geometry , IMap map)
        {
            //First find all the feature classes that exist for your features
            Dictionary<IFeatureClass, List<int>> uniqueFeatureClasses = new Dictionary<IFeatureClass, List<int>>();
            IFeatureClass fc = null;
            foreach (var feature in featureList)
            {
                fc = feature.Table as IFeatureClass;

                if (!uniqueFeatureClasses.ContainsKey(fc))
                    uniqueFeatureClasses.Add(fc, new List<int>());

                uniqueFeatureClasses[fc].Add(feature.OID);
            }

            //get the current map object
            IMap mapInstance = map;

            //clear the current selection
            mapInstance.ClearSelection();

            IFeatureLayer fLayer = null;

            IQueryFilter filter = null;
            //create and add the layers
            foreach (var pair in uniqueFeatureClasses)
            {
                //create the FeatureLayer
                fLayer = new FeatureLayerClass();
                fLayer.FeatureClass = pair.Key;
                fLayer.Name = pair.Key.AliasName; //this could be anything



         IEnumLayer enumLayer = mapInstance.get_Layers(null, true);
                 ILayer esriLayer = null;
                 while ((esriLayer=enumLayer.Next()) != null)
                {
                     // Check this if part
                        if (esriLayer.Valid)
                        {
                                if (esriLayer == featureLayer)
                                {
                        fLayer = esriLayer;

                                }
                    else
                    {
                        // Adding the layer 
                        mapInstance.AddLayer(fLayer as ILayer);
                    }
                        }
                }

                //select on the layer
                filter = new QueryFilter();
                filter.WhereClause = pair.Key.OIDFieldName + " IN (" + String.Join(",", pair.Value) + ")";

                // filter.WhereClause = fc.OIDFieldName + " IN (" + String.Join(",", pair.Value) + ")";

                activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, activeView.FocusMap, null);

                (fLayer as IFeatureSelection).SelectFeatures(filter, esriSelectionResultEnum.esriSelectionResultAdd, false);

                activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, activeView.FocusMap, null);
            }   

        }
        #endregion
    }
3
  • Selection is something that happens on layers, not features or feature classes. So you will either need to create a new layer (or layers) and add your features to them, or you will need to identify existing layers that already contain the current feature. You can then use the IFeatureSelection.SelectFeatures method to select the features.
    – danielm
    Commented Jun 21, 2017 at 21:51
  • So if I want to highlight or select these features I need to create layers on top of it and add the features to the layers thereafter I can select. Ok got some clue. Thanks
    – royan
    Commented Jun 22, 2017 at 3:41
  • There are 2 points which I want to mention# 1)can you please guide me how to add my features to the newly created layers. I am trying to do that but no luck. But I can not select all the features in the layer. Only those features need to be selected. 2) But regarding what you said for selection if the selection is done on a layer there will be multiple features (may 3 transmedias are there or 10 structures ). All will be selected. Do we have any query to specify these features from the layer needs to be selected and these are not ?
    – royan
    Commented Jun 22, 2017 at 11:49

1 Answer 1

1

There may be better ways of doing this, but this is how I'd do it off the top of my head.

//First find all the feature classes that exist for your features
Dictionary<IFeatureClass, List<int>> uniqueFeatureClasses= new Dictionary<IFeatureClass, List<int>>();
foreach(var feature in MyListOfFeatures)
{   
    IFeatureClass fc = feature.Table as IFeatureClass;

    if(!uniqueFeatureClasses.ContainsKey(fc))
        uniqueFeatureClasses.Add(fc, new List<int>());

    uniqueFeatureClasses[fc].Add(feature.OID);
}

//get the current map object
IMap map = (ArcMap.Application.Document as IMxDocument).FocusMap;

//clear the current selection
map.ClearSelection();

//create and add the layers
foreach(var pair in uniqeFeatureClasses)
{
    //create the FeatureLayer
    IFeatureLayer fLayer = new IFeatureLayer();
    fLayer.FeatureClass = pair.Key;
    fLayer.Name = pair.Key.AliasName; //this could be anything
    map.AddLayer(fLayer as ILayer);

    //select on the layer
    IQueryFilter filter = new QueryFilter();
    filter.WhereClause = fc.OIDFieldName + " IN (" + String.Join(",",pair.Value) + ")";

    (fLayer as IFeatureSelection).SelectFeatures(filter, esriSelectionResultEnum.esriSelectionResultAdd, false);
}

Update:

I have tried your code and with a small modification it works (at least as far as I understand what you are trying to do). The issue is that comparing Layers against each other just doesn't work super well. So I changed this:

if (esriLayer == featureLayer)
{
    fLayer = esriLayer;
}

to this:

if (fLayer.FeatureClass == (esriLayer as IFeatureLayer).FeatureClass)
{
   fLayer = (esriLayer as IFeatureLayer); 
}

After I made that change I input features into the method from several layers and all of those features were highlighted at once.

13
  • Thanks Danielm for guiding me. With little modification the code works fine except it is only selecting last layers it gets in the loop. I think activeView.PartialRefresh would work. I did not try refresh but I was debugging the code and found that the loop is taking all the OIDs.
    – royan
    Commented Jun 23, 2017 at 12:04
  • only last lyers are selected. I tried refresh but did not work. The additional layers are also added in TOC. Can we avoid adding duplicate layers in the TOC?
    – royan
    Commented Jun 23, 2017 at 12:29
  • You would need to loop through the layers already on the map (IMap.Layers) and check if any of those already reference a feature class you are looking for. I would say, at least for testing purposes just use activeView.Refresh() to make sure you can see everything that was and was not updated.
    – danielm
    Commented Jun 23, 2017 at 14:20
  • There is a note on the IActiveView.PartialRefresh documentation: "*When selecting features, you must call PartialRefresh twice, once before and once after the selection operation"
    – danielm
    Commented Jun 23, 2017 at 14:20
  • I used partial Refresh 2 times as in # activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, activeView.FocusMap, null); (fLayer as IFeatureSelection).SelectFeatures(filter, esriSelectionResultEnum.esriSelectionResultAdd, false); activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, activeView.FocusMap, null); Thiis did nor work. regarding the layers being added ... tried to get the layers from map and then compare it in a loop but no luck
    – royan
    Commented Jun 23, 2017 at 17:15

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.