This project is read-only.

Working with iPolygonScheme

Mar 22, 2011 at 10:01 PM

I build a featureset on the fly (from model inputs) and would like to build a color ramp to represent the model outputs, for instance

red would be Value<1

orange would be 1<Value<3

yellow would be 3<Value<5

The dataTable has a  field "Values" that is a series of Doubles from 0 to 100K (ish)

and so on.  I looked at the code in the FeatureCategoryControl and pieced this together:

            IPolygonScheme ifs = new PolygonScheme();
            ifs.LegendText = "Loadings";
            ifs.EditorSettings.RampColors = true;
            ifs.EditorSettings.ClassificationType = ClassificationType.Quantities;
            ifs.EditorSettings.IntervalMethod = IntervalMethod.EqualInterval;
            ifs.EditorSettings.NumBreaks = totalBreaks;
            ifs.EditorSettings.FieldName = "Value";
            ifs.EditorSettings.IntervalSnapMethod = IntervalSnapMethod.Rounding;
            ifs.AppearsInLegend = true;
            ifs.CreateCategories(nitroFeatures.DataTable);
            ifs.RegenerateColors();
            IMapPolygonLayer impl = new MapPolygonLayer(nitroFeatures);
            impl.Symbology = ifs;
           
           

          
            impl.LegendText = "Nitrogen";
            _mapArgs.Map.Layers.Add(impl);
            _mapArgs.Map.ViewExtents = impl.Extent;

Where nitroFeatures is a valid feature set.  However all I ever get is one red color for all the shapes.  Clearly I'm missing something important but I can't put my finger on it.  Any help appreciated.

 

Aaron Parks

RTI International

Mar 23, 2011 at 6:34 PM

To answer my own question the  code above wouldn't automagically generate my polygon category breaks for me, so I did it myself with something like the following in the for loop:

 

            stats.Calculate(vals);
            int totalBreaks = 5;
            IPolygonScheme ifs = new PolygonScheme();
            ifs.LegendText = "Loadings";
            ifs.EditorSettings.RampColors = true;
            ifs.EditorSettings.ClassificationType = ClassificationType.Quantities;
            ifs.EditorSettings.IntervalMethod = IntervalMethod.EqualInterval;
            ifs.EditorSettings.NumBreaks = totalBreaks;
            ifs.EditorSettings.FieldName = "Value";
            ifs.EditorSettings.IntervalSnapMethod = IntervalSnapMethod.Rounding;
            ifs.AppearsInLegend = true;
            IPolygonCategory ifc = null;
            double curMin = stats.Minimum;
            double diff = (stats.Maximum - stats.Minimum)/totalBreaks;
            double curMax = curMin + diff;
            ifs.Categories.Clear();
            for (int i = 0; i < totalBreaks; i++)
            {
                //ifc = (IPolygonCategory)ifs.CreateRandomCategory();
                //Color newColor=Color.FromArgb((255*i)/totalBreaks,(255*(totalBreaks-i))/totalBreaks,0);
                Color newColor = Color.FromArgb((255 * (i+1)) / totalBreaks, (255 * (i+1)) / totalBreaks, 0);
               
                ifc=new PolygonCategory(newColor,Color.Black,1);
                ifc.FilterExpression = "[Value]>" + curMin +" AND [Value]<" + curMax;
                //ifc.Range = new Range(curMin, curMax);
                ifc.LegendText = curMin + "->" + curMax;
               
               
                ifs.AddCategory(ifc);

               
                curMin = curMax;
                curMax = curMin + diff;
            }
            //ifs.RegenerateColors();
            IMapPolygonLayer impl = new MapPolygonLayer(nitroFeatures);
            //impl.Symbolizer = null;
            impl.Symbology = ifs;
           
           

          
            //impl.Reproject(LRSFeatures.Projection);
            impl.LegendText = "Nitrogen";
            //impl.Symbolizer.SetFillColor(Color.Transparent);
            //impl.Symbolizer.SetOutlineWidth(3);
            //impl.Symbolizer.OutlineSymbolizer.SetFillColor(Color.Pink);
            _mapArgs.Map.Layers.Add(impl);
            _mapArgs.Map.ViewExtents = impl.Extent;

 

Aaron Parks

RTI International

May 19, 2011 at 7:13 PM

Replying to myself here in the hope that it will help somebody.  Turns out the easiest way to do this is to use IFeatureScheme thusly:

 

IMapPolygonLayer impl = null;
             if (File.Exists(fileName))
             {
                 IFeatureSet fs = FeatureSet.OpenFile(fileName);
                 impl = new MapPolygonLayer(fs);
                 impl.Reproject(_mapArgs.Map.Projection);
                 impl.LegendText = config.AppSettings.Settings["MRLALayerName"].Value;
                 impl.Symbology.SuspendEvents();
                 IFeatureScheme ifs = impl.Symbology.Copy();
                 ifs.EditorSettings.ClassificationType = ClassificationType.UniqueValues;
                 ifs.EditorSettings.FieldName = "LRR_NAME";
                 ifs.CreateCategories(fs.DataTable);
                 impl.Symbology.CopyProperties(ifs);
                 impl.Symbology.ResumeEvents();
             }

 

where LRR_NAME is the name in the Shape file.

 

Aaron Parks

RTI International

Feb 21, 2014 at 10:01 AM
Dear aparks,

First of all i would like to say thanks to you as your post helps me a lot.
It would be nice if u help me to set color for each category. Each category has a unique color but i want to set my own colors.

Regards,
Dheeraj
Apr 1, 2014 at 3:48 PM
hi every one
Excuse me dear aparks but I have a question and if you let me I ask here.If you dont like I delete this question.
when I create PolygonCategory with Color parameter its Ok ,but when I create with Image when I set Layer symbology with this,it return null exception:
it
s ok:
PolygonCategory pc = new DotSpatial.Symbology.PolygonCategory(Color.Red, Color.Pink, 0, DotSpatial.Symbology.GradientType.Circular, Color.Red, 1);

but it return null exception :
PolygonSymbolizer obj1 = new PolygonSymbolizer(ImageObj, System.Drawing.Drawing2D.WrapMode.TileFlipXY, 0, Color.Red, 1);

thanks .