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
264 lines (252 loc) · 8.56 KB
/
Util.js
File metadata and controls
264 lines (252 loc) · 8.56 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
/* Copyright© 2000 - 2025 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 "../core/Base";
import mapboxgl from 'mapbox-gl';
import { Bounds } from "@supermapgis/iclient-common/commontypes/Bounds";
import { Point as GeometryPoint } from "@supermapgis/iclient-common/commontypes/geometry/Point";
import { Polygon } from "@supermapgis/iclient-common/commontypes/geometry/Polygon";
import { LinearRing } from "@supermapgis/iclient-common/commontypes/geometry/LinearRing";
import { GeoJSON as GeoJSONFormat } from "@supermapgis/iclient-common/format/GeoJSON";
import { isMatchAdministrativeName } from "@supermapgis/iclient-common/mapping/utils/util";
import { isArray, isString } from "@supermapgis/iclient-common/util/BaseUtil";
/**
* @name Util
* @namespace
* @category BaseTypes Util
* @description 工具类。
* @usage
* ```
* // 浏览器
* <script type="text/javascript" src="{cdn}"></script>
* <script>
* const result = {namespace}.Util.toSuperMapGeometry(geoJSON);
*
* </script>
* // ES6 Import
* import { Util } from '{npm}';
*
* const result = Util.toSuperMapGeometry(geoJSON);
* ```
*/
export const Util = {
/**
* @function Util.toSuperMapGeometry
* @description 将 GeoJSON 对象转为 SuperMap 几何图形。
* @param {GeoJSONObject} geoJSON - GeoJSON 对象。
* @returns {Geometry}
*/
toSuperMapGeometry(geoJSON) {
if (geoJSON && geoJSON.type) {
var format = new GeoJSONFormat();
var result = format.read(geoJSON, "FeatureCollection");
return result && result[0].geometry;
}
},
toSuperMapBounds(bounds) {
if (isArray(bounds)) {
//左下右上
return new Bounds(bounds[0], bounds[1], bounds[2], bounds[3]);
}
if (bounds instanceof mapboxgl.LngLatBounds) {
return new Bounds(bounds.getWest(), bounds.getSouth(), bounds.getEast(), bounds.getNorth());
}
if (bounds instanceof Bounds) {
return bounds;
}
return bounds;
},
toSuperMapPoint(lnglat) {
//客户端可传入 geojson 对象 或者 mapboxgl lnglat 点对象,或者是点数组
if (isArray(lnglat)) {
return new GeometryPoint(lnglat[0], lnglat[1]);
} else if (lnglat.lng && lnglat.lat) {
return new GeometryPoint(lnglat.lng, lnglat.lat);
}
return new GeometryPoint(lnglat.geometry.coordinates[0], lnglat.geometry.coordinates[1]);
},
/**
* @function Util.toSuperMapPolygon
* @description 将 Mapbox GL LngLatbounds 对象转为 SuperMap 几何图形。
* @param {mapboxgl.LngLatBounds} lnglatBounds - Mapbox GL LngLatbounds对象。
* @returns {GeometryPolygon}
*/
toSuperMapPolygon(lnglatBounds) {
const west = lnglatBounds.getWest();
const east = lnglatBounds.getEast();
const sourth = lnglatBounds.getSouth();
const north = lnglatBounds.getNorth();
return new Polygon([
new LinearRing([
new GeometryPoint(west, sourth),
new GeometryPoint(east, sourth),
new GeometryPoint(east, north),
new GeometryPoint(west, north)
])
]);
},
/**
* @function Util.isArray
* @description 判断是否为数组格式。
* @param {Object} obj - 待判断对象。
* @returns {boolean} 是否是数组。
*/
isArray,
/**
* @function Util.toGeoJSON
* @description 将传入对象转为 GeoJSON 格式。
* @param {Object} smObj - 待转对象。
*/
toGeoJSON(smObj) {
if (smObj) {
var format = new GeoJSONFormat();
return format.toGeoJSON(smObj);
}
},
/**
* @function Util.toProcessingParam
* @description 将 Region 节点数组转为 Processing 服务需要的分析参数。
* @param {Array} points - Region 各个节点数组。
* @returns {Object} processing 服务裁剪、查询分析的分析参数。
*/
toProcessingParam(points) {
var geometryParam = {};
if (points.length < 1) {
geometryParam = "";
} else {
var results = [];
for (var i = 0; i < points.length; i++) {
var point = {};
point.x = points[i][0];
point.y = points[i][1];
results.push(point);
}
geometryParam.type = "REGION";
geometryParam.points = results;
}
return geometryParam;
},
/**
* @function Util.extend
* @description 对象拷贝赋值。
* @param {Object} dest - 目标对象。
* @param {Object} arguments - 待拷贝的对象。
* @returns {Object} 赋值后的目标对象。
*/
extend(dest) {
for (var index = 0; index < Object.getOwnPropertyNames(arguments).length; index++) {
var arg = Object.getOwnPropertyNames(arguments)[index];
if (arg == "caller" || arg == "callee" || arg == "length" || arg == "arguments") {
continue;
}
var obj = arguments[arg];
if (obj) {
for (var j = 0; j < Object.getOwnPropertyNames(obj).length; j++) {
var key = Object.getOwnPropertyNames(obj)[j];
if (arg == "caller" || arg == "callee" || arg == "length" || arg == "arguments") {
continue;
}
dest[key] = obj[key];
}
}
}
return dest;
},
/**
* 检测数据是否为number
* @param value 值,未知数据类型
* @returns {boolean}
*/
isNumber(value) {
if (value === "") {
return false;
}
let mdata = Number(value);
if (mdata === 0) {
return true;
}
return !isNaN(mdata);
},
isString,
/**
* 随机生成id
* @param attr
* @returns {string}
*/
newGuid(attr) {
let len = attr || 32;
let guid = "";
for (let i = 1; i < len; i++) {
let n = Math.floor(Math.random() * 16.0).toString(16);
guid += n;
}
return guid;
},
/**
* @description 十六进制转 RGBA 格式。
* @param {Object} hex - 十六进制格式。
* @param {number} opacity - 不透明度Alpha。
* @returns {string} 生成的 RGBA 格式。
*/
hexToRgba(hex, opacity) {
var color = [],
rgba = [];
hex = hex.replace(/#/, "");
if (hex.length == 3) {
var tmp = [];
for (let i = 0; i < 3; i++) {
tmp.push(hex.charAt(i) + hex.charAt(i));
}
hex = tmp.join("");
}
for (let i = 0; i < 6; i += 2) {
color[i] = "0x" + hex.substr(i, 2);
rgba.push(parseInt(Number(color[i])));
}
rgba.push(opacity);
return "rgba(" + rgba.join(",") + ")";
},
/**
* @param {string} featureName 原始数据中的地名
* @param {string} fieldName 待匹配的地名
* @returns {boolean} 是否匹配
*/
isMatchAdministrativeName,
/**
* @description 墨卡托转经纬度。
* @param {Array} point - 待转换的点。
* @returns {Object} 经纬度坐标。
*/
unproject(point) {
var d = 180 / Math.PI,
r = 6378137,
ts = Math.exp(-point[1] / r),
phi = Math.PI / 2 - 2 * Math.atan(ts);
for (var i = 0, dphi = 0.1, con; i < 15 && Math.abs(dphi) > 1e-7; i++) {
con = 1;
dphi = Math.PI / 2 - 2 * Math.atan(ts * con) - phi;
phi += dphi;
}
return new mapboxgl.LngLat((point[0] * d) / r, phi * d);
},
/**
* @description url 拼接代理或者凭证信息
* @param {string} point - 待转换的 url
* @returns {string} 转换后的 url
*/
transformUrl({ url, server, excludePortalProxyUrl, credentialValue, credentialKey }) {
let mapUrl = url.indexOf('.json') === -1 ? `${url}.json` : url;
let filter = 'getUrlResource.json?url=';
if (excludePortalProxyUrl && server.indexOf(filter) > -1) {
//大屏需求,或者有加上代理的
let urlArray = server.split(filter);
if (urlArray.length > 1) {
mapUrl = urlArray[0] + filter + mapUrl;
}
}
if (credentialValue && credentialKey) {
mapUrl += '?' + credentialKey + '=' + credentialValue;
}
return mapUrl;
}
}