private static async Task ModifyLayerSymbologyFromLyrFileAsync(FeatureLayer featureLayer, string layerFile) { await QueuedTask.Run(() => { //Does layer file exist if (!System.IO.File.Exists(layerFile)) { return; } //Get the Layer Document from the lyrx file var lyrDocFromLyrxFile = new LayerDocument(layerFile); var cimLyrDoc = lyrDocFromLyrxFile.GetCIMLayerDocument(); //Get the renderer from the layer file var rendererFromLayerFile = ((CIMFeatureLayer)cimLyrDoc.LayerDefinitions[0]).Renderer as CIMSimpleRenderer; if (rendererFromLayerFile == null) { return; } //Apply the renderer to the feature layer featureLayer?.SetRenderer(rendererFromLayerFile); }); }
protected async override void OnClick() { //Get the Hydrology layer from the TOC var hydrologyLyr = MapView.Active.Map.GetLayersAsFlattenedList().OfType <FeatureLayer>().FirstOrDefault(f => f.Name == "Hydrology"); if (hydrologyLyr == null) { MessageBox.Show("This renderer works with the Hydrology feature layer available with the ArcGIS Pro SDK Sample data", "Data missing"); return; } await QueuedTask.Run(() => { //Get the Layer Document from the hydrology layer var lyrDocFromFeatureLayer = new LayerDocument(hydrologyLyr); //Get the CIM Document var cimLyrDefn = lyrDocFromFeatureLayer.GetCIMLayerDocument(); var layerDefns = cimLyrDefn.LayerDefinitions; var oneLyrDef = cimLyrDefn.LayerDefinitions[0] as CIMFeatureLayer; //Create a list of Display Filters var arrayDisplayFilters = new List <CIMDisplayFilter>() { new CIMDisplayFilter { Name = "StreamOrder > 6", WhereClause = "StreamOrder > 6", MinScale = 0, MaxScale = 50000000 }, new CIMDisplayFilter { Name = "StreamOrder > 5", WhereClause = "StreamOrder > 5", MinScale = 50000000, MaxScale = 20000000 }, new CIMDisplayFilter { Name = "StreamOrder > 4", WhereClause = "StreamOrder > 4", MinScale = 20000000, MaxScale = 5000000 }, new CIMDisplayFilter { Name = "StreamOrder > 3", WhereClause = "StreamOrder > 3", MinScale = 5000000, MaxScale = 1000000 }, new CIMDisplayFilter { Name = "StreamOrder > 2", WhereClause = "StreamOrder > 2", MinScale = 1000000, MaxScale = 100000 }, new CIMDisplayFilter { Name = "StreamOrder > 1", WhereClause = "StreamOrder > 1", MinScale = 100000, MaxScale = 24000 }, }; //Add the Display Filters to the CIM Layer Doc ((CIMFeatureLayer)cimLyrDefn.LayerDefinitions[0]).DisplayFilters = arrayDisplayFilters.ToArray(); //Enable the Display filters. ((CIMFeatureLayer)cimLyrDefn.LayerDefinitions[0]).EnableDisplayFilters = true; //Add a new layer to the map using the configured LayerDocument in memory. var featureLayerParams = new LayerCreationParams(cimLyrDefn); featureLayerParams.MapMemberPosition = MapMemberPosition.AddToTop; var configuredLyr = LayerFactory.Instance.CreateLayer <FeatureLayer>(featureLayerParams, MapView.Active.Map); #region Turn off the original layer visibility hydrologyLyr.SetVisibility(false); hydrologyLyr.SetExpanded(false); hydrologyLyr?.SetLabelVisibility(false); #endregion }); }
public static T GetRenderer <T>([NotNull] LayerDocument template) where T : CIMRenderer { CIMLayerDocument layerDocument = template.GetCIMLayerDocument(); // todo daro: implement more robust CIMDefinition definition = layerDocument.LayerDefinitions[0]; return(((CIMFeatureLayer)definition)?.Renderer as T); }
private static void RepairWithLayerFile(Map map, Layer layer, string newLayerFile, bool keepBrokenLayer) { // Create Layer from *.lyrx file: https://github.com/esri/arcgis-pro-sdk/wiki/ProSnippets-MapAuthoring#create-layer-from-a-lyrx-file // Pro Addin SDK can only open *.lyrx, not *.lyr: // Assume Data Manager manually created a shadow *.lyrx file for the *.lyr file in the moves database // TODO: If *.lyrx file does not exist, create *.lyrx from *.lyr file; must be done with an external python process if (newLayerFile.EndsWith(".lyr", StringComparison.OrdinalIgnoreCase)) { newLayerFile += "x"; } Layer newLayer = null; try { var layerDocument = new LayerDocument(newLayerFile); var layerParameters = new LayerCreationParams(layerDocument.GetCIMLayerDocument()); newLayer = LayerFactory.Instance.CreateLayer <Layer>(layerParameters, map, LayerPosition.AutoArrange); } catch (Exception) {} if (newLayer != null) { if (!keepBrokenLayer) { map.RemoveLayer(layer); } } else { // Notify the user that the LayerFile could not be opened (missing, corrupt, ...) var title = @"Map Fixer Error"; var msg = $"The layer file '{newLayerFile}' could not be opened."; MessageBox.Show(msg, title, System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); } }