GIS理論(墨卡托投影、地理坐標系、地面分辨率、地圖比例尺、Bing Maps Tile System)
墨卡托投影(Mercator Projection),又名“等角正軸圓柱投影”,荷蘭地圖學家墨卡托(Mercator)在1569年擬定,假設地球被圍在一個中空的圓柱里,其赤道與圓柱相接觸,然后再假想地
球中心有一盞燈,把球面上的圖形投影到圓柱體上,再把圓柱體展開,這就是一幅標准緯線為零度(即赤道)的“墨卡托投影”繪制出的世界地圖。
一、墨卡托投影坐標系(Mercator Projection)
墨卡托投影以整個世界范圍,赤道作為標准緯線,本初子午線作為中央經線,兩者交點為坐標原點,向東向北為正,向西向南為負。南北極在地圖的正下、上方,而東西方向處於地圖的正右、左。
由於Mercator Projection在兩極附近是趨於無限值得,因此它並沒完整展現了整個世界,地圖上最高緯度是85.05度。為了簡化計算,我們采用球形映射,而不是橢球體形狀。雖然采用Mercator Projection只是為了方便展示地圖,需要知道的是,這種映射會給Y軸方向帶來0.33%的誤差。
------------------------------------------------------------------------------------------------------------------------------------------
earthRadius =6378137
20037508.3427892 = earthRadius * (math.pi - 0)
85.05112877980659 = (math.atan(math.exp(aa / earthRadius))-math.pi/4)*2 * 180 / math.pi
image = 512 * 512
groundResolution(1 level) = (20037508.3427892 * 2) / 512 = 78271.516964
screendpi = 96
mapScale = groundResolution * 96 / 0.0254 = 295829355.455
---------------------------------------------------------------------------------------------------------------------------------------
由於赤道半徑為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)。
二、地理坐標系(Geographical coordinates)
地理經度的取值范圍是[-180,180],緯度不可能到達90°,通過緯度取值范圍為[20037508.3427892,20037508.3427892]反計算可得到緯度值為85.05112877980659。因此緯度取值范圍是[-85.05112877980659,85.05112877980659]。因此,地理坐標系(經緯度)對應的范圍是:最小地理坐標(-180,-85.05112877980659),最大地理坐標(180, 85.05112877980659)。
三、地面分辨率(Ground Resolution)
地面分辨率是以一個像素(pixel)代表的地面尺寸(米)。以微軟Bing Maps為例,當Level為1時,圖片大小為512*512(4個Tile),那么赤道空間分辨率為:赤道周長/512。其他緯度的空間分辨率則為 緯度圈長度/512,極端的北極則為0。Level為2時,赤道的空間分辨率為 赤道周長/1024,其他緯度為 緯度圈長度1024。很明顯,Ground Resolution取決於兩個參數,縮放級別Level和緯度latitude ,Level決定像素的多少,latitude決定地面距離的長短。
地面分辨率的公式為,單位:米/像素:
ground resolution = (cos(latitude * pi/180) * 2 * pi * 6378137 meters) / (256 * 2level pixels)
最低地圖放大級別(1級),地圖是512 x 512像素。每下一個放大級別,地圖的高度和寬度分別乘於2:2級是1024 x 1024像素,3級是2048 x 2048像素,4級是4096 x 4096像素,等等。通常而言,地圖的寬度和高度可以由以下式子計算得到:map width = map height = 256 * 2^level pixels
四、地圖比例尺(Map Scale)
地圖比例尺是指測量相同目標時,地圖上距離與實際距離的比例。通過地圖分辨率在計算可知由Level可得到圖片的像素大小,那么需要把其轉換為以米為單位的距離,涉及到DPI(dot per inch),暫時可理解為類似的PPI(pixel per inch),即每英寸代表多少個像素。256 * 2level / DPI 即得到相應的英寸inch,再把英寸inch除以0.0254轉換為米。實地距離仍舊是:cos(latitude * pi/180) * 2 * pi * 6378137 meters; 因此比例尺的公式為:
map scale = 256 * 2level / screen dpi / 0.0254 / (cos(latitude * pi/180) * 2 * pi * 6378137)
比例尺= 1 : (cos(latitude * pi/180) * 2 * pi * 6378137 * screen dpi) / (256 * 2level * 0.0254)
地面分辨率和地圖比例尺之間的關系:
map scale = 1 : ground resolution * screen dpi / 0.0254 meters/inch
縮放級別 |
地圖寬度、高度(像素) |
地面分辨率(米/像素) |
地圖比例尺(以96dpi為例) |
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 |
五、Bing Maps像素坐標系和地圖圖片編碼
為了優化地圖系統性能,提高地圖下載和顯示速度,所有地圖都被分割成256 x 256像素大小的正方形小塊。由於在每個放大級別下的像素數量都不一樣,因此地圖圖片(Tile)的數量也不一樣。每個tile都有一個XY坐標值,從左上角的(0, 0)至右下角的(2^level–1, 2^level–1)。例如在3級放大級別下,所有tile的坐標值范圍為(0, 0)至(7, 7),如下圖:
已知一個像素的XY坐標值時,我們很容易得到這個像素所在的Tile的XY坐標值:
tileX = floor(pixelX / 256) tileY = floor(pixelY / 256)
為了簡化索引和存儲地圖圖片,每個tile的二維XY值被轉換成一維字串,即四叉樹鍵值(quardtree key,簡稱quadkey)。每個quadkey獨立對應某個放大級別下的一個tile,並且它可以被用作數據庫中B-tree索引值。為了將坐標值轉換成quadkey,需要將Y和X坐標二進制值交錯組合,並轉換成4進制值及對應的字符串。例如,假設在放大級別為3時,tile的XY坐標值為(3,5),quadkey計算如下:
tileX = 3 = 011(二進制)
tileY = 5 = 101(二進制)
quadkey = 100111(二進制) = 213(四進制) = “213”
Quadkey還有其他一些有意思的特性。第一,quadkey的長度等於該tile所對應的放大級別;第二,每個tile的quadkey的前幾位和其父tile(上一放大級別所對應的tile)的quadkey相同,下圖中,tile 2是tile 20至23的父tile,tile 13是tile 130至133的父級:
最后,quadkey提供的一維索引值通常顯示了兩個tile在XY坐標系中的相似性。換句話說,兩個相鄰的tile對應的quadkey非常接近。這對於優化數據庫的性能非常重要,因為相鄰的tile通常被同時請求顯示,因此可以將這些tile存放在相同的磁盤區域中,以減少磁盤的讀取次數。
下面是微軟Bing Maps的TileSystem相關算法:
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(intlevelOfDetail) { 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, intscreenDpi) { 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, intlevelOfDetail, 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, intlevelOfDetail, 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, intlevelOfDetail) { StringBuilder quadKey = newStringBuilder(); 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 intlevelOfDetail) { 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."); } } } } }
--------------------------------------------------------------------------------------------------------------------------------------------------------
當我們在用arcgis server 構建切片時,我們會發現在緩存生成的conf.xml中有這樣的片段:
在上述片段中<LODInfo>代表了每一級切片的信息,<LevelID>代表切片的級數。
在這里,<Scale>代表比例尺。比例尺是表示圖上距離比實地距離縮小的程度,也叫縮尺。公式為:比例尺=圖上距離/實地距離。用數字的比例式或分數式表示比例尺的大小。例如地圖上1厘米代表實地距離500千米,可寫成:1∶50,000,000或寫成:1/50,000,000。
<Resolution>,代表分辨率。Resolution 的實際含義代表當前地圖范圍內,1像素代表多少地圖單位(X地圖單位/像素),地圖單位取決於數據本身的空間參考。
當我們在進行Web API的開發時,經常會碰到根據Resolution來縮放地圖的情況。但是實際需求中我們更需要根據Scale來縮放,因此就涉及到Scale和Resolution的轉換。
Resolution和Scale的轉換算法:
Resolution跟dpi有關,跟地圖的單位有關。(dpi代表每英寸的像素數)
Resolution和Scale的轉換算法
舉例:
案例一:如果地圖的坐標單位是米, dpi為96
1英寸= 2.54厘米;
1英寸=96像素;
最終換算的單位是米;
如果當前地圖比例尺為1: 125000000,則代表圖上1米實地125000000米;
米和像素間的換算公式:
1英寸=0.0254米=96像素
1像素=0.0254/96 米
則根據1:125000000比例尺,圖上1像素代表實地距離是125000000*0.0254/96 = 33072.9166666667米。我們這個換算結果和切片的結果略微有0.07米的誤差。這個誤差產生的原因是英寸換算厘米的參數決定的,server使用的換算參數1英寸約等於0.0254000508米。
案例二:如果地理坐標系是wgs84,地圖的單位是度,dpi為96
Server中度和米之間的換算參數:
1度約等於 111194.872221777米
接下來就需要進行度和像素間的換算:
當比例尺為1:64000000米時,相當於1像素 = 64000000*0.0254000508/96 = 16933.3672米
再將米轉換為度 16933.3672/111194.872221777 = 0.1522855043731385度
因此當地圖單位為度時,近似計算在1:64000000 對應的Resolution為0.1522855043731385度
驗證結果:
-----------------------------------------------------------------------------------------------------------------------
double resolution = scale * 0.0254000508/96/111194.872221777;