EasyGIS.NET icon indicating copy to clipboard operation
EasyGIS.NET copied to clipboard

SfMap Render Line Elements

Open blinkor opened this issue 3 years ago • 1 comments

I'm trying to render a shapefile's specific items by record types in a .DBF file. I tried to use example 8 sample project's code in my projects, but couldn't get my shapefile to render lines by a specific color.

My ShapeFile is not a collection of polygons, but rather a collection of lines. The DBF file looks something like this: image

I try to load the ShapeFile like this:

        Dim path As String = Application.StartupPath & "\assets\gisdata\tsunami\testShapeFile.shp"
        Dim sf As EGIS.ShapeFileLib.ShapeFile = Me.SfMap1.AddShapeFile(path, "ShapeFile", "name")
        sf.RenderSettings.FieldName = "name"
        sf.RenderSettings.Font = New Font(Me.Font.FontFamily, 12)
        Dim colors As Dictionary(Of String, Color) = New Dictionary(Of String, Color)()
        colors.Add("Region1", Color.Green)
        Dim rs As RoadTypeCustomRenderSettings = New RoadTypeCustomRenderSettings(sf.RenderSettings, "name", colors)
        sf.RenderSettings.CustomRenderSettings = rs
        sf.RenderSettings.UseToolTip = True
        sf.RenderSettings.ToolTipFieldName = "name"
        sf.RenderSettings.MaxPixelPenWidth = 20
        sf.RenderSettings.IsSelectable = True

I have a class with these functions that looks like this:

Imports EGIS.ShapeFileLib

Class RoadTypeCustomRenderSettings
    Inherits BaseCustomRenderSettings

    Private colorList As List(Of System.Drawing.Color)

    Public Sub New(ByVal defaultSettings As RenderSettings, ByVal typeField As String, ByVal roadtypeColors As Dictionary(Of String, System.Drawing.Color))
        MyBase.New(defaultSettings)
        BuildColorList(defaultSettings, typeField, roadtypeColors)
    End Sub

    Private Sub BuildColorList(ByVal defaultSettings As RenderSettings, ByVal typeField As String, ByVal roadtypeColors As Dictionary(Of String, System.Drawing.Color))
        Dim fieldIndex As Integer = defaultSettings.DbfReader.IndexOfFieldName("NAME")

        If fieldIndex >= 0 Then
            colorList = New List(Of System.Drawing.Color)()
            Dim numRecords As Integer = defaultSettings.DbfReader.DbfRecordHeader.RecordCount

            For n As Integer = 0 To numRecords - 1
                Dim nextField As String = defaultSettings.DbfReader.GetField(n, fieldIndex).Trim()
                Console.WriteLine("FINDING: " & nextField)
                If roadtypeColors.ContainsKey(nextField) Then
                    colorList.Add(roadtypeColors(nextField))
                    MsgBox("FOUND")

                Else
'If it is not found, use a red color.
                    Dim defColor = Color.FromArgb(255, 0, 0)
                    '  colorList.Add(defaultSettings.FillColor)
                    colorList.Add(defColor)
                End If
            Next
        End If
    End Sub

    Public Overrides Function GetRecordFillColor(ByVal recordNumber As Integer) As System.Drawing.Color
        If colorList IsNot Nothing Then
            Return colorList(recordNumber)
        End If

        Return renderSettings.SelectOutlineColor
    End Function
End Class

Class POICustomRenderSettings
    ' Inherits ICustomRenderSettings

    Private imageList As List(Of System.Drawing.Image)
    Private defaultSettings As RenderSettings

    Public Sub New(ByVal defaultSettings As RenderSettings, ByVal typeField As String, ByVal poiImages As Dictionary(Of String, System.Drawing.Image), ByVal defaultImage As System.Drawing.Image)
        Me.defaultSettings = defaultSettings
        BuildPOIList(defaultSettings, typeField, poiImages, defaultImage)
    End Sub

    Private Sub BuildPOIList(ByVal defaultSettings As RenderSettings, ByVal typeField As String, ByVal poiImages As Dictionary(Of String, System.Drawing.Image), ByVal defaultImage As System.Drawing.Image)
        Dim fieldIndex As Integer = defaultSettings.DbfReader.IndexOfFieldName(typeField)

        If fieldIndex >= 0 Then
            imageList = New List(Of System.Drawing.Image)()
            Dim numRecords As Integer = defaultSettings.DbfReader.DbfRecordHeader.RecordCount

            For n As Integer = 0 To numRecords - 1
                Dim nextField As String = defaultSettings.DbfReader.GetField(n, fieldIndex).Trim()

                If poiImages.ContainsKey(nextField) Then
                    imageList.Add(poiImages(nextField))
                Else
                    imageList.Add(defaultImage)
                End If
            Next
        End If
    End Sub

    Public Function GetRecordFillColor(ByVal recordNumber As Integer) As System.Drawing.Color
        Return defaultSettings.FillColor
    End Function

    Public Function GetRecordFontColor(ByVal recordNumber As Integer) As System.Drawing.Color
        Return defaultSettings.FontColor
    End Function

    Public Function GetRecordImageSymbol(ByVal recordNumber As Integer) As System.Drawing.Image
        Return imageList(recordNumber)
    End Function

    Public Function GetRecordOutlineColor(ByVal recordNumber As Integer) As System.Drawing.Color
        Return defaultSettings.OutlineColor
    End Function

    Public Function GetRecordToolTip(ByVal recordNumber As Integer) As String
        Return ""
    End Function

    Public Function RenderShape(ByVal recordNumber As Integer) As Boolean
        Return True
    End Function

    Public Function GetRecordLabel(ByVal recordNumber As Integer) As String
        Return ""
    End Function

    Public Function GetDirection(ByVal recordNumber As Integer) As Integer
        Return If(defaultSettings.DrawDirectionArrows, 1, 0)
    End Function

    Public ReadOnly Property UseCustomImageSymbols As Boolean
        Get
            Return True
        End Get
    End Property

    Public ReadOnly Property UseCustomTooltips As Boolean
        Get
            Return False
        End Get
    End Property

    Public ReadOnly Property UseCustomRecordLabels As Boolean
        Get
            Return False
        End Get
    End Property
End Class

When I run the application, I sucessfully get the message boxes which tell me that the record was found. However, I cannot color the specific element rendering on the SFMAP. I also know that the lines are being rendered correctly, as if I hover over the lines, a popup shows with the item's name ("Region1").

However, when the map renders, I don't get any green lines, but only gray lines. How can I render specific items on the SFMAP with a different color by a name in the shapefile's DBF file?

blinkor avatar Feb 13 '22 00:02 blinkor