Geo3D.js
4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import * as echarts from 'echarts/lib/echarts';
import glmatrix from 'claygl/src/dep/glmatrix';
var vec3 = glmatrix.vec3;
var mat4 = glmatrix.mat4;
import textCoord from 'echarts/lib/coord/geo/fix/textCoord';
import geoCoord from 'echarts/lib/coord/geo/fix/geoCoord';
// Geo fix functions
var geoFixFuncs = [textCoord, geoCoord];
function Geo3D(name, map, geoJson, specialAreas, nameMap) {
this.name = name;
this.map = map;
this.regionHeight = 0;
this.regions = [];
this._nameCoordMap = {};
this.loadGeoJson(geoJson, specialAreas, nameMap);
this.transform = mat4.identity(new Float64Array(16));
this.invTransform = mat4.identity(new Float64Array(16));
// Which dimension to extrude. Y or Z
this.extrudeY = true;
this.altitudeAxis;
}
Geo3D.prototype = {
constructor: Geo3D,
type: 'geo3D',
dimensions: ['lng', 'lat', 'alt'],
containPoint: function () {},
loadGeoJson: function (geoJson, specialAreas, nameMap) {
var parseGeoJSON = echarts.parseGeoJSON || echarts.parseGeoJson;
try {
this.regions = geoJson ? parseGeoJSON(geoJson) : [];
}
catch (e) {
throw 'Invalid geoJson format\n' + e;
}
specialAreas = specialAreas || {};
nameMap = nameMap || {};
var regions = this.regions;
var regionsMap = {};
for (var i = 0; i < regions.length; i++) {
var regionName = regions[i].name;
// Try use the alias in nameMap
regionName = nameMap[regionName] || regionName;
regions[i].name = regionName;
regionsMap[regionName] = regions[i];
// Add geoJson
this.addGeoCoord(regionName, regions[i].getCenter());
// Some area like Alaska in USA map needs to be tansformed
// to look better
var specialArea = specialAreas[regionName];
if (specialArea) {
regions[i].transformTo(
specialArea.left, specialArea.top, specialArea.width, specialArea.height
);
}
}
this._regionsMap = regionsMap;
this._geoRect = null;
geoFixFuncs.forEach(function (fixFunc) {
fixFunc(this);
}, this);
},
getGeoBoundingRect: function () {
if (this._geoRect) {
return this._geoRect;
}
var rect;
var regions = this.regions;
for (var i = 0; i < regions.length; i++) {
var regionRect = regions[i].getBoundingRect();
rect = rect || regionRect.clone();
rect.union(regionRect);
}
// FIXME Always return new ?
return (this._geoRect = rect || new echarts.graphic.BoundingRect(0, 0, 0, 0));
},
/**
* Add geoCoord for indexing by name
* @param {string} name
* @param {Array.<number>} geoCoord
*/
addGeoCoord: function (name, geoCoord) {
this._nameCoordMap[name] = geoCoord;
},
/**
* @param {string} name
* @return {module:echarts/coord/geo/Region}
*/
getRegion: function (name) {
return this._regionsMap[name];
},
getRegionByCoord: function (coord) {
var regions = this.regions;
for (var i = 0; i < regions.length; i++) {
if (regions[i].contain(coord)) {
return regions[i];
}
}
},
setSize: function (width, height, depth) {
this.size = [width, height, depth];
var rect = this.getGeoBoundingRect();
var scaleX = width / rect.width;
var scaleZ = -depth / rect.height;
var translateX = -width / 2 - rect.x * scaleX;
var translateZ = depth / 2 - rect.y * scaleZ;
var position = this.extrudeY ? [translateX, 0, translateZ] : [translateX, translateZ, 0];
var scale = this.extrudeY ? [scaleX, 1, scaleZ] : [scaleX, scaleZ, 1];
var m = this.transform;
mat4.identity(m);
mat4.translate(m, m, position);
mat4.scale(m, m, scale);
mat4.invert(this.invTransform, m);
},
dataToPoint: function (data, out) {
out = out || [];
var extrudeCoordIndex = this.extrudeY ? 1 : 2;
var sideCoordIndex = this.extrudeY ? 2 : 1;
var altitudeVal = data[2];
// PENDING.
if (isNaN(altitudeVal)) {
altitudeVal = 0;
}
// lng
out[0] = data[0];
// lat
out[sideCoordIndex] = data[1];
if (this.altitudeAxis) {
out[extrudeCoordIndex] = this.altitudeAxis.dataToCoord(altitudeVal);
}
else {
out[extrudeCoordIndex] = 0;
}
// PENDING different region height.
out[extrudeCoordIndex] += this.regionHeight;
vec3.transformMat4(out, out, this.transform);
return out;
},
pointToData: function (point, out) {
// TODO
}
};
export default Geo3D;