Skip to content

Commit

Permalink
style: format codes
Browse files Browse the repository at this point in the history
  • Loading branch information
hujiulong committed Feb 18, 2021
1 parent cb48d77 commit fd9b5ff
Show file tree
Hide file tree
Showing 18 changed files with 390 additions and 260 deletions.
6 changes: 2 additions & 4 deletions src/crs/BD09.ts
@@ -1,10 +1,8 @@
import { Position } from '../geojson';

const {
sin, cos, atan2, sqrt, PI,
} = Math;
const { sin, cos, atan2, sqrt, PI } = Math;

const baiduFactor = PI * 3000.0 / 180.0;
const baiduFactor = (PI * 3000.0) / 180.0;

export function BD09ToGCJ02(coord: Position): Position {
const [lon, lat] = coord;
Expand Down
24 changes: 9 additions & 15 deletions src/crs/BD09MC.ts
Expand Up @@ -2,14 +2,7 @@ import { Position } from '../geojson';

const { abs } = Math;

const MCBAND = [
12890594.86,
8362377.87,
5591021,
3481989.83,
1678043.12,
0,
];
const MCBAND = [12890594.86, 8362377.87, 5591021, 3481989.83, 1678043.12, 0];
const LLBAND = [75, 60, 45, 30, 15, 0];
const MC2LL = [
[
Expand Down Expand Up @@ -164,13 +157,14 @@ 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] * (cc ** 2)
+ factors[5] * (cc ** 3)
+ factors[6] * (cc ** 4)
+ factors[7] * (cc ** 5)
+ factors[8] * (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
11 changes: 7 additions & 4 deletions src/crs/EPSG3857.ts
Expand Up @@ -9,18 +9,21 @@ const MAXEXTENT = 20037508.342789244;

export function ESPG3857ToWGS84(xy: Position): Position {
return [
(xy[0] * R2D / A),
((Math.PI * 0.5) - 2.0 * Math.atan(Math.exp(-xy[1] / A))) * R2D,
(xy[0] * R2D) / A,
(Math.PI * 0.5 - 2.0 * Math.atan(Math.exp(-xy[1] / A))) * R2D,
];
}

export function WGS84ToEPSG3857(lonLat: Position): Position {
// compensate longitudes passing the 180th meridian
// from https://github.com/proj4js/proj4js/blob/master/lib/common/adjust_lon.js
const adjusted = (Math.abs(lonLat[0]) <= 180) ? lonLat[0] : (lonLat[0] - ((lonLat[0] < 0 ? -1 : 1) * 360));
const adjusted =
Math.abs(lonLat[0]) <= 180
? lonLat[0]
: lonLat[0] - (lonLat[0] < 0 ? -1 : 1) * 360;
const xy: Position = [
A * adjusted * D2R,
A * Math.log(Math.tan((Math.PI * 0.25) + (0.5 * lonLat[1] * D2R))),
A * Math.log(Math.tan(Math.PI * 0.25 + 0.5 * lonLat[1] * D2R)),
];

// if xy value is beyond maxextent (e.g. poles), return maxextent
Expand Down
26 changes: 12 additions & 14 deletions src/crs/GCJ02.ts
@@ -1,46 +1,44 @@
import { Position } from '../geojson';

const {
sin, cos, sqrt, abs, PI,
} = Math;
const { sin, cos, sqrt, abs, PI } = Math;

const a = 6378245;
const ee = 0.006693421622965823;


// 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 {
let ret = -100 + 2 * x + 3 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(abs(x));
ret += (20 * sin(6 * x * PI) + 20 * sin(2 * x * PI)) * 2 / 3;
ret += (20 * sin(y * PI) + 40 * sin(y / 3 * PI)) * 2 / 3;
ret += (160 * sin(y / 12 * PI) + 320 * sin(y * PI / 30)) * 2 / 3;
let ret =
-100 + 2 * x + 3 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(abs(x));
ret += ((20 * sin(6 * x * PI) + 20 * sin(2 * x * PI)) * 2) / 3;
ret += ((20 * sin(y * PI) + 40 * sin((y / 3) * PI)) * 2) / 3;
ret += ((160 * sin((y / 12) * PI) + 320 * sin((y * PI) / 30)) * 2) / 3;
return ret;
}

function transformLon(x: number, y: number): number {
let ret = 300 + x + 2 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(abs(x));
ret += (20 * sin(6 * x * PI) + 20 * sin(2 * x * PI)) * 2 / 3;
ret += (20 * sin(x * PI) + 40 * sin(x / 3 * PI)) * 2 / 3;
ret += (150 * sin(x / 12 * PI) + 300 * sin(x / 30 * PI)) * 2 / 3;
ret += ((20 * sin(6 * x * PI) + 20 * sin(2 * x * PI)) * 2) / 3;
ret += ((20 * sin(x * PI) + 40 * sin((x / 3) * PI)) * 2) / 3;
ret += ((150 * sin((x / 12) * PI) + 300 * sin((x / 30) * PI)) * 2) / 3;
return ret;
}

function delta(lon: number, lat: number): number[] {
let dLon = transformLon(lon - 105, lat - 35);
let dLat = transformLat(lon - 105, lat - 35);

const radLat = lat / 180 * PI;
const radLat = (lat / 180) * PI;
let magic = sin(radLat);

magic = 1 - ee * magic * magic;

const sqrtMagic = sqrt(magic);
dLon = (dLon * 180) / (a / sqrtMagic * cos(radLat) * PI);
dLat = (dLat * 180) / ((a * (1 - ee)) / (magic * sqrtMagic) * PI);
dLon = (dLon * 180) / ((a / sqrtMagic) * cos(radLat) * PI);
dLat = (dLat * 180) / (((a * (1 - ee)) / (magic * sqrtMagic)) * PI);

return [dLon, dLat];
}
Expand Down
18 changes: 14 additions & 4 deletions src/crs/index.ts
Expand Up @@ -34,8 +34,8 @@ export enum CRSTypes {

export interface CRS {
to: {
[key in keyof typeof CRSTypes]?: Function
}
[key in keyof typeof CRSTypes]?: Function;
};
}

export const WGS84: CRS = {
Expand Down Expand Up @@ -70,15 +70,25 @@ export const EPSG3857: CRS = {
[CRSTypes.WGS84]: ESPG3857ToWGS84,
[CRSTypes.GCJ02]: compose(WGS84ToGCJ02, ESPG3857ToWGS84),
[CRSTypes.BD09]: compose(GCJ02ToBD09, WGS84ToGCJ02, ESPG3857ToWGS84),
[CRSTypes.BD09MC]: compose(BD09toBD09MC, GCJ02ToBD09, WGS84ToGCJ02, ESPG3857ToWGS84),
[CRSTypes.BD09MC]: compose(
BD09toBD09MC,
GCJ02ToBD09,
WGS84ToGCJ02,
ESPG3857ToWGS84
),
},
};

export const BD09MC: CRS = {
to: {
[CRSTypes.WGS84]: compose(GCJ02ToWGS84, BD09ToGCJ02, BD09MCtoBD09),
[CRSTypes.GCJ02]: compose(BD09ToGCJ02, BD09MCtoBD09),
[CRSTypes.EPSG3857]: compose(WGS84ToEPSG3857, GCJ02ToWGS84, BD09ToGCJ02, BD09MCtoBD09),
[CRSTypes.EPSG3857]: compose(
WGS84ToEPSG3857,
GCJ02ToWGS84,
BD09ToGCJ02,
BD09MCtoBD09
),
[CRSTypes.BD09]: BD09MCtoBD09,
},
};
Expand Down
53 changes: 31 additions & 22 deletions src/geojson.ts
Expand Up @@ -19,13 +19,14 @@
* https://tools.ietf.org/html/rfc7946#section-1.4
* The valid values for the 'type' property of GeoJSON geometry objects.
*/
export type GeometryTypes = 'Point' |
'LineString' |
'Polygon' |
'MultiPoint' |
'MultiLineString' |
'MultiPolygon' |
'GeometryCollection';
export type GeometryTypes =
| 'Point'
| 'LineString'
| 'Polygon'
| 'MultiPoint'
| 'MultiLineString'
| 'MultiPolygon'
| 'GeometryCollection';

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

Expand Down Expand Up @@ -76,17 +77,18 @@ export type Position = [number, number] | [number, number, number];
* A Feature object has a member with the name 'properties'.
* The value of the properties member is an object (any JSON object or a JSON null value).
*/
export type Properties = { [name: string]: any; } | null;
export type Properties = { [name: string]: any } | null;

/**
* Geometries
*/
export type Geometries = Point |
LineString |
Polygon |
MultiPoint |
MultiLineString |
MultiPolygon;
export type Geometries =
| Point
| LineString
| Polygon
| MultiPoint
| MultiLineString
| MultiPolygon;

/**
* GeoJSON Object
Expand Down Expand Up @@ -125,10 +127,7 @@ export interface GeometryObject extends GeoJSONObject {
* https://tools.ietf.org/html/rfc7946#section-3
*/
export interface Geometry extends GeoJSONObject {
coordinates: Position |
Position[] |
Position[][] |
Position[][][];
coordinates: Position | Position[] | Position[][] | Position[][][];
}

/**
Expand Down Expand Up @@ -203,7 +202,9 @@ export interface MultiPolygon extends GeometryObject {
*/
export interface GeometryCollection extends GeometryObject {
type: 'GeometryCollection';
geometries: Array<Point | LineString | Polygon | MultiPoint | MultiLineString | MultiPolygon>;
geometries: Array<
Point | LineString | Polygon | MultiPoint | MultiLineString | MultiPolygon
>;
}

/**
Expand All @@ -213,7 +214,8 @@ export interface GeometryCollection extends GeometryObject {
* A Feature object represents a spatially bounded thing.
* Every Feature object is a GeoJSON object no matter where it occurs in a GeoJSON text.
*/
export interface Feature<G = Geometry | GeometryCollection, P = Properties> extends GeoJSONObject {
export interface Feature<G = Geometry | GeometryCollection, P = Properties>
extends GeoJSONObject {
type: 'Feature';
geometry: G;
/**
Expand All @@ -236,7 +238,10 @@ export interface Feature<G = Geometry | GeometryCollection, P = Properties> exte
* The value of 'features' is a JSON array. Each element of the array is a Feature object as defined above.
* It is possible for this array to be empty.
*/
export interface FeatureCollection<G = Geometry | GeometryCollection, P = Properties> extends GeoJSONObject {
export interface FeatureCollection<
G = Geometry | GeometryCollection,
P = Properties
> extends GeoJSONObject {
type: 'FeatureCollection';
features: Array<Feature<G, P>>;
}
Expand All @@ -246,4 +251,8 @@ export interface FeatureCollection<G = Geometry | GeometryCollection, P = Proper
*
* All GeoJSON objects
*/
export type GeoJSON = Feature | FeatureCollection | Geometry | GeometryCollection;
export type GeoJSON =
| Feature
| FeatureCollection
| Geometry
| GeometryCollection;

0 comments on commit fd9b5ff

Please sign in to comment.