Skip to content

Commit

Permalink
feat(utils): 新增 GeoCoord 地理坐标系转换工具
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Mar 7, 2022
1 parent 8a833f8 commit b87f3bb
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
"date-fns": "^2.24.0",
"decimal.js-light": "^2.5.1",
"fast-xml-parser": "^3.18.0",
"gcoord": "^0.3.2",
"ioredis": "^4.22.0",
"lodash-uni": "^1.1.0",
"nanoid": "^3.1.20",
Expand Down
12 changes: 12 additions & 0 deletions src/utils/GeoCoord.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { GeoCoord } from './GeoCoord'

describe('GeoCoord', () => {
test('ok', () => {
expect(
GeoCoord.WGS84ToGCJ02({
longitude: 238,
latitude: 34,
}),
).toMatchSnapshot()
})
})
87 changes: 87 additions & 0 deletions src/utils/GeoCoord.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import gcoord from 'gcoord'
import type { CRSTypes } from 'gcoord/dist/types/crs/index'

export interface GeoCoordTransformInput {
/**
* 经度。
*/
longitude: number

/**
* 纬度。
*/
latitude: number
}

export interface GeoCoordTransformOutput {
/**
* 经度。
*/
longitude: number

/**
* 纬度。
*/
latitude: number
}

const makeTransformer =
(from: CRSTypes, to: CRSTypes) =>
(input: GeoCoordTransformInput): GeoCoordTransformOutput => {
const res = gcoord.transform([input.longitude, input.latitude], from, to)
return {
longitude: res[0],
latitude: res[1],
}
}

/**
* 地理坐标系转换工具。
*
* - `大地坐标系(WGS84 坐标系)`: GPS 全球卫星定位系统使用的坐标系;
* - `火星坐标系(GCJ02 坐标系)`: 腾讯地图、高德地图等使用的坐标系,是由中国国家测绘局制定的由 WGS84 加密后得到的坐标系;
* - `百度坐标系(BD09 坐标系)`: 百度地图使用的坐标系,是在 GCJ02 基础上再次加密得到的坐标系。
*/
export class GeoCoord {
/**
* `WGS84 坐标系` 转 `GCJ02 坐标系`。
*
* 应用场景:GPS 坐标转腾讯地图、高德地图坐标。
*/
static WGS84ToGCJ02 = makeTransformer(gcoord.WGS84, gcoord.GCJ02)

/**
* `WGS84 坐标系` 转 `BD09 坐标系`。
*
* 应用场景:GPS 坐标转百度地图坐标。
*/
static WGS84ToBD09 = makeTransformer(gcoord.WGS84, gcoord.BD09)

/**
* `GCJ02 坐标系` 转 `WGS84 坐标系`。
*
* 应用场景:腾讯地图、高德地图坐标转 GPS 坐标。
*/
static GCJ02ToWGS84 = makeTransformer(gcoord.GCJ02, gcoord.WGS84)

/**
* `GCJ02 坐标系` 转 `BD09 坐标系`。
*
* 应用场景:腾讯地图、高德地图坐标转百度地图坐标。
*/
static GCJ02ToBD09 = makeTransformer(gcoord.GCJ02, gcoord.BD09)

/**
* `BD09 坐标系` 转 `WGS84 坐标系`。
*
* 应用场景:百度地图坐标转 GPS 坐标。
*/
static BD09ToWGS84 = makeTransformer(gcoord.BD09, gcoord.WGS84)

/**
* `BD09 坐标系` 转 `GCJ02 坐标系`。
*
* 应用场景:百度地图坐标转腾讯地图、高德地图坐标。
*/
static BD09ToGCJ02 = makeTransformer(gcoord.BD09, gcoord.GCJ02)
}
8 changes: 8 additions & 0 deletions src/utils/__snapshots__/GeoCoord.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`GeoCoord ok 1`] = `
Object {
"latitude": 34,
"longitude": 238,
}
`;
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export * from './devOrProd'
export * from './EventBus'
export * from './formatBytes'
export * from './formatNumber'
export * from './GeoCoord'
export * from './getCurrentScript'
export * from './getEnvironment'
export * from './getWechatPublicAccountQrcodeUrl'
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4495,6 +4495,11 @@ functional-red-black-tree@^1.0.1:
resolved "https://registry.npm.taobao.org/functional-red-black-tree/download/functional-red-black-tree-1.0.1.tgz#1b0ab3bd553b2a0d6399d29c0e3ea0b252078327"
integrity sha1-GwqzvVU7Kg1jmdKcDj6gslIHgyc=

gcoord@^0.3.2:
version "0.3.2"
resolved "https://registry.npmmirror.com/gcoord/-/gcoord-0.3.2.tgz#57efe1535e1fe79095d0b2166600941c33438d42"
integrity sha512-LwhA+ev6fBXadAFprI+Q593TTC1QYa0Quewd3YUT51wQS2S6gekrC1YCQlJhKA8HpqCF+dX+fBcX/lkYM5Kgog==

generic-names@^2.0.1:
version "2.0.1"
resolved "https://registry.npm.taobao.org/generic-names/download/generic-names-2.0.1.tgz?cache=0&sync_timestamp=1603542435591&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fgeneric-names%2Fdownload%2Fgeneric-names-2.0.1.tgz#f8a378ead2ccaa7a34f0317b05554832ae41b872"
Expand Down

0 comments on commit b87f3bb

Please sign in to comment.