一、墨卡托投影(Mercator Projection)
Bing Maps的地圖數據采用墨卡托投影進行存儲和展現。墨卡托(Mercator)投影,又名“等角正軸圓柱投影”,荷蘭地圖學家墨卡托(Mercator)在1569年擬定,假設地球被圍在一個中空的圓柱里,其赤道與圓柱相接觸,然后再假想地球中心有一盞燈,把球面上的圖形投影到圓柱體上,再把圓柱體展開,這就是一幅標准緯線為零度(即赤道)的“墨卡托投影”繪制出的世界地圖。
->
二、為什么選擇墨卡托投影?
墨卡托投影的“等角”特性,保證了對象的形狀的不變形,正方形的物體投影后不會變為長方形。“等角”也保證了方向和相互位置的正確性,因此在航海和航空中常常應用,在線地圖也利用此特性,在計算人們查詢地物的方向時不會出錯。
墨卡托投影的“圓柱”特性,保證了南北(緯線)和東西(經線)都是平行直線,並且相互垂直。而且經線間隔是相同的,緯線間隔從標准緯線(此處是赤道,也可能是其他緯線)向兩級逐漸增大。
但是,“等角”不可避免的帶來的面積的巨大變形,特別是兩極地區,明顯的如格陵蘭島比實際面積擴大了N倍。不過要是去兩極地區探險或科考的同志們,一般有更詳細的資料,不會來查看網絡地圖的,這個不要緊。
三、墨卡托投影坐標系
墨卡托投影以整個世界范圍,赤道作為標准緯線,本初子午線作為中央經線,兩者交點為坐標原點,向東向北為正,向西向南為負。南北極在地圖的正下、上方,而東西方向處於地圖的正右、左。
由於墨卡托投影在兩極附近是趨於無限值得,因此它並沒完整展現了整個世界,地圖上最高緯度是85.05度。為了簡化計算,我們采用球形映射,而不是橢球體形狀。雖然采用Mercator Projection只是為了方便展示地圖,需要知道的是,這種映射會給Y軸方向帶來0.33%的誤差。
由於赤道半徑為6378137米,則赤道周長為2*PI*r = 20037508.3427892,因此X軸的取值范圍:[-20037508.3427892,20037508.3427892]。當緯度φ接近兩極,即90°時,Y值趨向於無窮。因此通常把Y軸的取值范圍也限定在[-20037508.3427892,20037508.3427892]之間。因此在墨卡托投影坐標系(米)下的坐標范圍是:最小為(-20037508.3427892, -20037508.3427892 )到最大 坐標為(20037508.3427892, 20037508.3427892)。
四、地理坐標系
地理經度的取值范圍是[-180,180],緯度不可能到達90°,通過緯度取值范圍為[20037508.3427892,20037508.3427892]反計算可得到緯度值為85.05112877980659。因此緯度取值范圍是[-85.05112877980659,85.05112877980659]。因此,地理坐標系(經緯度)對應的范圍是:最小地理坐標(-180,-85.05112877980659),最大地理坐標(180, 85.05112877980659)。
五、地面分辨率(Ground Resolution)和地圖比例尺(Map Scale)
地圖分辨率,Bing Maps的地圖分辨率在第1級是512*512像素,第2級是1024*1024像素,第3級是2048*2048像素。。。最高好像到19級。
計算公式(單位:像素):
地圖寬度 = 地圖高度 = 256 * 2 ^ 圖像等級
地面分辨率,是指地圖圖像每個像素代表的實際距離,比如地面分辨率是10米/像素,就是說地圖圖像上的一個像素對應地面上10米的距離(應該是10平方米?還是10*10平方米?)。地面分辨率受地圖圖像級別和緯度影響,取地球赤道半徑為6378137米。
地面分辨率的計算公式為(單位:米/像素):
地面分辨率 = cos(緯度 * pi/180) * 地球赤道長度 / 地圖寬度
= (cos(緯度 * pi/180) * 2 * pi * 6378137 ) / (256 * 2 ^ 圖像等級)
地圖比例尺,指示的是地圖上的長度與地面實際長度的比例。例如,在一個地圖比例尺為1:100000,地圖上的每一英寸代表一個地面距離100000英寸。地面分辨率、地圖比例尺都是隨地圖圖像級別和緯度隨時變化的。以目前一般的屏幕分辨率(DPI:每英寸的像素數)96dpi,即每英寸有96個像素來計算。
地圖比例尺的計算公式為(注:1英寸=2.54厘米=0.0254米):
地圖比例尺 = 1 : 地面分辨率 * 屏幕分辨率 / 0.0254
= 1 : (cos(緯度 * pi/180) * 2 * pi * 6378137 * 屏幕分辨率) / (256 * 2 ^ 圖像等級 * 0.0254)
下表是在赤道上的地圖圖像等級、地圖分辨率、地面分辨率和地圖比例尺的關系,這並不是完整的對應表,因為它們的對應關系還會受到緯度變化的影響。
圖像等級 |
地圖的寬高 (單位:像素) |
地面比例尺 (米/像素) |
地圖比例尺 (以 96 dpi 為准) |
1 |
512 |
78,271.5170 |
1 : 295,829,355.45 |
2 |
1,024 |
39,135.7585 |
1 : 147,914,677.73 |
3 |
2,048 |
19,567.8792 |
1 : 73,957,338.86 |
4 |
4,096 |
9,783.9396 |
1 : 36,978,669.43 |
5 |
8,192 |
4,891.9698 |
1 : 18,489,334.72 |
6 |
16,384 |
2,445.9849 |
1 : 9,244,667.36 |
7 |
32,768 |
1,222.9925 |
1 : 4,622,333.68 |
8 |
65,536 |
611.4962 |
1 : 2,311,166.84 |
9 |
131,072 |
305.7481 |
1 : 1,155,583.42 |
10 |
262,144 |
152.8741 |
1 : 577,791.71 |
11 |
524,288 |
76.4370 |
1 : 288,895.85 |
12 |
1,048,576 |
38.2185 |
1 : 144,447.93 |
13 |
2,097,152 |
19.1093 |
1 : 72,223.96 |
14 |
4,194,304 |
9.5546 |
1 : 36,111.98 |
15 |
8,388,608 |
4.7773 |
1 : 18,055.99 |
16 |
16,777,216 |
2.3887 |
1 : 9,028.00 |
17 |
33,554,432 |
1.1943 |
1 : 4,514.00 |
18 |
67,108,864 |
0.5972 |
1 : 2,257.00 |
19 |
134,217,728 |
0.2986 |
1 : 1,128.50 |
20 |
268,435,456 |
0.1493 |
1 : 564.25 |
21 |
536,870,912 |
0.0746 |
1 : 282.12 |
22 |
1,073,741,824 |
0.0373 |
1 : 141.06 |
23 |
2,147,483,648 |
0.0187 |
1 : 70.53 |
六、像素坐標系(Pixel Coordinates)
因為不同圖像級別的分辨率不同,所以每一級別的地圖圖像都有以自己級別像素寬高為准的坐標系,以地圖左上角為原點(0,0),向右向下為正方向。每一級的坐標系最大值為(地圖寬度 - 1, 地圖高度 - 1),或者套用上面的公式:(256 * 2 ^ 圖像等級 - 1, 256 * 2 ^ 圖像等級 - 1)。以第3級為例:像素坐標范圍從(0, 0)到(2047, 2047)。
在給定經度、緯度和圖像級別的情況下,我們可以通過以下公式計算其像素坐標:
sinLatitude = sin(緯度 * pi/180)
X坐標 = ((經度 + 180) / 360) * 256 * 2 ^ level
Y坐標 = (0.5 – log((1 + sinLatitude) / (1 – sinLatitude)) / (4 * pi)) * 256 * 2 ^ level
因為Bing Maps使用的是墨卡托投影,所以這里的經度取值范圍從[-180°, +180°],緯度取值范圍從[-85.05112878°, 85.05112878°]。
七、地圖瓦片坐標系(Tile Coordinates)和Quadkeys
為了優化地圖的顯示和檢索速度,呈現到客戶端的地圖整體被切割成為邊長為256像素的正方形瓦片(Tile)。因為每個圖像級別的地圖分辨率不同,所以不同圖像級別被切割成為的瓦片數量也不同。
計算公式(單位:塊):
垂直方向上的瓦片數量 = 水平方向上的瓦片數量 = 2 ^ 圖像級別
瓦片坐標系以左上角為原點(0, 0),到右下角(2 ^ 圖像級別 - 1, 2 ^ 圖像級別 - 1)為止。例如:第3級的瓦片坐標系是從(0, 0)到(7, 7)。
關於瓦片坐標系的計算:
已知一個像素坐標XY,可以很容易求得其瓦片坐標:
瓦片X坐標 = floor(像素X坐標 / 256)
瓦片Y坐標 = floor(像素Y坐標 / 256)
為了簡化索引和存儲地圖圖片,每個瓦片的二維XY坐標值會被轉換成一維字串,即四叉樹鍵值(quardtree keys,縮寫為quadkeys)。每個quadkey獨立對應某個放大級別下的一個瓦片,並且它可以被用作數據庫中B-tree索引值(我還不知道這個B-tree怎么用。。。)。為了將坐標值轉換成quadkey,需要將Y和X坐標二進制值交錯組合,並轉換成4進制值及對應的字符串。例如,假設在放大級別為3時,瓦片的XY坐標值為(3, 5) ,quadkey計算如下:
瓦片X坐標 = 3 = 011 (二進制)
瓦片Y坐標 = 5 = 101 (二進制)
quadkey = 100111 (二進制) = 213 (四進制) = "213"
(說實話,我沒明白這里是怎么交錯組合的。。。經過和朋友討論發現,首先XY轉換成二進制后,要在前面補0來補齊成與當前圖像等級相同的位數;然后開始“交錯”。。。先是Y坐標的首位,然后是X坐標的首位,然后是Y坐標的第二位,然后是X坐標的第二位。。。以此類推)
quadkey還有其他一些有意思的特性。第一,quadkey的長度等於該瓦片所對應的圖像級別;第二,每個瓦片的quadkey的前幾位和其父瓦片(上一圖像級別所對應的瓦片)的quadkey相同,下圖中,第1級的 '瓦片2' 是第2級的 '瓦片20' 至 '瓦片23' 的父瓦片,第2級的 '瓦片13' 是 第3級的 '瓦片130' 至 '瓦片133' 的父瓦片:
最后,quadkey提供的一維索引值通常顯示了兩個tile在XY坐標系中的相似性。換句話說,兩個相鄰的tile對應的quadkey非常接近。這對於優化數據庫的性能非常重要,因為相鄰的tile通常被同時請求顯示,因此可以將這些tile存放在相同的磁盤區域中,以減少磁盤的讀取次數。
以上就是對於Bing Maps Tile System的一些簡單學習和總結,內容部分來源於網絡。
下面是官方提供的一些有關Bing Maps的轉換算法方法示例,編程語言:C#。
//------------------------------------------------------------------------------ // <copyright company="Microsoft"> // Copyright (c) 2006-2009 Microsoft Corporation. All rights reserved. // </copyright> //------------------------------------------------------------------------------ using System; using System.Text; namespace Microsoft.MapPoint { static class TileSystem { private const double EarthRadius = 6378137; private const double MinLatitude = -85.05112878; private const double MaxLatitude = 85.05112878; private const double MinLongitude = -180; private const double MaxLongitude = 180; /// <summary> /// Clips a number to the specified minimum and maximum values. /// </summary> /// <param name="n">The number to clip.</param> /// <param name="minValue">Minimum allowable value.</param> /// <param name="maxValue">Maximum allowable value.</param> /// <returns>The clipped value.</returns> private static double Clip(double n, double minValue, double maxValue) { return Math.Min(Math.Max(n, minValue), maxValue); } /// <summary> /// Determines the map width and height (in pixels) at a specified level /// of detail. /// </summary> /// <param name="levelOfDetail">Level of detail, from 1 (lowest detail) /// to 23 (highest detail).</param> /// <returns>The map width and height in pixels.</returns> public static uint MapSize(int levelOfDetail) { return (uint) 256 << levelOfDetail; } /// <summary> /// Determines the ground resolution (in meters per pixel) at a specified /// latitude and level of detail. /// </summary> /// <param name="latitude">Latitude (in degrees) at which to measure the /// ground resolution.</param> /// <param name="levelOfDetail">Level of detail, from 1 (lowest detail) /// to 23 (highest detail).</param> /// <returns>The ground resolution, in meters per pixel.</returns> public static double GroundResolution(double latitude, int levelOfDetail) { latitude = Clip(latitude, MinLatitude, MaxLatitude); return Math.Cos(latitude * Math.PI / 180) * 2 * Math.PI * EarthRadius / MapSize(levelOfDetail); } /// <summary> /// Determines the map scale at a specified latitude, level of detail, /// and screen resolution. /// </summary> /// <param name="latitude">Latitude (in degrees) at which to measure the /// map scale.</param> /// <param name="levelOfDetail">Level of detail, from 1 (lowest detail) /// to 23 (highest detail).</param> /// <param name="screenDpi">Resolution of the screen, in dots per inch.</param> /// <returns>The map scale, expressed as the denominator N of the ratio 1 : N.</returns> public static double MapScale(double latitude, int levelOfDetail, int screenDpi) { return GroundResolution(latitude, levelOfDetail) * screenDpi / 0.0254; } /// <summary> /// Converts a point from latitude/longitude WGS-84 coordinates (in degrees) /// into pixel XY coordinates at a specified level of detail. /// </summary> /// <param name="latitude">Latitude of the point, in degrees.</param> /// <param name="longitude">Longitude of the point, in degrees.</param> /// <param name="levelOfDetail">Level of detail, from 1 (lowest detail) /// to 23 (highest detail).</param> /// <param name="pixelX">Output parameter receiving the X coordinate in pixels.</param> /// <param name="pixelY">Output parameter receiving the Y coordinate in pixels.</param> public static void LatLongToPixelXY(double latitude, double longitude, int levelOfDetail, out int pixelX, out int pixelY) { latitude = Clip(latitude, MinLatitude, MaxLatitude); longitude = Clip(longitude, MinLongitude, MaxLongitude); double x = (longitude + 180) / 360; double sinLatitude = Math.Sin(latitude * Math.PI / 180); double y = 0.5 - Math.Log((1 + sinLatitude) / (1 - sinLatitude)) / (4 * Math.PI); uint mapSize = MapSize(levelOfDetail); pixelX = (int) Clip(x * mapSize + 0.5, 0, mapSize - 1); pixelY = (int) Clip(y * mapSize + 0.5, 0, mapSize - 1); } /// <summary> /// Converts a pixel from pixel XY coordinates at a specified level of detail /// into latitude/longitude WGS-84 coordinates (in degrees). /// </summary> /// <param name="pixelX">X coordinate of the point, in pixels.</param> /// <param name="pixelY">Y coordinates of the point, in pixels.</param> /// <param name="levelOfDetail">Level of detail, from 1 (lowest detail) /// to 23 (highest detail).</param> /// <param name="latitude">Output parameter receiving the latitude in degrees.</param> /// <param name="longitude">Output parameter receiving the longitude in degrees.</param> public static void PixelXYToLatLong(int pixelX, int pixelY, int levelOfDetail, out double latitude, out double longitude) { double mapSize = MapSize(levelOfDetail); double x = (Clip(pixelX, 0, mapSize - 1) / mapSize) - 0.5; double y = 0.5 - (Clip(pixelY, 0, mapSize - 1) / mapSize); latitude = 90 - 360 * Math.Atan(Math.Exp(-y * 2 * Math.PI)) / Math.PI; longitude = 360 * x; } /// <summary> /// Converts pixel XY coordinates into tile XY coordinates of the tile containing /// the specified pixel. /// </summary> /// <param name="pixelX">Pixel X coordinate.</param> /// <param name="pixelY">Pixel Y coordinate.</param> /// <param name="tileX">Output parameter receiving the tile X coordinate.</param> /// <param name="tileY">Output parameter receiving the tile Y coordinate.</param> public static void PixelXYToTileXY(int pixelX, int pixelY, out int tileX, out int tileY) { tileX = pixelX / 256; tileY = pixelY / 256; } /// <summary> /// Converts tile XY coordinates into pixel XY coordinates of the upper-left pixel /// of the specified tile. /// </summary> /// <param name="tileX">Tile X coordinate.</param> /// <param name="tileY">Tile Y coordinate.</param> /// <param name="pixelX">Output parameter receiving the pixel X coordinate.</param> /// <param name="pixelY">Output parameter receiving the pixel Y coordinate.</param> public static void TileXYToPixelXY(int tileX, int tileY, out int pixelX, out int pixelY) { pixelX = tileX * 256; pixelY = tileY * 256; } /// <summary> /// Converts tile XY coordinates into a QuadKey at a specified level of detail. /// </summary> /// <param name="tileX">Tile X coordinate.</param> /// <param name="tileY">Tile Y coordinate.</param> /// <param name="levelOfDetail">Level of detail, from 1 (lowest detail) /// to 23 (highest detail).</param> /// <returns>A string containing the QuadKey.</returns> public static string TileXYToQuadKey(int tileX, int tileY, int levelOfDetail) { StringBuilder quadKey = new StringBuilder(); for (int i = levelOfDetail; i > 0; i--) { char digit = '0'; int mask = 1 << (i - 1); if ((tileX & mask) != 0) { digit++; } if ((tileY & mask) != 0) { digit++; digit++; } quadKey.Append(digit); } return quadKey.ToString(); } /// <summary> /// Converts a QuadKey into tile XY coordinates. /// </summary> /// <param name="quadKey">QuadKey of the tile.</param> /// <param name="tileX">Output parameter receiving the tile X coordinate.</param> /// <param name="tileY">Output parameter receiving the tile Y coordinate.</param> /// <param name="levelOfDetail">Output parameter receiving the level of detail.</param> public static void QuadKeyToTileXY(string quadKey, out int tileX, out int tileY, out int levelOfDetail) { tileX = tileY = 0; levelOfDetail = quadKey.Length; for (int i = levelOfDetail; i > 0; i--) { int mask = 1 << (i - 1); switch (quadKey[levelOfDetail - i]) { case '0': break; case '1': tileX |= mask; break; case '2': tileY |= mask; break; case '3': tileX |= mask; tileY |= mask; break; default: throw new ArgumentException("Invalid QuadKey digit sequence."); } } } } }
參考資料:
[1] 微軟官方幫助文檔:http://msdn.microsoft.com/en-us/library/bb259689.aspx
[2] Bing Maps系列文章:http://www.cnblogs.com/beniao/archive/2010/01/13/1646446.html
[3] 地圖投影全解析:http://hi.baidu.com/liongg/item/2840be728b738b10d0dcb39b