地图瓦片编号与经纬度的换算关系

 

芒果香蕉_关注

0.432021.02.23 10:49:57字数 563阅读 611


前言

地图瓦片编号与与经纬度坐标之间的转换与简单理解。相关资料看了好多次,每次看完就忘,这里做一个简单的学习笔记。

Web墨卡托投影

通常提到Web墨卡托投影,我最先想到的关键词是: “3857”、“谷歌地图”。再往深了想就是“正轴等角圆柱投影”、“越靠近两极变形越大”等特性。以前对“越靠近两极变形越大”的理解是:越靠近两极,地图横向拉伸越严重。今天查资料时突然意识到一点:越靠近两级纵向拉伸同样越严重。也就是说纬度分布是不均匀的

墨卡托投影示意图

 

地图瓦片分割

目前接触的绝大多数地图瓦片是以左上角为原点开始编号的(TMS 瓦片除外)。从左至右为 x 轴, 从上到下为 y轴。

 

对于整个地球而言,基于 Web 墨卡托投影的地图左上角经纬度坐标为(180°,85.0511 °),右下角经纬度为(-180°,-85.0511°)。纬度范围是[-85.0511, 85.0511 ] 的原因是:保证整个地图为正方形。85.0511 这个数字是由如下公式得到:

 

公式的具体推导过程见:https://en.wikipedia.org/wiki/Mercator_projection

ArcGIS JS API 加载 TMS 地图瓦片 这篇笔记中提到过缩放等级 z 和每行(或每列)瓦片数量 n 的关系如下:

 

由上述可知投影后地图经度范围是[-180, 180],在第 z 级别每行的瓦片数为 n。那么等级 z 下某一经度对应的 x 轴编号为:

 
其中 lon 的单位为度。

而等级 z 下某一纬度对应的 y 轴编号则比较复杂(因为纬度分布不均匀):

 

其中 lat 的单位为弧度制。

已知瓦片编号反算该瓦片左上角经纬度坐标公式如下:

 
 

相应转换代码如下:
JS:

// 经纬度转瓦片编号
function lon2tile(lon,zoom) { 
  return (Math.floor((lon+180)/360*Math.pow(2,zoom))); 
}
 function lat2tile(lat,zoom)  { 
   return (Math.floor((1-Math.log(Math.tan(lat*Math.PI/180) + 1/Math.cos(lat*Math.PI/180))/Math.PI)/2 *Math.pow(2,zoom))); 
}

// 瓦片编号转经纬度
 function tile2long(x,z) {
  return (x/Math.pow(2,z)*360-180);
 }
 function tile2lat(y,z) {
  var n=Math.PI-2*Math.PI*y/Math.pow(2,z);
  return (180/Math.PI*Math.atan(0.5*(Math.exp(n)-Math.exp(-n))));
 }

JAVA:

public class slippytest {
 public static void main(String[] args) {
   int zoom = 10;
   double lat = 47.968056d;
   double lon = 7.909167d;
   System.out.println("https://tile.openstreetmap.org/" + getTileNumber(lat, lon, zoom) + ".png");
 }
 public static String getTileNumber(final double lat, final double lon, final int zoom) {
   int xtile = (int)Math.floor( (lon + 180) / 360 * (1<<zoom) ) ;
   int ytile = (int)Math.floor( (1 - Math.log(Math.tan(Math.toRadians(lat)) + 1 / Math.cos(Math.toRadians(lat))) / Math.PI) / 2 * (1<<zoom) ) ;
    if (xtile < 0)
     xtile=0;
    if (xtile >= (1<<zoom))
     xtile=((1<<zoom)-1);
    if (ytile < 0)
     ytile=0;
    if (ytile >= (1<<zoom))
     ytile=((1<<zoom)-1);
    return("" + zoom + "/" + xtile + "/" + ytile);
   }
 }

class BoundingBox {
    double north;
    double south;
    double east;
    double west;   
  }
  BoundingBox tile2boundingBox(final int x, final int y, final int zoom) {
    BoundingBox bb = new BoundingBox();
    bb.north = tile2lat(y, zoom);
    bb.south = tile2lat(y + 1, zoom);
    bb.west = tile2lon(x, zoom);
    bb.east = tile2lon(x + 1, zoom);
    return bb;
  }

  static double tile2lon(int x, int z) {
     return x / Math.pow(2.0, z) * 360.0 - 180;
  }

  static double tile2lat(int y, int z) {
    double n = Math.PI - (2.0 * Math.PI * y) / Math.pow(2.0, z);
    return Math.toDegrees(Math.atan(Math.sinh(n)));
  }

 

4人点赞

 

GIS笔记

 

  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值