Skip to content

Commit

Permalink
More explicite comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ger-benjamin committed Nov 23, 2020
1 parent 24386b1 commit 7f78aa7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
17 changes: 9 additions & 8 deletions src/query/Querent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand All @@ -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]),
];
}
Expand Down
16 changes: 8 additions & 8 deletions test/spec/services/querent.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 7f78aa7

Please sign in to comment.