Skip to content

Commit

Permalink
style: update code style
Browse files Browse the repository at this point in the history
  • Loading branch information
hujiulong committed Oct 18, 2018
1 parent 6c85304 commit 9cab38e
Show file tree
Hide file tree
Showing 21 changed files with 1,044 additions and 1,084 deletions.
28 changes: 14 additions & 14 deletions .babelrc
@@ -1,17 +1,17 @@
{
"env": {
"development": {
"presets": [
["env", {
"modules": false
}]
],
"plugins": [ "external-helpers" ]
},
"test": {
"presets": [
["env"]
]
}
"env": {
"development": {
"presets": [
["env", {
"modules": false
}]
],
"plugins": ["external-helpers"]
},
"test": {
"presets": [
["env"]
]
}
}
}
3 changes: 3 additions & 0 deletions .eslintrc.js
@@ -0,0 +1,3 @@
module.exports = {
extends: '@hujiulong/eslint-config'
}
3 changes: 0 additions & 3 deletions .eslintrc.json

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018 Jiulong Hu
Copyright (c) 2018-present, Jiulong Hu

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
28 changes: 14 additions & 14 deletions README.md
Expand Up @@ -6,7 +6,7 @@
[![gzip size](http://img.badgesize.io/https://unpkg.com/gcoord/dist/gcoord.js?compression=gzip)](https://unpkg.com/gcoord/dist/gcoord.js)
[![LICENSE](https://img.shields.io/npm/l/vue.svg)](https://www.npmjs.com/package/gcoord)

**gcoord**( **g**eographic **coord**inates)是一个处理地理坐标系的js库,用来修正百度地图、高德地图及其它互联网地图坐标系不统一的问题
**gcoord**(**g**eographic **coord**inates)是一个处理地理坐标系的js库,用来修正百度地图、高德地图及其它互联网地图坐标系不统一的问题

(**gcoord** is a JS library for converting Chinese geographic coordinate encryption. In most cases, only Chinese developers need to use it, so there is no English document. If you need an English document, please open a new issue)

Expand All @@ -33,9 +33,9 @@ npm install gcoord --save
## 引入
CommonJS:
```js
const gcoord = require( 'gcoord' );
const gcoord = require('gcoord');
// 或者
const { transform, WGS84, GCJ02 } = require( 'gcoord' );
const { transform, WGS84, GCJ02 } = require('gcoord');
```
ES Module:
```js
Expand All @@ -50,18 +50,18 @@ import { transform, WGS84, GCJ02 } from 'gcoord'
例如从手机的GPS得到一个经纬度坐标,需要将其展示在百度地图上,则应该将当前坐标从[WGS-84](#wgs-84---世界大地测量系统)坐标系转换为[BD-09](#bd-09---百度坐标系)坐标系
```js
var result = gcoord.transform(
[ 116.403988, 39.914266 ], // 经纬度坐标
gcoord.WGS84, // 当前坐标系
gcoord.BD09 // 目标坐标系
[116.403988, 39.914266], // 经纬度坐标
gcoord.WGS84, // 当前坐标系
gcoord.BD09 // 目标坐标系
);

console.log( result ); // [ 116.41661560068297, 39.92196580126834 ]
console.log(result); // [116.41661560068297, 39.92196580126834]
```
同时gcoord还可以转换GeoJSON对象的坐标系,详细使用方式可以参考[API](#api)

## API

### transform( input, from, to )
### transform(input, from, to)
进行坐标转换

**参数**
Expand All @@ -76,18 +76,18 @@ console.log( result ); // [ 116.41661560068297, 39.92196580126834 ]
**示例**
```js
// 将GCJ02坐标转换为WGS84坐标
var result = gcoord.transform( [ 123, 45 ], gcoord.GCJ02, gcoord.WGS84 );
console.log( result ); // [ 122.99395597, 44.99804071 ]
var result = gcoord.transform([123, 45], gcoord.GCJ02, gcoord.WGS84);
console.log(result); // [122.99395597, 44.99804071]
```

```js
// 转换GeoJSON坐标
var geojson = {
"type": "Point",
"coordinates": [ 123, 45 ]
"type": "Point",
"coordinates": [123, 45]
}
gcoord.transform( geojson, gcoord.GCJ02, gcoord.WGS84 );
console.log( geojson.coordinates ); // [ 122.99395597, 44.99804071 ]
gcoord.transform(geojson, gcoord.GCJ02, gcoord.WGS84);
console.log(geojson.coordinates); // [122.99395597, 44.99804071]
```

返回数组或GeoJSON对象(由输入决定),**注意:当输入为GeoJSON时,transform会改变输入对象**
Expand Down
8 changes: 5 additions & 3 deletions package.json
Expand Up @@ -4,6 +4,7 @@
"description": "geographic coordinate library",
"main": "dist/gcoord.js",
"module": "dist/gcoord.esm.js",
"types": "index.d.ts",
"scripts": {
"rollup": "rollup -c ./rollup.config.js",
"uglify": "uglifyjs dist/gcoord.js -c -m -o dist/gcoord.js --source-map filename=dist/gcoord.js.map --in-source-map dist/gcoord.js.map --source-map-url gcoord.js.map --comments",
Expand All @@ -16,6 +17,7 @@
"roots": [
"<rootDir>/test/"
],
"testURL": "http://localhost/",
"testRegex": ".*\\.(test|spec)\\.jsx?$",
"coveragePathIgnorePatterns": [
"/node_modules/",
Expand All @@ -35,13 +37,13 @@
},
"homepage": "https://github.com/hujiulong/gcoord#readme",
"devDependencies": {
"@hujiulong/eslint-config": "^0.0.5",
"@hujiulong/eslint-config": "^0.0.6",
"babel-core": "^6.26.0",
"babel-plugin-external-helpers": "^6.22.0",
"babel-preset-env": "^1.6.1",
"codecov": "^3.0.0",
"eslint": "^4.19.1",
"jest": "^22.1.4",
"eslint": "^5.7.0",
"jest": "^23.6.0",
"rollup": "^0.55.3",
"rollup-plugin-babel": "^3.0.3",
"uglify-js": "^3.3.9"
Expand Down
42 changes: 21 additions & 21 deletions rollup.config.js
@@ -1,5 +1,5 @@
import pkg from './package.json'
import babel from 'rollup-plugin-babel';
import pkg from './package.json';

const banner = `/* @preserve
* gcoord ${pkg.version}, ${pkg.description}
Expand All @@ -8,24 +8,24 @@ const banner = `/* @preserve
`;

export default {
input: 'src/index.js',
output: [
{
file: pkg.main,
format: 'umd',
name: pkg.name,
banner: banner,
sourcemap: true
},
{
file: pkg.module,
format: 'es',
banner: banner,
sourcemap: true
}
],
legacy: true, // Needed to create files loadable by IE8
plugins: [
babel()
]
input: 'src/index.js',
output: [
{
file: pkg.main,
format: 'umd',
name: pkg.name,
banner,
sourcemap: true,
},
{
file: pkg.module,
format: 'es',
banner,
sourcemap: true,
},
],
legacy: true, // Needed to create files loadable by IE8
plugins: [
babel(),
],
};
43 changes: 21 additions & 22 deletions src/crs/BD09.js
@@ -1,33 +1,32 @@
const { sin, cos, atan2, sqrt, PI } = Math;
const {
sin, cos, atan2, sqrt, PI,
} = Math;

const baiduFactor = PI * 3000.0 / 180.0;

export function BD09ToGCJ02( coord ) {
export function BD09ToGCJ02(coord) {
const [lon, lat] = coord;

const [ lon, lat ] = coord;
const x = lon - 0.0065;
const y = lat - 0.006;
const z = sqrt(x * x + y * y) - 0.00002 * sin(y * baiduFactor);
const theta = atan2(y, x) - 0.000003 * cos(x * baiduFactor);
const newLon = z * cos(theta);
const newLat = z * sin(theta);

const x = lon - 0.0065;
const y = lat - 0.006;
const z = sqrt( x * x + y * y ) - 0.00002 * sin( y * baiduFactor );
const theta = atan2( y, x ) - 0.000003 * cos( x * baiduFactor );
const newLon = z * cos( theta );
const newLat = z * sin( theta );

return [ newLon, newLat ];
return [newLon, newLat];
}

export function GCJ02ToBD09( coord ) {

const [ lon, lat ] = coord;

const x = lon;
const y = lat;
const z = sqrt( x * x + y * y ) + 0.00002 * sin( y * baiduFactor );
const theta = atan2( y, x ) + 0.000003 * cos( x * baiduFactor );
export function GCJ02ToBD09(coord) {
const [lon, lat] = coord;

const newLon = z * cos( theta ) + 0.0065;
const newLat = z * sin( theta ) + 0.006;
const x = lon;
const y = lat;
const z = sqrt(x * x + y * y) + 0.00002 * sin(y * baiduFactor);
const theta = atan2(y, x) + 0.000003 * cos(x * baiduFactor);

return [ newLon, newLat ];
const newLon = z * cos(theta) + 0.0065;
const newLat = z * sin(theta) + 0.006;

return [newLon, newLat];
}
38 changes: 19 additions & 19 deletions src/crs/EPSG3857.js
Expand Up @@ -5,27 +5,27 @@ const D2R = Math.PI / 180;
const A = 6378137.0;
const MAXEXTENT = 20037508.342789244;

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

export function WGS84ToEPSG3857( lonLat ) {
// 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 xy = [
A * adjusted * D2R,
A * Math.log( Math.tan( ( Math.PI * 0.25 ) + ( 0.5 * lonLat[ 1 ] * D2R ) ) )
];
export function WGS84ToEPSG3857(lonLat) {
// 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 xy = [
A * adjusted * 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
if ( xy[ 0 ] > MAXEXTENT ) xy[ 0 ] = MAXEXTENT;
if ( xy[ 0 ] < -MAXEXTENT ) xy[ 0 ] = -MAXEXTENT;
if ( xy[ 1 ] > MAXEXTENT ) xy[ 1 ] = MAXEXTENT;
if ( xy[ 1 ] < -MAXEXTENT ) xy[ 1 ] = -MAXEXTENT;
// if xy value is beyond maxextent (e.g. poles), return maxextent
if (xy[0] > MAXEXTENT) xy[0] = MAXEXTENT;
if (xy[0] < -MAXEXTENT) xy[0] = -MAXEXTENT;
if (xy[1] > MAXEXTENT) xy[1] = MAXEXTENT;
if (xy[1] < -MAXEXTENT) xy[1] = -MAXEXTENT;

return xy;
return xy;
}

0 comments on commit 9cab38e

Please sign in to comment.