Specify Custom Color Ramp from plugin

Dec 16, 2015 at 1:05 PM
This should be simple, but I can't find how to do it. How can you set a custom color ramp that is made up of 5 categories with a predefined color for a specific column on a layer on the map through code? I have tried creating 5 featurecategory, with ranges of values from -1 to 1 that will need to be symboloized, but there is nothing there to specify colors and I haven't figured out how to add these categories to the layer. The ranges will be -1 to -.6, -.59 to -.2, -.19 to .2, .21 to .6, and .61 to 1.

Any help would be appreciated.
Steve
Coordinator
Dec 18, 2015 at 1:44 PM
For MapWinGIS Sergei made an example at http://www.mapwindow.org/documentation/mapwingis4.9/_add_category_range_8cs-example.html

In MW5 a shapefile is calles a FeatureSet.
If you need more help, just ask.

Paul
Dec 20, 2015 at 11:56 PM
I will take a look at this now.

Steve
Dec 21, 2015 at 5:11 AM
Before I read the article, this is what I saw:

FeatureSet fs = MWContext.Layers[LayerOffset].FeatureSet as FeatureSet; // I chose to take a layer of the main map and place it here so that I made sure I had a valid layer to test
            fs.Categories.Clear();

            ColorRamp cr = new ColorRamp();
            cr.Add(new ColorInterval(-1, Color.FromArgb(255, 0, 0)));
            cr.Add(new ColorInterval(-0.6, Color.FromArgb(240, 128, 128)));
            cr.Add(new ColorInterval(-0.2, Color.FromArgb(229, 204, 255)));
            cr.Add(new ColorInterval(0.2, Color.FromArgb(142, 229, 238)));
            cr.Add(new ColorInterval(0.6, Color.FromArgb(0, 0, 139)));
            FeatureCategory fc = new FeatureCategory("Very Low");                
            fc.SetRange(-1.0, -0.6);
            FeatureCategory fc2 = new FeatureCategory("Low");
            fc2.SetRange(-0.59, -0.2);
            FeatureCategory fc3 = new FeatureCategory("Moderate");
            fc3.SetRange(-.19, 0.2);
            FeatureCategory fc4 = new FeatureCategory("High");
            fc4.SetRange(0.21, 0.6);
            FeatureCategory fc5 = new FeatureCategory("Very High");
            fc5.SetRange(0.61, 1);                
            fs.Categories.Add(fc);
            fs.Categories.Add(fc2);
            fs.Categories.Add(fc3);
            fs.Categories.Add(fc4);
            fs.Categories.Add(fc5);
            fs.Categories.ClassificationField = "nw__" + textToUse;  
            fs.Categories.ApplyColorScheme(Api.Enums.SchemeType.Graduated, cr);
            //svc.AddDatabaseLayer(conn, RootOfLayerName, Api.Enums.GeometryType.Polygon, Api.Enums.ZValueType.None);
            SelectionMap1.Layers.Add(fs, true);
            SelectionMap1.Layers[SelectionMap1.Layers.Count - 1].Name = textToUse; 
            SelectionMap1.Redraw(Api.Enums.RedrawType.All);
The results is:
Image
The color range should be divided into something similar as below.
-1 to -.6 = “Very Low” - red
-.6 to -.2 = “Low” - orange
-.2 to .2 = “Moderate” -blue
.2 to .6 = “High” -yellow
.6 to 1 = “Very High” -green
The data table that it is getting information from is:
Image


I then tried to follow the example, trying to adapt with the absence of DefaultDrawingOptions, SetColors2, and ApplyColorScheme3.

The first attempt to alter my code is this:
FeatureSet fs = MWContext.Layers[LayerOffset].FeatureSet as FeatureSet; // I chose to take a layer of the main map and place it here so that I made sure I had a valid layer to test
            fs.Categories.Clear();
cr.SetColors(Color.FromArgb(255, 0, 0), Color.FromArgb(240, 128, 128));
            fs.Categories.AddRange(FieldOffset, Api.Enums.Classification.NaturalBreaks, 4, -1.0, -0.6);
            fs.Categories.ApplyColorScheme(Api.Enums.SchemeType.Graduated, cr, Api.Enums.StyleElement.Fill, 0, 4);
            cr.SetColors(Color.FromArgb(240, 128, 128), Color.FromArgb(229, 204, 255));
            fs.Categories.AddRange(FieldOffset, Api.Enums.Classification.NaturalBreaks, 5, -.6, -.2);
            fs.Categories.ApplyColorScheme(Api.Enums.SchemeType.Graduated, cr, Api.Enums.StyleElement.Fill, 5, 9);
            cr.SetColors(Color.FromArgb(229, 204, 255), Color.FromArgb(142, 229, 238));
            fs.Categories.AddRange(FieldOffset, Api.Enums.Classification.NaturalBreaks, 5, -.2, .2);
            fs.Categories.ApplyColorScheme(Api.Enums.SchemeType.Graduated, cr, Api.Enums.StyleElement.Fill, 10, 14);
            cr.SetColors(Color.FromArgb(142, 229, 238), Color.FromArgb(0, 0, 139));
            fs.Categories.AddRange(FieldOffset, Api.Enums.Classification.NaturalBreaks, 5, .2, .6);
            fs.Categories.ApplyColorScheme(Api.Enums.SchemeType.Graduated, cr, Api.Enums.StyleElement.Fill, 15, 19);
            cr.SetColors(Color.FromArgb(0, 0, 139), Color.FromArgb(0, 0, 5));
            fs.Categories.AddRange(FieldOffset, Api.Enums.Classification.NaturalBreaks, 5, .6, 1.0);
            fs.Categories.ApplyColorScheme(Api.Enums.SchemeType.Graduated, cr, Api.Enums.StyleElement.Fill, 20, 24);
                 fs.Categories.ApplyExpressions();
            SelectionMap1.Layers.Add(fs, true);
            SelectionMap1.Layers[SelectionMap1.Layers.Count - 1].Name = textToUse; 
            SelectionMap1.Redraw(Api.Enums.RedrawType.All);
But this leaves me with a map with nothing in the legend but a single color.

If I do something like this, merging the 2 codes:
FeatureCategory fc = new FeatureCategory("Very Low");
            fc.SetRange(-1.0, -0.6);
            FeatureCategory fc2 = new FeatureCategory("Low");
            fc2.SetRange(-0.59, -0.2);
            FeatureCategory fc3 = new FeatureCategory("Moderate");
            fc3.SetRange(-.19, 0.2);
            FeatureCategory fc4 = new FeatureCategory("High");
            fc4.SetRange(0.21, 0.6);
            FeatureCategory fc5 = new FeatureCategory("Very High");
            fc5.SetRange(0.61, 1);
            fs.Categories.Clear();
            fs.Categories.Add(fc);
            fs.Categories.Add(fc2);
            fs.Categories.Add(fc3);
            fs.Categories.Add(fc4);
            fs.Categories.Add(fc5);
            fs.Categories.ClassificationField = "nw__" + textToUse;
            cr.SetColors(Color.FromArgb(255, 0, 0), Color.FromArgb(240, 128, 128));
            fs.Categories.AddRange(FieldOffset, Api.Enums.Classification.NaturalBreaks, 4, -1.0, -0.6);
            fs.Categories.ApplyColorScheme(Api.Enums.SchemeType.Graduated, cr, Api.Enums.StyleElement.Fill, 0, 4);
            cr.SetColors(Color.FromArgb(240, 128, 128), Color.FromArgb(229, 204, 255));
            fs.Categories.AddRange(FieldOffset, Api.Enums.Classification.NaturalBreaks, 5, -.6, -.2);
            fs.Categories.ApplyColorScheme(Api.Enums.SchemeType.Graduated, cr, Api.Enums.StyleElement.Fill, 5, 9);
            cr.SetColors(Color.FromArgb(229, 204, 255), Color.FromArgb(142, 229, 238));
            fs.Categories.AddRange(FieldOffset, Api.Enums.Classification.NaturalBreaks, 5, -.2, .2);
            fs.Categories.ApplyColorScheme(Api.Enums.SchemeType.Graduated, cr, Api.Enums.StyleElement.Fill, 10, 14);
            cr.SetColors(Color.FromArgb(142, 229, 238), Color.FromArgb(0, 0, 139));
            fs.Categories.AddRange(FieldOffset, Api.Enums.Classification.NaturalBreaks, 5, .2, .6);
            fs.Categories.ApplyColorScheme(Api.Enums.SchemeType.Graduated, cr, Api.Enums.StyleElement.Fill, 15, 19);
            cr.SetColors(Color.FromArgb(0, 0, 139), Color.FromArgb(0, 0, 5));
            fs.Categories.AddRange(FieldOffset, Api.Enums.Classification.NaturalBreaks, 5, .6, 1.0);
            fs.Categories.ApplyColorScheme(Api.Enums.SchemeType.Graduated, cr, Api.Enums.StyleElement.Fill, 20, 24);
            //fs.Categories.ApplyColorScheme(Api.Enums.SchemeType.Graduated, cr);
            //svc.AddDatabaseLayer(conn, RootOfLayerName, Api.Enums.GeometryType.Polygon, Api.Enums.ZValueType.None);
            fs.Categories.ApplyExpressions(); 
            SelectionMap1.Layers.Add(fs, true);
            SelectionMap1.Layers[SelectionMap1.Layers.Count - 1].Name = textToUse; 
            SelectionMap1.Redraw(Api.Enums.RedrawType.All);
The result is :
Image
There is a range of values, but it is not correct. All the values are changed, and not correctly. The field name is not correct, only having a couple of characters.

What I am hoping for is to add to a BoundMapControl, the same table from the database or layer which is loaded on the main screen map, and to be able to add it to a layer multiple times to the BoundMapControl on a dialog box, with each instance of the layer displaying a separate Classification field for each layer. So, if I am looking to display upland condition or road density for the results of the model inside this dialog box, I want to get the data from (in this case) from the results layer from the main map, I should have 2 layers added to the dialog box’s bound map control, each having a different classification field, and they have a set range of color values:
-1 to -.6 = “Very Low” - red
-.6 to -.2 = “Low” - orange
-.2 to .2 = “Moderate” -blue
.2 to .6 = “High” -yellow
.6 to 1 = “Very High” -green


Any help would be appreciated or a pointer in the right direction.

Steve