oak-domain 5.1.9 → 5.1.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/base-app-domain/RelationId.d.ts +1 -0
- package/lib/base-app-domain/RelationId.js +2 -0
- package/lib/compiler/dependencyBuilder.js +21 -3
- package/lib/compiler/schemalBuilder.js +28 -10
- package/lib/types/Configuration.d.ts +4 -0
- package/lib/types/Exception.d.ts +3 -0
- package/lib/types/Exception.js +16 -1
- package/lib/types/Sync.d.ts +1 -0
- package/lib/utils/SimpleConnector.d.ts +2 -0
- package/lib/utils/SimpleConnector.js +30 -4
- package/lib/utils/geo.d.ts +30 -0
- package/lib/utils/geo.js +123 -1
- package/package.json +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare namespace RelationId { }
|
|
@@ -461,7 +461,7 @@ function outputIntializeDev(cwd, dependencies, briefNames, sourceFile, printer,
|
|
|
461
461
|
}
|
|
462
462
|
});
|
|
463
463
|
// startRoutine
|
|
464
|
-
if ((0, fs_1.existsSync)(join(destDir, 'routines', 'start'))) {
|
|
464
|
+
if ((0, fs_1.existsSync)(join(destDir, 'routines', 'start.js'))) {
|
|
465
465
|
const variableName = `${briefNames[idx]}StartRoutines`;
|
|
466
466
|
importStatements.push(factory.createImportDeclaration(undefined, factory.createImportClause(false, factory.createIdentifier(variableName), undefined), factory.createStringLiteral(join(dep, destDirName, 'routines/start')), undefined));
|
|
467
467
|
if (objectDict.startRoutines) {
|
|
@@ -583,11 +583,28 @@ function outputFeatureIndex(dependencies, briefNames, sourceFile, printer, filen
|
|
|
583
583
|
if (dependencies.length > 0) {
|
|
584
584
|
const importStatements = [];
|
|
585
585
|
const fdNames = [];
|
|
586
|
+
const adNames = [];
|
|
586
587
|
dependencies.forEach((dep, idx) => {
|
|
587
588
|
const fdName = `${(0, string_1.firstLetterUpperCase)(briefNames[idx])}FeatureDict`;
|
|
588
|
-
|
|
589
|
+
const adName = `${(0, string_1.firstLetterUpperCase)(briefNames[idx])}AspectDict`;
|
|
590
|
+
// 导入FeatureDict和AspectDict
|
|
591
|
+
importStatements.push(factory.createImportDeclaration(undefined, factory.createImportClause(false, undefined, factory.createNamedImports([
|
|
592
|
+
factory.createImportSpecifier(false, factory.createIdentifier("FeatureDict"), factory.createIdentifier(fdName)),
|
|
593
|
+
factory.createImportSpecifier(false, factory.createIdentifier("AspectDict"), factory.createIdentifier(adName))
|
|
594
|
+
])), factory.createStringLiteral(dep), undefined));
|
|
589
595
|
fdNames.push(fdName);
|
|
596
|
+
adNames.push(adName);
|
|
590
597
|
});
|
|
598
|
+
// 导入自己的AspectDict:import { AspectDict } from '../aspects/AspectDict';
|
|
599
|
+
importStatements.push(factory.createImportDeclaration(undefined, factory.createImportClause(false, undefined, factory.createNamedImports([factory.createImportSpecifier(false, factory.createIdentifier("AspectDict"), factory.createIdentifier("ProjectAspectDict"))])), factory.createStringLiteral("../aspects/AspectDict"), undefined));
|
|
600
|
+
// 创建一个这样的type: type MergeAspectDict = ProjectAspectDict & GenernalAspectDict<EntityDict>;
|
|
601
|
+
// 除了ProjectAspectDict,还有其他的AspectDict,需要<EntityDict>参数
|
|
602
|
+
const adTypeDeclaration = factory.createTypeAliasDeclaration(undefined, factory.createIdentifier("MergeAspectDict"), undefined, factory.createIntersectionTypeNode([
|
|
603
|
+
factory.createTypeReferenceNode(factory.createIdentifier("ProjectAspectDict"), undefined),
|
|
604
|
+
...adNames.map(ad => {
|
|
605
|
+
return factory.createTypeReferenceNode(factory.createIdentifier(ad), [factory.createTypeReferenceNode(factory.createIdentifier("EntityDict"), undefined)]);
|
|
606
|
+
})
|
|
607
|
+
]));
|
|
591
608
|
let i = 0;
|
|
592
609
|
while (true) {
|
|
593
610
|
const stmt = statements[i];
|
|
@@ -599,7 +616,7 @@ function outputFeatureIndex(dependencies, briefNames, sourceFile, printer, filen
|
|
|
599
616
|
const stmt3 = statements[i - 1], stmt4 = statements[i];
|
|
600
617
|
(0, assert_1.default)(ts.isImportDeclaration(stmt3) && ts.isFunctionDeclaration(stmt4));
|
|
601
618
|
const { name, parameters } = stmt4;
|
|
602
|
-
(0, assert_1.default)(ts.isIdentifier(name) && name.text === 'create' && parameters.length === 1);
|
|
619
|
+
(0, assert_1.default)(name && ts.isIdentifier(name) && name.text === 'create' && parameters.length === 1);
|
|
603
620
|
const [param] = parameters;
|
|
604
621
|
const { name: paramName, type } = param;
|
|
605
622
|
(0, assert_1.default)(ts.isIdentifier(paramName) && paramName.text === 'features' && ts.isTypeReferenceNode(type));
|
|
@@ -614,6 +631,7 @@ function outputFeatureIndex(dependencies, briefNames, sourceFile, printer, filen
|
|
|
614
631
|
statements2 = [
|
|
615
632
|
...statements.slice(0, i),
|
|
616
633
|
...importStatements,
|
|
634
|
+
adTypeDeclaration,
|
|
617
635
|
...statements.slice(i)
|
|
618
636
|
];
|
|
619
637
|
if (isModule) {
|
|
@@ -4220,24 +4220,42 @@ exports.registerDeducedRelationMap = registerDeducedRelationMap;
|
|
|
4220
4220
|
*/
|
|
4221
4221
|
function outputRelation2(outputDir, printer) {
|
|
4222
4222
|
const entityRelations = [];
|
|
4223
|
+
const stmts = [
|
|
4224
|
+
factory.createImportDeclaration(undefined, factory.createImportClause(false, undefined, factory.createNamedImports([factory.createImportSpecifier(false, undefined, factory.createIdentifier("EntityDict"))])), factory.createStringLiteral("./EntityDict"), undefined),
|
|
4225
|
+
factory.createImportDeclaration(undefined, factory.createImportClause(false, undefined, factory.createNamedImports([factory.createImportSpecifier(false, factory.createIdentifier("CreateOperationData"), factory.createIdentifier("Relation"))])), factory.createStringLiteral("./Relation/Schema"), undefined),
|
|
4226
|
+
];
|
|
4227
|
+
const enumDelarations = [];
|
|
4223
4228
|
for (const entity in Schema) {
|
|
4224
4229
|
const { relations } = Schema[entity];
|
|
4225
4230
|
if (relations) {
|
|
4226
|
-
|
|
4231
|
+
const entity2 = (0, string_1.firstLetterLowerCase)(entity);
|
|
4232
|
+
const enumMembers = [];
|
|
4233
|
+
relations.forEach((relation) => {
|
|
4234
|
+
const id = (0, uuid_1.formUuid)(entity2, relation);
|
|
4235
|
+
entityRelations.push([entity2, relation, id]);
|
|
4236
|
+
enumMembers.push(factory.createEnumMember((0, string_1.firstLetterUpperCase)(relation), factory.createStringLiteral(id)));
|
|
4237
|
+
});
|
|
4238
|
+
enumDelarations.push(factory.createEnumDeclaration([
|
|
4239
|
+
factory.createToken(ts.SyntaxKind.ExportKeyword),
|
|
4240
|
+
factory.createToken(ts.SyntaxKind.ConstKeyword)
|
|
4241
|
+
], factory.createIdentifier(entity), enumMembers));
|
|
4227
4242
|
}
|
|
4228
4243
|
}
|
|
4229
|
-
|
|
4230
|
-
|
|
4231
|
-
|
|
4232
|
-
|
|
4233
|
-
|
|
4234
|
-
factory.createPropertyAssignment(factory.createIdentifier("entity"), factory.createStringLiteral(entity)),
|
|
4235
|
-
factory.createPropertyAssignment(factory.createIdentifier("name"), factory.createStringLiteral(relation))
|
|
4236
|
-
], true)))), true))], ts.NodeFlags.Const))
|
|
4237
|
-
];
|
|
4244
|
+
stmts.push(factory.createVariableStatement([factory.createToken(ts.SyntaxKind.ExportKeyword)], factory.createVariableDeclarationList([factory.createVariableDeclaration(factory.createIdentifier("relations"), undefined, factory.createArrayTypeNode(factory.createTypeReferenceNode(factory.createIdentifier("Relation"), undefined)), factory.createArrayLiteralExpression(entityRelations.map(([entity, relation, id]) => factory.createObjectLiteralExpression([
|
|
4245
|
+
factory.createPropertyAssignment(factory.createIdentifier("id"), factory.createStringLiteral(id)),
|
|
4246
|
+
factory.createPropertyAssignment(factory.createIdentifier("entity"), factory.createStringLiteral(entity)),
|
|
4247
|
+
factory.createPropertyAssignment(factory.createIdentifier("name"), factory.createStringLiteral(relation))
|
|
4248
|
+
], true)), true))], ts.NodeFlags.Const)));
|
|
4238
4249
|
const result = printer.printList(ts.ListFormat.SourceFileStatements, factory.createNodeArray(stmts), ts.createSourceFile("someFileName.ts", "", ts.ScriptTarget.Latest, /*setParentNodes*/ false, ts.ScriptKind.TS));
|
|
4239
4250
|
const filename = path_1.default.join(outputDir, 'Relation.ts');
|
|
4240
4251
|
(0, fs_1.writeFileSync)(filename, result, { flag: 'w' });
|
|
4252
|
+
// 输出RelationIds.ts
|
|
4253
|
+
const stmts2 = [
|
|
4254
|
+
factory.createModuleDeclaration([factory.createToken(ts.SyntaxKind.ExportKeyword)], factory.createIdentifier("RelationId"), factory.createModuleBlock(enumDelarations), ts.NodeFlags.Namespace),
|
|
4255
|
+
];
|
|
4256
|
+
const result2 = printer.printList(ts.ListFormat.SourceFileStatements, factory.createNodeArray(stmts2), ts.createSourceFile("someFileName.ts", "", ts.ScriptTarget.Latest, /*setParentNodes*/ false, ts.ScriptKind.TS));
|
|
4257
|
+
const filename2 = path_1.default.join(outputDir, 'RelationId.ts');
|
|
4258
|
+
(0, fs_1.writeFileSync)(filename2, result2, { flag: 'w' });
|
|
4241
4259
|
}
|
|
4242
4260
|
/**
|
|
4243
4261
|
* 输出oak-app-domain中的StyleDict.ts文件
|
|
@@ -34,6 +34,9 @@ export type ServerConfiguration = {
|
|
|
34
34
|
methods?: string[];
|
|
35
35
|
};
|
|
36
36
|
internalExceptionMask?: string;
|
|
37
|
+
koaBody?: {
|
|
38
|
+
maxFileSize?: number;
|
|
39
|
+
};
|
|
37
40
|
};
|
|
38
41
|
/**
|
|
39
42
|
* 前后台访问配置
|
|
@@ -52,6 +55,7 @@ export type AccessConfiguration = {
|
|
|
52
55
|
ssl?: boolean;
|
|
53
56
|
path?: string;
|
|
54
57
|
};
|
|
58
|
+
timeout?: number;
|
|
55
59
|
};
|
|
56
60
|
/**
|
|
57
61
|
* 业务逻辑的通用配置
|
package/lib/types/Exception.d.ts
CHANGED
|
@@ -19,6 +19,9 @@ export declare class OakException<ED extends EntityDict & BaseEntityDict> extend
|
|
|
19
19
|
tag2?: boolean;
|
|
20
20
|
tag3?: any;
|
|
21
21
|
}
|
|
22
|
+
export declare class OakRequestTimeoutException<ED extends EntityDict & BaseEntityDict> extends OakException<ED> {
|
|
23
|
+
constructor(message?: string);
|
|
24
|
+
}
|
|
22
25
|
export declare class OakMakeSureByMySelfException<ED extends EntityDict & BaseEntityDict> extends OakException<ED> {
|
|
23
26
|
}
|
|
24
27
|
export declare class OakPartialSuccess<ED extends EntityDict & BaseEntityDict> extends OakException<ED> {
|
package/lib/types/Exception.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.makeException = exports.OakSocketConnectException = exports.OakExternalException = exports.OakPreConditionUnsetException = exports.OakDeadlock = exports.OakCongruentRowExists = exports.OakRowLockedException = exports.OakUnloggedInException = exports.OakUserInvisibleException = exports.OakUserUnpermittedException = exports.OakAttrCantUpdateException = exports.OakAttrNotNullException = exports.OakInputIllegalException = exports.OakRowInconsistencyException = exports.OakClockDriftException = exports.OakServerProxyException = exports.OakNetworkException = exports.OakImportDataParseException = exports.OakUniqueViolationException = exports.OakUserException = exports.OakRowUnexistedException = exports.OakOperExistedException = exports.OakNoRelationDefException = exports.OakDataException = exports.OakPartialSuccess = exports.OakMakeSureByMySelfException = exports.OakException = void 0;
|
|
3
|
+
exports.makeException = exports.OakSocketConnectException = exports.OakExternalException = exports.OakPreConditionUnsetException = exports.OakDeadlock = exports.OakCongruentRowExists = exports.OakRowLockedException = exports.OakUnloggedInException = exports.OakUserInvisibleException = exports.OakUserUnpermittedException = exports.OakAttrCantUpdateException = exports.OakAttrNotNullException = exports.OakInputIllegalException = exports.OakRowInconsistencyException = exports.OakClockDriftException = exports.OakServerProxyException = exports.OakNetworkException = exports.OakImportDataParseException = exports.OakUniqueViolationException = exports.OakUserException = exports.OakRowUnexistedException = exports.OakOperExistedException = exports.OakNoRelationDefException = exports.OakDataException = exports.OakPartialSuccess = exports.OakMakeSureByMySelfException = exports.OakRequestTimeoutException = exports.OakException = void 0;
|
|
4
4
|
const relation_1 = require("../store/relation");
|
|
5
5
|
const lodash_1 = require("../utils/lodash");
|
|
6
6
|
class OakException extends Error {
|
|
@@ -90,6 +90,13 @@ class OakException extends Error {
|
|
|
90
90
|
tag3;
|
|
91
91
|
}
|
|
92
92
|
exports.OakException = OakException;
|
|
93
|
+
// 请求超时
|
|
94
|
+
class OakRequestTimeoutException extends OakException {
|
|
95
|
+
constructor(message) {
|
|
96
|
+
super(message || '请求超时');
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
exports.OakRequestTimeoutException = OakRequestTimeoutException;
|
|
93
100
|
// 这个异常表示模块自己处理跨事务一致性,框架pass(在分布式数据传递时会用到)(backend-base老版本有使用先保留)
|
|
94
101
|
class OakMakeSureByMySelfException extends OakException {
|
|
95
102
|
}
|
|
@@ -495,6 +502,14 @@ function makeException(data) {
|
|
|
495
502
|
e = new OakSocketConnectException(data.message);
|
|
496
503
|
break;
|
|
497
504
|
}
|
|
505
|
+
case 'OakPartialSuccess': {
|
|
506
|
+
e = new OakPartialSuccess(data.message);
|
|
507
|
+
break;
|
|
508
|
+
}
|
|
509
|
+
case 'OakRequestTimeoutException': {
|
|
510
|
+
e = new OakRequestTimeoutException(data.message);
|
|
511
|
+
break;
|
|
512
|
+
}
|
|
498
513
|
default:
|
|
499
514
|
return;
|
|
500
515
|
}
|
package/lib/types/Sync.d.ts
CHANGED
|
@@ -76,6 +76,7 @@ export interface SyncRemoteConfig<ED extends EntityDict & BaseEntityDict, Cxt ex
|
|
|
76
76
|
}[];
|
|
77
77
|
reason: Error;
|
|
78
78
|
}, context: Cxt) => Promise<void>;
|
|
79
|
+
timeout?: number;
|
|
79
80
|
}
|
|
80
81
|
export interface SyncSelfConfigBase<ED extends EntityDict & BaseEntityDict> {
|
|
81
82
|
endpoint?: string;
|
|
@@ -17,6 +17,7 @@ export default class SimpleConnector<ED extends EntityDict & BaseEntityDict, Fro
|
|
|
17
17
|
private serverSubscribePointUrl;
|
|
18
18
|
private configuration;
|
|
19
19
|
private makeException;
|
|
20
|
+
private timeout;
|
|
20
21
|
constructor(configuration: AccessConfiguration, makeException: (exceptionData: any) => OakException<ED>);
|
|
21
22
|
getCorsHeader(): string[];
|
|
22
23
|
protected makeHeadersAndBody(name: string, data: any, context?: FrontCxt): Promise<{
|
|
@@ -82,4 +83,5 @@ export default class SimpleConnector<ED extends EntityDict & BaseEntityDict, Fro
|
|
|
82
83
|
headers?: Record<string, string> | undefined;
|
|
83
84
|
};
|
|
84
85
|
getFullData(): Promise<{}>;
|
|
86
|
+
private fetchWithTimeout;
|
|
85
87
|
}
|
|
@@ -18,9 +18,11 @@ class SimpleConnector {
|
|
|
18
18
|
serverSubscribePointUrl;
|
|
19
19
|
configuration;
|
|
20
20
|
makeException;
|
|
21
|
+
timeout;
|
|
21
22
|
constructor(configuration, makeException) {
|
|
22
23
|
this.configuration = configuration;
|
|
23
|
-
const { routerPrefixes, http } = configuration;
|
|
24
|
+
const { routerPrefixes, http, timeout } = configuration;
|
|
25
|
+
this.timeout = timeout || 5000;
|
|
24
26
|
const { ssl, hostname, port, path } = http;
|
|
25
27
|
const protocol = ssl ? 'https:' : 'http:';
|
|
26
28
|
let serverUrl = `${protocol}//${hostname}`;
|
|
@@ -108,11 +110,11 @@ class SimpleConnector {
|
|
|
108
110
|
const { headers, body } = await this.makeHeadersAndBody(name, params, context);
|
|
109
111
|
let response;
|
|
110
112
|
try {
|
|
111
|
-
response = await
|
|
113
|
+
response = await this.fetchWithTimeout(this.serverAspectUrl, {
|
|
112
114
|
method: 'POST',
|
|
113
115
|
headers,
|
|
114
116
|
body,
|
|
115
|
-
});
|
|
117
|
+
}, this.timeout);
|
|
116
118
|
}
|
|
117
119
|
catch (err) {
|
|
118
120
|
// fetch返回异常一定是网络异常
|
|
@@ -135,7 +137,7 @@ class SimpleConnector {
|
|
|
135
137
|
async getSocketPoint() {
|
|
136
138
|
let response;
|
|
137
139
|
try {
|
|
138
|
-
response = await
|
|
140
|
+
response = await this.fetchWithTimeout(this.serverSubscribePointUrl, {}, this.timeout);
|
|
139
141
|
}
|
|
140
142
|
catch (err) {
|
|
141
143
|
throw new types_1.OakNetworkException();
|
|
@@ -231,5 +233,29 @@ class SimpleConnector {
|
|
|
231
233
|
console.error('前后台模式下暂时不支持此操作,请到数据库查看数据');
|
|
232
234
|
return {};
|
|
233
235
|
}
|
|
236
|
+
async fetchWithTimeout(url, options, timeout = 5000) {
|
|
237
|
+
if (typeof AbortController === 'undefined') {
|
|
238
|
+
return global.fetch(url, options);
|
|
239
|
+
}
|
|
240
|
+
const controller = new AbortController();
|
|
241
|
+
const signal = controller.signal;
|
|
242
|
+
// 设置超时
|
|
243
|
+
const timeoutId = setTimeout(() => {
|
|
244
|
+
controller.abort();
|
|
245
|
+
}, timeout);
|
|
246
|
+
// 发起 fetch 请求并传递 signal
|
|
247
|
+
return global.fetch(url, Object.assign({}, options, { signal }))
|
|
248
|
+
.then(response => {
|
|
249
|
+
clearTimeout(timeoutId); // 如果请求成功,清除超时
|
|
250
|
+
return response;
|
|
251
|
+
})
|
|
252
|
+
.catch(error => {
|
|
253
|
+
clearTimeout(timeoutId); // 如果请求失败,清除超时
|
|
254
|
+
if (error.name === 'AbortError') {
|
|
255
|
+
throw new types_1.OakRequestTimeoutException();
|
|
256
|
+
}
|
|
257
|
+
throw error; // 其他错误
|
|
258
|
+
});
|
|
259
|
+
}
|
|
234
260
|
}
|
|
235
261
|
exports.default = SimpleConnector;
|
package/lib/utils/geo.d.ts
CHANGED
|
@@ -2,3 +2,33 @@
|
|
|
2
2
|
* 计算地球上两点之间的球面距离
|
|
3
3
|
*/
|
|
4
4
|
export declare function getDistanceBetweenPoints(lat1: number, lon1: number, lat2: number, lon2: number): number;
|
|
5
|
+
/**
|
|
6
|
+
* 百度坐标系 (BD-09) 与 火星坐标系 (GCJ-02)的转换
|
|
7
|
+
* 即 百度 转 谷歌、高德
|
|
8
|
+
* @param bd_lon
|
|
9
|
+
* @param bd_lat
|
|
10
|
+
* @returns {*[]}
|
|
11
|
+
*/
|
|
12
|
+
export declare function bd09togcj02(coord: [number, number]): number[];
|
|
13
|
+
/**
|
|
14
|
+
* 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换
|
|
15
|
+
* 即谷歌、高德 转 百度
|
|
16
|
+
* @param lng
|
|
17
|
+
* @param lat
|
|
18
|
+
* @returns {*[]}
|
|
19
|
+
*/
|
|
20
|
+
export declare function gcj02tobd09(coord: [number, number]): number[];
|
|
21
|
+
/**
|
|
22
|
+
* WGS84转GCj02
|
|
23
|
+
* @param lng
|
|
24
|
+
* @param lat
|
|
25
|
+
* @returns {*[]}
|
|
26
|
+
*/
|
|
27
|
+
export declare function wgs84togcj02(coord: [number, number]): number[];
|
|
28
|
+
/**
|
|
29
|
+
* GCJ02 转换为 WGS84
|
|
30
|
+
* @param lng
|
|
31
|
+
* @param lat
|
|
32
|
+
* @returns {*[]}
|
|
33
|
+
*/
|
|
34
|
+
export declare function gcj02towgs84(coord: [number, number]): number[];
|
package/lib/utils/geo.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getDistanceBetweenPoints = void 0;
|
|
3
|
+
exports.gcj02towgs84 = exports.wgs84togcj02 = exports.gcj02tobd09 = exports.bd09togcj02 = exports.getDistanceBetweenPoints = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* 计算地球上两点之间的球面距离
|
|
6
6
|
*/
|
|
@@ -22,3 +22,125 @@ function getDistanceBetweenPoints(lat1, lon1, lat2, lon2) {
|
|
|
22
22
|
return d * 1000;
|
|
23
23
|
}
|
|
24
24
|
exports.getDistanceBetweenPoints = getDistanceBetweenPoints;
|
|
25
|
+
//定义一些常量
|
|
26
|
+
const x_PI = 3.14159265358979324 * 3000.0 / 180.0;
|
|
27
|
+
const PI = 3.1415926535897932384626;
|
|
28
|
+
const a = 6378245.0;
|
|
29
|
+
const ee = 0.00669342162296594323;
|
|
30
|
+
function transformlat(lng, lat) {
|
|
31
|
+
let ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng));
|
|
32
|
+
ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
|
|
33
|
+
ret += (20.0 * Math.sin(lat * PI) + 40.0 * Math.sin(lat / 3.0 * PI)) * 2.0 / 3.0;
|
|
34
|
+
ret += (160.0 * Math.sin(lat / 12.0 * PI) + 320 * Math.sin(lat * PI / 30.0)) * 2.0 / 3.0;
|
|
35
|
+
return ret;
|
|
36
|
+
}
|
|
37
|
+
;
|
|
38
|
+
function transformlng(lng, lat) {
|
|
39
|
+
let ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng));
|
|
40
|
+
ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
|
|
41
|
+
ret += (20.0 * Math.sin(lng * PI) + 40.0 * Math.sin(lng / 3.0 * PI)) * 2.0 / 3.0;
|
|
42
|
+
ret += (150.0 * Math.sin(lng / 12.0 * PI) + 300.0 * Math.sin(lng / 30.0 * PI)) * 2.0 / 3.0;
|
|
43
|
+
return ret;
|
|
44
|
+
}
|
|
45
|
+
;
|
|
46
|
+
/**
|
|
47
|
+
* 判断是否在国内,不在国内则不做偏移
|
|
48
|
+
* @param lng
|
|
49
|
+
* @param lat
|
|
50
|
+
* @returns {boolean}
|
|
51
|
+
*/
|
|
52
|
+
function out_of_china(lng, lat) {
|
|
53
|
+
// 纬度3.86~53.55,经度73.66~135.05
|
|
54
|
+
return !(lng > 73.66 && lng < 135.05 && lat > 3.86 && lat < 53.55);
|
|
55
|
+
}
|
|
56
|
+
;
|
|
57
|
+
/**
|
|
58
|
+
* 百度坐标系 (BD-09) 与 火星坐标系 (GCJ-02)的转换
|
|
59
|
+
* 即 百度 转 谷歌、高德
|
|
60
|
+
* @param bd_lon
|
|
61
|
+
* @param bd_lat
|
|
62
|
+
* @returns {*[]}
|
|
63
|
+
*/
|
|
64
|
+
function bd09togcj02(coord) {
|
|
65
|
+
const [bd_lon, bd_lat] = coord;
|
|
66
|
+
const x = bd_lon - 0.0065;
|
|
67
|
+
const y = bd_lat - 0.006;
|
|
68
|
+
const z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_PI);
|
|
69
|
+
const theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_PI);
|
|
70
|
+
const gg_lng = z * Math.cos(theta);
|
|
71
|
+
const gg_lat = z * Math.sin(theta);
|
|
72
|
+
return [gg_lng, gg_lat];
|
|
73
|
+
}
|
|
74
|
+
exports.bd09togcj02 = bd09togcj02;
|
|
75
|
+
;
|
|
76
|
+
/**
|
|
77
|
+
* 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换
|
|
78
|
+
* 即谷歌、高德 转 百度
|
|
79
|
+
* @param lng
|
|
80
|
+
* @param lat
|
|
81
|
+
* @returns {*[]}
|
|
82
|
+
*/
|
|
83
|
+
function gcj02tobd09(coord) {
|
|
84
|
+
const [lng, lat] = coord;
|
|
85
|
+
const z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_PI);
|
|
86
|
+
const theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_PI);
|
|
87
|
+
const bd_lng = z * Math.cos(theta) + 0.0065;
|
|
88
|
+
const bd_lat = z * Math.sin(theta) + 0.006;
|
|
89
|
+
return [bd_lng, bd_lat];
|
|
90
|
+
}
|
|
91
|
+
exports.gcj02tobd09 = gcj02tobd09;
|
|
92
|
+
;
|
|
93
|
+
/**
|
|
94
|
+
* WGS84转GCj02
|
|
95
|
+
* @param lng
|
|
96
|
+
* @param lat
|
|
97
|
+
* @returns {*[]}
|
|
98
|
+
*/
|
|
99
|
+
function wgs84togcj02(coord) {
|
|
100
|
+
const [lng, lat] = coord;
|
|
101
|
+
if (out_of_china(lng, lat)) {
|
|
102
|
+
return [lng, lat];
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
let dlat = transformlat(lng - 105.0, lat - 35.0);
|
|
106
|
+
let dlng = transformlng(lng - 105.0, lat - 35.0);
|
|
107
|
+
const radlat = lat / 180.0 * PI;
|
|
108
|
+
let magic = Math.sin(radlat);
|
|
109
|
+
magic = 1 - ee * magic * magic;
|
|
110
|
+
const sqrtmagic = Math.sqrt(magic);
|
|
111
|
+
dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);
|
|
112
|
+
dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
|
|
113
|
+
const mglat = lat + dlat;
|
|
114
|
+
const mglng = lng + dlng;
|
|
115
|
+
return [mglng, mglat];
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
exports.wgs84togcj02 = wgs84togcj02;
|
|
119
|
+
;
|
|
120
|
+
/**
|
|
121
|
+
* GCJ02 转换为 WGS84
|
|
122
|
+
* @param lng
|
|
123
|
+
* @param lat
|
|
124
|
+
* @returns {*[]}
|
|
125
|
+
*/
|
|
126
|
+
function gcj02towgs84(coord) {
|
|
127
|
+
const [lng, lat] = coord;
|
|
128
|
+
if (out_of_china(lng, lat)) {
|
|
129
|
+
return [lng, lat];
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
let dlat = transformlat(lng - 105.0, lat - 35.0);
|
|
133
|
+
let dlng = transformlng(lng - 105.0, lat - 35.0);
|
|
134
|
+
const radlat = lat / 180.0 * PI;
|
|
135
|
+
let magic = Math.sin(radlat);
|
|
136
|
+
magic = 1 - ee * magic * magic;
|
|
137
|
+
const sqrtmagic = Math.sqrt(magic);
|
|
138
|
+
dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);
|
|
139
|
+
dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
|
|
140
|
+
const mglat = lat + dlat;
|
|
141
|
+
const mglng = lng + dlng;
|
|
142
|
+
return [lng * 2 - mglng, lat * 2 - mglat];
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
exports.gcj02towgs84 = gcj02towgs84;
|
|
146
|
+
;
|