Adding a layer to a map from a layer file via GxLayer
Layer files are a common way to distribute cartographic properties for a dataset. In this section, the GxLayer class is used to open a layer and add it to a map. GxLayer is commonly used in ArcGIS for Desktop to interact with layer files.
Complete the following steps to add a layer to a map from a layer file:
- Create a GxLayer and initialize its path property to the location where the applicable layer file is stored. See the following code example:
//Create a GxLayer.
ESRI.ArcGIS.Catalog.IGxLayer gxLayerCls = new ESRI.ArcGIS.Catalog.GxLayer();
ESRI.ArcGIS.Catalog.IGxFile gxFile = (ESRI.ArcGIS.Catalog.IGxFile)LayerCls;
//Explicit Cast.
//Set the path where the layer file is located on disk.
gxFile.Path = layerPathFile;
[VB.NET]
'Create a GxLayer.
Dim gxLayerCls As ESRI.ArcGIS.Catalog.IGxLayer = New ESRI.ArcGIS.Catalog.GxLayer
Dim gxFile As ESRI.ArcGIS.Catalog.IGxFile = gxLayerCls 'Implicit Cast.
'Set the path where the layer file is located on disk.
gxFile.Path = layerPathFile
- Test if you have a valid GxLayer, then add it to the map. See the following code example:
//Test if you have a valid layer and add it to the map.
if (!(gxLayerCls.Layer == null))
{
ESRI.ArcGIS.Carto.IMap map = activeView.FocusMap;
map.AddLayer(gxLayerCls.Layer);
}
[VB.NET]
'Test if you have a valid layer and add it to the map.
If Not gxLayerCls.Layer Is Nothing Then
Dim map As ESRI.ArcGIS.Carto.IMap = activeView.FocusMap
map.AddLayer(gxLayerCls.Layer)
End If
End Sub
Adding a layer to a map from a layer file via MapDocument
This operation is performed using the MapDocumentClass, which can open layer files, as well as map documents (.mxds) and published map files (.pmfs). MapDocument is available in ArcGIS for Desktop, ArcGIS for Server, and ArcGIS Engine.
- Create a MapDocument and use it to open the layer. See the following code example:
IMapDocument pMapDocument = new MapDocumentClass();
//Use the map document class to open the layer.
pMapDocument.Open(layerPathFile, "");
[VB.NET]
Dim pMapDocument As IMapDocument = New MapDocumentClass()
'Use the map document class to open the layer.
pMapDocument.Open(layerPathFile, "")
- Add the layer to the map. See the following code example:
//When opening a layer with the map document class,
//the layer is added to a new document as the first
//layer in the first map.
pMap.AddLayer(pMapDocument.get_Map(0).get_Layer(0));
[VB.NET]
'When opening a layer with the map document class,
'the layer is added to a new document as the first
'layer in the first map.
pMap.AddLayer(pMapDocument.get_Map(0).get_Layer(0))
Development licensing | Deployment licensing |
---|---|
ArcGIS for Desktop Basic | ArcGIS for Desktop Basic |
ArcGIS for Desktop Standard | ArcGIS for Desktop Standard |
ArcGIS for Desktop Advanced | ArcGIS for Desktop Advanced |
Engine Developer Kit | Engine |