Common CustomDataSource
Common_CustomDataSource_VBNet\CustomDataSource_VBNet\TiledMapDataSource_VBNet\MapInformation.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.Xml
Imports ESRI.ArcGIS.ADF.Web.Display.Graphics
Imports ESRI.ArcGIS.ADF.Web.Geometry
Imports ESRI.ArcGIS.ADF.Web.SpatialReference
Imports ESRI.ArcGIS.ADF.Web.DataSources

Namespace TiledMapDataSource_VBNet
  Public Class MapInformation
    Implements IMapInformation
    Public Sub New(ByVal dataSourceDefinition As String, ByVal resourceDefinition As String)
      Me.dataSourceConfig = dataSourceDefinition
      resourceConfig = resourceDefinition
      parseConfig()
    End Sub

    Private dataSourceConfig As String = String.Empty
    Private resourceConfig As String = String.Empty
    Private defaultSpatialReference_Renamed As SpatialReference = Nothing
    Private tileCacheInfo_Renamed As TileCacheInfo = Nothing
    Private defaultExtent_Renamed, fullExtent_Renamed As Envelope

    Public ReadOnly Property DataFrame() As String Implements IMapInformation.DataFrame
      Get
        Return "(default)"
      End Get
    End Property

    Public ReadOnly Property DefaultSpatialReference() As SpatialReference Implements IMapInformation.DefaultSpatialReference
      Get
        Return defaultSpatialReference_Renamed
      End Get
    End Property

    Public ReadOnly Property DefaultExtent() As Envelope Implements IMapInformation.DefaultExtent
      Get
        Return defaultExtent_Renamed
      End Get
    End Property

    Public ReadOnly Property FullExtent() As Envelope Implements IMapInformation.FullExtent
      Get
        Return fullExtent_Renamed
      End Get
    End Property

    Public Property TileCacheInfo() As ESRI.ArcGIS.ADF.Web.DataSources.TileCacheInfo Implements IMapInformation.TileCacheInfo
      Get
        Return tileCacheInfo_Renamed
      End Get
      Set
        tileCacheInfo_Renamed = New TileCacheInfo(Value)
      End Set
    End Property

    Private Sub parseConfig()
'      #Region "Get resource config nodes from document"
      Dim doc As XmlDocument = New XmlDocument()
      doc.Load(dataSourceConfig)
      Dim root As XmlNode = doc.DocumentElement 'TileCacheInfos
      Dim tileCacheInfoNodes As XmlNodeList = root.SelectNodes("TileCacheInfo")

      If tileCacheInfoNodes Is Nothing OrElse tileCacheInfoNodes.Count < 1 Then
        Throw New Exception("Could not find configuration for resource " & resourceConfig)
      End If

      Dim tileCacheNode As XmlNode = Nothing
      Dim tileUrls As System.Collections.Generic.Dictionary(Of Integer, String) = New Dictionary(Of Integer, String)()
      Dim layers As System.Collections.Generic.Dictionary(Of String, String) = New Dictionary(Of String, String)()

      Dim t As Integer = 0
      Do While t < tileCacheInfoNodes.Count
        If tileCacheInfoNodes(t).Attributes("Name").InnerText = resourceConfig Then
          tileCacheNode = tileCacheInfoNodes(t)
          Exit Do
        End If
        t += 1
      Loop
      If tileCacheNode Is Nothing Then
        tileCacheNode = tileCacheInfoNodes(0)
      End If
'      #End Region
'      #Region "Tile Cache Info"
      Dim srtext As String = tileCacheNode.Attributes("SpatialReference").InnerText
      Dim srid As Integer
      If Int32.TryParse(srtext, srid) Then
        defaultSpatialReference_Renamed = New SpatialReference(srid)
      Else
        defaultSpatialReference_Renamed = New SpatialReference(srtext)
      End If

      Dim dpi As Integer = Convert.ToInt32(tileCacheNode.Attributes("DPI").InnerText)
      Dim height As Integer = Convert.ToInt32(tileCacheNode.Attributes("Height").InnerText)
      Dim width As Integer = Convert.ToInt32(tileCacheNode.Attributes("Width").InnerText)
      Dim originCoords As String() = tileCacheNode.Attributes("TileOrigin").InnerText.Split(New Char() { ","c })
      Dim xmin As Double = Convert.ToDouble(originCoords(0))
      Dim ymin As Double = Convert.ToDouble(originCoords(1))
      Dim origin As Point = New Point(xmin, ymin)

      Dim extentNode As XmlNode = tileCacheNode.SelectSingleNode("FullExtent")
      xmin = Convert.ToDouble(extentNode.Attributes("XMin").InnerText)
      ymin = Convert.ToDouble(extentNode.Attributes("YMin").InnerText)
      Dim xmax As Double = Convert.ToDouble(extentNode.Attributes("XMax").InnerText)
      Dim ymax As Double = Convert.ToDouble(extentNode.Attributes("YMax").InnerText)
      fullExtent_Renamed = New Envelope(xmin, ymin, xmax, ymax)
      fullExtent_Renamed.SpatialReference = defaultSpatialReference_Renamed

      extentNode = tileCacheNode.SelectSingleNode("DefaultExtent")
      xmin = Convert.ToDouble(extentNode.Attributes("XMin").InnerText)
      ymin = Convert.ToDouble(extentNode.Attributes("YMin").InnerText)
      xmax = Convert.ToDouble(extentNode.Attributes("XMax").InnerText)
      ymax = Convert.ToDouble(extentNode.Attributes("YMax").InnerText)
      defaultExtent_Renamed = New Envelope(xmin, ymin, xmax, ymax)
      defaultExtent_Renamed.SpatialReference = defaultSpatialReference_Renamed

'#End Region
'      #Region "Layers"
            Dim layersNode As XmlNode = tileCacheNode.SelectSingleNode("Layers")
            If Not layersNode Is Nothing Then
                Dim layerNodes As XmlNodeList = layersNode.SelectNodes("Layer")
                If Not layerNodes Is Nothing Then
                    Dim li As Integer = 0
                    Do While li < layerNodes.Count
                        Dim layerId As String = layerNodes(li).Attributes("LayerID").InnerText
                        Dim layerName As String = layerNodes(li).Attributes("Name").InnerText
                        layers.Add(layerId, layerName)
                        li += 1
                    Loop
                End If
            End If
'      #End Region
'      #Region "LOD Info"
      Dim lodInfos As System.Collections.Generic.List(Of LodInfo) = New List(Of LodInfo)()
      Dim levels As System.Collections.Generic.Dictionary(Of Integer, Integer) = New Dictionary(Of Integer, Integer)()
      Dim lodInfoNode As XmlNode = tileCacheNode.SelectSingleNode("LodInfos")
      Dim lodInfoNodes As XmlNodeList = lodInfoNode.SelectNodes("LodInfo")
      Dim l As Integer = 0
      Do While l < lodInfoNodes.Count
        Dim levelid As Integer = Convert.ToInt32(lodInfoNodes(l).Attributes("LevelID").InnerText)
        levels.Add(l, levelid)
        Dim rows As Integer = Convert.ToInt32(lodInfoNodes(l).Attributes("Rows").InnerText)
        Dim columns As Integer = Convert.ToInt32(lodInfoNodes(l).Attributes("Columns").InnerText)
        Dim tileUrl As String = lodInfoNodes(l).Attributes("TileUrl").InnerText
        Dim tilewidth As Double = Convert.ToDouble(lodInfoNodes(l).Attributes("TileExtentWidth").InnerText)
        Dim tileheight As Double = Convert.ToDouble(lodInfoNodes(l).Attributes("TileExtentHeight").InnerText)
        Dim scale As Double = Convert.ToDouble(lodInfoNodes(l).Attributes("Scale").InnerText)
        Dim resolution As Double = Convert.ToDouble(lodInfoNodes(l).Attributes("Resolution").InnerText)
        Dim lodInfo As LodInfo = New LodInfo(levelid, resolution, scale, columns, rows, tilewidth, tileheight)
        lodInfos.Add(lodInfo)
        tileUrls.Add(levelid, tileUrl)
        l += 1
      Loop
'      #End Region

      tileCacheInfo_Renamed = New TileCacheInfo(width, height, dpi, origin, lodInfos.ToArray())
      tileCacheInfo_Renamed.TileUrls = tileUrls
      tileCacheInfo_Renamed.Layers = layers
      tileCacheInfo_Renamed.Levels = levels

      Dim urlGenNode As XmlNode = tileCacheNode.SelectSingleNode("TileUrlGenerator")
      If Not urlGenNode Is Nothing Then
        tileCacheInfo_Renamed.TileUrlGeneratorAssembly = urlGenNode.Attributes("Assembly").InnerText
        tileCacheInfo_Renamed.TileUrlGeneratorClass = urlGenNode.Attributes("Class").InnerText
      End If

    End Sub
  End Class
End Namespace