Select to view content in your preferred language

IFeatureLayer from IFeatureClass

2853
3
08-29-2011 07:26 AM
ValeriaAndronaco
New Contributor
Hi Forum,

how can obtain a IFeatureLayer from a IFeatureClass?

I have a FeatureClass:
IFeatureClass pTargetFeatureClass;
pTargetFeatureClass = GetFeatureClassFromShapefileOnDisk("c:\\", "feature_name");


How can get the FeatureLayer from that?
The follow code I think that is incorrect
IFeatureLayer pTargetFeatureLayer = pTargetFeatureClass as IFeatureLayer;


How can I do it?
Can you suggest me? Please?
0 Kudos
3 Replies
NeilClemmons
Regular Contributor III
You can't.  A feature layer and a feature class are two different things.  A feature layer is a type of layer object that is displayed in a map.  A feature class is a type of table with a geometry field.  Feature layers use feature classes as data sources.  You can create a new feature layer and set the feature class to be its data source but you can't get a feature layer reference directly from the feature class.
0 Kudos
JeffreyHamblin
New Contributor III
Like Neil wrote, you can't directly retrieve an IFeatureLayer from an IFeatureClass. And if you are adding a new layer, you do it the way he states.

However, if you are trying to get an existing layer from its feature class, you can iterate through the layers in a map and compare the source feature class of each feature layer looking for a match. Of course, be aware that a single feature class can be the source for more than one layer.
0 Kudos
MichaelMurphy8
New Contributor
If you're just trying to add a featureclass to the map, give this a shot.

IFeatureClass myFeature = CreateFeatureClassWithSR("MyPoints", (IFeatureWorkspace)workspace, gds);
                if(myFeature != null) {
                    //Create an IFeatureLayer
                    IFeatureLayer featureLayer = new FeatureLayerClass();

                    CreateDummyPoint(myFeature);
                    featureLayer.FeatureClass = myFeature;
                    ILayer layer = (ILayer)featureLayer;
                    layer.Name = featureLayer.FeatureClass.AliasName;
                    
                    IMxDocument mxDocument = (IMxDocument)myApplication.Document;
                    IMap map = mxDocument.FocusMap;
                    map.AddLayer(layer);
                }
0 Kudos