Select to view content in your preferred language

How do I determine the type of data referenced in a layer file?

912
2
10-05-2010 03:46 PM
DennisGeasan
Frequent Contributor
I want to determine the type of data that is referenced in a layer file.  For example, I open a layer file that is referencing a raster dataset.  In code how do you determine that the layer file is referencing a raster dataset?  Right now I first use a 'try' block and QI to the IFeatureLayer interface.  If this fails then in the 'catch' block I QI to IRasterLayer.  I would rather first determine that the layer is pointing to a raster dataset and then use a 'switch/case' block to work with the appropriate interface. 

My current method:

string LayerFileLocation = "C:\SomeLayerFile.lyr";
ILayerFile layerFile = new ESRI.ArcGIS.Carto.LayerFileClass();
layerFile.Open(LayerFileLocation);
ILayer pLayer = layerFile.Layer;
IFeatureLayer2 pFeatureLayer2;
IRasterLayer pRasterLayer;
try
{
   pFeatureLayer2 = (IFeatureLayer2)pLayer;
}
catch
{
   pRasterLayer = (IRasterLayer)pLayer;
}

Dennis Geasan
GIS Technolgies
0 Kudos
2 Replies
NeilClemmons
Honored Contributor
You just need to check the type using Is in C#:

If (layer Is IFeatureLayer)
{
// do something
}
ElseIf (layer Is IRasterLayer)
{
// do something
}

It would be nice if you could use a Switch block to do this instead of a bunch of Else/ElseIfs but I don't think you can do that.
0 Kudos
DennisGeasan
Frequent Contributor
So that's what Is is!  Thanks Neil.
0 Kudos