private static async void SetRasterColorByAttributeField(RasterLayer raster, string fieldName, StyleProjectItem styleProjItem)
 {
     await QueuedTask.Run(() =>
     {
         var ramps = styleProjItem.SearchColorRamps("Green Blues");
         CIMColorRamp colorRamp = ramps[0].ColorRamp;
         var colorizerDef       = new UniqueValueColorizerDefinition(fieldName, colorRamp);
         var colorizer          = raster.CreateColorizer(colorizerDef);
         // fix up colorizer ... due to a problem with getting the values for different attribute table fields:
         // 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 = raster.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.ToLower(), 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].ToLower();
                 var correctField   = landuseToFieldValue[oldValue];
                 grpClass.Values[0] = correctField;
                 grpClass.Label     = $@"{correctField}";
             }
         }
         raster.SetColorizer(colorizer);
     });
 }
        private static async void SetRasterColorByRGBAttributeFields(RasterLayer raster, List <string> fields)
        {
            await QueuedTask.Run(() =>
            {
                // set fieldName to the first column
                var fieldName     = "n/a";
                bool setFieldName = false;
                foreach (var attributeColumn in fields)
                {
                    if (attributeColumn.Equals("value", StringComparison.OrdinalIgnoreCase))
                    {
                        setFieldName = true;
                    }
                    else
                    {
                        if (setFieldName)
                        {
                            fieldName = attributeColumn;
                            break;
                        }
                    }
                }
                var colorizerDef = new UniqueValueColorizerDefinition(fieldName);
                var colorizer    = raster.CreateColorizer(colorizerDef);

                raster.SetColorizer(colorizer);
            });
        }
 protected override async void OnClick()
 {
     try
     {
         var raster = MapView.Active.Map.GetLayersAsFlattenedList().OfType <RasterLayer>().FirstOrDefault();
         if (raster == null)
         {
             return;
         }
         MessageBox.Show("In ArcGIS Pro 2.5 and older this method only works when using the 'value' field, unless you deploy the 'RecalculateColorizer' workaround.");
         await QueuedTask.Run(() =>
         {
             string fieldName = "Value";
             var style        = Project.Current.GetItems <StyleProjectItem>().First(s => s.Name == "ArcGIS Colors");
             var ramps        = style.SearchColorRamps("Green Blues");
             var colorizerDef = new UniqueValueColorizerDefinition(fieldName, ramps[0].ColorRamp);
             var colorizer    = raster.CreateColorizer(colorizerDef);
             raster.SetColorizer(RecalculateColorizer(raster, colorizer, fieldName));
         });
     }
     catch (Exception ex)
     {
         MessageBox.Show($@"Error trying to set colorizer: {ex.ToString()}");
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Applies the Unique Value colorizer to the basic raster layer.
        /// </summary>
        /// <param name="basicRasterLayer">The basic raster layer is either a raster or image service layer, or the image sub-layer of the mosaic layer.</param>
        /// <returns></returns>
        public static async Task SetToUniqueValueColorizer(BasicRasterLayer basicRasterLayer)
        {
            // Creates a new UV Colorizer Definition using the default constructor.
            UniqueValueColorizerDefinition UVColorizerDef = new UniqueValueColorizerDefinition();

            // Creates a new UV colorizer using the colorizer definition created above.
            CIMRasterUniqueValueColorizer newColorizer = await basicRasterLayer.CreateColorizerAsync(UVColorizerDef) as CIMRasterUniqueValueColorizer;

            // Sets the newly created colorizer on the layer.
            await QueuedTask.Run(() =>
            {
                basicRasterLayer.SetColorizer(newColorizer);
            });
        }
Ejemplo n.º 5
0
        public static async Task SetToUniqueValueColorizer(string layerName, string styleCategory,
                                                           string styleName, string fieldName)
        {
            // Get the layer we want to symbolize from the map
            Layer oLayer =
                MapView.Active.Map.Layers.FirstOrDefault <Layer>(m => m.Name.Equals(layerName, StringComparison.CurrentCultureIgnoreCase));

            if (oLayer == null)
            {
                return;
            }
            RasterLayer rasterLayer = (RasterLayer)oLayer;

            StyleProjectItem style =
                Project.Current.GetItems <StyleProjectItem>().FirstOrDefault(s => s.Name == styleCategory);

            if (style == null)
            {
                return;
            }
            var colorRampList = await QueuedTask.Run(() => style.SearchColorRamps(styleName));

            if (colorRampList == null || colorRampList.Count == 0)
            {
                return;
            }
            CIMColorRamp cimColorRamp = colorRampList[0].ColorRamp;

            // Creates a new UV Colorizer Definition using the default constructor.
            UniqueValueColorizerDefinition UVColorizerDef = new UniqueValueColorizerDefinition(fieldName, cimColorRamp);

            // Creates a new UV colorizer using the colorizer definition created above.
            CIMRasterUniqueValueColorizer newColorizer = await rasterLayer.CreateColorizerAsync(UVColorizerDef) as CIMRasterUniqueValueColorizer;

            // Sets the newly created colorizer on the layer.
            await QueuedTask.Run(() =>
            {
                rasterLayer.SetColorizer(MapTools.RecalculateColorizer(newColorizer));
            });
        }