Problem with applying colorizer to raster layer

752
3
04-02-2020 11:49 AM
BenRufenacht
New Contributor III

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);
        };‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
3 Replies
GintautasKmieliauskas
Regular Contributor

Hi Ben,

Have you tried eliminate one by one layer to know is it one layer problem or all layers?

To check if raster exists better to use :

var currentItem = ItemFactory.Instance.Create(sLyrPath, ItemFactory.ItemType.PathItem);

if (LayerFactory.Instance.CanCreateLayerFrom(currentItem)) {

}

It will  work with all types of rasters and feature classes

0 Kudos
BenRufenacht
New Contributor III

It seems that no matter how many and which rasters I add, the last one that was added is fine, while the others show the warning and revert back to default symbology.

0 Kudos
GintautasKmieliauskas
Regular Contributor

Hi Ben,

I had many problems with loading raster layers with lyr files. So now I use such way:

1. Create layer from lyr file.

2. Update data connection to raster path.

                await QueuedTask.Run(async() =>
                {
                        Item currentItem = ItemFactory.Instance.Create(rasterPath, ItemFactory.ItemType.PathItem);
                        if (!LayerFactory.Instance.CanCreateLayerFrom(currentItem))
                        {
                            return;
                        }
                        int nPos = await FindPlaceInTOCBelowFeaturesAboveRasters(myMap);
                        rasterLayer = (RasterLayer)LayerFactory.Instance.CreateLayer(new Uri(lyrFile), myMap,                                     nPos);
                        if (rasterLayer != null)
                        {
                            await ChangeRasterLayerDataConnectionAsync(rasterLayer, rasterPath);
                        }
                    }

        public static Task ChangeRasterLayerDataConnectionAsync(RasterLayer rasterLayer, string catalogPath)
        {
            return QueuedTask.Run(() => {
                CIMDataConnection currentDataConnection = rasterLayer.GetDataConnection();
                string connection = System.IO.Path.GetDirectoryName(catalogPath);
                string dataset = System.IO.Path.GetFileName(catalogPath);
                // provide a replace data connection method
                CIMStandardDataConnection updatedDataConnection = new CIMStandardDataConnection()
                {
                    WorkspaceConnectionString = string.Format("DATABASE={0}", connection),
                    WorkspaceFactory = WorkspaceFactory.Raster,
                    Dataset = dataset,
                    DatasetType = esriDatasetType.esriDTRasterDataset
                };
                rasterLayer.SetDataConnection(updatedDataConnection);
                //Bug #2, we manually invalidate the cache
                rasterLayer.ClearDisplayCache();
            });
        }
0 Kudos