From 7f78aa70194ea5d093afb1c0fc87b4590cf80730 Mon Sep 17 00:00:00 2001 From: Benjamin Gerber Date: Mon, 23 Nov 2020 16:01:52 +0100 Subject: [PATCH] More explicite comments --- src/query/Querent.js | 17 +++++++++-------- test/spec/services/querent.spec.js | 16 ++++++++-------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/query/Querent.js b/src/query/Querent.js index 6c52c628cace..58f6de7338c5 100644 --- a/src/query/Querent.js +++ b/src/query/Querent.js @@ -672,7 +672,7 @@ export class Querent { let queryIconPosition; if (dataSource.queryIconPosition) { hasAtLeastOneQueryIconPosition = true; - queryIconPosition = this.extendBboxWithQueryIconPosition_( + queryIconPosition = this.bufferBboxWithQueryIconPosition_( dataSource.queryIconPosition, resolution, olExtent.createOrUpdateFromCoordinate(coordinate) @@ -799,19 +799,18 @@ export class Querent { } /** - * Extend the given bbox with the queryIconPosition values. + * Add a buffer (based on the queryIconPosition values) around the given bbox. * @param {!number[]} queryIconPosition The values in px to buffer the bbox. * @param {number} resolution The map view resolution to define the px size correctly. - * @param {!import("ol/extent.js").Extent} bbox The bbox to extend. - * @return {!import("ol/extent.js").Extent} The extended bbox or null if the queryIconPosition param + * @param {!import("ol/extent.js").Extent} bbox The bbox to buffer. + * @return {!import("ol/extent.js").Extent} The new bbox or null if the queryIconPosition param * is not valid. * @private */ - extendBboxWithQueryIconPosition_(queryIconPosition, resolution, bbox) { + bufferBboxWithQueryIconPosition_(queryIconPosition, resolution, bbox) { const buffers = queryIconPosition.map((value) => value * resolution); const length = buffers.length; if (!length || length > 4) { - // Bad format. return null; } if (length === 1) { @@ -823,9 +822,11 @@ export class Querent { bbox[0] - buffers[0], // bbox[0] is top, always set with buffer[0]; // bbox[1] is right, always set with buffer[1] for length > 1; bbox[1] - buffers[1], - // bbox[2] is bottom. Length === 2 is top-bottom and right-left. Length > 2 defines the bottom value. + // bbox[2] is bottom. For length === 2 (top-bottom, right-left), use buffer[0] (top). + // For length === 3 (top, right-left, bottom) or length === 4, use buffer[2] (specific bottom). bbox[2] + (length === 2 ? buffers[0] : buffers[2]), - // bbox[3] is left. Length === 4 is each side defined and the only manner to define the left value. + // bbox[3] is left. For length === 4 (top, right, bottom, left), use buffer[3] (specific left). + // For length === 2 (top-bottom, right-left) or length === 3 (top, right-left, bottom), use buffer[1]. bbox[3] + (length === 4 ? buffers[3] : buffers[1]), ]; } diff --git a/test/spec/services/querent.spec.js b/test/spec/services/querent.spec.js index 4191712dcd74..4c994d64e6f3 100644 --- a/test/spec/services/querent.spec.js +++ b/test/spec/services/querent.spec.js @@ -32,29 +32,29 @@ describe('ngeo.Querent', () => { }); }); - it('Extends bbox with queryIconPosition', () => { + it('Buffers bbox with queryIconPosition', () => { const resolution = 10; const bbox = [50, 51, 52, 53]; - let result = ngeoQuerent.extendBboxWithQueryIconPosition_([], resolution, bbox); + let result = ngeoQuerent.bufferBboxWithQueryIconPosition_([], resolution, bbox); expect(result).toBe(null); - result = ngeoQuerent.extendBboxWithQueryIconPosition_([1], resolution, bbox); + result = ngeoQuerent.bufferBboxWithQueryIconPosition_([1], resolution, bbox); expect(result).toEqual([40, 41, 62, 63]); - result = ngeoQuerent.extendBboxWithQueryIconPosition_([1, 2], resolution, bbox); + result = ngeoQuerent.bufferBboxWithQueryIconPosition_([1, 2], resolution, bbox); expect(result).toEqual([40, 31, 62, 73]); - result = ngeoQuerent.extendBboxWithQueryIconPosition_([1, 2, 3], resolution, bbox); + result = ngeoQuerent.bufferBboxWithQueryIconPosition_([1, 2, 3], resolution, bbox); expect(result).toEqual([40, 31, 82, 73]); - result = ngeoQuerent.extendBboxWithQueryIconPosition_([1, 2, 3, 4], resolution, bbox); + result = ngeoQuerent.bufferBboxWithQueryIconPosition_([1, 2, 3, 4], resolution, bbox); expect(result).toEqual([40, 31, 82, 93]); - result = ngeoQuerent.extendBboxWithQueryIconPosition_([0, 0, 0, 4], resolution, bbox); + result = ngeoQuerent.bufferBboxWithQueryIconPosition_([0, 0, 0, 4], resolution, bbox); expect(result).toEqual([50, 51, 52, 93]); - result = ngeoQuerent.extendBboxWithQueryIconPosition_([1, 2, 3, 4, 5], resolution, bbox); + result = ngeoQuerent.bufferBboxWithQueryIconPosition_([1, 2, 3, 4, 5], resolution, bbox); expect(result).toBe(null); }); });