forked from SuperMap/iClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.js
More file actions
98 lines (92 loc) · 3.63 KB
/
Util.js
File metadata and controls
98 lines (92 loc) · 3.63 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
/* Copyright© 2000 - 2021 SuperMap Software Co.Ltd. All rights reserved.
* This program are made available under the terms of the Apache License, Version 2.0
* which accompanies this distribution and is available at http://www.apache.org/licenses/LICENSE-2.0.html.*/
import L from 'leaflet';
import { GeoJSON as GeoJSONFormat, getMeterPerMapUnit as MeterPerMapUnit } from '@supermap/iclient-common';
/**
* @namespace L.Util
* @category BaseTypes Util
*/
export var supermap_callbacks = {};
L.Util.supermap_callbacks = supermap_callbacks;
export var toGeoJSON = function(feature) {
if (!feature) {
return feature;
}
return new GeoJSONFormat().toGeoJSON(feature);
};
export var toSuperMapGeometry = function(geometry) {
if (!geometry) {
return geometry;
}
var result,
format = new GeoJSONFormat();
if (['FeatureCollection', 'Feature', 'Geometry'].indexOf(geometry.type) != -1) {
result = format.read(geometry, geometry.type);
} else if (typeof geometry.toGeoJSON === 'function') {
var geojson = geometry.toGeoJSON();
result = geojson ? format.read(geojson, geojson.type) : geometry;
}
var serverResult = result;
if (L.Util.isArray(result)) {
if (result.length === 1) {
serverResult = result[0];
} else if (result.length > 1) {
serverResult = [];
result.map(function(item) {
serverResult.push(item.geometry);
return item;
});
}
}
return serverResult && serverResult.geometry ? serverResult.geometry : serverResult;
};
export var getMeterPerMapUnit = MeterPerMapUnit;
export var resolutionToScale = function(resolution, dpi, mapUnit) {
var inchPerMeter = 1 / 0.0254;
// 地球半径。
var meterPerMapUnit = getMeterPerMapUnit(mapUnit);
var scale = resolution * dpi * inchPerMeter * meterPerMapUnit;
scale = 1 / scale;
return scale;
};
export var scaleToResolution = function(scale, dpi, mapUnit) {
var inchPerMeter = 1 / 0.0254;
var meterPerMapUnitValue = getMeterPerMapUnit(mapUnit);
var resolution = scale * dpi * inchPerMeter * meterPerMapUnitValue;
resolution = 1 / resolution;
return resolution;
};
export var GetResolutionFromScaleDpi = function(scale, dpi, coordUnit, datumAxis) {
var resolution = null,
ratio = 10000;
//用户自定义地图的Options时,若未指定该参数的值,则系统默认为6378137米,即WGS84参考系的椭球体长半轴。
datumAxis = datumAxis || 6378137;
coordUnit = coordUnit || '';
if (scale > 0 && dpi > 0) {
scale = L.Util.NormalizeScale(scale);
if (
coordUnit.toLowerCase() === 'degree' ||
coordUnit.toLowerCase() === 'degrees' ||
coordUnit.toLowerCase() === 'dd'
) {
//scale = SuperMap.Util.normalizeScale(scale);
resolution = (0.0254 * ratio) / dpi / scale / ((Math.PI * 2 * datumAxis) / 360) / ratio;
return resolution;
} else {
resolution = (0.0254 * ratio) / dpi / scale / ratio;
return resolution;
}
}
return -1;
};
export var NormalizeScale = function(scale) {
return scale > 1.0 ? 1.0 / scale : scale;
};
L.Util.toGeoJSON = toGeoJSON;
L.Util.toSuperMapGeometry = toSuperMapGeometry;
L.Util.resolutionToScale = resolutionToScale;
L.Util.scaleToResolution = scaleToResolution;
L.Util.getMeterPerMapUnit = getMeterPerMapUnit;
L.Util.GetResolutionFromScaleDpi = GetResolutionFromScaleDpi;
L.Util.NormalizeScale = NormalizeScale;