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
268 lines (247 loc) · 9.3 KB
/
Util.js
File metadata and controls
268 lines (247 loc) · 9.3 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
/* Copyright© 2000 - 2018 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 ol from 'openlayers';
import {Unit, Bounds, GeoJSON as GeoJSONFormat} from '@supermap/iclient-common';
ol.supermap = ol.supermap || {};
/**
* @class ol.supermap.Util
* @category BaseTypes Util
* @classdesc 工具类。
*/
export class Util {
constructor() {
}
/**
* @function ol.supermap.Util.toGeoJSON
* @description 将传入对象转为 GeoJSON 格式。
* @param {Object} smObj - 待转参数。
*/
static toGeoJSON(smObj) {
if (smObj) {
var format = new GeoJSONFormat();
return JSON.parse(format.write(smObj));
}
}
/**
* @function ol.supermap.Util.toSuperMapGeometry
* @description 将 GeoJSON 对象转为 SuperMap 几何图形。
* @param {GeoJSONObject} geoJSON - GeoJSON 对象。
*/
static toSuperMapGeometry(geoJSON) {
if (geoJSON && geoJSON.type) {
var format = new GeoJSONFormat();
var result = format.read(geoJSON, "FeatureCollection");
return result[0].geometry;
}
}
/**
* @function ol.supermap.Util.resolutionToScale
* @description 通过分辨率计算比例尺。
* @param {number} resolution - 分辨率。
* @param {number} dpi - 屏幕分辨率。
* @param {string} mapUnit - 地图单位。
* @returns {number} 比例尺。
*/
static resolutionToScale(resolution, dpi, mapUnit) {
var inchPerMeter = 1 / 0.0254;
// 地球半径。
var meterPerMapUnit = this.getMeterPerMapUnit(mapUnit);
var scale = resolution * dpi * inchPerMeter * meterPerMapUnit;
scale = 1 / scale;
return scale;
}
/**
* @function ol.supermap.Util.toSuperMapBounds
* @description 转为 SuperMapBounds 格式。
* @param {Array.<number>} bounds - bounds 数组。
* @returns {SuperMap.Bounds} 返回 SuperMap 的 Bounds 对象。
*/
static toSuperMapBounds(bounds) {
return new Bounds(
bounds[0],
bounds[1],
bounds[2],
bounds[3]
);
}
/**
* @function ol.supermap.Util.toProcessingParam
* @description 将 Region 节点数组转为 Processing 服务需要的分析参数。
* @param {Array} points - Region 各个节点数组。
* @returns processing 服务裁剪、查询分析的分析参数。
*/
static 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);
}
results.push(results[0]);
geometryParam.type = "REGION";
geometryParam.points = results;
}
return geometryParam;
}
/**
* @function ol.supermap.Util.scaleToResolution
* @description 通过比例尺计算分辨率。
* @param {number} scale - 比例尺。
* @param {number} dpi - 屏幕分辨率。
* @param {string} mapUnit - 地图单位。
* @returns {number} 分辨率。
*/
static scaleToResolution(scale, dpi, mapUnit) {
var inchPerMeter = 1 / 0.0254;
var meterPerMapUnitValue = this.getMeterPerMapUnit(mapUnit);
var resolution = scale * dpi * inchPerMeter * meterPerMapUnitValue;
resolution = 1 / resolution;
return resolution;
}
/**
* @private
* @function ol.supermap.Util.getMeterPerMapUnit
* @description 获取每地图单位多少米。
* @param {string} mapUnit - 地图单位。
* @returns {number} 返回每地图单位多少米。
*/
static getMeterPerMapUnit(mapUnit) {
var earchRadiusInMeters = 6378137;
var meterPerMapUnit;
if (mapUnit === Unit.METER) {
meterPerMapUnit = 1;
} else if (mapUnit === Unit.DEGREE) {
// 每度表示多少米。
meterPerMapUnit = Math.PI * 2 * earchRadiusInMeters / 360;
} else if (mapUnit === Unit.KILOMETER) {
meterPerMapUnit = 1.0E-3;
} else if (mapUnit === Unit.INCH) {
meterPerMapUnit = 1 / 2.5399999918E-2;
} else if (mapUnit === Unit.FOOT) {
meterPerMapUnit = 0.3048;
} else {
return meterPerMapUnit;
}
return meterPerMapUnit;
}
/**
* @function ol.supermap.Util.isArray
* @description 判断是否为数组格式。
* @param {Object} obj - 待判断对象。
* @returns {boolean} 是否是数组。
*/
static isArray(obj) {
return Object.prototype.toString.call(obj) == '[object Array]'
}
/**
* @function ol.supermap.Util.Csv2GeoJSON
* @description 将 csv 格式转为 GeoJSON。
* @param {Object} csv - csv 对象。
* @param {Object} options - 转换参数。
*/
static Csv2GeoJSON(csv, options) {
var defaultOptions = {
titles: ['lon', 'lat'],
latitudeTitle: 'lat',
longitudeTitle: 'lon',
fieldSeparator: ',',
lineSeparator: '\n',
deleteDoubleQuotes: true,
firstLineTitles: false
};
options = options || defaultOptions;
var _propertiesNames = []
if (typeof csv === 'string') {
var titulos = options.titles;
if (options.firstLineTitles) {
csv = csv.split(options.lineSeparator);
if (csv.length < 2) {
return;
}
titulos = csv[0];
csv.splice(0, 1);
csv = csv.join(options.lineSeparator);
titulos = titulos.trim().split(options.fieldSeparator);
for (let i = 0; i < titulos.length; i++) {
titulos[i] = _deleteDoubleQuotes(titulos[i]);
}
options.titles = titulos;
}
for (let i = 0; i < titulos.length; i++) {
var prop = titulos[i].toLowerCase().replace(/[^\w ]+/g, '').replace(/ +/g, '_');
if (prop == '' || prop == '_') {
prop = 'prop-' + i;
}
_propertiesNames[i] = prop;
}
csv = _csv2json(csv);
}
return csv;
function _deleteDoubleQuotes(cadena) {
if (options.deleteDoubleQuotes) {
cadena = cadena.trim().replace(/^"/, "").replace(/"$/, "");
}
return cadena;
}
function _csv2json(csv) {
var json = {};
json["type"] = "FeatureCollection";
json["features"] = [];
var titulos = options.titles;
csv = csv.split(options.lineSeparator);
for (var num_linea = 0; num_linea < csv.length; num_linea++) {
var campos = csv[num_linea].trim().split(options.fieldSeparator)
, lng = parseFloat(campos[titulos.indexOf(options.longitudeTitle)])
, lat = parseFloat(campos[titulos.indexOf(options.latitudeTitle)]);
var isInRange = lng < 180 && lng > -180 && lat < 90 && lat > -90;
if (!(campos.length == titulos.length && isInRange)) {
continue;
}
var feature = {};
feature["type"] = "Feature";
feature["geometry"] = {};
feature["properties"] = {};
feature["geometry"]["type"] = "Point";
feature["geometry"]["coordinates"] = [lng, lat];
for (var i = 0; i < titulos.length; i++) {
if (titulos[i] != options.latitudeTitle && titulos[i] != options.longitudeTitle) {
feature["properties"][_propertiesNames[i]] = _deleteDoubleQuotes(campos[i]);
}
}
json["features"].push(feature);
}
return json;
}
}
/**
* @function ol.supermap.Util.createCanvasContext2D
* @description 创建 2D 画布。
* @param {number} opt_width - 画布宽度。
* @param {number} opt_height - 画布高度。
*/
static createCanvasContext2D(opt_width, opt_height) {
var canvas = document.createElement('CANVAS');
if (opt_width) {
canvas.width = opt_width;
}
if (opt_height) {
canvas.height = opt_height;
}
return canvas.getContext('2d');
}
/**
* @function ol.supermap.Util.supportWebGL2
* @description 是否支持 webgl2。
*/
static supportWebGL2() {
var canvas = document.createElement('canvas');
return Boolean(canvas && canvas.getContext("webgl2"));
}
}
ol.supermap.Util = Util;