-
Notifications
You must be signed in to change notification settings - Fork 4
/
FeatureService.js
233 lines (217 loc) · 7.37 KB
/
FeatureService.js
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
import cors from './../Utils/cors';
import Service from './Service';
import MetadataTask from './../Support/MetadataTask';
import QueryTask from './../Support/QueryTask';
import FeaturesTask from './../Support/FeaturesTask';
import { Util } from 'maptalks';
import { outService } from '../servicequeryqueue';
import Promise from './../Utils/Promise';
/**
* default ImageService options
*/
const options = {
proxy: false,
useCors: cors,
timeout: 0
};
/**
* imageService对应arcgis发布的的supported operations
* -Export Image
* -Query
* -Identify
* -Coumpute Histograms
* -Get Samples
* -Compute Class Statistics
*/
export default class FeatureService extends Service {
constructor(options = {}) {
super(options.url, options);
this.gridQueries = {};
this.gridIndex = 0;
}
queryInGrids(queryId) {
return this.gridQueries[queryId];
}
/**
* 获取服务的 ?f=json 配置信息
* @returns {Promise}
*/
metadata() {
/**
* @type {MetadataTask}
*/
this._metadataTask = this._metadataTask || new MetadataTask(this);
return this._metadataTask.run();
}
_QueryParams(option) {
const geometry = option.geometry || '';
const condition = {
where: option.where || '1=1',
geometry: (geometry instanceof Object) ? JSON.stringify(geometry) : geometry,
geometryType: option.geometryType || 'esriGeometryPoint',
inSR: option.inSR || '',
spatialRel: option.esriSpatialRelIntersects || 'esriSpatialRelIntersects',
relationParam: option.relationParam || '',
objectIds: option.objectIds || '',
time: option.time || '',
returnCountOnly: option.returnCountOnly || false,
returnGeometry: option.returnGeometry || true,
maxAllowableOffset: option.maxAllowableOffset || '',
outSR: option.outSR || '',
text: option.text || '',
outFields: option.outFields || '*'
};
return condition;
}
_AddParams(features) {
return {
features: features,
gdbVersion: '',
rollbackOnFailure: true,
f: 'pjson'
}
}
_UpdateParams(features) {
return {
features: features,
gdbVersion: '',
rollbackOnFailure: true,
f: 'pjson'
}
}
_deleteParams(option) {
if (option.objectIds) {
return {
objectIds: option.objectIds,
f: 'pjson'
}
} else {
const geometry = option.geometry;
return {
where: option.where || '1=1',
geometry: (geometry instanceof Object) ? JSON.stringify(geometry) : geometry,
geometryType: option.geometryType || 'esriGeometryPoint',
inSR: option.inSR || '',
spatialRel: option.esriSpatialRelIntersects || 'esriSpatialRelIntersects',
f: 'pjson'
};
}
}
/**
*
* @param {*} extent
* @returns
*/
quarterExtent(extent) {
const [xmin, ymin, xmax, ymax] = extent.split(',').map(number => {
return parseFloat(number);
});
const extents = [];
for (let i = 0, cols = 2; i < cols; i++) {
const minx = xmin + (xmax - xmin) / cols * i, maxx = minx + (xmax - xmin) / cols;
for (let j = 0, rows = 2; j < rows; j++) {
const miny = ymin + (ymax - ymin) / rows * j, maxy = miny + (ymax - ymin) / rows;
extents.push([minx, miny, maxx, maxy].join(','));
}
}
return extents;
}
_getGrids(params) {
const extents = this.quarterExtent(params.geometry);
const grids = extents.map(extent => {
const queryId = this.getQueryTaskId();
const service = Util.extend({ id: this.id, queryId, result: null });
this.gridQueries[queryId] = service;
const options = Util.extend({}, params, { geometry: extent, __queryId: queryId });
const task = new QueryTask(this);
task.params = options;
return {
queryId,
params: options,
task
};
});
return grids;
}
_gridsQuery(grids, callback) {
grids.forEach(grid => {
grid.task.run().then(res => {
const { queryId, params } = grid;
let result = {};
try {
result = JSON.parse(res);
} catch (error) {
console.error(error);
}
if (result && result.features.length >= 1000) {
//delete parent tile
delete this.gridQueries[queryId];
//tile children
const grids = this._getGrids(params);
this._gridsQuery(grids, callback);
} else if (this.gridQueries[queryId]) {
this.gridQueries[queryId].result = result;
this.gridIndex++;
}
if (this.gridIndex === Object.keys(this.gridQueries).length) {
callback();
}
})
});
}
_mergeGridQueryResult() {
const result = {};
for (const id in this.gridQueries) {
Util.extend(result, this.gridQueries[id].result);
}
result.features = [];
for (const id in this.gridQueries) {
const features = (this.gridQueries[id].result || {}).features || [];
features.forEach(feature => {
result.features.push(feature);
});
}
return result;
}
query(option = {}) {
for (const id in this.gridQueries) {
outService(this.gridQueries[id]);
}
this.gridQueries = {};
this.gridIndex = 0;
const params = this._QueryParams(option);
if (option.gridQuery) {
const grids = this._getGrids(params);
return new Promise((resolve, reject) => {
this._gridsQuery(grids, () => {
resolve(JSON.stringify(this._mergeGridQueryResult()));
});
});
} else {
this._queryTask = this._queryTask || new QueryTask(this);
this._queryTask.params = params;
return this._queryTask.run();
}
}
addFeatures(features) {
const params = this._AddParams(features);
this._FeaturesTask = this._FeaturesTask || new FeaturesTask(this);
this._FeaturesTask.params = params;
this._FeaturesTask._path = 'addFeatures';
return this._FeaturesTask.run();
}
updateFeatures(features) {
const params = this._UpdateParams(features);
this._FeaturesTask = this._FeaturesTask || new FeaturesTask(this);
this._FeaturesTask.params = params;
this._FeaturesTask._path = 'updateFeatures';
return this._FeaturesTask.run();
}
deleteFeatures(option = {}) {
const params = this._deleteParams(option);
this._FeaturesTask = this._FeaturesTask || new FeaturesTask(this);
this._FeaturesTask.params = params;
this._FeaturesTask._path = 'deleteFeatures';
return this._FeaturesTask.run();
}
}