PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Branches/02/Source/Extensions/geoCache.Caches.Disk/VirtualEarthTileSystem.cs

http://geoCache.codeplex.com
C# | 157 lines | 68 code | 24 blank | 65 comment | 5 complexity | 224c91780546cea1d014c6b28f353f06 MD5 | raw file
  1. //------------------------------------------------------------------------------
  2. // <copyright file="VirtualEarthTileSystem.cs" company="Microsoft">
  3. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. //
  6. // Source from http://msdn.microsoft.com/en-us/library/bb259689.aspx
  7. //------------------------------------------------------------------------------
  8. using System;
  9. using System.Text;
  10. namespace Microsoft.MapPoint
  11. {
  12. public static class VirtualEarthTileSystem
  13. {
  14. private const double EarthRadius = 6378137;
  15. private const double MinLatitude = -85.05112878;
  16. private const double MaxLatitude = 85.05112878;
  17. private const double MinLongitude = -180;
  18. private const double MaxLongitude = 180;
  19. /// <summary>
  20. /// Clips a number to the specified minimum and maximum values.
  21. /// </summary>
  22. /// <param name="n">The number to clip.</param>
  23. /// <param name="minValue">Minimum allowable value.</param>
  24. /// <param name="maxValue">Maximum allowable value.</param>
  25. /// <returns>The clipped value.</returns>
  26. private static double Clip(double n, double minValue, double maxValue)
  27. {
  28. return Math.Min(Math.Max(n, minValue), maxValue);
  29. }
  30. /// <summary>
  31. /// Determines the map width and height (in pixels) at a specified level
  32. /// of detail.
  33. /// </summary>
  34. /// <param name="levelOfDetail">Level of detail, from 1 (lowest detail)
  35. /// to 23 (highest detail).</param>
  36. /// <returns>The map width and height in pixels.</returns>
  37. public static uint MapSize(int levelOfDetail)
  38. {
  39. return (uint) 256 << levelOfDetail;
  40. }
  41. /// <summary>
  42. /// Determines the ground resolution (in meters per pixel) at a specified
  43. /// latitude and level of detail.
  44. /// </summary>
  45. /// <param name="latitude">Latitude (in degrees) at which to measure the
  46. /// ground resolution.</param>
  47. /// <param name="levelOfDetail">Level of detail, from 1 (lowest detail)
  48. /// to 23 (highest detail).</param>
  49. /// <returns>The ground resolution, in meters per pixel.</returns>
  50. public static double GroundResolution(double latitude, int levelOfDetail)
  51. {
  52. latitude = Clip(latitude, MinLatitude, MaxLatitude);
  53. return Math.Cos(latitude * Math.PI / 180) * 2 * Math.PI * EarthRadius /
  54. MapSize(levelOfDetail);
  55. }
  56. /// <summary>
  57. /// Determines the map scale at a specified latitude, level of detail,
  58. /// and screen resolution.
  59. /// </summary>
  60. /// <param name="latitude">Latitude (in degrees) at which to measure the
  61. /// map scale.</param>
  62. /// <param name="levelOfDetail">Level of detail, from 1 (lowest detail)
  63. /// to 23 (highest detail).</param>
  64. /// <param name="screenDpi">Resolution of the screen, in dots per inch.</param>
  65. /// <returns>The map scale, expressed as the denominator N of the ratio 1 : N.</returns>
  66. public static double MapScale(double latitude, int levelOfDetail, int screenDpi)
  67. {
  68. return GroundResolution(latitude, levelOfDetail) * screenDpi / 0.0254;
  69. }
  70. /// <summary>
  71. /// Converts a point from latitude/longitude WGS-84 coordinates (in degrees)
  72. /// into pixel XY coordinates at a specified level of detail.
  73. /// </summary>
  74. /// <param name="latitude">Latitude of the point, in degrees.</param>
  75. /// <param name="longitude">Longitude of the point, in degrees.</param>
  76. /// <param name="levelOfDetail">Level of detail, from 1 (lowest detail)
  77. /// to 23 (highest detail).</param>
  78. /// <param name="pixelX">Output parameter receiving the X coordinate in pixels.</param>
  79. /// <param name="pixelY">Output parameter receiving the Y coordinate in pixels.</param>
  80. public static void LatLongToPixelXY(double latitude, double longitude, int levelOfDetail,
  81. out int pixelX, out int pixelY)
  82. {
  83. latitude = Clip(latitude, MinLatitude, MaxLatitude);
  84. longitude = Clip(longitude, MinLongitude, MaxLongitude);
  85. double x = (longitude + 180) / 360;
  86. double sinLatitude = Math.Sin(latitude * Math.PI / 180);
  87. double y = 0.5 - Math.Log((1 + sinLatitude) / (1 - sinLatitude)) / (4 * Math.PI);
  88. uint mapSize = MapSize(levelOfDetail);
  89. pixelX = (int) Clip(x * mapSize + 0.5, 0, mapSize - 1);
  90. pixelY = (int) Clip(y * mapSize + 0.5, 0, mapSize - 1);
  91. }
  92. /// <summary>
  93. /// Converts pixel XY coordinates into tile XY coordinates.
  94. /// </summary>
  95. /// <param name="pixelX">Pixel X coordinate.</param>
  96. /// <param name="pixelY">Pixel Y coordinate.</param>
  97. /// <param name="tileX">Output parameter receiving the tile X coordinate.</param>
  98. /// <param name="tileY">Output parameter receiving the tile Y coordinate.</param>
  99. public static void PixelXYToTileXY(int pixelX, int pixelY, out int tileX, out int tileY)
  100. {
  101. tileX = pixelX / 256;
  102. tileY = pixelY / 256;
  103. }
  104. /// <summary>
  105. /// Converts tile XY coordinates into a QuadKey at a specified level of detail.
  106. /// </summary>
  107. /// <param name="tileX">Tile X coordinate.</param>
  108. /// <param name="tileY">Tile Y coordinate.</param>
  109. /// <param name="levelOfDetail">Level of detail, from 1 (lowest detail)
  110. /// to 23 (highest detail).</param>
  111. /// <returns>A string containing the QuadKey.</returns>
  112. public static string TileXYToQuadKey(int tileX, int tileY, int levelOfDetail)
  113. {
  114. var quadKey = new StringBuilder();
  115. for (int i = levelOfDetail; i > 0; i--)
  116. {
  117. char digit = '0';
  118. int mask = 1 << (i - 1);
  119. if ((tileX & mask) != 0)
  120. {
  121. digit++;
  122. }
  123. if ((tileY & mask) != 0)
  124. {
  125. digit++;
  126. digit++;
  127. }
  128. quadKey.Append(digit);
  129. }
  130. return quadKey.ToString();
  131. }
  132. }
  133. }