CreateLayer<T>(LayerCreationParams,ILayerContainerEdit) Method
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.
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 .
Create layer with create-params
{
var flyrCreatnParam = new FeatureLayerCreationParams(new Uri(@"c:\data\world.gdb\cities"))
{
Name = "World Cities",
IsVisible = false,
MinimumScale = 1000000,
MaximumScale = 5000,
DefinitionQuery = new DefinitionQuery(whereClause: "Population > 100000", name: "More than 100k"),
RendererDefinition = new SimpleRendererDefinition()
{
SymbolTemplate = SymbolFactory.Instance.ConstructPointSymbol(
CIMColor.CreateRGBColor(255, 0, 0), 8, SimpleMarkerStyle.Hexagon).MakeSymbolReference()
}
};
//Note: Needs QueuedTask to run
var featureLayerWithParams = LayerFactory.Instance.CreateLayer<FeatureLayer>(flyrCreatnParam, map);
}
Create FeatureLayer and add to Map using LayerCreationParams
{
//Get the LayerDocument from a lyrx file
var layerDoc = new LayerDocument(@"E:\Data\SDK\Default2DPointSymbols.lyrx");
//Get the CIMLayerDocument from the LayerDocument and use it to create LayerCreationParams
var createParams = new LayerCreationParams(layerDoc.GetCIMLayerDocument());
//Create a FeatureLayer using the LayerCreationParams
//Note: Needs QueuedTask to run
LayerFactory.Instance.CreateLayer<FeatureLayer>(createParams, MapView.Active.Map);
}
Create FeatureLayer and set to not display in Map.
{
//The catalog path of the feature layer to add to the map
var featureClassUriVisibility = new Uri(@"C:\Data\Admin\AdminData.gdb\USA\cities");
//Define the Feature Layer's parameters.
var layerParamsVisibility = new FeatureLayerCreationParams(featureClassUriVisibility)
{
//Set visibility
IsVisible = false,
};
//Create the layer with the feature layer parameters and add it to the active map
//Note: Needs QueuedTask to run
var createdFC = LayerFactory.Instance.CreateLayer<FeatureLayer>(layerParamsVisibility, MapView.Active.Map);
}
Create FeatureLayer with a Renderer
{
//Define a simple renderer to draw the Point US Cities feature class.
var simpleRender = new SimpleRendererDefinition
{
SymbolTemplate = SymbolFactory.Instance.ConstructPointSymbol(ColorFactory.Instance.RedRGB, 4.0, SimpleMarkerStyle.Circle).MakeSymbolReference()
};
//The catalog path of the feature layer to add to the map
var featureClassUri = new Uri(@"C:\Data\Admin\AdminData.gdb\USA\cities");
//Define the Feature Layer's parameters.
var layerParams = new FeatureLayerCreationParams(featureClassUri)
{
//Set visibility
IsVisible = true,
//Set Renderer
RendererDefinition = simpleRender,
};
//Create the layer with the feature layer parameters and add it to the active map
//Note: Needs QueuedTask to run
var createdFCWithRenderer = LayerFactory.Instance.CreateLayer<FeatureLayer>(layerParams, MapView.Active.Map);
}
Create FeatureLayer with a Query Definition
{
//The catalog path of the feature layer to add to the map
var featureClassUriDefinition = new Uri(@"C:\Data\Admin\AdminData.gdb\USA\cities");
//Define the Feature Layer's parameters.
var layerParamsQueryDefn = new FeatureLayerCreationParams(featureClassUriDefinition)
{
IsVisible = true,
DefinitionQuery = new DefinitionQuery(whereClause: "STATE_NAME = 'California'", name: "CACities")
};
//Note: Needs QueuedTask to run
//Create the layer with the feature layer parameters and add it to the active map
var createdFCWithQueryDefn = LayerFactory.Instance.CreateLayer<FeatureLayer>(layerParamsQueryDefn, MapView.Active.Map);
}
Create TopologyLayer with an Uri pointing to a Topology dataset
{
var path = @"D:\Data\CommunitySamplesData\Topology\GrandTeton.gdb\BackCountry\Backcountry_Topology";
var lcp = new TopologyLayerCreationParams(new Uri(path));
lcp.Name = "GrandTeton_Backcountry";
lcp.AddAssociatedLayers = true;
//Note: Needs QueuedTask to run
var topoLayer = LayerFactory.Instance.CreateLayer<ArcGIS.Desktop.Mapping.TopologyLayer>(lcp, MapView.Active.Map);
}
Create Topology Layer using Topology dataset
{
//Get the Topology of another Topology layer
var existingTopology = MapView.Active.Map.GetLayersAsFlattenedList().OfType<TopologyLayer>().FirstOrDefault();
if (existingTopology != null)
{
var topology = existingTopology.GetTopology();
//Configure the settings for a new Catalog layer using the CatalogDataset of an existing layer
var topologyLyrParams = new TopologyLayerCreationParams(topology);
topologyLyrParams.Name = "NewTopologyLayerFromAnotherTopologyLayer";
topologyLyrParams.AddAssociatedLayers = true;
//Note: Needs QueuedTask to run
LayerFactory.Instance.CreateLayer<TopologyLayer>(topologyLyrParams, MapView.Active.Map);
}
}
Create Catalog Layer using Uri to a Catalog Feature Class
{
var createParams = new CatalogLayerCreationParams(new Uri(@"C:\CatalogLayer\CatalogDS.gdb\HurricaneCatalogDS"));
//Set the definition query
createParams.DefinitionQuery = new DefinitionQuery("Query1", "cd_itemname = 'PuertoRico'");
//Set name of the new Catalog Layer
createParams.Name = "PuertoRico";
//Create Layer
//Note: Needs QueuedTask to run
var catalogLayer = LayerFactory.Instance.CreateLayer<CatalogLayer>(createParams, MapView.Active.Map);
}
Create Catalog Layer using CatalogDataset
{
//Get the CatalogDataset of another Catalog layer
var existingCatalogLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<CatalogLayer>().FirstOrDefault();
if (existingCatalogLayer != null)
{
var catalogDataset = existingCatalogLayer.GetCatalogDataset();
//Configure the settings for a new Catalog layer using the CatalogDataset of an existing layer
var catalogLyrParams = new CatalogLayerCreationParams(catalogDataset);
catalogLyrParams.Name = "NewCatalogLayerFromAnotherCatalogLayer";
catalogLyrParams.DefinitionQuery = new DefinitionQuery("Query1", "cd_itemname = 'Asia'");
//Note: Needs QueuedTask to run
LayerFactory.Instance.CreateLayer<CatalogLayer>(catalogLyrParams, MapView.Active.Map);
}
}
Add MapNotes to the active map
{
//Gets the collection of layer template packages installed with Pro for use with maps
var items = MapView.Active.Map.LayerTemplatePackages;
//Iterate through the collection of items to add each Map Note to the active map
foreach (var item in items)
{
//Create a parameter item for the map note
var layer_params = new LayerCreationParams(item);
layer_params.IsVisible = false;
//Note: Needs QueuedTask to run
//Create a feature layer for the map note
var layerCreated = LayerFactory.Instance.CreateLayer<Layer>(layer_params, MapView.Active.Map);
}
}
Create a new SubTypeGroupLayer
{
var subtypeGroupLayerCreateParam = new SubtypeGroupLayerCreationParams
(new Uri(@"c:\data\SubtypeAndDomain.gdb\Fittings"));
// Define Subtype layers
var rendererDefn1 = new UniqueValueRendererDefinition(new List<string> { "type" });
var renderDefn2 = new SimpleRendererDefinition()
{
SymbolTemplate = SymbolFactory.Instance.ConstructPointSymbol(
CIMColor.CreateRGBColor(255, 0, 0), 8, SimpleMarkerStyle.Hexagon).MakeSymbolReference()
};
subtypeGroupLayerCreateParam.SubtypeLayers = new List<SubtypeFeatureLayerCreationParams>()
{
//define first subtype layer with unique value renderer
new SubtypeFeatureLayerCreationParams(new UniqueValueRendererDefinition(new List<string> { "type" }), 1),
//define second subtype layer with simple symbol renderer
new SubtypeFeatureLayerCreationParams(renderDefn2, 2)
};
// Define additional parameters
subtypeGroupLayerCreateParam.DefinitionQuery = new DefinitionQuery(whereClause: "Enabled = 1", name: "IsActive");
subtypeGroupLayerCreateParam.IsVisible = true;
subtypeGroupLayerCreateParam.MinimumScale = 50000;
//Note: Needs QueuedTask to run
SubtypeGroupLayer subtypeGroupLayer2 = LayerFactory.Instance.CreateLayer<SubtypeGroupLayer>(
subtypeGroupLayerCreateParam, MapView.Active.Map);
}
Create layer from a lyrx file
{
//Note: Call within QueuedTask.Run()
var lyrDocFromLyrxFile = new LayerDocument(@"d:\data\cities.lyrx");
var cimLyrDoc = lyrDocFromLyrxFile.GetCIMLayerDocument();
//modifying its renderer symbol to red
var cimSimpleRenderer = ((CIMFeatureLayer)cimLyrDoc.LayerDefinitions[0]).Renderer as CIMSimpleRenderer;
cimSimpleRenderer?.Symbol.Symbol.SetColor(new CIMRGBColor() { R = 255 });
//optionally save the updates out as a file
lyrDocFromLyrxFile.Save(@"c:\data\cities_red.lyrx");
//get a json representation of the layer document and you want store away...
var aJSONString = lyrDocFromLyrxFile.AsJson();
//... and load it back when needed
lyrDocFromLyrxFile.Load(aJSONString);
cimLyrDoc = lyrDocFromLyrxFile.GetCIMLayerDocument();
//create a layer and add it to a map
var lcp = new LayerCreationParams(cimLyrDoc);
var lyr = LayerFactory.Instance.CreateLayer<FeatureLayer>(lcp, map);
}
Add a WMS service
{
// Create a connection to the WMS server
var serverConnection = new CIMInternetServerConnection { URL = "URL of the WMS service" };
var connection = new CIMWMSServiceConnection { ServerConnection = serverConnection };
// Add a new layer to the map
var layerParams = new LayerCreationParams(connection);
//Note: Needs QueuedTask to run
var layerCreated = LayerFactory.Instance.CreateLayer<FeatureLayer>(layerParams, MapView.Active.Map);
}
Create a query layer
{
Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri(@"C:\Connections\mySDE.sde")));
CIMSqlQueryDataConnection sqldc = new CIMSqlQueryDataConnection()
{
WorkspaceConnectionString = geodatabase.GetConnectionString(),
GeometryType = esriGeometryType.esriGeometryPolygon,
OIDFields = "OBJECTID",
Srid = "102008",
SqlQuery = "select * from MySDE.dbo.STATES",
Dataset = "States"
};
var lcp = new LayerCreationParams(sqldc)
{
Name = "States"
};
//Note: Needs QueuedTask to run
FeatureLayer flyr = LayerFactory.Instance.CreateLayer<FeatureLayer>(lcp, map);
}
EsriHttpClient: Get a Service Layer and Add it to Pro
{
UriBuilder searchURL = new UriBuilder(ArcGISPortalManager.Current.GetActivePortal().PortalUri)
{
Path = "sharing/rest/search"
};
string layers = "(type:\"Map Service\" OR type:\"Image Service\" OR type:\"Feature Service\" OR type:\"WMS\" OR type:\"KML\")";
//any public layer content
searchURL.Query = string.Format("q={0}&f=json", layers);
EsriHttpClient httpClient = new EsriHttpClient();
var searchResponse = httpClient.Get(searchURL.Uri.ToString());
dynamic resultItems = JObject.Parse(await searchResponse.Content.ReadAsStringAsync());
long numberOfTotalItems = resultItems.total.Value;
if (numberOfTotalItems == 0)
return;
List<dynamic> resultItemList = new List<dynamic>();
resultItemList.AddRange(resultItems.results);
//get the first result
dynamic item = resultItemList[0];
string itemID = item.id;
Item currentItem = ItemFactory.Instance.Create(itemID, ItemFactory.ItemType.PortalItem);
await QueuedTask.Run(() =>
{
//Create a LayerCreationParam
var layerParam = new LayerCreationParams(currentItem);
// if we have an item that can be turned into a layer
// add it to the map
if (LayerFactory.Instance.CanCreateLayerFrom(currentItem))
LayerFactory.Instance.CreateLayer<FeatureLayer>(layerParam, MapView.Active.Map);
});
}
Create GraphicsLayer
{
if (map.MapType != MapType.Map)
return;// not 2D
var gl_param = new GraphicsLayerCreationParams { Name = "Graphics Layer" };
// Note: must be called on the QueuedTask
{
//By default will be added to the top of the TOC
graphicsLayer = LayerFactory.Instance.CreateLayer<ArcGIS.Desktop.Mapping.GraphicsLayer>(gl_param, map);
//Add to the bottom of the TOC
gl_param.MapMemberIndex = -1; //bottom
LayerFactory.Instance.CreateLayer<ArcGIS.Desktop.Mapping.GraphicsLayer>(gl_param, map);
//Add a graphics layer to a group layer...
var group_layer = map.GetLayersAsFlattenedList().OfType<GroupLayer>().First();
LayerFactory.Instance.CreateLayer<ArcGIS.Desktop.Mapping.GraphicsLayer>(gl_param, group_layer);
//TODO...use the graphics layer
//
// or use the specific CreateGroupLayer method
LayerFactory.Instance.CreateGroupLayer(map, -1, "Graphics Layer");
}
}
Create a Scene Layer
{
// Note: call within 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
//...
}
}
Create a stream layer with a definition query
{
// Note: call within QueuedTask.Run()
{
//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,
DefinitionQuery = new DefinitionQuery(whereClause: "RWY = '29L'", name: "Runway")
};
streamLayer = LayerFactory.Instance.CreateLayer<StreamLayer>(lyrCreateParam, map);
}
}
Setting a unique value renderer for latest observations
{
// Note: call within QueuedTask.Run()
{
var url = @"https://geoeventsample1.esri.com:6443/arcgis/rest/services/AirportTraffics/StreamServer";
var uri = new Uri(url, UriKind.Absolute);
var createParams = new FeatureLayerCreationParams(uri)
{
IsVisible = false
};
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
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()
},
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 a New Elevation Surface
{
//Define a ServiceConnection to use for the new Elevation surface
var serverConnection = new CIMInternetServerConnection
{
Anonymous = true,
HideUserProperty = true,
URL = "https://elevation.arcgis.com/arcgis/services"
};
CIMAGSServiceConnection serviceConnection = new CIMAGSServiceConnection
{
ObjectName = "WorldElevation/Terrain",
ObjectType = "ImageServer",
URL = "https://elevation.arcgis.com/arcgis/services/WorldElevation/Terrain/ImageServer",
ServerConnection = serverConnection
};
//Defines a new elevation source set to the CIMAGSServiceConnection defined above
//Get the elevation surfaces defined in the map
var listOfElevationSurfaces = map.GetElevationSurfaceLayers();
//Add the new elevation surface
var elevationLyrCreationParams = new ElevationLayerCreationParams(serviceConnection);
//Note: needs to be called on the MCT
var elevationSurface = LayerFactory.Instance.CreateLayer<ElevationSurfaceLayer>(
elevationLyrCreationParams, map);
}
Add an elevation source to an existing elevation surface layer
{
ElevationSurfaceLayer surfaceLayer = null;
// surfaceLayer could also be the ground layer
string uri = "https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer";
var createParams = new ElevationLayerCreationParams(new Uri(uri));
createParams.Name = "Terrain 3D";
//Note: needs to be called on the MCT
var eleSourceLayer = LayerFactory.Instance.CreateLayer<Layer>(createParams, surfaceLayer);
}
Create a raster layer with a new colorizer definition
{
// Create a new stretch colorizer definition using default constructor.
StretchColorizerDefinition stretchColorizerDef = new StretchColorizerDefinition();
// Create a raster layer creation parameters object with the raster file path.
var rasterLayerCreationParams = new RasterLayerCreationParams(new Uri("rasterPath"))
{
ColorizerDefinition = stretchColorizerDef,
Name = "rasterLayerName",
MapMemberIndex = 0
};
// Create a raster layer using the colorizer definition created above.
// Note: You can create a raster layer from a url, project item, or data connection.
//Note: Run within a QueuedTask
RasterLayer rasterLayerfromURL =
LayerFactory.Instance.CreateLayer<RasterLayer>(rasterLayerCreationParams, map);
}
Create a mosaic layer with a new colorizer definition
// Create a new colorizer definition using default constructor.
{
StretchColorizerDefinition stretchColorizerDef = new StretchColorizerDefinition();
var rasterLayerCreationParams = new RasterLayerCreationParams(new Uri("url"))
{
Name = "layerRasterName",
ColorizerDefinition = stretchColorizerDef,
MapMemberIndex = 0
};
// Create a mosaic layer using the colorizer definition created above.
// Note: You can create a mosaic layer from a url, project item, or data connection.
//Note: Run within a QueuedTask
MosaicLayer newMosaicLayer = LayerFactory.Instance.CreateLayer<MosaicLayer>(rasterLayerCreationParams, map);
}
Create an image service layer with a new colorizer definition
{
// Create a new colorizer definition using default constructor.
StretchColorizerDefinition stretchColorizerDef = new StretchColorizerDefinition();
var rasterLayerCreationParams = new RasterLayerCreationParams(new Uri(url))
{
Name = "RasterLayer",
ColorizerDefinition = stretchColorizerDef,
MapMemberIndex = 0
};
// Create an image service layer using the colorizer definition created above.
//Note: Run within a QueuedTask
ImageServiceLayer imageServiceLayerWithColorizer =
LayerFactory.Instance.CreateLayer<ImageServiceLayer>(rasterLayerCreationParams, map);
}
Target Platforms: Windows 11 Home, Pro, Enterprise (64 bit)
ArcGIS Pro version: 3.0 or higher.