Hello,
I have created an ArcGIS Pro button using ArcGIS Pro SDK to add a feature class to a map. My code is below. How would I add to this button to now symbolize that polygon feature class to have a black outline and yellow fill?
Here is what I have to add a feature class:
protected override void OnClick()
{
if (MapView.Active?.Map == null) return;
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
using (Geodatabase geodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(@"L:\data\ESRI_10_6\usa\census\states.gdb"))))
{
using (FeatureClass addFeatureClass = geodatabase.OpenDataset<FeatureClass>("states"))
{
LayerFactory.Instance.CreateFeatureLayer(addFeatureClass, MapView.Active.Map, 0, "states");
}
}
});
How do I now add to this button code to now symbolize as a black outline and yellow fill? How do I reference the above feature class that I just added to the map? I can't seem to figure out how to do this. Your help would be much appreciated.
Thank you!
Hi Joshua
Here is the code for a Simple renderer that could help: Simple Renderer for a Polygon feature layer.
internal static Task SimpleRendererPolygon(FeatureLayer featureLayer)
{
return QueuedTask.Run(() =>
{
//Creating a polygon with a red fill and blue outline.
CIMStroke outline = SymbolFactory.Instance.ConstructStroke(
ColorFactory.Instance.BlueRGB, 2.0, SimpleLineStyle.Solid);
CIMPolygonSymbol fillWithOutline = SymbolFactory.Instance.ConstructPolygonSymbol(
ColorFactory.Instance.CreateRGBColor(255, 190, 190), SimpleFillStyle.Solid, outline);
//Get the layer's current renderer
CIMSimpleRenderer renderer = featureLayer.GetRenderer() as CIMSimpleRenderer;
//Update the symbol of the current simple renderer
renderer.Symbol = fillWithOutline.MakeSymbolReference();
//Update the feature layer renderer
featureLayer.SetRenderer(renderer);
});
}
Using this code snippet above, here is how the polygon feature will get renderered -
Thank you, how do I use that for my feature class that I am using? Do I need to add more code to do so? Or do I just add it below my add feature class code?
Your help is much appreciated.
For example, I am getting an error "feature layer does not exist in the current context". How do I tell it that I want to apply this symbology to a certain feature class, such as the one that I just added?
Hi
You can get your feature layer like this:
//Gets the first layer in the active map
var lyr = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().First();
//Or
//Gets layer with a specific name
var lyr = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(l => l.Name == "U.S. States (Generalized)").First(); ;
To give you an idea how you could use these code snippets, given below is the entire workflow. If you have a button, you can do the following in your button's OnClick method:
protected override async void OnClick()
{
//Gets your feature layer
var lyr = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().First();
await SimpleRendererPolygon(lyr); //calls the method below to apply the simple renderer
}
internal static Task SimpleRendererPolygon(FeatureLayer featureLayer)
{
return QueuedTask.Run(() =>
{
//Creating a polygon with a red fill and blue outline.
CIMStroke outline = SymbolFactory.Instance.ConstructStroke(
ColorFactory.Instance.BlueRGB, 2.0, SimpleLineStyle.Solid);
CIMPolygonSymbol fillWithOutline = SymbolFactory.Instance.ConstructPolygonSymbol(
ColorFactory.Instance.CreateRGBColor(255, 190, 190), SimpleFillStyle.Solid, outline);
//Get the layer's current renderer
CIMSimpleRenderer renderer = featureLayer.GetRenderer() as CIMSimpleRenderer;
//Update the symbol of the current simple renderer
renderer.Symbol = fillWithOutline.MakeSymbolReference();
//Update the feature layer renderer
featureLayer.SetRenderer(renderer);
});
}
This is exactly what I was looking to do. Thank you so much for the detailed response!