forked from SuperMap/iClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapCalculateUtil.js
More file actions
338 lines (324 loc) · 9.25 KB
/
MapCalculateUtil.js
File metadata and controls
338 lines (324 loc) · 9.25 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import { Unit } from '../REST';
/**
* @private
* @function getMeterPerMapUnit
* @description 单位换算,把米|度|千米|英寸|英尺换算成米。
* @category BaseTypes Util
* @param {string} mapUnit 需要换算的地图单位。
* @returns {number} 返回地图的距离单位。
* @usage
* ```
* // 浏览器
* <script type="text/javascript" src="{cdn}"></script>
* <script>
* const result = {namespace}.getMeterPerMapUnit(mapUnit);
*
* </script>
*
* // ES6 Import
* import { getMeterPerMapUnit } from '{npm}';
*
* const result = getMeterPerMapUnit(mapUnit);
* ```
*/
export var getMeterPerMapUnit = function(mapUnit) {
var meterPerMapUnit;
if(!mapUnit){
return meterPerMapUnit;
}
var earchRadiusInMeters = 6378137;
if (['m','meter','meters'].indexOf(mapUnit.toLocaleLowerCase())>-1) {
meterPerMapUnit = 1;
} else if (['degrees','deg','degree','dd'].indexOf(mapUnit.toLocaleLowerCase())>-1) {
// 每度表示多少米。
meterPerMapUnit = (Math.PI * 2 * earchRadiusInMeters) / 360;
} else if (mapUnit === Unit.KILOMETER) {
meterPerMapUnit = 1000;
} else if (mapUnit === Unit.INCH) {
meterPerMapUnit = 2.5399999918e-2;
} else if (mapUnit === Unit.FOOT) {
meterPerMapUnit = 0.3048;
}
return meterPerMapUnit;
};
/**
* @private
* @function getSquareMeterPerMapUnit
* @description 单位换算,把平方米|平方千米|平方英寸|平方英尺换算成平方米。
* @category BaseTypes Util
* @param {string} mapUnit 需要换算的地图面积单位。
* @returns {number} 返回地图的面积单位。
* @usage
* ```
* // 浏览器
* <script type="text/javascript" src="{cdn}"></script>
* <script>
* const result = {namespace}.getSquareMeterPerMapUnit(mapUnit);
*
* </script>
*
* // ES6 Import
* import { getMeterPerMapUnit } from '{npm}';
*
* const result = getMeterPerMapUnit(mapUnit);
* ```
*/
const AREA_MAP = {
SquareFoot: 10.763910417,
SquareKiloMeter: 0.000001,
SquareMeter: 1,
SquareMile: 3.86e-7,
SquareYard: 1.195990046
}
export var getSquareMeterPerMapUnit = function(mapUnit) {
return AREA_MAP[mapUnit];
};
/**
* @private
* @function getWrapNum
* @description 获取该坐标系的经纬度范围的经度或纬度。
* @category BaseTypes Util
* @param {number} x 经度或纬度。
* @param {boolean} includeMax 是否获取经度或纬度的最大值。
* @param {boolean} includeMin 是否获取经度或纬度的最小值。
* @param {number} range 坐标系的经纬度范围。
* @returns {number} 返回经度或纬度的值。
* @usage
* ```
* // 浏览器
* <script type="text/javascript" src="{cdn}"></script>
* <script>
* const result = {namespace}.getWrapNum(x, includeMax, includeMin, range);
*
* </script>
*
* // ES6 Import
* import { getWrapNum } from '{npm}';
*
* const result = getWrapNum(x, includeMax, includeMin, range);
* ```
*/
export function getWrapNum(x, includeMax = true, includeMin = true, range = [-180, 180]) {
var max = range[1],
min = range[0],
d = max - min;
if (x === max && includeMax) {
return x;
}
if (x === min && includeMin) {
return x;
}
var tmp = (((x - min) % d) + d) % d;
if (tmp === 0 && includeMax) {
return max;
}
return ((((x - min) % d) + d) % d) + min;
}
/**
* @private
* @function conversionDegree
* @description 转换经纬度。
* @category BaseTypes Util
* @param {number} degrees 经度或纬度。
* @returns {string} 返回度分秒。
* @usage
* ```
* // 浏览器
* <script type="text/javascript" src="{cdn}"></script>
* <script>
* const result = {namespace}.conversionDegree(degrees);
*
* </script>
*
* // ES6 Import
* import { conversionDegree } from '{npm}';
*
* const result = conversionDegree(degrees);
* ```
*/
export function conversionDegree(degrees) {
const degree = parseInt(degrees);
let fraction = parseInt((degrees - degree) * 60);
let second = parseInt(((degrees - degree) * 60 - fraction) * 60);
fraction = parseInt(fraction / 10) === 0 ? `0${fraction}` : fraction;
second = parseInt(second / 10) === 0 ? `0${second}` : second;
return `${degree}°${fraction}'${second}`;
}
/**
* @private
* @function scalesToResolutions
* @description 通过比例尺数组计算分辨率数组,没有传入比例尺数组时通过地图范围与地图最大级别进行计算。
* @version 11.0.1
* @param {Array} scales - 比例尺数组。
* @param {Object} bounds - 地图范围。
* @param {number} dpi - 屏幕分辨率。
* @param {string} mapUnit - 地图单位。
* @param {number} [level=22] - 地图最大级别。
* @returns {number} 分辨率。
* @usage
* ```
* // 浏览器
* <script type="text/javascript" src="{cdn}"></script>
* <script>
* const result = {namespace}.scalesToResolutions(scales, bounds, dpi, mapUnit);
*
* </script>
*
* // ES6 Import
* import { scalesToResolutions } from '{npm}';
*
* const result = scalesToResolutions(scales, bounds, dpi, mapUnit);
* ```
*/
export function scalesToResolutions(scales, bounds, dpi, mapUnit, level = 22, baseScale) {
var resolutions = [];
if (scales && scales.length > 0) {
for (let i = 0; i < scales.length; i++) {
resolutions.push(scaleToResolution(scales[i], dpi, mapUnit));
}
} else if (baseScale){
const maxReolution = Math.abs(bounds.left - bounds.right) / 256;
const baseRes = scaleToResolution(baseScale, dpi, mapUnit);
let topRes = baseRes;
for (let i = 0; i < level; i++) {
const temp = baseRes * Math.pow(2, i);
if(Math.abs(temp,maxReolution)<= 1E-6 || temp>maxReolution){
topRes = temp;
break;
}
}
for (let i = 0; i < level; i++) {
resolutions.push(topRes / Math.pow(2, i));
}
} else {
const maxReolution = Math.abs(bounds.left - bounds.right) / 256;
for (let i = 0; i < level; i++) {
resolutions.push(maxReolution / Math.pow(2, i));
}
}
return resolutions.sort(function (a, b) {
return b - a;
});
}
/**
* @private
* @function getZoomByResolution
* @description 通过分辨率获取地图级别。
* @version 11.0.1
* @param {number} resolution - 分辨率。
* @param {Array} resolutions - 分辨率数组。
* @returns {number} 地图级别。
* @usage
* ```
* // 浏览器
* <script type="text/javascript" src="{cdn}"></script>
* <script>
* const result = {namespace}.getZoomByResolution(resolution, resolutions);
*
* </script>
*
* // ES6 Import
* import { getZoomByResolution } from '{npm}';
*
* const result = getZoomByResolution(resolution, resolutions);
* ```
*/
export function getZoomByResolution(resolution, resolutions) {
let zoom = 0;
let minDistance;
for (let i = 0; i < resolutions.length; i++) {
if (i === 0) {
minDistance = Math.abs(resolution - resolutions[i]);
}
if (minDistance > Math.abs(resolution - resolutions[i])) {
minDistance = Math.abs(resolution - resolutions[i]);
zoom = i;
}
}
return zoom;
}
/**
* @private
* @function scaleToResolution
* @description 通过比例尺计算分辨率。
* @version 11.0.1
* @param {number} scale - 比例尺。
* @param {number} dpi - 屏幕分辨率。
* @param {string} mapUnit - 地图单位。
* @returns {number} 分辨率。
* @usage
* ```
* // 浏览器
* <script type="text/javascript" src="{cdn}"></script>
* <script>
* const result = {namespace}.scaleToResolution(scale, dpi, mapUnit);
*
* </script>
*
* // ES6 Import
* import { scaleToResolution } from '{npm}';
*
* const result = scaleToResolution(scale, dpi, mapUnit);
* ```
*/
export function scaleToResolution(scale, dpi, mapUnit) {
const inchPerMeter = 1 / 0.0254;
const meterPerMapUnitValue = getMeterPerMapUnit(mapUnit);
const resolution = 1 / (scale * dpi * inchPerMeter * meterPerMapUnitValue);
return resolution;
}
export function getDpi(scale, resolution, mapUnit) {
const inchPerMeter = 1 / 0.0254;
const meterPerMapUnitValue = getMeterPerMapUnit(mapUnit);
const dpi = 1.0/resolution/(scale * inchPerMeter * meterPerMapUnitValue);
return dpi;
}
/**
* @private
* 范围是否相交。
* @param {Array} extent1 范围 1。
* @param {Array} extent2 范围 2。
* @return {boolean} 范围是否相交。
*/
export function intersects(extent1, extent2) {
return (
extent1[0] <= extent2[2] &&
extent1[2] >= extent2[0] &&
extent1[1] <= extent2[3] &&
extent1[3] >= extent2[1]
);
}
/**
* @private
* 获取两个范围的交集。
* @param {Array} extent1 范围 1。
* @param {Array} extent2 范围 2。
* @return {Array} 相交范围数组。
* @api
*/
export function getIntersection(extent1, extent2) {
const intersection = [];
if (intersects(extent1, extent2)) {
if (extent1[0] > extent2[0]) {
intersection[0] = extent1[0];
} else {
intersection[0] = extent2[0];
}
if (extent1[1] > extent2[1]) {
intersection[1] = extent1[1];
} else {
intersection[1] = extent2[1];
}
if (extent1[2] < extent2[2]) {
intersection[2] = extent1[2];
} else {
intersection[2] = extent2[2];
}
if (extent1[3] < extent2[3]) {
intersection[3] = extent1[3];
} else {
intersection[3] = extent2[3];
}
}
return intersection;
}