GIS理论(墨卡托投影、地理坐标系、地面分辨率、地图比例尺、Bing Maps Tile System)

墨卡托投影(Mercator Projection),又名“等角正轴圆柱投影”,荷兰地图学家墨卡托(Mercator)在1569年拟定,假设地球被围在一个中空的圆柱里,其赤道与圆柱相接触,然后再假想地球中心有一盏灯,把球面上的图形投影到圆柱体上,再把圆柱体展开,这就是一幅标准纬线为零度(即赤道)的“墨卡托投影”绘制出的世界地图。

一、墨卡托投影坐标系(Mercator Projection)
墨卡托投影以整个世界范围,赤道作为标准纬线,本初子午线作为中央经线,两者交点为坐标原点,向东向北为正,向西向南为负。南北极在地图的正下、上方,而东西方向处于地图的正右、左。 
由于Mercator Projection在两极附近是趋于无限值得,因此它并没完整展现了整个世界,地图上最高纬度是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)。

二、地理坐标系(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(pixelper 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相关算法:

   
   
   
   
1 using System; 2   using System.Text; 3 4 namespace Microsoft.MapPoint 5 { 6 static class TileSystem 7 { 8 private const double EarthRadius = 6378137 ; 9 private const double MinLatitude = - 85.05112878 ; 10 private const double MaxLatitude = 85.05112878 ; 11 private const double MinLongitude = - 180 ; 12 private const double MaxLongitude = 180 ; 13 14 15 /// <summary> 16 /// Clips a number to the specified minimum and maximum values. 17 /// </summary> 18 /// <param name="n"> The number to clip. </param> 19 /// <param name="minValue"> Minimum allowable value. </param> 20 /// <param name="maxValue"> Maximum allowable value. </param> 21 /// <returns> The clipped value. </returns> 22 private static double Clip( double n, double minValue, double maxValue) 23 { 24 return Math.Min(Math.Max(n, minValue), maxValue); 25 } 26 27 28 29 /// <summary> 30 /// Determines the map width and height (in pixels) at a specified level 31 /// of detail. 32 /// </summary> 33 /// <param name="levelOfDetail"> Level of detail, from 1 (lowest detail) 34 /// to 23 (highest detail). </param> 35 /// <returns> The map width and height in pixels. </returns> 36 public static uint MapSize( int levelOfDetail) 37 { 38 return ( uint ) 256 << levelOfDetail; 39 } 40 41 42 43 /// <summary> 44 /// Determines the ground resolution (in meters per pixel) at a specified 45 /// latitude and level of detail. 46 /// </summary> 47 /// <param name="latitude"> Latitude (in degrees) at which to measure the 48 /// ground resolution. </param> 49 /// <param name="levelOfDetail"> Level of detail, from 1 (lowest detail) 50 /// to 23 (highest detail). </param> 51 /// <returns> The ground resolution, in meters per pixel. </returns> 52 public static double GroundResolution( double latitude, int levelOfDetail) 53 { 54 latitude = Clip(latitude, MinLatitude, MaxLatitude); 55 return Math.Cos(latitude * Math.PI / 180 ) * 2 * Math.PI * EarthRadius / MapSize(levelOfDetail); 56 } 57 58 59 60 /// <summary> 61 /// Determines the map scale at a specified latitude, level of detail, 62 /// and screen resolution. 63 /// </summary> 64 /// <param name="latitude"> Latitude (in degrees) at which to measure the 65 /// map scale. </param> 66 /// <param name="levelOfDetail"> Level of detail, from 1 (lowest detail) 67 /// to 23 (highest detail). </param> 68 /// <param name="screenDpi"> Resolution of the screen, in dots per inch. </param> 69 /// <returns> The map scale, expressed as the denominator N of the ratio 1 : N. </returns> 70 public static double MapScale( double latitude, int levelOfDetail, int screenDpi) 71 { 72 return GroundResolution(latitude, levelOfDetail) * screenDpi / 0.0254 ; 73 } 74 75 76 77 /// <summary> 78 /// Converts a point from latitude/longitude WGS-84 coordinates (in degrees) 79 /// into pixel XY coordinates at a specified level of detail. 80 /// </summary> 81 /// <param name="latitude"> Latitude of the point, in degrees. </param> 82 /// <param name="longitude"> Longitude of the point, in degrees. </param> 83 /// <param name="levelOfDetail"> Level of detail, from 1 (lowest detail) 84 /// to 23 (highest detail). </param> 85 /// <param name="pixelX"> Output parameter receiving the X coordinate in pixels. </param> 86 /// <param name="pixelY"> Output parameter receiving the Y coordinate in pixels. </param> 87 public static void LatLongToPixelXY( double latitude, double longitude, int levelOfDetail, out int pixelX, out int pixelY) 88 { 89 latitude = Clip(latitude, MinLatitude, MaxLatitude); 90 longitude = Clip(longitude, MinLongitude, MaxLongitude); 91 92 double x = (longitude + 180 ) / 360 ; 93 double sinLatitude = Math.Sin(latitude * Math.PI / 180 ); 94 double y = 0.5 - Math.Log(( 1 + sinLatitude) / ( 1 - sinLatitude)) / ( 4 * Math.PI); 95 96 uint mapSize = MapSize(levelOfDetail); 97 pixelX = ( int ) Clip(x * mapSize + 0.5 , 0 , mapSize - 1 ); 98 pixelY = ( int ) Clip(y * mapSize + 0.5 , 0 , mapSize - 1 ); 99 } 100 101 102 103 /// <summary> 104 /// Converts a pixel from pixel XY coordinates at a specified level of detail 105 /// into latitude/longitude WGS-84 coordinates (in degrees). 106 /// </summary> 107 /// <param name="pixelX"> X coordinate of the point, in pixels. </param> 108 /// <param name="pixelY"> Y coordinates of the point, in pixels. </param> 109 /// <param name="levelOfDetail"> Level of detail, from 1 (lowest detail) 110 /// to 23 (highest detail). </param> 111 /// <param name="latitude"> Output parameter receiving the latitude in degrees. </param> 112 /// <param name="longitude"> Output parameter receiving the longitude in degrees. </param> 113 public static void PixelXYToLatLong( int pixelX, int pixelY, int levelOfDetail, out double latitude, out double longitude) 114 { 115 double mapSize = MapSize(levelOfDetail); 116 double x = (Clip(pixelX, 0 , mapSize - 1 ) / mapSize) - 0.5 ; 117 double y = 0.5 - (Clip(pixelY, 0 , mapSize - 1 ) / mapSize); 118 119 latitude = 90 - 360 * Math.Atan(Math.Exp( - y * 2 * Math.PI)) / Math.PI; 120 longitude = 360 * x; 121 } 122 123 124 125 /// <summary> 126 /// Converts pixel XY coordinates into tile XY coordinates of the tile containing 127 /// the specified pixel. 128 /// </summary> 129 /// <param name="pixelX"> Pixel X coordinate. </param> 130 /// <param name="pixelY"> Pixel Y coordinate. </param> 131 /// <param name="tileX"> Output parameter receiving the tile X coordinate. </param> 132 /// <param name="tileY"> Output parameter receiving the tile Y coordinate. </param> 133 public static void PixelXYToTileXY( int pixelX, int pixelY, out int tileX, out int tileY) 134 { 135 tileX = pixelX / 256 ; 136 tileY = pixelY / 256 ; 137 } 138 139 140 141 /// <summary> 142 /// Converts tile XY coordinates into pixel XY coordinates of the upper-left pixel 143 /// of the specified tile. 144 /// </summary> 145 /// <param name="tileX"> Tile X coordinate. </param> 146 /// <param name="tileY"> Tile Y coordinate. </param> 147 /// <param name="pixelX"> Output parameter receiving the pixel X coordinate. </param> 148 /// <param name="pixelY"> Output parameter receiving the pixel Y coordinate. </param> 149 public static void TileXYToPixelXY( int tileX, int tileY, out int pixelX, out int pixelY) 150 { 151 pixelX = tileX * 256 ; 152 pixelY = tileY * 256 ; 153 } 154 155 156 157 /// <summary> 158 /// Converts tile XY coordinates into a QuadKey at a specified level of detail. 159 /// </summary> 160 /// <param name="tileX"> Tile X coordinate. </param> 161 /// <param name="tileY"> Tile Y coordinate. </param> 162 /// <param name="levelOfDetail"> Level of detail, from 1 (lowest detail) 163 /// to 23 (highest detail). </param> 164 /// <returns> A string containing the QuadKey. </returns> 165 public static string TileXYToQuadKey( int tileX, int tileY, int levelOfDetail) 166 { 167 StringBuilder quadKey = new StringBuilder(); 168 for ( int i = levelOfDetail; i > 0 ; i -- ) 169 { 170 char digit = ' 0 ' ; 171 int mask = 1 << (i - 1 ); 172 if ((tileX & mask) != 0 ) 173 { 174 digit ++ ; 175 } 176 if ((tileY & mask) != 0 ) 177 { 178 digit ++ ; 179 digit ++ ; 180 } 181 quadKey.Append(digit); 182 } 183 return quadKey.ToString(); 184 } 185 186 187 188 /// <summary> 189 /// Converts a QuadKey into tile XY coordinates. 190 /// </summary> 191 /// <param name="quadKey"> QuadKey of the tile. </param> 192 /// <param name="tileX"> Output parameter receiving the tile X coordinate. </param> 193 /// <param name="tileY"> Output parameter receiving the tile Y coordinate. </param> 194 /// <param name="levelOfDetail"> Output parameter receiving the level of detail. </param> 195 public static void QuadKeyToTileXY( string quadKey, out int tileX, out int tileY, out int levelOfDetail) 196 { 197 tileX = tileY = 0 ; 198 levelOfDetail = quadKey.Length; 199 for ( int i = levelOfDetail; i > 0 ; i -- ) 200 { 201 int mask = 1 << (i - 1 ); 202 switch (quadKey[levelOfDetail - i]) 203 { 204 case ' 0 ' : 205 break ; 206 207 case ' 1 ' : 208 tileX |= mask; 209 break ; 210 211 case ' 2 ' : 212 tileY |= mask; 213 break ; 214 215 case ' 3 ' : 216 tileX |= mask; 217 tileY |= mask; 218 break ; 219 220 default : 221 throw new ArgumentException( " Invalid QuadKey digit sequence. " ); 222 } 223 } 224 } 225 } 226 }
复制代码

你可能感兴趣的:(数据库,优化,String,System,bing,磁盘)