AnsweredAssumed Answered

Problem with applying colorizer to raster layer

Question asked by BRufenacht on Apr 2, 2020
Latest reply on Apr 3, 2020 by gkmieliauskas_cellexp

I am trying to apply symbology to a group of raster layers, and one feature layer with an add-in. Everything appears to work fine with all layers loaded into the map and showing the correct symbology, however, when I click on any raster symbology in the contents pane, an Update Colorizer Failed! warning shows up in the symbology pane, and the symbology reverts back to default. My current workaround is the save the map, close the project, and reopen before I click on anything in the contents. This seems to correct the update colorizer issue.

Is there anything I can do so I don't have to close/reopen to get this to work?  Code shown below:

 public async void loadLayers(string path, string plan)
        {
            string projectPath = path;
            string modelPath = System.IO.Path.GetDirectoryName(projectPath);
            string planName = plan;
            string depthPath = System.IO.Path.Combine(modelPath, plan,"Depth (Max).hydroDEM.tif");
            string durationPath = System.IO.Path.Combine(modelPath, plan, "Duration (0.5ft hrs).hydroDEM.tif");
            string elevationPath = System.IO.Path.Combine(modelPath, "Terrain", "Terrain.hydroDEM.tif");
            string velocityPath = System.IO.Path.Combine(modelPath, plan, "Velocity (Max).hydroDEM.tif");
            string wsePath = System.IO.Path.Combine(modelPath, plan, "WSE (Max).hydroDEM.tif");
            string meshPath = System.IO.Path.Combine(modelPath, "2DMesh.shp");
            string depthLayer = System.IO.Path.Combine(AddinAssemblyLocation(), "Content", "depth.lyrx");
            string durationLayer = System.IO.Path.Combine(AddinAssemblyLocation(), "Content", "duration.lyrx");
            string elevationLayer = System.IO.Path.Combine(AddinAssemblyLocation(), "Content", "elevation.lyrx");
            string velocityLayer = System.IO.Path.Combine(AddinAssemblyLocation(), "Content", "velocity.lyrx");
            string wseLayer = System.IO.Path.Combine(AddinAssemblyLocation(), "Content", "WSE.lyrx");
            string meshLayer = System.IO.Path.Combine(AddinAssemblyLocation(), "Content", "mesh.lyrx");
            RasterLayer velocityMap = null;
            RasterLayer depthMap = null;
            RasterLayer durationMap = null;
            RasterLayer elevationMap = null;
            RasterLayer wseMap = null;
            FeatureLayer meshMap = null;
            CancelableProgressorSource ps = new CancelableProgressorSource("Starting Process...", "Cancel", true);
            if(MapView.Active != null)
            {
                if(System.IO.File.Exists(elevationPath))
                {
                    ps.Progressor.Message = "Loading terrain grid";
                    elevationMap = await QueuedTask.Run(() =>
                    {
                        return (RasterLayer)LayerFactory.Instance.CreateLayer(new Uri(elevationPath), MapView.Active.Map, 0, "Terrain");
                    });
                }
                if (System.IO.File.Exists(wsePath))
                {
                    ps.Progressor.Message = "Loading WSE grid";
                    wseMap = await QueuedTask.Run(() =>
                    {
                        return (RasterLayer)LayerFactory.Instance.CreateLayer(new Uri(wsePath), MapView.Active.Map, 0, "Maximum WSE");
                    });
                }
                if (System.IO.File.Exists(depthPath))
                {
                    ps.Progressor.Message = "Loading depth grid";
                    depthMap = await QueuedTask.Run(() =>
                    {
                        return (RasterLayer)LayerFactory.Instance.CreateLayer(new Uri(depthPath), MapView.Active.Map, 0, "Maximum Depth");
                    });
                }
                if (System.IO.File.Exists(durationPath))
                {
                    ps.Progressor.Message = "Loading duration grid";
                    durationMap = await QueuedTask.Run(() =>
                    {
                        return (RasterLayer)LayerFactory.Instance.CreateLayer(new Uri(durationPath), MapView.Active.Map, 0, "Duration over 0.5ft depth");
                    });
                }
                if (System.IO.File.Exists(velocityPath))
                {
                    ps.Progressor.Message = "Loading velocity grid";
                    velocityMap = await QueuedTask.Run(() =>
                    {
                        return (RasterLayer)LayerFactory.Instance.CreateLayer(new Uri(velocityPath), MapView.Active.Map, 0, "Maximum Velocity");
                    });
                }
                if (System.IO.File.Exists(meshPath))
                {
                    ps.Progressor.Message = "Loading 2D mesh polygon";
                    meshMap = await QueuedTask.Run(() =>
                    {
                        return (FeatureLayer)LayerFactory.Instance.CreateLayer(new Uri(meshPath), MapView.Active.Map);
                    });
                }
            }
            await QueuedTask.Run(() =>
            {
                ps.Progressor.Message = "Applying symbology...";
                if (MapView.Active != null)
                {
                    if (elevationMap != null)
                    {
                        var elevationDoc = new LayerDocument(elevationLayer);
                        var elevationParams = elevationDoc.GetCIMLayerDocument();
                        var elevationColorizer = ((CIMRasterLayer)elevationParams.LayerDefinitions[0]).Colorizer as CIMRasterStretchColorizer;
                        elevationMap.SetColorizer(elevationColorizer);
                    }
                        if (wseMap != null)
                    {
                        var wseDoc = new LayerDocument(wseLayer);
                        var wseParams = wseDoc.GetCIMLayerDocument();
                        var wseColorizer = ((CIMRasterLayer)wseParams.LayerDefinitions[0]).Colorizer as CIMRasterDiscreteColorColorizer;
                        wseMap.SetColorizer(wseColorizer);
                    }
                    if (depthMap != null)
                    {
                        var depthDoc = new LayerDocument(depthLayer);
                        var depthParams = depthDoc.GetCIMLayerDocument();
                        var depthColorizer = ((CIMRasterLayer)depthParams.LayerDefinitions[0]).Colorizer as CIMRasterClassifyColorizer;
                        depthMap.SetColorizer(depthColorizer);
                    }
                    if (durationMap != null)
                    {
                        var durationDoc = new LayerDocument(durationLayer);
                        var durationParams = durationDoc.GetCIMLayerDocument();
                        var durationColorizer = ((CIMRasterLayer)durationParams.LayerDefinitions[0]).Colorizer as CIMRasterClassifyColorizer;
                        durationMap.SetColorizer(durationColorizer);
                    }
                    if (velocityMap != null)
                    {
                        var velocityDoc = new LayerDocument(velocityLayer);
                        var velocityParams = velocityDoc.GetCIMLayerDocument();
                        var velocityColorizer = ((CIMRasterLayer)velocityParams.LayerDefinitions[0]).Colorizer as CIMRasterClassifyColorizer;
                        velocityMap.SetColorizer(velocityColorizer);
                    }
                    if (meshMap != null)
                    {
                        LayerDocument meshDocument = new LayerDocument(meshLayer);
                        var meshParameters = meshDocument.GetCIMLayerDocument();
                        var layerRenderer = ((CIMFeatureLayer)meshParameters.LayerDefinitions[0]).Renderer as CIMRenderer;
                        meshMap.SetRenderer(layerRenderer);
                    }
                }
            }, ps.Progressor);
        };

Outcomes