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
101 lines (91 loc) · 2.96 KB
/
Util.js
File metadata and controls
101 lines (91 loc) · 2.96 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
import mapboxgl from 'mapbox-gl';
import '../core/Base';
import {Bounds, GeometryPoint, GeoJSON as GeoJSONFormat} from '@supermap/iclient-common';
/**
* @class mapboxgl.supermap.Util
* @classdesc 工具类
*/
export class Util {
/**
* @function mapboxgl.supermap.Util.toSuperMapGeometry
* @description 将 geoJSON 对象转为SuperMap几何图形
* @param geoJSON - {Object} geoJSON 对象
*/
static toSuperMapGeometry(geoJSON) {
if (geoJSON && geoJSON.type) {
var format = new GeoJSONFormat();
var result = format.read(geoJSON, "FeatureCollection");
return result[0].geometry;
}
}
static toSuperMapBounds(bounds) {
if (this.isArray(bounds)) {
//左下右上
return new Bounds(
bounds[0],
bounds[1],
bounds[2],
bounds[3],
);
}
return new Bounds(
bounds.getWest(),
bounds.getSouth(),
bounds.getEast(),
bounds.getNorth()
);
}
static toSuperMapPoint(lnglat) {
//客户端可传入 geojson 对象 或者 mapboxgl lnglat 点对象,或者是点数组
if (this.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 mapboxgl.supermap.Util.isArray
* @description 判断是否为数组格式
* @param obj - {Object} 待判断对象
* @return {boolean} 是否是数组
*/
static isArray(obj) {
return Object.prototype.toString.call(obj) == '[object Array]'
}
/**
* @function mapboxgl.supermap.Util.toGeoJSON
* @description 将传入对象转为 GeoJSON 格式
* @param smObj - {Object} 待转参数
*/
static toGeoJSON(smObj) {
if (smObj) {
var format = new GeoJSONFormat();
return JSON.parse(format.write(smObj));
}
}
/**
* @function mapboxgl.supermap.Util.toProcessingParam
* @description 将Region节点数组转为Processing服务需要的分析参数
* @param points - Region各个节点数组
* @return 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);
}
geometryParam.type = "REGION";
geometryParam.points = results;
}
return geometryParam;
}
}
mapboxgl.supermap.Util = Util;