@mapgis/supercluster 17.6.0 → 17.8.0
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/Projection.js +93 -0
- package/dist/supercluster.js +8610 -336
- package/dist/supercluster.min.js +1 -1
- package/index.js +453 -377
- package/package.json +3 -7
package/Projection.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import proj4 from "proj4";
|
|
2
|
+
/**
|
|
3
|
+
* @description: 单个空间参考系的投影转换类,用于将地理坐标和平面坐标之间的互转
|
|
4
|
+
*/
|
|
5
|
+
export default class Projection {
|
|
6
|
+
/**
|
|
7
|
+
* @description 实现投影对象
|
|
8
|
+
* @param {String} code
|
|
9
|
+
* @param {String} def 参考系对象
|
|
10
|
+
* @param {Array<Number>} bounds 投影系范围
|
|
11
|
+
* @param {Number} width 投影系宽度
|
|
12
|
+
* @param {Number} height 投影系高度
|
|
13
|
+
* @return {*}
|
|
14
|
+
*/
|
|
15
|
+
constructor(code, def, bounds, width, height) {
|
|
16
|
+
this.bounds = bounds;
|
|
17
|
+
this.width = width;
|
|
18
|
+
this.height = height;
|
|
19
|
+
if (!def) throw new Error("自定义参考系必须指定参数系信息def");
|
|
20
|
+
// 验证proj4库是否存在
|
|
21
|
+
if (!proj4) throw new Error("proj4对象不存在,自定义投影必须引入proj4对象");
|
|
22
|
+
const isP4 = this._isProj4Obj(code);
|
|
23
|
+
this._proj = isP4 ? code : this._projFromCodeDef(code, def);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @description: 投影方法
|
|
28
|
+
* @param {Array|Object} lnglat
|
|
29
|
+
* @return {*}
|
|
30
|
+
*/
|
|
31
|
+
project(lnglat) {
|
|
32
|
+
let lng, lat;
|
|
33
|
+
if (Array.isArray(lnglat)) {
|
|
34
|
+
lng = lnglat[0];
|
|
35
|
+
lat = lnglat[1];
|
|
36
|
+
} else {
|
|
37
|
+
lng = lnglat.lng;
|
|
38
|
+
lat = lnglat.lat;
|
|
39
|
+
}
|
|
40
|
+
const coords = this._proj.forward([lng, lat]);
|
|
41
|
+
const bounds = this.bounds;
|
|
42
|
+
const x = (coords[0] - bounds[0]) / this.width;
|
|
43
|
+
const y = (bounds[3] - coords[1]) / this.height;
|
|
44
|
+
return [x, y];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @description: 反投影方法
|
|
49
|
+
* @param {Array|Object} point
|
|
50
|
+
* @return {*}
|
|
51
|
+
*/
|
|
52
|
+
unproject(point) {
|
|
53
|
+
let x, y;
|
|
54
|
+
if (Array.isArray(point)) {
|
|
55
|
+
x = point[0];
|
|
56
|
+
y = point[1];
|
|
57
|
+
} else {
|
|
58
|
+
x = point.x;
|
|
59
|
+
y = point.y;
|
|
60
|
+
}
|
|
61
|
+
return this._proj.inverse([x, y]);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @description: 定义proj投影
|
|
66
|
+
* @param {*} code
|
|
67
|
+
* @param {*} def
|
|
68
|
+
* @return {*}
|
|
69
|
+
*/
|
|
70
|
+
_projFromCodeDef(code, def) {
|
|
71
|
+
if (def) {
|
|
72
|
+
proj4.defs(code, def);
|
|
73
|
+
} else if (proj4.defs[code] === undefined) {
|
|
74
|
+
const urn = code.split(":");
|
|
75
|
+
if (urn.length > 3) {
|
|
76
|
+
code = `${urn[urn.length - 3]}:${urn[urn.length - 1]}`;
|
|
77
|
+
}
|
|
78
|
+
if (proj4.defs[code] === undefined) {
|
|
79
|
+
throw new Error(`No projection definition for code ${code}`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return proj4(code);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* @description: 判断是否是proj4
|
|
87
|
+
* @param {*} a
|
|
88
|
+
* @return {*}
|
|
89
|
+
*/
|
|
90
|
+
_isProj4Obj(a) {
|
|
91
|
+
return typeof a.inverse !== "undefined" && typeof a.forward !== "undefined";
|
|
92
|
+
}
|
|
93
|
+
}
|