ArcGIS Pro 3.2 API Reference Guide
ArcGIS.Desktop.Mapping Namespace / LayerFactory Class / CreateLayer Method / CreateLayer(Uri,ILayerContainerEdit,Int32,String) Method
An URI represents the path or url to a dataset or .lyrx or .lpkx file.
A map or group layer instance where the Layer will be added
The position of the layer in the map or group layer. 0 represents the top most position.
(optional) When it is not provided, the default display name gets used e.g. the dataset name or alias. (default value = "")
Example

In This Topic
    CreateLayer(Uri,ILayerContainerEdit,Int32,String) Method
    In This Topic
    Creates a new Layer instance with the specified path to a dataset and adds it to a container such as a map or a group layer. Optionally you can provide a name to override the default display name. This method must be called on the MCT. Use QueuedTask.Run.
    Syntax
    Public Overloads Function CreateLayer( _
       ByVal dataUri As Uri, _
       ByVal container As ILayerContainerEdit, _
       Optional ByVal index As Integer, _
       Optional ByVal layerName As String _
    ) As Layer

    Parameters

    dataUri
    An URI represents the path or url to a dataset or .lyrx or .lpkx file.
    container
    A map or group layer instance where the Layer will be added
    index
    The position of the layer in the map or group layer. 0 represents the top most position.
    layerName
    (optional) When it is not provided, the default display name gets used e.g. the dataset name or alias. (default value = "")

    Return Value

    Exceptions
    ExceptionDescription
    This method or property must be called within the lambda passed to QueuedTask.Run.
    DataUri or conainer is null.
    Remarks

    Some sample paths to:

    A FeatureClass in a FileGeodatabase C:\Data\MyFileGDB.gdb\Census
    A shape file in a folder \\Machine\SharedFolder\MySpatialData.dbf
    A RasterDataset in a FileGeodatabase C:\Data\MyFileGDB.gdb\DEM
    A FeatureClass from a SDE C:\Connections\MySDEConnection.sde\Roads
    An image file in a folder \\Machine\SharedFolder\Imagery.tif
    A .lyrx or .lpkx file \\Machine\SharedFolder\Fires.lyrx
    A map service layer http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer
    A feature layer off a map or feature service http://sampleserver6.arcgisonline.com/arcgis/rest/services/NapervilleShelters/FeatureServer/0

    Example
    Create and add a layer to the active map
          /*
    * string url = @"c:\data\project.gdb\DEM";  //Raster dataset from a FileGeodatabase
    * string url = @"c:\connections\mySDEConnection.sde\roads";  //FeatureClass of a SDE
    * string url = @"c:\connections\mySDEConnection.sde\States\roads";  //FeatureClass within a FeatureDataset from a SDE
    * string url = @"c:\data\roads.shp";  //Shapefile
    * string url = @"c:\data\imagery.tif";  //Image from a folder
    * string url = @"c:\data\mySDEConnection.sde\roads";  //.lyrx or .lpkx file
    * string url = @"c:\data\CAD\Charlottesville\N1W1.dwg\Polyline";  //FeatureClass in a CAD dwg file
    * string url = @"C:\data\CAD\UrbanHouse.rvt\Architectural\Windows"; //Features in a Revit file
    * string url = @"http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer";  //map service
    * string url = @"http://sampleserver6.arcgisonline.com/arcgis/rest/services/NapervilleShelters/FeatureServer/0";  //FeatureLayer off a map service or feature service
    */
          string url = @"c:\data\project.gdb\roads";  //FeatureClass of a FileGeodatabase
    
          Uri uri = new Uri(url);
          await QueuedTask.Run(() => LayerFactory.Instance.CreateLayer(uri, MapView.Active.Map));
    
    Create a raster layer
    string url = @"C:\Images\Italy.tif";
    await QueuedTask.Run(() =>
    {
      // Create a raster layer using a path to an image.
      // Note: You can create a raster layer from a url, project item, or data connection.
      rasterLayer = LayerFactory.Instance.CreateLayer(new Uri(url), aMap) as RasterLayer;
    });
    Create a mosaic layer
    MosaicLayer mosaicLayer = null;
    string url = @"C:\Images\countries.gdb\Italy";
    await QueuedTask.Run(() =>
    {
      // Create a mosaic layer using a path to a mosaic dataset.
      // Note: You can create a mosaic layer from a url, project item, or data connection.
      mosaicLayer = LayerFactory.Instance.CreateLayer(new Uri(url), aMap) as MosaicLayer;
    });
    Create an image service layer
    ImageServiceLayer isLayer = null;
    string url =
    @"http://imagery.arcgisonline.com/arcgis/services/LandsatGLS/GLS2010_Enhanced/ImageServer";
    await QueuedTask.Run(() =>
    {
      // Create an image service layer using the url for an image service.
      isLayer = LayerFactory.Instance.CreateLayer(new Uri(url), aMap) as ImageServiceLayer;
    });
    Create a stream layer with a definition query
    //Must be on the QueuedTask
    var url = "https://geoeventsample1.esri.com:6443/arcgis/rest/services/AirportTraffics/StreamServer";
    var lyrCreateParam = new FeatureLayerCreationParams(new Uri(url))
    {
      IsVisible = true,
      //At 2.x - DefinitionFilter = new CIMDefinitionFilter()
      //{
      //  DefinitionExpression = "RWY = '29L'",
      //  Name = "Runway"
      //}
      DefinitionQuery = new DefinitionQuery(whereClause: "RWY = '29L'", name: "Runway")
    };
    
    var streamLayer = LayerFactory.Instance.CreateLayer<StreamLayer>(lyrCreateParam, map);
    Create a stream layer with a simple renderer
    var url = @"https://geoeventsample1.esri.com:6443/arcgis/rest/services/LABus/StreamServer";
    var uri = new Uri(url, UriKind.Absolute);
    //Must be on QueuedTask!
    var createParams = new FeatureLayerCreationParams(uri)
    {
      RendererDefinition = new SimpleRendererDefinition()
      {
        SymbolTemplate = SymbolFactory.Instance.ConstructPointSymbol(
                            ColorFactory.Instance.BlueRGB,
                            12,
                     SimpleMarkerStyle.Pushpin).MakeSymbolReference()
      }
    };
    var streamLayer = LayerFactory.Instance.CreateLayer<StreamLayer>(
                        createParams, map);
    
    
    Setting a unique value renderer for latest observations
    var url = @"https://geoeventsample1.esri.com:6443/arcgis/rest/services/AirportTraffics/StreamServer";
    var uri = new Uri(url, UriKind.Absolute);
    //Must be on QueuedTask!
    
    var createParams = new FeatureLayerCreationParams(uri)
    {
      IsVisible = false
    };
    var streamLayer = LayerFactory.Instance.CreateLayer<StreamLayer>(
                        createParams, map);
    //Define the unique values by hand
    var uvr = new CIMUniqueValueRenderer()
    {
      Fields = new string[] { "ACTYPE" },
      UseDefaultSymbol = true,
      DefaultLabel = "Others",
      DefaultSymbol = SymbolFactory.Instance.ConstructPointSymbol(
                  CIMColor.CreateRGBColor(185, 185, 185), 8, SimpleMarkerStyle.Hexagon).MakeSymbolReference()
    };
    
    var classes = new List<CIMUniqueValueClass>();
    //add in classes - one for ACTYPE of 727, one for DC 9
    classes.Add(
      new CIMUniqueValueClass() {
            Values = new CIMUniqueValue[] {
                  new CIMUniqueValue() { FieldValues = new string[] { "B727" } } },
            Visible = true,
            Label = "Boeing 727",
            Symbol = SymbolFactory.Instance.ConstructPointSymbol(
                  ColorFactory.Instance.RedRGB, 10, SimpleMarkerStyle.Hexagon).MakeSymbolReference()
    });
    classes.Add(
      new CIMUniqueValueClass()
      {
        Values = new CIMUniqueValue[] {
                  new CIMUniqueValue() { FieldValues = new string[] { "DC9" } } },
        Visible = true,
        Label = "DC 9",
        Symbol = SymbolFactory.Instance.ConstructPointSymbol(
                  ColorFactory.Instance.GreenRGB, 10, SimpleMarkerStyle.Hexagon).MakeSymbolReference()
      });
    //add the classes to a group
    var groups = new List<CIMUniqueValueGroup>()
    {
      new CIMUniqueValueGroup() {
         Classes = classes.ToArray()
      }
    };
    //add the groups to the renderer
    uvr.Groups = groups.ToArray();
    //Apply the renderer (for current observations)
    streamLayer.SetRenderer(uvr);
    streamLayer.SetVisibility(true);//turn on the layer
    
    Create Voxel Layer
    //Must be on the QueuedTask.Run()
    
    //Must be a .NetCDF file for voxels
    var url = @"C:\MyData\AirQuality_Redlands.nc";
    var cim_connection = new CIMVoxelDataConnection()
    {
        URI = url
    };
    //Create a VoxelLayerCreationParams
    var createParams = VoxelLayerCreationParams.Create(cim_connection);
    createParams.IsVisible = true;
    
    //Can also just use the path directly...
    //var createParams = VoxelLayerCreationParams.Create(url);
    
    //Use VoxelLayerCreationParams to enumerate the variables within
    //the voxel
    var variables = createParams.Variables;
    foreach (var variable in variables)
    {
        var line = $"{variable.Variable}: {variable.DataType}, " +
           $"{variable.Description}, {variable.IsDefault}, {variable.IsSelected}";
        System.Diagnostics.Debug.WriteLine(line);
    }
    //Optional: set the default variable
    createParams.SetDefaultVariable(variables.Last());
    
    //Create the layer - map must be a local scene
    VoxelLayer voxelLayer = LayerFactory.Instance.CreateLayer<VoxelLayer>(createParams, map);
    Requirements

    Target Platforms: Windows 11, Windows 10

    ArcGIS Pro version: 3 or higher.
    See Also