[WEBGIS] JS_TS实现WGS84_GCJ02_BD09坐标转换

369 阅读2分钟

1 坐标系介绍

  1. WGS84

1984世界大地测量坐标系,是一种用于地图学、大地测量学和导航(包括全球定位系统)的大地测量系统标准。 **应用:**一般的终端设备,如果自己有定位功能,比如车载系统,他们发出的gps坐标就是原始坐标(WGS84),如:Web开发过程中浏览器调用原生H5的geolocation api返回的定位坐标即为WGS84坐标。

  1. GCJ02

中国国测局制定的地理坐标系统,是由WGS84加密后得到的坐标系,又称火星坐标系。 **应用:**高德、腾讯地图所采用的坐标系就是GCJ02。高德、腾讯定位api返回的坐标都是GCJ02坐标。

  1. BD09

百度在GCJ02基础上做二次加密制订的坐标系统。其中BD09ll表示百度经纬度坐标(以下坐标转换过程中转换的是BD09ll,用BD09简写表示),BD09mc表示百度墨卡托米制坐标。 **应用:**百度地图所采用的坐标系就是BD09。百度定位api返回的坐标是BD09坐标。

2 WGS84转GCJ02

export const wgsToGcj: CoordTransformFcn = ([longitude, latitude]) => {
  // 必要常量
  // PI
  const PI = Math.PI;
  // 克拉索索夫斯基椭球长半轴长,也叫赤道半径,a
  const a = 6378245.0;
  // 克拉索索夫斯基椭球第一偏心率的平方,e^2
  const ee = 0.00669342162296594323;

  if (outOfChina([longitude, latitude])) {
    return [longitude, latitude];
  }
  let adjustLat = transformLatWithXY(longitude - 105.0, latitude - 35.0);
  let adjustLon = transformLonWithXY(longitude - 105.0, latitude - 35.0);
  const radLat = (latitude / 180.0) * PI;
  let magic = Math.sin(radLat);
  magic = 1 - ee * magic * magic;
  const sqrtMagic = Math.sqrt(magic);
  adjustLat =
    (adjustLat * 180.0) / (((a * (1 - ee)) / (magic * sqrtMagic)) * PI);
  adjustLon = (adjustLon * 180.0) / ((a / sqrtMagic) * Math.cos(radLat) * PI);
  return [longitude + adjustLon, latitude + adjustLat];
};

WGS84转GCJ02算法主要分为3部分:

  1. 计算坐标点与中国大地原点的偏移
  2. 经纬度做正余弦多项式计算(transformLatWithXYtransformLonWithXY
  3. 利用辅助公式计算偏移量

在做WGS84转GCJ02算法时首先要判断是否在中国区域内,GCJ02坐标只对中国区域内的坐标进行加密加偏。

const outOfChina = ([lon, lat]: CoordArr): boolean => !(lon > 72.004 && lon < 137.8347 && lat > 0.8293 && lat < 55.8271);

正余弦多项式计算公式(transformLatWithXYtransformLonWithXY):

const transformLatWithXY = (x: number, y: number) => {
  const PI = Math.PI;
  let lat =
    -100.0 +
    2.0 * x +
    3.0 * y +
    0.2 * y * y +
    0.1 * x * y +
    0.2 * Math.sqrt(Math.abs(x));
  lat +=
    ((20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0) /
    3.0;
  lat +=
    ((20.0 * Math.sin(y * PI) + 40.0 * Math.sin((y / 3.0) * PI)) * 2.0) / 3.0;
  lat +=
    ((160.0 * Math.sin((y / 12.0) * PI) + 320 * Math.sin((y * PI) / 30.0)) *
     2.0) /
    3.0;
  return lat;
};

const transformLonWithXY = (x: number, y: number) => {
  const PI = Math.PI;
  let lon =
    300.0 +
    x +
    2.0 * y +
    0.1 * x * x +
    0.1 * x * y +
    0.1 * Math.sqrt(Math.abs(x));
  lon +=
    ((20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0) /
    3.0;
  lon +=
    ((20.0 * Math.sin(x * PI) + 40.0 * Math.sin((x / 3.0) * PI)) * 2.0) / 3.0;
  lon +=
    ((150.0 * Math.sin((x / 12.0) * PI) + 300.0 * Math.sin((x / 30.0) * PI)) *
     2.0) /
    3.0;
  return lon;
};

3 GCJ02转BD09

export const gcjToBd: CoordTransformFcn = ([longitude, latitude]) => {
  // 必要常量
  const X_PI = (Math.PI * 3000.0) / 180.0;

  const z =
    Math.sqrt(longitude * longitude + latitude * latitude) +
    0.00002 * Math.sin(latitude * X_PI);
  const theta =
    Math.atan2(latitude, longitude) + 0.000003 * Math.cos(longitude * X_PI);
  const a_latitude = z * Math.sin(theta) + 0.006;
  const a_longitude = z * Math.cos(theta) + 0.0065;

  return [a_longitude, a_latitude];
};

4 BD09转GCJ02

export const bdToGcj: CoordTransformFcn = ([longitude, latitude]) => {
  const X_PI = (Math.PI * 3000.0) / 180.0;

  const x = longitude - 0.0065;
  const y = latitude - 0.006;
  const z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * X_PI);
  const theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * X_PI);
  const a_latitude = z * Math.sin(theta);
  const a_longitude = z * Math.cos(theta);

  return [a_longitude, a_latitude];
};

5 WGS84转BD09

export const wgsToBd: CoordTransformFcn = (coord) => gcjToBd(wgsToGcj(coord));

文章参考:

blog.csdn.net/a1352675847… blog.csdn.net/weixin_5159… blog.csdn.net/feinifi/art… juejin.cn/post/699064…