Skip to content

Commit

Permalink
refactor: unified code style
Browse files Browse the repository at this point in the history
  • Loading branch information
hujiulong committed Dec 27, 2019
1 parent 77f5d82 commit b4fb39d
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 97 deletions.
8 changes: 8 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module.exports = {
extends: ['airbnb-typescript/base'],
rules: {
'no-continue': 'off',
'no-mixed-operators': 'off',
'no-plusplus': 'off',
'no-nested-ternary': 'off',
'consistent-return': 'off',
'max-len': 'off'
}
};
49 changes: 24 additions & 25 deletions src/crs/BD09MC.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Position } from "../geojson";
import { Position } from '../geojson';

const { abs, pow } = Math;
const { abs } = Math;

const MCBAND = [
12890594.86,
8362377.87,
5591021,
3481989.83,
1678043.12,
0
0,
];
const LLBAND = [75, 60, 45, 30, 15, 0];
const MC2LL = [
Expand All @@ -22,7 +22,7 @@ const MC2LL = [
-23.38765649603339,
2.57121317296198,
-0.03801003308653,
17337981.2
17337981.2,
],
[
-7.435856389565537e-9,
Expand All @@ -34,7 +34,7 @@ const MC2LL = [
47.40033549296737,
-16.50741931063887,
2.28786674699375,
10260144.86
10260144.86,
],
[
-3.030883460898826e-8,
Expand All @@ -46,7 +46,7 @@ const MC2LL = [
13.45380521110908,
-3.29883767235584,
0.32710905363475,
6856817.37
6856817.37,
],
[
-1.981981304930552e-8,
Expand All @@ -58,7 +58,7 @@ const MC2LL = [
0.85341911805263,
0.12923347998204,
-0.04625736007561,
4482777.06
4482777.06,
],
[
3.09191371068437e-9,
Expand All @@ -70,7 +70,7 @@ const MC2LL = [
-0.00663494467273,
0.03430082397953,
-0.00466043876332,
2555164.4
2555164.4,
],
[
2.890871144776878e-9,
Expand All @@ -82,8 +82,8 @@ const MC2LL = [
-0.00001234426596,
0.00010322952773,
-0.00000323890364,
826088.5
]
826088.5,
],
];
const LL2MC = [
[
Expand All @@ -96,7 +96,7 @@ const LL2MC = [
26595700718403920,
-10725012454188240,
1800819912950474,
82.5
82.5,
],
[
0.0008277824516172526,
Expand All @@ -108,7 +108,7 @@ const LL2MC = [
12053065338.62167,
-5124939663.577472,
913311935.9512032,
67.5
67.5,
],
[
0.00337398766765,
Expand All @@ -120,7 +120,7 @@ const LL2MC = [
97236711.15602145,
-43661946.33752821,
8477230.501135234,
52.5
52.5,
],
[
0.00220636496208,
Expand All @@ -132,7 +132,7 @@ const LL2MC = [
1340652.697009075,
-620943.6990984312,
144416.9293806241,
37.5
37.5,
],
[
-0.0003441963504368392,
Expand All @@ -144,7 +144,7 @@ const LL2MC = [
9540.606633304236,
-2710.55326746645,
1405.483844121726,
22.5
22.5,
],
[
-0.0003218135878613132,
Expand All @@ -156,22 +156,21 @@ const LL2MC = [
1.58060784298199,
8.77738589078284,
0.37238884252424,
7.45
]
7.45,
],
];

function transform(x: number, y: number, factors: number[]): Position {
const cc = abs(y) / factors[9];

let xt = factors[0] + factors[1] * abs(x);
let yt =
factors[2] +
factors[3] * cc +
factors[4] * pow(cc, 2) +
factors[5] * pow(cc, 3) +
factors[6] * pow(cc, 4) +
factors[7] * pow(cc, 5) +
factors[8] * pow(cc, 6);
let yt = factors[2]
+ factors[3] * cc
+ factors[4] * (cc ** 2)
+ factors[5] * (cc ** 3)
+ factors[6] * (cc ** 4)
+ factors[7] * (cc ** 5)
+ factors[8] * (cc ** 6);

xt *= x < 0 ? -1 : 1;
yt *= y < 0 ? -1 : 1;
Expand Down
69 changes: 35 additions & 34 deletions src/crs/GCJ02.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,10 @@ const {
const a = 6378245;
const ee = 0.006693421622965823;

export function GCJ02ToWGS84(coord: Position): Position {
const [lon, lat] = coord;

if (!isInChinaBbox(lon, lat)) return [lon, lat];

let [wgsLon, wgsLat] = [lon, lat];

let tempPoint = WGS84ToGCJ02([wgsLon, wgsLat]);

let dx = tempPoint[0] - lon;
let dy = tempPoint[1] - lat;

while (abs(dx) > 1e-6 || abs(dy) > 1e-6) {
wgsLon -= dx;
wgsLat -= dy;

tempPoint = WGS84ToGCJ02([wgsLon, wgsLat]);
dx = tempPoint[0] - lon;
dy = tempPoint[1] - lat;
}

return [wgsLon, wgsLat];
}

export function WGS84ToGCJ02(coord: Position): Position {
const [lon, lat] = coord;

if (!isInChinaBbox(lon, lat)) return [lon, lat];

const d = delta(lon, lat);

return [lon + d[0], lat + d[1]];
// roughly check whether coordinates are in China.
function isInChinaBbox(lon: number, lat: number): boolean {
return lon >= 72.004 && lon <= 137.8347 && lat >= 0.8293 && lat <= 55.8271;
}

function transformLat(x: number, y: number): number {
Expand Down Expand Up @@ -73,7 +45,36 @@ function delta(lon: number, lat: number): number[] {
return [dLon, dLat];
}

// roughly check whether coordinates are in China.
function isInChinaBbox(lon: number, lat: number): boolean {
return lon >= 72.004 && lon <= 137.8347 && lat >= 0.8293 && lat <= 55.8271;
export function WGS84ToGCJ02(coord: Position): Position {
const [lon, lat] = coord;

if (!isInChinaBbox(lon, lat)) return [lon, lat];

const d = delta(lon, lat);

return [lon + d[0], lat + d[1]];
}

export function GCJ02ToWGS84(coord: Position): Position {
const [lon, lat] = coord;

if (!isInChinaBbox(lon, lat)) return [lon, lat];

let [wgsLon, wgsLat] = [lon, lat];

let tempPoint = WGS84ToGCJ02([wgsLon, wgsLat]);

let dx = tempPoint[0] - lon;
let dy = tempPoint[1] - lat;

while (abs(dx) > 1e-6 || abs(dy) > 1e-6) {
wgsLon -= dx;
wgsLat -= dy;

tempPoint = WGS84ToGCJ02([wgsLon, wgsLat]);
dx = tempPoint[0] - lon;
dy = tempPoint[1] - lat;
}

return [wgsLon, wgsLat];
}
10 changes: 5 additions & 5 deletions src/crs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const BD09: CRS = {
WGS84: compose(GCJ02ToWGS84, BD09ToGCJ02),
GCJ02: BD09ToGCJ02,
EPSG3857: compose(WGS84ToEPSG3857, GCJ02ToWGS84, BD09ToGCJ02),
BD09MC: BD09toBD09MC
BD09MC: BD09toBD09MC,
},
};

Expand All @@ -51,14 +51,14 @@ const BD09MC: CRS = {
WGS84: compose(GCJ02ToWGS84, BD09ToGCJ02, BD09MCtoBD09),
GCJ02: compose(BD09ToGCJ02, BD09MCtoBD09),
EPSG3857: compose(WGS84ToEPSG3857, GCJ02ToWGS84, BD09ToGCJ02, BD09MCtoBD09),
BD09: BD09MCtoBD09
}
}
BD09: BD09MCtoBD09,
},
};

export {
WGS84,
GCJ02,
BD09,
EPSG3857,
BD09MC
BD09MC,
};
28 changes: 14 additions & 14 deletions src/geojson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
* The valid values for the 'type' property of GeoJSON geometry objects.
*/
export type GeometryTypes = 'Point' |
'LineString' |
'Polygon' |
'MultiPoint' |
'MultiLineString' |
'MultiPolygon' |
'GeometryCollection';
'LineString' |
'Polygon' |
'MultiPoint' |
'MultiLineString' |
'MultiPolygon' |
'GeometryCollection';

export type CollectionTypes = 'FeatureCollection' | 'GeometryCollection';

Expand Down Expand Up @@ -82,11 +82,11 @@ export type Properties = { [name: string]: any; } | null;
* Geometries
*/
export type Geometries = Point |
LineString |
Polygon |
MultiPoint |
MultiLineString |
MultiPolygon;
LineString |
Polygon |
MultiPoint |
MultiLineString |
MultiPolygon;

/**
* GeoJSON Object
Expand Down Expand Up @@ -126,9 +126,9 @@ export interface GeometryObject extends GeoJSONObject {
*/
export interface Geometry extends GeoJSONObject {
coordinates: Position |
Position[] |
Position[][] |
Position[][][];
Position[] |
Position[][] |
Position[][][];
}

/**
Expand Down
Loading

0 comments on commit b4fb39d

Please sign in to comment.