How can i get each feature's color of a layer

Topics: Algorithms, SharpMap v0.9 / v1.x, WinForms Controls
Mar 11, 2009 at 12:53 PM
Edited Mar 12, 2009 at 7:23 AM
i wrote this code to change each feature of a layer , but when i change color of a feature another feature color change to black......

Private Sub FeatureColorPicker_SelectedColorChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FeatureColorPicker.SelectedColorChanged
        Try
            Dim needLayer As SharpMap.Layers.VectorLayer = MapBox1.Map.Layers(ActiveLayerIndex)

            If needLayer.DataSource.IsOpen Then
                needLayer.DataSource.Close()
                needLayer.DataSource.Open()
            Else
                needLayer.DataSource.Open()
            End If

            Dim ext As SharpMap.Geometries.BoundingBox = needLayer.Envelope
            needLayer.DataSource.Open()
            Dim myDataSet As New SharpMap.Data.FeatureDataSet()
            needLayer.DataSource.ExecuteIntersectionQuery(ext, myDataSet)
            Dim dt As SharpMap.Data.FeatureDataTable = myDataSet.Tables(0)
            Dim row As SharpMap.Data.FeatureDataRow
            Dim DgvRows As DataGridViewRow
            Dim DgvName As String = FeatureColorPicker.Tag
            Select Case DgvName
                Case "1"
                    For Each DgvRows In DataGridViewX1.SelectedRows
                        For Each row In dt.Rows
                            Dim x As String = row.Item(0)
                            If x = DgvRows.Cells(0).Value Then
                                FeatureColorPicker.Tag = DgvRows.Cells(0).Value
                                Exit For
                            End If
                        Next
                    Next
                Case "2"
                    For Each DgvRows In DataGridViewX2.SelectedRows
                        For Each row In dt.Rows
                            Dim x As String = row.Item(0)
                            If x = DgvRows.Cells(0).Value Then
                                FeatureColorPicker.Tag = DgvRows.Cells(0).Value
                                Exit For
                            End If
                        Next
                    Next
            End Select

            Dim myTheme As SharpMap.Rendering.Thematics.CustomTheme = New SharpMap.Rendering.Thematics.CustomTheme(AddressOf GetCountryStyle)

            If row IsNot Nothing Then

                Dim Vlayer As SharpMap.Layers.VectorLayer = Map.Layers(ActiveLayerIndex)
                Vlayer.Theme = myTheme
                Try
                    Map = MapBox1.Map
                    Dim WillBeRemoveLayer As SharpMap.Layers.ILayer = Map.Layers(ActiveLayerIndex)
                    Map.Layers.Remove(WillBeRemoveLayer)
                    Map.Layers.Add(Vlayer)
                    MapBox1.Map = Map
                Catch ex As Exception
                    Map.Layers.Add(Vlayer)
                    MapBox1.Map = Map
                End Try
            End If
        Catch ex As Exception
            MsgBox("Please Select a layer in Layers List")
        End Try
    End Sub

      Private Function GetCountryStyle(ByVal row As SharpMap.Data.FeatureDataRow) As SharpMap.Styles.VectorStyle
        Dim style As SharpMap.Styles.VectorStyle = New SharpMap.Styles.VectorStyle
        Select Case row(0).ToString().ToLower()
            Case FeatureColorPicker.Tag.ToString
                Dim c As Color = FeatureColorPicker.SelectedColor
                Dim b As Brush = New SolidBrush(c)
                style.Fill = b
                Return style
            Case Else
                Dim c As Color = Color.Black
                Dim b As Brush = New SolidBrush(c)
                style.Fill = b
                Return style ''''------------------>>>>>>>> change another feature color to black , but when i write " Return Nothing " , i received this error " Object reference not set to an instance of an object. "
        End Select
    End Function

Sorry for bad english
Please help me . Thanks....
Coordinator
Mar 11, 2009 at 2:59 PM
Hi mahdiii, I am not really sure what you mean. In your GetCountryStyle method you are creating a new VectorStyle (this will default to black) then you are setting the colour only when a certain condition is met - any time it is not met it will still be black. hth jd
Mar 11, 2009 at 6:14 PM
Edited Mar 12, 2009 at 7:18 AM
Thanks Johndiss
for example i want to change just one of the feature's color of a layer , and don't want to change another feature's color.
please see this pictures :

before change feature's color :  http://www.freeimagehosting.net/uploads/8462dd71b0.jpg
After change feature's color :  http://www.freeimagehosting.net/uploads/c9657a6b45.jpg
But i want to change like this picture : http://www.freeimagehosting.net/uploads/0f3c32cc46.jpg    sorry for bad edit :D

how can i do this?


Mar 12, 2009 at 7:01 AM
Edited Mar 12, 2009 at 7:21 AM
Or how can i get feature's color?
Coordinator
Mar 12, 2009 at 10:54 AM
Hi mahdiii, you cannot get the features color after rendering unless you store a dictionary of features and colours or pass the feature data row back into the styling method and investigate the properties of the style object returned. 
If I am understanding your issue correctly you just need to set the 'other' colour to be non black:      

    Private Function GetCountryStyle(ByVal row As SharpMap.Data.FeatureDataRow) As SharpMap.Styles.VectorStyle
        Dim activeColor as Color
        Dim lowlightColor as Color = Color.Green ' just set the color here to the 'normal' color you want
        Dim highlightColor as Color = FeatureColorPicker.SelectedColor
        Select Case row(0).ToString().ToLower()
            Case FeatureColorPicker.Tag.ToString
               activeColor = highlightColor 
            Case Else
                color = lowlightColor
        End Select
        Dim b As Brush = New SolidBrush(activeColor)
        Dim style As SharpMap.Styles.VectorStyle = New SharpMap.Styles.VectorStyle
        style.Fill = b
        Return style 

    End Function

hth jd
Mar 12, 2009 at 11:21 AM
Hi Johndiss , very thansk....
ok i use a feature's color dictionary.