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);
     });
 }
        public async Task ApplyColorRampAsync(FeatureLayer featureLayer, string[] fields)
        {
            StyleProjectItem style =
                Project.Current.GetItems <StyleProjectItem>().FirstOrDefault(s => s.Name == "ColorBrewer Schemes (RGB)");

            if (style == null)
            {
                return;
            }
            var colorRampList = await QueuedTask.Run(() => style.SearchColorRamps("Red-Gray (10 Classes)"));

            if (colorRampList == null || colorRampList.Count == 0)
            {
                return;
            }
            CIMColorRamp cimColorRamp = null;
            CIMRenderer  renderer     = null;
            await QueuedTask.Run(() =>
            {
                cimColorRamp    = colorRampList[0].ColorRamp;
                var rendererDef = new UniqueValueRendererDefinition(fields, null, cimColorRamp);
                renderer        = featureLayer?.CreateRenderer(rendererDef);
                featureLayer?.SetRenderer(renderer);
            });
        }
 internal static CIMColorRamp GetColorRamp()
 {
     StyleProjectItem style =
       Project.Current.GetItems<StyleProjectItem>().FirstOrDefault(s => s.Name == "ArcGIS Colors");
     if (style == null) return null;
     var colorRampList = style.SearchColorRamps("Heat Map 4 - Semitransparent");
     if (colorRampList == null || colorRampList.Count == 0) return null;
     return colorRampList[0].ColorRamp;
 }
        public async Task <IList <ColorRampStyleItem> > GetColorRampsFromStyleAsync(StyleProjectItem style, string searchString)
        {
            //StyleProjectItem can be "ColorBrewer Schemes (RGB)", "ArcGIS 2D"...
            if (style == null)
            {
                throw new System.ArgumentNullException();
            }

            //Search for color ramps
            //Color Ramp searchString can be "Spectral (7 Classes)", "Pastel 1 (3 Classes)", "Red-Gray (10 Classes)"..
            return(await QueuedTask.Run(() => style.SearchColorRamps(searchString)));
        }
Esempio 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));
            });
        }
Esempio n. 6
0
        /// <summary>
        /// Gets the color ramp from the color ramp style.
        /// </summary>
        /// <param name="project">The current project.</param>
        /// <param name="styleName">A string represents the color ramp style name. For example, "ArcGIS Colors".</param>
        /// <param name="rampName">A string represents the color ramp name. For example, "Aspect".</param>
        /// <returns></returns>
        private static IList <ColorRampStyleItem> GetColorRampsFromStyleAsync(Project project, string styleName, string rampName)
        {
            try
            {
                // Gets a collection of style items in project that matches the given style name.
                StyleProjectItem styleItem =
                    project.GetItems <StyleProjectItem>().FirstOrDefault(style => (style.Name.Equals(styleName, StringComparison.CurrentCultureIgnoreCase)));

                if (styleItem == null)
                {
                    throw new System.ArgumentNullException();
                }

                //Search for color ramp by name in the collection of style items.
                return(styleItem.SearchColorRamps(rampName));
            }
            catch (Exception ex)
            {
                ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(null, $@"Error in GetColorRampsFromStyleAsync: {ex.Message}", MessageBoxButton.OK, MessageBoxImage.Error);

                return(null);
            }
        }
        private void ExecuteRenderingClick()
        {
            // コンボ ボックスでレイヤー、フィールド、レンダリング手法が選択されているかをチェックする
            if (_selectedRenderingLayer is null)
            {
                MessageBox.Show("レイヤーを選択してください。");
            }
            else if (_selectedField is null)
            {
                MessageBox.Show("フィールドを選択してください。");
            }
            else if (_selectedRenderingMethod is null)
            {
                MessageBox.Show("レンダリング手法を選択してください。");
            }
            else
            {
                try
                {
                    // レンダラー作成の処理を実装
                    QueuedTask.Run(() =>
                    {
                        // レイヤー名で検索してマップ上のレイヤーを取得する
                        var lyr = MapView.Active.Map.FindLayers(_selectedRenderingLayer.Name).FirstOrDefault() as FeatureLayer;

                        // 「ArcGIS カラー」(ArcGIS Pro の表示言語が英語の場合は、「ArcGIS Colors」を指定)プロジェクト スタイル アイテムを取得
                        StyleProjectItem style = Project.Current.GetItems <StyleProjectItem>().FirstOrDefault(s => s.Name == "ArcGIS カラー");

                        // 名前で検索してカラーランプ アイテムを取得する(ArcGIS Pro の表示言語が英語の場合は、「Spectrum - Full Light」を指定)
                        IList <ColorRampStyleItem> colorRampList = style.SearchColorRamps("フル スペクトル (明るい)");

                        if (_selectedRenderingMethod == "個別値レンダリング")
                        {
                            // 個別値レンダラーの定義を作成する
                            UniqueValueRendererDefinition uvrDef = new
                                                                   UniqueValueRendererDefinition()
                            {
                                ValueFields = new String[] { _selectedField },   // 分類に使用するフィールド
                                ColorRamp   = colorRampList[0].ColorRamp,        // カラーランプ
                            };

                            // 個別値レンダラーを作成する
                            CIMUniqueValueRenderer cimRenderer = (CIMUniqueValueRenderer)lyr.CreateRenderer(uvrDef);
                            // レンダラーをレイヤーに設定する
                            lyr.SetRenderer(cimRenderer);
                        }
                        else // 等級色レンダリングの場合
                        {
                            if (GetNumericField(lyr, _selectedField)) // 数値型のフィールドを選択している場合のみ実行する
                            {
                                // 等級色レンダラーの定義を作成する
                                GraduatedColorsRendererDefinition gcDef = new GraduatedColorsRendererDefinition()
                                {
                                    ClassificationField  = _selectedField,                     // 分類に使用するフィールド
                                    ColorRamp            = colorRampList[0].ColorRamp,         // カラーランプ
                                    ClassificationMethod = ClassificationMethod.NaturalBreaks, // 分類方法(自然分類)
                                    BreakCount           = 5,                                  // 分類数(5段階で分類)
                                };

                                // 等級色(クラス分類)レンダラーを作成する
                                CIMClassBreaksRenderer cimClassBreakRenderer = (CIMClassBreaksRenderer)lyr.CreateRenderer(gcDef);
                                // レンダラーをレイヤーに設定する
                                lyr.SetRenderer(cimClassBreakRenderer);
                            }
                        }
                    });
                }
                catch (Exception)
                {
                    MessageBox.Show("レンダリングに失敗しました。");
                }
            }
        }