/// <summary>
        /// Set the stretch type on the first selected image service layer in the first open 2D map.
        /// </summary>
        /// <param name="stretschType">The stretch type to set on the layer.</param>
        /// <param name="statsType">The stretch statistics type to set on the layer. This lets you pick between Dataset, Area of View (to enable DRA) or Custom statistics.</param>
        /// <returns></returns>
        public static async Task SetStretchTypeAsync(RasterStretchType stretschType, RasterStretchStatsType statsType = RasterStretchStatsType.Dataset)
        {
            try
            {
                // Get the first 2D map from the project that is called Map.
                Map _map = await GetMapFromProject(Project.Current, "Map");

                if (_map == null)
                {
                    ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("SetStretchType: Failed to get map.");
                    return;
                }

                // Get the most recently selected layer.
                Layer firstSelectedLayer = MapView.Active.GetSelectedLayers().First();
                // Check if the first selected layer is an image service layer.
                if (firstSelectedLayer is ImageServiceLayer)
                {
                    // Set the colorizer on the most recently selected layer.
                    // The colorizer has to be set on the Main CIM Thread (MCT).
                    await QueuedTask.Run(() =>
                    {
                        // Get the colorizer from the selected layer.
                        CIMRasterColorizer newColorizer = ((BasicRasterLayer)firstSelectedLayer).GetColorizer();
                        // Set the stretch type and stretch statistics type on the colorizer.
                        // Theese parameters only apply to the Stretch and RGB colorizers.
                        if (newColorizer is CIMRasterRGBColorizer)
                        {
                            ((CIMRasterRGBColorizer)newColorizer).StretchType      = stretschType;
                            ((CIMRasterRGBColorizer)newColorizer).StretchStatsType = statsType;
                        }
                        else if (newColorizer is CIMRasterStretchColorizer)
                        {
                            ((CIMRasterStretchColorizer)newColorizer).StretchType = stretschType;
                            ((CIMRasterStretchColorizer)newColorizer).StatsType   = statsType;
                        }
                        else
                        {
                            MessageBox.Show("Selected layer must be visualized using the RGB or Stretch colorizer");
                        }
                        // Update the image service with the new colorizer
                        ((BasicRasterLayer)firstSelectedLayer).SetColorizer(newColorizer);
                    });
                }
            }
            catch (Exception exc)
            {
                // Catch any exception found and display a message box.
                ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Exception caught while trying to set stretch type: " + exc.Message);
                return;
            }
        }
Exemplo n.º 2
0
        // This method addresses the issue that the CreateColorizer does not systematically assign colors
        // from the associated color ramp.
        // https://community.esri.com/message/867870-re-creating-unique-value-colorizer-for-raster
        public static CIMRasterColorizer RecalculateColorizer(CIMRasterColorizer colorizer)
        {
            if (colorizer is CIMRasterUniqueValueColorizer uvrColorizer)
            {
                var colorRamp = uvrColorizer.ColorRamp;
                if (colorRamp == null)
                {
                    throw new InvalidOperationException("Colorizer must have a color ramp");
                }

                //get the total number of colors to be assigned
                var total_colors = uvrColorizer.Groups.Select(g => g.Classes.Count()).Sum();
                var colors       = ColorFactory.Instance.GenerateColorsFromColorRamp(colorRamp, total_colors);
                var c            = 0;
                foreach (var uvr_group in uvrColorizer.Groups)
                {
                    foreach (var uvr_class in uvr_group.Classes)
                    {
                        //assign the generated colors to each class in turn
                        uvr_class.Color = colors[c++];
                    }
                }
            }
            else if (colorizer is CIMRasterClassifyColorizer classColorizer)
            {
                var colorRamp = classColorizer.ColorRamp;
                if (colorRamp == null)
                {
                    throw new InvalidOperationException("Colorizer must have a color ramp");
                }

                var total_colors = classColorizer.ClassBreaks.Count();
                var colors       = ColorFactory.Instance.GenerateColorsFromColorRamp(colorRamp, total_colors);
                var c            = 0;
                foreach (var cbreak in classColorizer.ClassBreaks)
                {
                    //assign the generated colors to each class break in turn
                    cbreak.Color = colors[c++];
                }
            }
            return(colorizer);
        }
        /// <summary>
        /// Set the resampling type on the first selected image service layer in the first open 2D map.
        /// </summary>
        /// <param name="resamplingType">The resampling type to set on the layer.</param>
        /// <returns></returns>
        public static async Task SetResamplingTypeAsync(RasterResamplingType resamplingType)
        {
            try
            {
                // Get the first 2D map from the project that is called Map.
                Map _map = await GetMapFromProject(Project.Current, "Map");

                if (_map == null)
                {
                    ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("SetResamplingType: Failed to get map.");
                    return;
                }

                // Get the most recently selected layer.
                Layer firstSelectedLayer = MapView.Active.GetSelectedLayers().First();
                // Check if the first selected layer is an image service layer.
                if (firstSelectedLayer is ImageServiceLayer)
                {
                    // Set the colorizer on the most recently selected layer.
                    // The colorizer has to be get/set on the Main CIM Thread (MCT).
                    await QueuedTask.Run(() =>
                    {
                        // Get the colorizer from the selected layer.
                        CIMRasterColorizer newColorizer = ((BasicRasterLayer)firstSelectedLayer).GetColorizer();
                        // Set the resampling type on the colorizer.
                        newColorizer.ResamplingType = resamplingType;
                        // Update the image service with the new colorizer
                        ((BasicRasterLayer)firstSelectedLayer).SetColorizer(newColorizer);
                    });
                }
            }
            catch (Exception exc)
            {
                // Catch any exception found and display a message box.
                ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Exception caught while trying to set resampling type: " + exc.Message);
                return;
            }
        }
        private static CIMRasterColorizer RecalculateColorizer(RasterLayer rasterLayer, CIMRasterColorizer colorizer, string fieldName)
        {
            // fix up colorizer ... turns out the values are wrong ... the landuse value is always inserted
            // we use the Raster's attribute table to collect a dictionary with the correct replacement values
            Dictionary <string, string> landuseToFieldValue = new Dictionary <string, string>();

            if (colorizer is CIMRasterUniqueValueColorizer uvrColorizer)
            {
                var rasterTbl = rasterLayer.GetRaster().GetAttributeTable();
                var cursor    = rasterTbl.Search();
                while (cursor.MoveNext())
                {
                    var row          = cursor.Current;
                    var correctField = row[fieldName].ToString();
                    var key          = row[uvrColorizer.Groups[0].Heading].ToString();
                    landuseToFieldValue.Add(key, correctField);
                }
                uvrColorizer.Groups[0].Heading = fieldName;
                for (var idxGrp = 0; idxGrp < uvrColorizer.Groups[0].Classes.Length; idxGrp++)
                {
                    var grpClass     = uvrColorizer.Groups[0].Classes[idxGrp];
                    var oldValue     = grpClass.Values[0];
                    var correctValue = landuseToFieldValue[oldValue];
                    grpClass.Values[0] = correctValue;
                    grpClass.Label     = $@"{correctValue}";
                }
            }
            return(colorizer);
        }