您的位置:首页 > 编程语言 > Go语言

也说Google卫星地图的URL地址的qrts编码算法

2009-04-19 23:07 302 查看
今天看到有人已经在讨论如何获取google卫星图片,见http://www.cnblogs.com/tangf/archive/2006/07/23/457902.html?login=1&CommentID=1507040#Post的一篇博客,里面抄了JavaScript和delphi的代码过来,我在此再重抄一遍免得大家找来找去。

算法思想如下:Google卫星图片服务器,由不同层次的256x256大小的jpeg图片无缝拼接而成,其编码方式是按照qrst编码方法进行索引:

zoom=1时,全球只有一个256x256的图片,它的中心经纬度为(0,0),其URL为“http://kh.google.com/kh?v=3&t=t”(大家可以把改URL复制到浏览器看下),其范围是地球按等角纵切圆柱投影后,左右为从西径180度到东径180度,上下范围为从南180度到北180度(这里并不是完全按地球南北只有90度进行划分),中点为赤道与中央子午线的交点,其编码为t。
当zoom=2时进行第二级编码,即从中点开始上下左右从中分成相等的四等份,从左上开始按顺时针方向分别编为左上q,右上r,右下s,左下t,每一块的编码就为:tq,tr,ts,tt。
依此类推,每增大一级编码,就放大一倍,每一块都从中分为四块进行下一级编码,编码在前组编码的基础上再分别加上q,r,s,t。即一级编码由一个字母组成,二级编码由两个字母组成,三级由三个字母组成,其它级依次类推,不同地区提供下载的图层级数不尽相同,最多可分到21级。
对于全球全部卫片的管理来讲,这种编码方法是最好的,是典型的金字塔索引编码方法,采用这种编码要得到某一个图块编号时,看似要对编号进行字串生成计算,其实不然,这种编码方法对于编号是可以用数值加或减就能直接计算,然后做一个数值与字符的转换即可。这对于查找、平移、定位等操作非常快速方便。

JavaScript代码如下:

1function GetQuadtreeURL_(lon, lat)
2function getSatURL(zoom: integer; X, Y: double): string;
2var
3 wx, wy, cx, cy: double;
4 tid: string;
5 i: integer;
6begin
7 cx := 0;
8 cy := 0;
9 wx := 180;
10 wy := 180;
11 tid := 't';
12
13 for i := 1 to zoom-1 do
14 begin
15 if (x >= cx) and (y >= cy) then
16 begin
17 tid := tid + 'r';
18 cx := cx + wx / 2;
19 cy := cy + wy / 2;
20 end
21 else if (x >= cx) and (y < cy) then
22 begin
23 tid := tid + 's';
24 cx := cx + wx / 2;
25 cy := cy - wy / 2;
26 end
27 else if (x < cx) and (y < cy) then
28 begin
29 tid := tid + 't';
30 cx := cx - wx / 2;
31 cy := cy - wy / 2;
32 end
33 else
34 begin
35 tid := tid + 'q';
36 cx := cx - wx / 2;
37 cy := cy + wy / 2;
38 end;
39 wx := wx / 2;
40 wy := wy / 2;
41 end;
42 result := 'http://kh.google.com/kh?v=2&t=' + tid;
43end;
C#代码如下:

1 public string getSatTileId(int row, int col, int zoom)
9//根据google地图的字符地址得到经纬度的度值,Sexagesimal:60进制
2function GoogleQRSTtoDegree(x)
3{
4 // 23° 27′ 30"
5 var ret = "";
6 if (x < 0)
7 {
8 ret += '-';
9 x = -x;
10 }
11 ret += Math.floor(x);
12 ret += '°';
13
14 x = (x - Math.floor(x)) * 60;
15 ret += Math.floor(x);
16 ret += "'";
17
18 x = (x - Math.floor(x)) * 60;
19 ret += Math.floor(x);
20 ret += '"';
21
22 return ret;
23}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: