0

I want to implement a theme based on category values for a field within an Arcview shapefile. There is a something similar in SharpMap but not in Mapsui.

Any ideas?

1 Answers1

0

Here is an implementation of a category theme for Mapsui. I converted code from SharpMap, replacing SharpMap references for Mapsui and eliminated table and datarow patterns to entities. I used master code branches.

To implement: I created a function passing in shapeFile object, field name (for shapefile), and list of value and color pairs. In the code example below, function is designed to apply a category theme to a regional gelogy shapefile based on colors used by United States Geological Survey.

public Style CreateUSGSAgeTheme(Mapsui.Desktop.Shapefile.ShapeFile aShapeFile, string aColumnName, IList<DRC_SQLITE_Mapping.USGSAgeColor> aUSGSAgeColors)
        {
            var wCategoryTheme = new Mapsui.Styles.Thematics.CategoryTheme<string>();
            wCategoryTheme.UseDefaultStyleForDbNull = true;
            wCategoryTheme.ColumnName = aColumnName;

            VectorStyle wdefaultStyle = new VectorStyle() { Fill = new Brush(Color.Transparent), Outline = new Pen(Color.Black), Opacity = 1 };
            wdefaultStyle.Enabled = true;
            //wdefaultStyle.MinVisible = 0;           
            //wdefaultStyle.MaxVisible = wMaxVisible;

            CategoryThemeValuesItem<string> defaultItem = new CategoryThemeValuesItem<string>();
            defaultItem.Values = new List<string>() { string.Empty };
            defaultItem.Style = wdefaultStyle;
            defaultItem.Title = "Default";
            wCategoryTheme.Default = wdefaultStyle;

            //For each row in shapefile
            var Boundingbox = aShapeFile.GetExtents();
            var features = aShapeFile.GetFeaturesInView(Boundingbox, 1);
            foreach (var feature in features)
            {
                bool bMatch = false;
                string ageType = feature[aColumnName.ToUpper()].ToString();


                //Loop and find match theme record
                foreach (var wUSGSAgeColor in aUSGSAgeColors)
                {
                    //if (wUSGSAgeColor.Code == ageType && ((Mapsui.Styles.VectorStyle)wMatchedStyle).Fill.Color == Color.Transparent)
                    if (wUSGSAgeColor.Code.Equals(ageType,StringComparison.OrdinalIgnoreCase))
                    {
                        var wMatchedStyle = wCategoryTheme.GetStyle(feature);
                        if (((Mapsui.Styles.VectorStyle)wMatchedStyle).Fill.Color == Color.Transparent)
                        {
                            Int32 red = Convert.ToInt32(wUSGSAgeColor.Red);
                            Int32 green = Convert.ToInt32(wUSGSAgeColor.Green);
                            Int32 blue = Convert.ToInt32(wUSGSAgeColor.Blue);
                            Brush wBrush = new Brush(new Color(red, green, blue));
                            wBrush.FillStyle = FillStyle.Solid;

                            VectorStyle wStyle = new VectorStyle() { Fill = wBrush, Outline = new Pen(Color.Black), Opacity = 1 };
                            wStyle.Enabled = true;
                            //wStyle.MinVisible = 0;                       
                            //wStyle.MaxVisible = wMaxVisible;

                            CategoryThemeValuesItem<string> wItem = new CategoryThemeValuesItem<string>();
                            wItem.Title = ageType;
                            wItem.Values = new List<string>() { ageType.Trim().ToUpper(), ageType.Trim().ToLower(), ageType.Trim(), ageType };
                            wItem.Style = wStyle;
                            wItem.DisplayOrder =  !String.IsNullOrWhiteSpace(wUSGSAgeColor.DisplayOrder) ? Convert.ToInt64(wUSGSAgeColor.DisplayOrder) : -1;
                            wItem.CategoryKey = wUSGSAgeColor.Key;

                            wCategoryTheme.Add(wItem);
                            bMatch = true;
                        }
                        else
                        {
                            bMatch = true;
                        }
                        break;
                    }
                }
                if (!bMatch)
                {
                    CategoryThemeValuesItem<string> wItem = new CategoryThemeValuesItem<string>();
                    wItem.Title = ageType;
                    wItem.Values = new List<string>() { ageType, ageType.Trim().ToLower(), ageType.Trim() };
                    wItem.Style = wdefaultStyle;
                    wCategoryTheme.Add(wItem);
                    bMatch = true;
                }


            }
            return wCategoryTheme;
        }

Here is my source code: https://github.com/garykindel/Mapsui.Styles.Thematics.CategoryTheme

References: https://github.com/Mapsui https://github.com/SharpMap/SharpMap.Rendering.Thematics.CategoryTheme

Result: enter image description here