Skip to content

Commit fe0b011

Browse files
修复多次调用获取要素服务导致参数类被修改, 补充 getFeaturesCount getFeaturesDatasetInfo ut review by luox
1 parent 7b7581a commit fe0b011

File tree

12 files changed

+469
-8
lines changed

12 files changed

+469
-8
lines changed

src/common/iServer/GetFeaturesByBufferService.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@ export class GetFeaturesByBufferService extends GetFeaturesServiceBase {
4747
* @returns {Object} 转化后的 JSON 字符串。
4848
*/
4949
getJsonParameters(params) {
50-
if (!(params instanceof GetFeaturesByBufferParameters)) {
51-
return;
52-
}
53-
return GetFeaturesByBufferParameters.toJsonParameters(params);
50+
return GetFeaturesByBufferParameters.toJsonParameters(params);
5451
}
5552

5653
}

src/common/iServer/GetFeaturesServiceBase.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,15 @@ export class GetFeaturesServiceBase extends CommonServiceBase {
129129
console.warn('recommend set returnFeaturesOnly config to true to imporve performance. if need get Total amount and Dataset information. FeatureService provide getFeaturesCount and getFeaturesDatasetInfo method');
130130
}
131131
if (params.returnCountOnly) {
132-
me.url = Util.urlAppend(me.url, "&returnCountOnly=" + params.returnCountOnly)
132+
me.url = Util.urlAppend(me.url, "returnCountOnly=" + params.returnCountOnly)
133133
}
134134

135135
if (params.returnDatasetInfoOnly) {
136-
me.url = Util.urlAppend(me.url, "&returnDatasetInfoOnly=" + params.returnDatasetInfoOnly)
136+
me.url = Util.urlAppend(me.url, "returnDatasetInfoOnly=" + params.returnDatasetInfoOnly)
137137
}
138138

139139
if (params.returnFeaturesOnly) {
140-
me.url = Util.urlAppend(me.url, "&returnFeaturesOnly=" + params.returnFeaturesOnly)
140+
me.url = Util.urlAppend(me.url, "returnFeaturesOnly=" + params.returnFeaturesOnly)
141141
}
142142
}
143143

src/leaflet/core/CommontypesConversion.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ export const CommontypesConversion = {
4141
bounds.max.y
4242
);
4343
}
44+
if (bounds instanceof Bounds) {
45+
return bounds;
46+
}
4447
if (isArray(bounds)) {
4548
return new Bounds(
4649
bounds[0],

src/mapboxgl/core/Util.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ export const Util = {
5858
if (bounds instanceof mapboxgl.LngLatBounds) {
5959
return new Bounds(bounds.getWest(), bounds.getSouth(), bounds.getEast(), bounds.getNorth());
6060
}
61+
if (bounds instanceof Bounds) {
62+
return bounds;
63+
}
6164
return bounds;
6265
},
6366

src/maplibregl/core/Util.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ export const Util = {
5858
if (bounds instanceof maplibregl.LngLatBounds) {
5959
return new Bounds(bounds.getWest(), bounds.getSouth(), bounds.getEast(), bounds.getNorth());
6060
}
61+
if (bounds instanceof Bounds) {
62+
return bounds;
63+
}
6164
return bounds;
6265
},
6366

src/openlayers/core/Util.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@
9898
* @returns {Bounds} 返回 SuperMap 的 Bounds 对象。
9999
*/
100100
toSuperMapBounds(bounds) {
101+
if (bounds instanceof Bounds) {
102+
return bounds;
103+
}
101104
return new Bounds(bounds[0], bounds[1], bounds[2], bounds[3]);
102105
},
103106

src/openlayers/services/FeatureService.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { FeatureService as CommonFeatureService } from '@supermap/iclient-common
88
import { Util } from '../core/Util';
99
import { ServiceBase } from './ServiceBase';
1010
import GeoJSON from 'ol/format/GeoJSON';
11+
import Geometry from 'ol/geom/Geometry';
1112

1213
/**
1314
* @class FeatureService
@@ -198,7 +199,7 @@ export class FeatureService extends ServiceBase {
198199
if (params.bounds) {
199200
params.bounds = Util.toSuperMapBounds(params.bounds);
200201
}
201-
if (params.geometry) {
202+
if (params.geometry && params.geometry instanceof Geometry) {
202203
params.geometry = Util.toSuperMapGeometry(JSON.parse(new GeoJSON().writeGeometry(params.geometry)));
203204
}
204205
if (params.editType) {

test/leaflet/services/GetFeaturesByBoundsSpec.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,4 +367,106 @@ describe('leaflet_FeatureService_getFeaturesByBounds', () => {
367367
done();
368368
});
369369
});
370+
371+
it('getFeaturesCount', done => {
372+
var polygon = L.polygon([
373+
[-20, 20],
374+
[0, 20],
375+
[0, 40],
376+
[-20, 40],
377+
[-20, 20]
378+
]);
379+
var getFeaturesByBoundsParams = new GetFeaturesByBoundsParameters({
380+
datasetNames: ['World:Capitals'],
381+
bounds: polygon.getBounds(),
382+
returnContent: true
383+
});
384+
var getFeaturesByBoundsService = featureService(dataServiceURL, options);
385+
spyOn(FetchRequest, 'commit').and.callFake((method, testUrl, params, options) => {
386+
expect(method).toBe('POST');
387+
expect(testUrl).toBe(dataServiceURL + '/featureResults?fromIndex=0&toIndex=19&returnCountOnly=true&returnContent=true');
388+
var paramsObj = JSON.parse(params.replace(/'/g, '"'));
389+
expect(paramsObj.datasetNames[0]).toBe('World:Capitals');
390+
expect(paramsObj.getFeatureMode).toBe('BOUNDS');
391+
expect(paramsObj.spatialQueryMode).toBe('CONTAIN');
392+
expect(options).not.toBeNull();
393+
return Promise.resolve(new Response(JSON.stringify({
394+
"features": null,
395+
"featureUriList": null,
396+
"datasetInfos": null,
397+
"totalCount": 1889,
398+
"featureCount": 20
399+
})));
400+
});
401+
getFeaturesByBoundsService.getFeaturesCount(getFeaturesByBoundsParams, result => {
402+
serviceResult = result;
403+
try {
404+
expect(getFeaturesByBoundsService).not.toBeNull();
405+
expect(serviceResult.result).not.toBeNull();
406+
expect(serviceResult.result.succeed).toBeTruthy();
407+
expect(serviceResult.result.totalCount).toEqual(1889);
408+
expect(serviceResult.result.features).toBe(null);
409+
done();
410+
} catch (exception) {
411+
console.log(
412+
"leafletGetFeaturesByBoundsService_'getFeaturesCount'案例失败:" +
413+
exception.name +
414+
':' +
415+
exception.message
416+
);
417+
getFeaturesByBoundsService.destroy();
418+
expect(false).toBeTruthy();
419+
done();
420+
}
421+
});
422+
});
423+
424+
it('getFeaturesDatasetInfo', done => {
425+
var polygon = L.polygon([
426+
[-20, 20],
427+
[0, 20],
428+
[0, 40],
429+
[-20, 40],
430+
[-20, 20]
431+
]);
432+
var getFeaturesByBoundsParams = new GetFeaturesByBoundsParameters({
433+
datasetNames: ['World:Capitals'],
434+
bounds: polygon.getBounds(),
435+
returnContent: true
436+
});
437+
var getFeaturesByBoundsService = featureService(dataServiceURL, options);
438+
spyOn(FetchRequest, 'commit').and.callFake((method, testUrl, params, options) => {
439+
expect(method).toBe('POST');
440+
expect(testUrl).toBe(dataServiceURL + '/featureResults?fromIndex=0&toIndex=19&returnDatasetInfoOnly=true&returnContent=true');
441+
var paramsObj = JSON.parse(params.replace(/'/g, '"'));
442+
expect(paramsObj.datasetNames[0]).toBe('World:Capitals');
443+
expect(paramsObj.getFeatureMode).toBe('BOUNDS');
444+
expect(paramsObj.spatialQueryMode).toBe('CONTAIN');
445+
expect(options).not.toBeNull();
446+
return Promise.resolve(new Response(JSON.stringify(getReturnDatasetInfoOnlyResult)));
447+
});
448+
getFeaturesByBoundsService.getFeaturesDatasetInfo(getFeaturesByBoundsParams, result => {
449+
serviceResult = result;
450+
try {
451+
expect(getFeaturesByBoundsService).not.toBeNull();
452+
expect(serviceResult.type).toBe('processCompleted');
453+
expect(serviceResult.result).not.toBeNull();
454+
expect(serviceResult.result.succeed).toBeTruthy();
455+
expect(serviceResult.result[0].datasetName).toBe("World:Countries");
456+
expect(serviceResult.result[0].fieldInfos.length).toEqual(13);
457+
getFeaturesByBoundsService.destroy();
458+
done();
459+
} catch (exception) {
460+
console.log(
461+
"leafletGetFeaturesByBoundsService_'getFeaturesDatasetInfo'案例失败:" +
462+
exception.name +
463+
':' +
464+
exception.message
465+
);
466+
getFeaturesByBoundsService.destroy();
467+
expect(false).toBeTruthy();
468+
done();
469+
}
470+
});
471+
});
370472
});

test/mapboxgl/services/GetFeaturesByBufferSpec.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,96 @@ describe('mapboxgl_FeatureService_getFeaturesByBuffer', () => {
198198
done();
199199
});
200200
});
201+
202+
it('getFeaturesCount', done => {
203+
var queryBufferGeometry = {
204+
type: 'Polygon',
205+
coordinates: [
206+
[
207+
[-20, 20],
208+
[-20, -20],
209+
[20, -20],
210+
[20, 20],
211+
[-20, 20]
212+
]
213+
]
214+
};
215+
var bufferParam = new GetFeaturesByBufferParameters({
216+
datasetNames: ['World:Capitals'],
217+
bufferDistance: 10,
218+
geometry: queryBufferGeometry,
219+
fromIndex: 1,
220+
toIndex: 3
221+
});
222+
var service = new FeatureService(url);
223+
spyOn(FetchRequest, 'commit').and.callFake((method, testUrl, params, options) => {
224+
expect(method).toBe('POST');
225+
expect(testUrl).toBe(url + '/featureResults?fromIndex=1&toIndex=3&returnCountOnly=true&returnContent=true');
226+
var paramsObj = JSON.parse(params.replace(/'/g, '"'));
227+
expect(paramsObj.datasetNames[0]).toBe('World:Capitals');
228+
expect(paramsObj.bufferDistance).toEqual(10);
229+
expect(paramsObj.getFeatureMode).toBe('BUFFER');
230+
expect(options).not.toBeNull();
231+
return Promise.resolve(new Response(JSON.stringify({
232+
"features": null,
233+
"featureUriList": null,
234+
"datasetInfos": null,
235+
"totalCount": 1889,
236+
"featureCount": 20
237+
})));
238+
});
239+
service.getFeaturesCount(bufferParam, testResult => {
240+
serviceResult = testResult;
241+
expect(serviceResult.result).not.toBeNull();
242+
expect(serviceResult.result.succeed).toBeTruthy();
243+
expect(serviceResult.result.totalCount).toEqual(1889);
244+
expect(serviceResult.result.features).toBe(null);
245+
done();
246+
bufferParam.destroy();
247+
done();
248+
});
249+
});
250+
251+
it('getFeaturesDatasetInfo', done => {
252+
var queryBufferGeometry = {
253+
type: 'Polygon',
254+
coordinates: [
255+
[
256+
[-20, 20],
257+
[-20, -20],
258+
[20, -20],
259+
[20, 20],
260+
[-20, 20]
261+
]
262+
]
263+
};
264+
var bufferParam = new GetFeaturesByBufferParameters({
265+
datasetNames: ['World:Capitals'],
266+
bufferDistance: 10,
267+
geometry: queryBufferGeometry,
268+
fromIndex: 1,
269+
toIndex: 3
270+
});
271+
var service = new FeatureService(url);
272+
spyOn(FetchRequest, 'commit').and.callFake((method, testUrl, params, options) => {
273+
expect(method).toBe('POST');
274+
expect(testUrl).toBe(url + '/featureResults?fromIndex=1&toIndex=3&returnDatasetInfoOnly=true&returnContent=true');
275+
var paramsObj = JSON.parse(params.replace(/'/g, '"'));
276+
expect(paramsObj.datasetNames[0]).toBe('World:Capitals');
277+
expect(paramsObj.bufferDistance).toEqual(10);
278+
expect(paramsObj.getFeatureMode).toBe('BUFFER');
279+
expect(options).not.toBeNull();
280+
return Promise.resolve(new Response(JSON.stringify(getReturnDatasetInfoOnlyResult)));
281+
});
282+
service.getFeaturesDatasetInfo(bufferParam, testResult => {
283+
serviceResult = testResult;
284+
expect(serviceResult.type).toBe('processCompleted');
285+
expect(serviceResult.result).not.toBeNull();
286+
expect(serviceResult.result.succeed).toBeTruthy();
287+
expect(serviceResult.result[0].datasetName).toBe("World:Countries");
288+
expect(serviceResult.result[0].fieldInfos.length).toEqual(13);
289+
bufferParam.destroy();
290+
done();
291+
});
292+
});
201293
});

test/maplibregl/services/GetFeaturesByBufferSpec.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,96 @@ describe('maplibregl_FeatureService_getFeaturesByBuffer', () => {
198198
done();
199199
});
200200
});
201+
202+
it('getFeaturesCount', done => {
203+
var queryBufferGeometry = {
204+
type: 'Polygon',
205+
coordinates: [
206+
[
207+
[-20, 20],
208+
[-20, -20],
209+
[20, -20],
210+
[20, 20],
211+
[-20, 20]
212+
]
213+
]
214+
};
215+
var bufferParam = new GetFeaturesByBufferParameters({
216+
datasetNames: ['World:Capitals'],
217+
bufferDistance: 10,
218+
geometry: queryBufferGeometry,
219+
fromIndex: 1,
220+
toIndex: 3
221+
});
222+
var service = new FeatureService(url);
223+
spyOn(FetchRequest, 'commit').and.callFake((method, testUrl, params, options) => {
224+
expect(method).toBe('POST');
225+
expect(testUrl).toBe(url + '/featureResults?fromIndex=1&toIndex=3&returnCountOnly=true&returnContent=true');
226+
var paramsObj = JSON.parse(params.replace(/'/g, '"'));
227+
expect(paramsObj.datasetNames[0]).toBe('World:Capitals');
228+
expect(paramsObj.bufferDistance).toEqual(10);
229+
expect(paramsObj.getFeatureMode).toBe('BUFFER');
230+
expect(options).not.toBeNull();
231+
return Promise.resolve(new Response(JSON.stringify({
232+
"features": null,
233+
"featureUriList": null,
234+
"datasetInfos": null,
235+
"totalCount": 1889,
236+
"featureCount": 20
237+
})));
238+
});
239+
service.getFeaturesCount(bufferParam, testResult => {
240+
serviceResult = testResult;
241+
expect(serviceResult.result).not.toBeNull();
242+
expect(serviceResult.result.succeed).toBeTruthy();
243+
expect(serviceResult.result.totalCount).toEqual(1889);
244+
expect(serviceResult.result.features).toBe(null);
245+
done();
246+
bufferParam.destroy();
247+
done();
248+
});
249+
});
250+
251+
it('getFeaturesDatasetInfo', done => {
252+
var queryBufferGeometry = {
253+
type: 'Polygon',
254+
coordinates: [
255+
[
256+
[-20, 20],
257+
[-20, -20],
258+
[20, -20],
259+
[20, 20],
260+
[-20, 20]
261+
]
262+
]
263+
};
264+
var bufferParam = new GetFeaturesByBufferParameters({
265+
datasetNames: ['World:Capitals'],
266+
bufferDistance: 10,
267+
geometry: queryBufferGeometry,
268+
fromIndex: 1,
269+
toIndex: 3
270+
});
271+
var service = new FeatureService(url);
272+
spyOn(FetchRequest, 'commit').and.callFake((method, testUrl, params, options) => {
273+
expect(method).toBe('POST');
274+
expect(testUrl).toBe(url + '/featureResults?fromIndex=1&toIndex=3&returnDatasetInfoOnly=true&returnContent=true');
275+
var paramsObj = JSON.parse(params.replace(/'/g, '"'));
276+
expect(paramsObj.datasetNames[0]).toBe('World:Capitals');
277+
expect(paramsObj.bufferDistance).toEqual(10);
278+
expect(paramsObj.getFeatureMode).toBe('BUFFER');
279+
expect(options).not.toBeNull();
280+
return Promise.resolve(new Response(JSON.stringify(getReturnDatasetInfoOnlyResult)));
281+
});
282+
service.getFeaturesDatasetInfo(bufferParam, testResult => {
283+
serviceResult = testResult;
284+
expect(serviceResult.type).toBe('processCompleted');
285+
expect(serviceResult.result).not.toBeNull();
286+
expect(serviceResult.result.succeed).toBeTruthy();
287+
expect(serviceResult.result[0].datasetName).toBe("World:Countries");
288+
expect(serviceResult.result[0].fieldInfos.length).toEqual(13);
289+
bufferParam.destroy();
290+
done();
291+
});
292+
});
201293
});

0 commit comments

Comments
 (0)