forked from SuperMap/iClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeatureService.js
More file actions
208 lines (195 loc) · 8.57 KB
/
Copy pathFeatureService.js
File metadata and controls
208 lines (195 loc) · 8.57 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
/* Copyright© 2000 - 2023 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 mapboxgl from 'mapbox-gl';
import '../core/Base';
import { Util } from '../core/Util';
import { ServiceBase } from './ServiceBase';
import { Util as CommonUtil} from '@supermap/iclient-common/commontypes/Util';
import { EditFeaturesService } from '@supermap/iclient-common/iServer/EditFeaturesService';
import { Point as GeometryPoint } from '@supermap/iclient-common/commontypes/geometry/Point';
import { Geometry } from '@supermap/iclient-common/commontypes/Geometry';
import { FeatureService as CommonFeatureService } from '@supermap/iclient-common/iServer/FeatureService';
/**
* @class FeatureService
* @category iServer Data Feature
* @classdesc 要素数据集类。提供:ID 查询、范围查询、SQL 查询、几何查询、bounds 查询、缓冲区查询、地物编辑。
* @modulecategory Services
* @example
* new FeatureService(url)
* .getFeaturesByIDs(param,function(result){
* //doSomething
* })
* @extends {ServiceBase}
* @param {string} url - 服务地址。
* @param {Object} options -参数。
* @param {string} [options.proxy] - 服务代理地址。
* @param {boolean} [options.withCredentials=false] - 请求是否携带 cookie。
* @param {boolean} [options.crossOrigin] - 是否允许跨域请求。
* @param {Object} [options.headers] - 请求头。
* @usage
*/
export class FeatureService extends ServiceBase {
constructor(url, options) {
super(url, options);
this._featureService = new CommonFeatureService(url, options);
}
/**
* @function FeatureService.prototype.getFeaturesByIDs
* @description 数据集 ID 查询服务。
* @param {GetFeaturesByIDsParameters} params - ID查询参数类。
* @param {RequestCallback} callback - 回调函数。
* @param {DataFormat} [resultFormat=DataFormat.GEOJSON] - 返回结果类型。
*/
getFeaturesByIDs(params, callback, resultFormat) {
params = this._processParams(params);
this._featureService.getFeaturesByIDs(params, callback, resultFormat);
}
/**
* @function FeatureService.prototype.getFeaturesByBounds
* @description 数据集 Bounds 查询服务。
* @param {GetFeaturesByBoundsParameters} params - 数据集范围查询参数类。
* @param {RequestCallback} callback - 回调函数。
* @param {DataFormat} [resultFormat=DataFormat.GEOJSON] - 返回结果类型。
*/
getFeaturesByBounds(params, callback, resultFormat) {
params = this._processParams(params);
this._featureService.getFeaturesByBounds(params, callback, resultFormat);
}
/**
* @function FeatureService.prototype.getFeaturesByBuffer
* @description 数据集 Buffer 查询服务。
* @param {GetFeaturesByBufferParameters} params - 数据集缓冲区查询参数类。
* @param {RequestCallback} callback 回调函数。
* @param {DataFormat} [resultFormat=DataFormat.GEOJSON] - 返回结果类型。
*/
getFeaturesByBuffer(params, callback, resultFormat) {
params = this._processParams(params);
this._featureService.getFeaturesByBuffer(params, callback, resultFormat);
}
/**
* @function FeatureService.prototype.getFeaturesBySQL
* @description 数据集 SQL 查询服务。
* @param {GetFeaturesBySQLParameters} params - 数据集 SQL 查询参数类。
* @param {RequestCallback} callback 回调函数。
* @param {DataFormat} [resultFormat=DataFormat.GEOJSON] - 返回结果类型。
*/
getFeaturesBySQL(params, callback, resultFormat) {
params = this._processParams(params);
this._featureService.getFeaturesBySQL(params, callback, resultFormat);
}
/**
* @function FeatureService.prototype.getFeaturesByGeometry
* @description 数据集几何查询服务类。
* @param {GetFeaturesByGeometryParameters} params - 数据集几何查询参数类。
* @param {RequestCallback} callback - 回调函数。
* @param {DataFormat} [resultFormat=DataFormat.GEOJSON] - 返回结果类型。
*/
getFeaturesByGeometry(params, callback, resultFormat) {
params = this._processParams(params);
this._featureService.getFeaturesByGeometry(params, callback, resultFormat);
}
/**
* @function FeatureService.prototype.editFeatures
* @description 地物编辑服务。
* @param {EditFeaturesParameters} params - 数据集添加、修改、删除参数类。
* @param {RequestCallback} callback 回调函数。
*/
editFeatures(params, callback) {
if (!params || !params.dataSourceName || !params.dataSetName) {
return;
}
var me = this,
url = me.url,
dataSourceName = params.dataSourceName,
dataSetName = params.dataSetName;
url = CommonUtil.urlPathAppend(url, 'datasources/' + dataSourceName + '/datasets/' + dataSetName);
var editFeatureService = new EditFeaturesService(url, {
proxy: me.options.proxy,
withCredentials: me.options.withCredentials,
crossOrigin: me.options.crossOrigin,
headers: me.options.headers,
eventListeners: {
processCompleted: callback,
processFailed: callback
}
});
editFeatureService.processAsync(me._processParams(params));
}
/**
* @function FeatureService.prototype.getMetadata
* @description 地理要素元信息。
* @param {Object} params - 包括数据源名称、数据集名称、要素ID。
* @param {RequestCallback} callback - 回调函数。
*/
getMetadata(params, callback) {
this._featureService.getMetadata(params, callback);
}
/**
* @private
* @description 参数类型转换。
* @param {Object} params - 参数 。
* @returns {Object} params - 转换后的对接 SuperMap 服务的参数。
*/
_processParams(params) {
if (!params) {
return {};
}
var me = this;
params.returnContent = params.returnContent == null ? true : params.returnContent;
params.fromIndex = params.fromIndex ? params.fromIndex : 0;
params.toIndex = params.toIndex === 0 ? 0 : params.toIndex ? params.toIndex : -1;
if (params.bounds) {
params.bounds = Util.toSuperMapBounds(params.bounds);
}
if (params.editType) {
params.editType = params.editType.toLowerCase();
}
//mapboxgl geojson要素对象转 SuperMap Geometry 对象
if (params.geometry) {
if (params.geometry instanceof mapboxgl.LngLatBounds) {
params.geometry = Util.toSuperMapPolygon(params.geometry);
params.geometry.SRID = 4326;
} else if (params.geometry instanceof mapboxgl.Point) {
params.geometry = new GeometryPoint(params.geometry.x, params.geometry.y);
} else if (params.geometry instanceof mapboxgl.LngLat) {
params.geometry = new GeometryPoint(params.geometry.lng, params.geometry.lat);
params.geometry.SRID = 4326;
} else if (!(params.geometry instanceof Geometry)) {
params.geometry = Util.toSuperMapGeometry(params.geometry);
}
}
//editFeature服务参数转换,传入单独得对象或对象数组
if (params.features) {
var features = [];
if (Util.isArray(params.features)) {
params.features.map(function (feature) {
features.push(me._createServerFeature(feature));
return features;
});
} else {
features.push(me._createServerFeature(params.features));
}
params.features = features;
}
return params;
}
//geoFeature严格按照 mapboxgl geojson的结构
_createServerFeature(geoFeature) {
var feature = {},
fieldNames = [],
fieldValues = [];
var properties = geoFeature.properties;
for (var key in properties) {
fieldNames.push(key);
fieldValues.push(properties[key]);
}
feature.fieldNames = fieldNames;
feature.fieldValues = fieldValues;
if (geoFeature.id) {
feature.id = geoFeature.id;
}
feature.geometry = Util.toSuperMapGeometry(geoFeature);
return feature;
}
}