Common CustomDataSource
Common_CustomDataSource_VBNet\CustomDataSource_VBNet\TiledMapDataSource_VBNet\VirtualEarthTileUrlGenerator.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

Namespace TiledMapDataSource_VBNet
  Public Class VirtualEarthTileUrlGenerator
    Implements ITileUrlGenerator
    #Region "ITileUrlGenerator Members"

        Public Function GetTileUrl(ByVal column As Long, ByVal row As Long, ByVal level As Integer, ByVal defaultUrl As String, ByVal layerVisibility As Dictionary(Of String, Boolean)) As String Implements ITileUrlGenerator.GetTileUrl
            Return GetUrl(Convert.ToInt32(column), Convert.ToInt32(row), level, layerVisibility)
        End Function

    #End Region

    Private Shared Function TileToQuadKey(ByVal tx As Integer, ByVal ty As Integer, ByVal zl As Integer) As String
      Dim quad As String = ""
      For i As Integer = zl To 1 Step -1
        Dim mask As Integer = 1 << (i - 1)
        Dim cell As Integer = 0
        If (tx And mask) <> 0 Then
          cell += 1
        End If
        If (ty And mask) <> 0 Then
          cell += 2
        End If
        quad &= cell
      Next i
      Return quad
    End Function

    Private Function GetUrl(ByVal column As Integer, ByVal row As Integer, ByVal level As Integer, ByVal layerVisibility As Dictionary(Of String, Boolean)) As String
      Dim mapType As String = Nothing
      Dim mapExtension As String = Nothing

      If layerVisibility("r") AndAlso layerVisibility("a") Then
        mapType = "h"
        mapExtension = ".jpeg"
      ElseIf layerVisibility("r") Then
        mapType = "r"
        mapExtension = ".png"
      ElseIf layerVisibility("a") Then
        mapType = "a"
        mapExtension = ".jpeg"
      Else
        Return Nothing
      End If

      Dim quadKey As String = TileToQuadKey(column, row, level)

      Dim url As String = String.Concat(New Object() { "http://", mapType, quadKey.Chars(quadKey.Length - 1), ".ortho.tiles.virtualearth.net/tiles/", mapType, quadKey, mapExtension, "?g=", 1 })
      Return url
    End Function
  End Class
End Namespace