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);
            });
        }
 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);
     });
 }
Ejemplo n.º 3
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));
            });
        }