This project is read-only.

A sample code for add symbology to layer

Sep 5, 2014 at 10:12 PM
Hi,I have a polugon layer and I want to add sybology to it base on a field.Can you give me some hints or sample codes about doing this task?I could not find anything about it
Sep 6, 2014 at 8:16 AM
Have a look at the Getting Started Guide or the tutorial Working with DotSpatial Symbology and Attribute table. They should help to do what you want.
Sep 6, 2014 at 9:26 PM
Dear Jani_
I have downloaded doc before but PDF is great.Thank you so much for it
Sep 8, 2014 at 4:29 AM
Here are parts how i do it

make a shape.... after that
fsAdres.SaveAs(Form1.txtExportPad.Text & "Adressen_Op_Bestemmingstypen.shp", True)
            ' *************************************************************************
            Dim Layer As IMapLayer = Form1.myVenster.Layers.Add(fsAdres)
            Dim pPointMapLayer As IMapFeatureLayer
            If Layer IsNot Nothing Then
                If Layer.ProjectionString Is Nothing Then
                    Layer.Projection = DefineProjection()
                End If
                fsAdres = TryCast(Layer.DataSet, IFeatureSet)
                pPointMapLayer = TryCast(Layer, IMapFeatureLayer)
                pPointMapLayer.Symbology = SetAdresBestemmingstypeLegendSWUNG(fsAdres)
                pPointMapLayer.LegendText = "Bestemmingstypen"

                ' *************************************************************************
                ' *  VOEG DATA TOE AAN STATUSBAR en LOGBOEK
                ' *************************************************************************
                statusTEKST = "Laag Adressen_Op_Bestemmingstypen gemaakt en weergegeven"
                Form1.ProgramStatusLabel.Text = statusTEKST
                frmDMClogboek.LogboekListBox.Items.Add(statusTEKST)
                Return
                fsAdres = Nothing
            Else
                Return
            End If
            '----------------------------------------------------------------------------------
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical, "Fout in MaakSwungAdresTabelLayer")
        End Try
and the part of the programm symbology is handled is:
 Public Function SetAdresBestemmingstypeLegendSWUNG(ByVal pfs As IFeatureSet) As IFeatureScheme
        Dim pPointSym As IPointSymbolizer = New PointSymbolizer
        Dim pScheme As IFeatureScheme
        Dim category1 As IPointCategory = New PointCategory(System.Drawing.Color.LightBlue, DotSpatial.Symbology.PointShape.Ellipse, 6.0 + Form1.TrackBarBolletjesGrootte.Value)
        Dim category2 As IPointCategory = New PointCategory(System.Drawing.Color.DeepPink, DotSpatial.Symbology.PointShape.Ellipse, 6.0 + Form1.TrackBarBolletjesGrootte.Value)
        Dim category3 As IPointCategory = New PointCategory(System.Drawing.Color.Violet, DotSpatial.Symbology.PointShape.Ellipse, 6.0 + Form1.TrackBarBolletjesGrootte.Value)
        Dim category4 As IPointCategory = New PointCategory(System.Drawing.Color.Red, DotSpatial.Symbology.PointShape.Ellipse, 6.0 + Form1.TrackBarBolletjesGrootte.Value)
        Dim category5 As IPointCategory = New PointCategory(System.Drawing.Color.Orange, DotSpatial.Symbology.PointShape.Ellipse, 6.0 + Form1.TrackBarBolletjesGrootte.Value)
        Dim category6 As IPointCategory = New PointCategory(System.Drawing.Color.Green, DotSpatial.Symbology.PointShape.Ellipse, 6.0 + Form1.TrackBarBolletjesGrootte.Value)
        Dim category7 As IPointCategory = New PointCategory(System.Drawing.Color.Gray, DotSpatial.Symbology.PointShape.Ellipse, 6.0 + Form1.TrackBarBolletjesGrootte.Value)
        Try
            pScheme = New PointScheme()
            pScheme.LegendText = "Bestemmingstype"
            pPointSym.LegendText = "Bestemmingstype"
            category1.FilterExpression = "[ID_BestemT] = 1 or [ID_BestemT] = 3 or [ID_BestemT] = 4 "
            category1.LegendText = "woning"
            category2.FilterExpression = "[ID_BestemT] = 2 "
            category2.LegendText = "flatwoning, balkon <=4m2"
            category3.FilterExpression = "[ID_BestemT] = 20 or [ID_BestemT] = 5 "
            category3.LegendText = "flatwoning, balkon >4m2"
            category4.FilterExpression = "[ID_BestemT] = 7 or [ID_BestemT] = 8 or [ID_BestemT] = 9 or [ID_BestemT] = 10 or [ID_BestemT] = 11 or [ID_BestemT] = 12 or [ID_BestemT] = 13 "
            category4.LegendText = "gezondheidszorg"
            category5.FilterExpression = "[ID_BestemT] = 14 or [ID_BestemT] = 15 or [ID_BestemT] = 16 or [ID_BestemT] = 17 or [ID_BestemT] = 57 "
            category5.LegendText = "school"
            category6.FilterExpression = "[ID_BestemT] = 6 or [ID_BestemT] = 21 "
            category6.LegendText = "woonwagenterrein of woonboot"
            category7.FilterExpression = "[ID_BestemT] > 21 "
            category7.LegendText = "niet geluidgevoelig"
            'scheme.ClearCategories
            pScheme.AddCategory(category1)
            pScheme.AddCategory(category2)
            pScheme.AddCategory(category3)
            pScheme.AddCategory(category4)
            pScheme.AddCategory(category5)
            pScheme.AddCategory(category6)
            pScheme.AddCategory(category7)
            'pLayer.ShowLabels = True
            'pLayer.Symbology = Scheme
            'Dim MyFeatureLayer As IFeatureLayer = Map.Layers(1)
            'Form1.myVenster.AddLabels(pfs, "My label expression", New Font("Courier", 8), Color.Black)
            Return pScheme
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical, "Fout in SetAdresBestemmingstypeLegendSWUNG")
            Return Nothing
        End Try
    End Function
Sep 8, 2014 at 7:39 AM
Thanks very muck..I could make it work using this method:
 public void DefineUniqueValueRenderer(string fieldName, string layername,Color startColor,Color endcolor)
        {
            IMapFeatureLayer lay = (IMapFeatureLayer)map1.Layers[0];
            lay.Symbology.ClearCategories();
            PolygonScheme myScheme = new PolygonScheme();
            myScheme.EditorSettings.StartColor = startColor;
            myScheme.EditorSettings.EndColor = endcolor;
            myScheme.EditorSettings.ClassificationType = ClassificationType.UniqueValues;
            myScheme.EditorSettings.FieldName = fieldName;
            myScheme.EditorSettings.UseGradient = true;
            myScheme.CreateCategories(lay.DataSet.DataTable);
            myScheme.LegendText = layername;
            lay.Symbology = myScheme;
}
How can I disable symbology and make my layer load normal..I used lay.Symbology.ClearCategories(); but it does not work ..Can you help me about it?
Sep 8, 2014 at 7:51 AM
I used this code to make it same as normal
            IMapFeatureLayer lay = (IMapFeatureLayer)map1.Layers[0];
            lay.Symbology.ClearCategories();
            PolygonSymbolizer lightblue = new PolygonSymbolizer(Color.LightBlue);
            lightblue.OutlineSymbolizer = new LineSymbolizer(Color.Blue, 1);
            lay.Symbolizer = lightblue;
Sep 11, 2014 at 9:38 AM
The above code should work base on decumention but It removes shape and shows nothing to map window.What is wrong in this method ?
    IMapFeatureLayer lay = (IMapFeatureLayer)map1.Layers[0];
            lay.Symbology.ClearCategories();
            PolygonSymbolizer lightblue = new PolygonSymbolizer(Color.LightBlue);
            lightblue.OutlineSymbolizer = new LineSymbolizer(Color.Blue, 1);
            lay.Symbolizer = lightblue;
Sep 11, 2014 at 8:08 PM
Hey am2,

have a look, whether this works for you:
 PolygonSymbolizer PS = New PolygonSymbolizer();
 PS.SetFillColor(Color.LightBlue);
 PS.SetOutline(outlineColor, outlineWidth);
 lay.Symbolizer = PS;
I'm not sure whether you need the lay.Symbology.ClearCategories(); or not.
Sep 12, 2014 at 9:52 AM
This is same as before.I want to clear categories and show map without any categories .Is it possible?Just like when I add a new layer