Common CustomDataSource
Common_CustomDataSource_VBNet\CustomDataSource_VBNet\REXMLDataSource_VBNet\MapResource.vb
' Copyright 2007 ESRI
' 
' All rights reserved under the copyright laws of the United States
' and applicable international laws, treaties, and conventions.
' 
' You may freely redistribute and use this sample code, with or
' without modification, provided you include the original copyright
' notice and use restrictions.
' 
' See the use restrictions.
'


Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Web.UI
Imports ESRI.ArcGIS.ADF.Web.Geometry
Imports ESRI.ArcGIS.ADF.Web.SpatialReference
Imports ESRI.ArcGIS.ADF.Web.DataSources
Imports System.Collections

Imports ESRI.ArcGIS.ADF.Web.Display.Graphics
Imports ESRI.ArcGIS.ADF.Web.Display.Drawing
Imports ESRI.ArcGIS.ADF.Web
Imports System.Xml

Namespace REXMLDataSource_VBNet
    Public Class MapResource
        Implements IMapResource
        Public Sub New()
        End Sub
        Public Sub New(ByVal name_Renamed As String, ByVal dataSource_Renamed As GISDataSource)
            Me.name_Renamed = name_Renamed
            Me.dataSource_Renamed = dataSource_Renamed
        End Sub

#Region "Graphics Resource Specific implementation"
        Private graphics_Renamed As GraphicsDataSet = Nothing

        Public ReadOnly Property Graphics() As GraphicsDataSet
            Get
                Return graphics_Renamed
            End Get
        End Property
#End Region

#Region "IMapResource implementation"

        Private mapInformation_Renamed As IMapInformation = Nothing
        Private displaySettings_Renamed As DisplaySettings = Nothing

        Public ReadOnly Property MapInformation() As IMapInformation Implements IMapResource.MapInformation
            Get
                Return mapInformation_Renamed
            End Get
        End Property

        Public Property DisplaySettings() As DisplaySettings Implements IMapResource.DisplaySettings
            Get
                Return displaySettings_Renamed
            End Get
            Set(ByVal value As DisplaySettings)
                displaySettings_Renamed = Value
            End Set
        End Property

#End Region

#Region "IGISResource implementation"

        Private initialized_Renamed As Boolean = False
        Private name_Renamed As String = String.Empty
        Private resourceDefinition_Renamed As String = String.Empty
        Private dataSource_Renamed As IGISDataSource = Nothing
        Private functionalities_Renamed As GISFunctionalityCollection = New GISFunctionalityCollection()
        Private validationtimeout_Renamed As Integer = 0

        Public Property ValidationTimeout() As Integer Implements IMapResource.ValidationTimeout
            Get
                Return validationtimeout_Renamed
            End Get
            Set(ByVal value As Integer)
                validationtimeout_Renamed = Value
            End Set
        End Property

        Public Property Name() As String Implements IMapResource.Name
            Get
                Return name_Renamed
            End Get
            Set(ByVal value As String)
                name_Renamed = Value
            End Set
        End Property

        Public Property ResourceDefinition() As String Implements IMapResource.ResourceDefinition
            Get
                Return resourceDefinition_Renamed
            End Get
            Set(ByVal value As String)
                resourceDefinition_Renamed = Value
            End Set
        End Property

        Public Property DataSource() As IGISDataSource Implements IMapResource.DataSource
            Get
                Return dataSource_Renamed
            End Get
            Set(ByVal value As IGISDataSource)
                dataSource_Renamed = Value
            End Set
        End Property

        Public Property Functionalities() As GISFunctionalityCollection Implements IMapResource.Functionalities
            Get
                Return functionalities_Renamed
            End Get
            Set(ByVal value As GISFunctionalityCollection)
                functionalities_Renamed = Value
            End Set
        End Property

        Public Function SupportsFunctionality(ByVal functionalityType As System.Type) As Boolean Implements IMapResource.SupportsFunctionality
            If functionalityType Is GetType(ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality) Then
                Return True
            ElseIf functionalityType Is GetType(ESRI.ArcGIS.ADF.Web.DataSources.IMapTocFunctionality) Then
                Return True
            ElseIf functionalityType Is GetType(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality) Then
                Return True
            Else
                Return False
            End If
        End Function

        Public Function CreateFunctionality(ByVal functionalityType As System.Type, ByVal functionalityName As String) As IGISFunctionality Implements IMapResource.CreateFunctionality
            Dim gisfunctionality As IGISFunctionality = Nothing
            If functionalityType Is GetType(ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality) Then
                gisfunctionality = New MapFunctionality(functionalityName, Me)
            ElseIf functionalityType Is GetType(ESRI.ArcGIS.ADF.Web.DataSources.IMapTocFunctionality) Then
                gisfunctionality = New MapTocFunctionality(functionalityName, Me)
            ElseIf functionalityType Is GetType(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality) Then
                gisfunctionality = New QueryFunctionality(functionalityName, Me)
            Else
                Throw New ArgumentException("functionalityType not supported")
            End If
            Return gisfunctionality
        End Function

        Public ReadOnly Property Initialized() As Boolean Implements IMapResource.Initialized
            Get
                Return initialized_Renamed
            End Get
        End Property

        Public Sub LoadState() Implements IMapResource.LoadState
            If dataSource_Renamed Is Nothing Then
                Return
            End If
            If dataSource_Renamed.State Is Nothing Then
                Return
            End If

            Dim o As Object = dataSource_Renamed.State(key)
            If Not o Is Nothing Then
                Dim mr As MapResource = TryCast(o, MapResource)
                graphics_Renamed = mr.Graphics
                mapInformation_Renamed = mr.mapInformation
                displaySettings_Renamed = mr.DisplaySettings
            End If
        End Sub

        Public Sub Initialize() Implements IMapResource.Initialize
            If mapInformation_Renamed Is Nothing Then
                graphics_Renamed = New GraphicsDataSet()

                ' To initialize once on load, use the following two lines
                'string dataSourceConfig = DataSource.DataSourceDefinition;
                'ProcessREXML(dataSourceConfig);


                mapInformation_Renamed = New MapInformation(graphics_Renamed)
                If Not DisplaySettings Is Nothing Then
                    graphics_Renamed.ImageDescriptor = displaySettings_Renamed.ImageDescriptor
                End If
            End If
            initialized_Renamed = True

            ' To initialize and read from the data source during each postback, use the following lines.
            '* This will be valuable when data source content changes during a session.
            '

            Dim dataSourceConfig As String = DataSource.DataSourceDefinition
            ProcessREXML(dataSourceConfig)
        End Sub

        Public Sub SaveState() Implements IMapResource.SaveState
            If dataSource_Renamed Is Nothing Then
                Return
            End If
            If dataSource_Renamed.State Is Nothing Then
                Return
            End If
            dataSource_Renamed.State(key) = Me
        End Sub

        Public Sub Dispose() Implements IMapResource.Dispose
            initialized_Renamed = False
        End Sub


        Public Sub ClearState() Implements IMapResource.ClearState
            If DataSource Is Nothing OrElse DataSource.State Is Nothing Then
                Return
            End If
            DataSource.State(key) = Nothing
        End Sub

#End Region

#Region "private Key Properties"
        Private ReadOnly Property key() As String
            Get
                Dim tmp As String = Me.GetType().ToString() & ":" & name_Renamed
                Return (tmp)
            End Get
        End Property
#End Region

        Private Sub ProcessREXML(ByVal dataSourceConfig As String)
            Try
                '        #Region "Use FeatureGraphicsLayer for feature type (complex) graphics"
                ' Read xml data doc
                Dim doc As XmlDocument = New XmlDocument()
                doc.Load(dataSourceConfig)
                Dim root As XmlNode = doc.DocumentElement

                ' Get layer information
                Dim layerNode As XmlNode = root.SelectSingleNode("LAYER")
                Dim layername As String = layerNode.Attributes.GetNamedItem("name").Value
                Dim layerid As String = layerNode.Attributes.GetNamedItem("id").Value

                Dim glayer As ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer = Nothing
                Dim gvisibility As Boolean = True

                ' Create graphics layer to store features
                If graphics_Renamed.Tables.Contains(layername) Then
                    If TypeOf graphics_Renamed.Tables(layername) Is FeatureGraphicsLayer Then
                        Dim fgl As FeatureGraphicsLayer = CType(graphics_Renamed.Tables(layername), FeatureGraphicsLayer)
                        ' Get visibility of existing layer so when it is recreated, it's visibility is correct.  
                        ' This will allow 1) setting visibility of the layer (checking\unchecking the node) in the toc 
                        ' and 2) data to be updated each time the map resource is initialized.
                        gvisibility = fgl.Visible
                    End If

                    graphics_Renamed.Tables.Remove(layername)
                End If

                glayer = New ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer()
                glayer.TableName = layername
                glayer.FeatureType = FeatureType.Point
                glayer.Visible = gvisibility

                ' Get renderer information
                Dim rendererNode As XmlNode = layerNode.SelectSingleNode("SIMPLERENDERER")
                Dim markerNode As XmlNode = rendererNode.SelectSingleNode("SIMPLEMARKERSYMBOL")
                Dim markercolor As String = markerNode.Attributes.GetNamedItem("color").Value
                Dim markertype As String = markerNode.Attributes.GetNamedItem("type").Value
                Dim markerwidth As String = markerNode.Attributes.GetNamedItem("width").Value
                Dim markeroutlinecolor As String = markerNode.Attributes.GetNamedItem("outlinecolor").Value

                ' Create symbol set
                Dim sms As ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleMarkerSymbol = New ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleMarkerSymbol()

                Dim markerrgb As String() = markercolor.Split(","c)
                Dim markerr As Integer = Int32.Parse(markerrgb(0))
                Dim markerg As Integer = Int32.Parse(markerrgb(1))
                Dim markerb As Integer = Int32.Parse(markerrgb(2))
                sms.Color = System.Drawing.Color.FromArgb(markerr, markerg, markerb)

                Select Case markertype
                    Case "circle"
                        sms.Type = ESRI.ArcGIS.ADF.Web.Display.Symbol.MarkerSymbolType.Circle
                    Case "square"
                        sms.Type = ESRI.ArcGIS.ADF.Web.Display.Symbol.MarkerSymbolType.Square
                    Case "triangle"
                        sms.Type = ESRI.ArcGIS.ADF.Web.Display.Symbol.MarkerSymbolType.Triangle
                    Case "star"
                        sms.Type = ESRI.ArcGIS.ADF.Web.Display.Symbol.MarkerSymbolType.Star
                    Case Else
                End Select

                Dim markerwidthInt As Integer
                If (Not Int32.TryParse(markerwidth, markerwidthInt)) Then
                    markerwidthInt = 10
                End If
                sms.Width = markerwidthInt

                Dim markeroutrgb As String() = markeroutlinecolor.Split(","c)
                Dim markeroutr As Integer = Int32.Parse(markeroutrgb(0))
                Dim markeroutg As Integer = Int32.Parse(markeroutrgb(1))
                Dim markeroutb As Integer = Int32.Parse(markeroutrgb(2))
                sms.OutlineColor = System.Drawing.Color.FromArgb(markeroutr, markeroutg, markeroutb)

                Dim sr As ESRI.ArcGIS.ADF.Web.Display.Renderer.SimpleRenderer = New ESRI.ArcGIS.ADF.Web.Display.Renderer.SimpleRenderer(sms)
                glayer.Renderer = sr

                ' Get features
                Dim featuresNode As XmlNode = layerNode.SelectSingleNode("FEATURES")
                Dim featureNodes As XmlNodeList = featuresNode.SelectNodes("FEATURE")

                ' Get shape field and create GraphicElement
                Dim t As Integer = 0
                Do While t < featureNodes.Count
                    Dim datarow As System.Data.DataRow = Nothing

                    Dim flds As XmlNodeList = featureNodes(t).SelectNodes("FIELD")
                    Dim s As Integer = 0
                    Do While s < flds.Count
                        ' The SHAPE column should be first in the config file - to create the DataRow
                        Dim fldnode As XmlNode = flds(s)
                        Dim fldattribute As XmlAttribute = fldnode.Attributes("name")

                        If fldattribute.Value = "SHAPE" Then
                            Dim fldvalues As XmlNodeList = fldnode.SelectSingleNode("FIELDVALUE").ChildNodes
                            If fldvalues.Count < 1 Then
                                Exit Do
                            ElseIf fldvalues.Count = 1 Then
                                Dim fldvalue As XmlNode = fldvalues(0)
                                If fldvalue.Name = "POINT" Then
                                    Dim dx As Double = Double.Parse(fldvalue.Attributes("x").Value)
                                    Dim dy As Double = Double.Parse(fldvalue.Attributes("y").Value)

                                    Dim point As ESRI.ArcGIS.ADF.Web.Geometry.Point = New ESRI.ArcGIS.ADF.Web.Geometry.Point(dx, dy)
                                    datarow = glayer.Add(point)
                                End If
                            ElseIf fldvalues.Count > 1 Then

                            End If
                        Else

                            Dim fldname As String = flds(s).Attributes("name").Value
                            If (Not glayer.Columns.Contains(fldname)) Then
                                Dim dcol As System.Data.DataColumn = New System.Data.DataColumn("Status", System.Type.GetType("System.String"))
                                glayer.Columns.Add(dcol)
                            End If
                            Dim fldvalue As XmlNode = flds(s).SelectSingleNode("FIELDVALUE")

                            Dim fieldattribute As XmlAttribute = fldvalue.Attributes("valuestring")
                            If (Not fieldattribute Is Nothing) Then
                                Dim dvalue As String = fieldattribute.Value
                                If (Not dvalue Is Nothing) OrElse (dvalue <> String.Empty) Then
                                    datarow(fldname) = dvalue
                                End If
                            End If
                        End If
                        s += 1
                    Loop
                    t += 1
                Loop

                graphics_Renamed.Tables.Add(glayer)

            Catch ex As Exception
                Throw New Exception("Exception: unable to load rexml data. " & ex.Message)
            End Try

        End Sub
    End Class
End Namespace