Select to view content in your preferred language

How to get the IFeatureLayer of currently selected feature

2980
6
09-18-2010 01:17 PM
SolomonPulapkura
Frequent Contributor
I have a feature selected on my map. I want to get the IFeatureLayer of the layer that this feature is contained in so I can use it somewhere else.

I'm trying something like this

IFeatureLayer featLyr = 'featurelayer of currently selected feature'

[I DONT WANT TO GET IT BY SPECIFYING THE LAYER INDEX IN THE TOC SUCH AS map.Layer(0) OR BY SPECIFYING LAYER NAME]

Then I can use featLyr in

IQueryByLayer queryByLyr = new QueryByLayerClass();
queryByLyr.ByLayer = featLyr; etc etc

Hope this was clear,

Thanks,
Solomon
0 Kudos
6 Replies
JakubSisak
Honored Contributor
Don't think this can be done without somehow referencing or looping through the map layers. (Which is what you mentioned you do not want to do) What exactly is it you need to achieve?  Did you look at IFeatureClass, IQueryFilter, etc?
0 Kudos
JeffMatson
Frequent Contributor
Here is a sample written by Miles Hitchen that should do the trick:

'Miles Hitchen
Private Function GetLayerFromFeature(pFtr As IFeature) As IFeatureLayer
'Dim pFtr As IFeature
Dim pFtrCls As IFeatureClass
Dim pMxDoc As IMxDocument
Dim pEnumLyr As IEnumLayer
Dim pLyr As ILayer
Dim pFtrLyr As IFeatureLayer
Dim pObj As IObject
    ' Get the featureclass of the feature
    Set pObj = pFtr
    Set pFtrCls = pObj.Class
 
    ' Get a layer enumerator on the map
    Set pMxDoc = ThisDocument
    Set pEnumLyr = pMxDoc.FocusMap.Layers
 
    ' Loop through the layers until we find one that contains
    ' the same featureclass as the feature
    pEnumLyr.Reset
    Set pLyr = pEnumLyr.Next
    While Not pLyr Is Nothing
        If TypeOf pLyr Is IFeatureLayer Then
            Set pFtrLyr = pLyr
            If pFtrLyr.FeatureClass Is pFtrCls Then
                ' Found it!
                Set GetLayerFromFeature = pLyr
                Exit Function
            End If
        End If
        Set pLyr = pEnumLyr.Next
    Wend
    ' Not found so return Nothing
    Set GetLayerFromFeature = Nothing
End Function
0 Kudos
SolomonPulapkura
Frequent Contributor
Thanks for your replies!

What I'm trying to do here is create a tool for automating selection of intersecting features.
So, when a user selects a polyline or polygon, all features that intersect selected feature are selected.
I want to use IQueryByLayer. But I wanted to get the IFeatureLayer of currently selected feature and assign it to IQueryByLayer's ByLayer and FromLayer properties.
Thanks Jeff, I'll try your suggestion. Is there any other way I can achieve what I'm trying?
0 Kudos
JohnHauck
Frequent Contributor
As the others have mentioned I'm pretty sure you will need to loop through the layers at some level if you must work with it's source layer.

You could use IMap::FeatureSelection to get any selected features from the map. From this you can find the source feature class but not the layer directly.

You have options for the selection, some that don't require an input feature layer. You could use the geometry of the selected feature(s) directly in some selection operations. 

The following will select features from any selectable layer in the map based on a currently selected feature.

Dim doc As IMxDocument
Set doc = ThisDocument

'will return all selected
Dim enumFeature As IEnumFeature
Set enumFeature = doc.FocusMap.FeatureSelection
enumFeature.Reset

'Get the first feature
Dim feature As IFeature
Set feature = enumFeature.Next

'you can set additional selection options here
Dim selectionEnvironment As ISelectionEnvironment
Set selectionEnvironment = New selectionEnvironment

doc.FocusMap.SelectByShape feature.Shape, selectionEnvironment, False

doc.ActiveView.Refresh
0 Kudos
SolomonPulapkura
Frequent Contributor
John,
That worked perfectly for what I was trying to do. Thanks so much! And how simple it was. I guess I was complicating an easy process.
Although, I am still wondering how I could have used IQueryByLayer just to expand my knowledge, without specifying the name or index of a layer. I guess I do have to loop through the layers.

Now, how do you mark a solution in the new forums?

Thanks again,
Solomon
0 Kudos
JakubSisak
Honored Contributor
You have to loop through the layers....

Unfortunately, you cannot mark your solution as "Answered" in the new forums... Yet.
This is really too bad because a lot of the former "gurus" are not participating as much.
0 Kudos