ArcGIS Pro 3.0 API Reference Guide
ArcGIS.Desktop.Mapping Namespace / LayerFactory Class / CreateLayer Method / CreateLayer<T>(LayerCreationParams,ILayerContainerEdit) Method
Expected Layer Type
Can be one of LayerCreationParams derived objects.
A map or group layer instance to which the Layer will be added.
Example

In This Topic
    CreateLayer<T>(LayerCreationParams,ILayerContainerEdit) Method
    In This Topic
    Creates a new Layer instance using the specified LayerCreationParams and adds that to a container such as a map or group layer. This method must be called on the MCT. Use QueuedTask.Run.
    Syntax
    Public Overloads Function CreateLayer(Of T As Layer)( _
       ByVal layerParams As LayerCreationParams, _
       ByVal container As ILayerContainerEdit _
    ) As T

    Parameters

    layerParams
    Can be one of LayerCreationParams derived objects.
    container
    A map or group layer instance to which the Layer will be added.

    Type Parameters

    T
    Expected Layer Type

    Return Value

    A specific Layer instance corresponding to type T.
    Exceptions
    ExceptionDescription
    This method or property must be called within the lambda passed to QueuedTask.Run.
    LayerCreationParams or container is null.
    Example
    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);
    Create a Scene Layer
    var sceneLayerUrl = @"https://myportal.com/server/rest/services/Hosted/SceneLayerServiceName/SceneServer";
    //portal items also ok as long as the portal is the current active portal...
    //var sceneLayerUrl = @"https://myportal.com/home/item.html?id=123456789abcdef1234567890abcdef0";
    
    await QueuedTask.Run(() =>
    {
      //Create with initial visibility set to false. Add to current scene
      var createparams = new LayerCreationParams(new Uri(sceneLayerUrl, UriKind.Absolute))
      {
        IsVisible = false
      };
    
      //cast to specific type of scene layer being created - in this case FeatureSceneLayer
      var sceneLayer = LayerFactory.Instance.CreateLayer<Layer>(
               createparams, MapView.Active.Map) as FeatureSceneLayer;
      //or...specify the cast directly
      var sceneLayer2 = LayerFactory.Instance.CreateLayer<FeatureSceneLayer>(
               createparams, MapView.Active.Map);
      //ditto for BuildingSceneLayer, PointCloudSceneLayer, IntegratedMeshSceneLayer
      //...
    });
          
    Requirements

    Target Platforms: Windows 11, Windows 10, Windows 8.1

    See Also