Skip to content

Commit

Permalink
feat(Extent): add setFromArray and setFromExtent methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Desplandis committed Nov 27, 2024
1 parent 96a74a4 commit f4b9270
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/Core/Geographic/Extent.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,36 @@ class Extent {
return this;
}

/**
* Set this extent `west` property to `array[offset + 0]`, `east` property
* to `array[offset + 1]`, `south` property to `array[offset + 2]` and
* `north` property to `array[offset + 3]`.
* @param {number[]} array - the source array
* @param {number} [offset=0] - offset into the array. Default is 0.
* @returns {this}
*/
setFromArray(array, offset = 0) {
this.west = array[offset];
this.east = array[offset + 1];
this.south = array[offset + 2];
this.north = array[offset + 3];
return this;
}

/**
* Set this extent `west`, `east`, `south` and `north` properties from an
* `extent` bounds.
* @param {Object} extent - the source extent
* @returns {this}
*/
setFromExtent(extent) {
this.west = extent.west;
this.east = extent.east;
this.south = extent.south;
this.north = extent.north;
return this;
}

/**
* Copy to this extent to input extent.
* @param {Extent} extent
Expand Down
36 changes: 35 additions & 1 deletion test/unit/extent.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ describe('Extent', function () {
const maxX = 10;
const minY = -1;
const maxY = 3;
const minZ = -50;
const maxZ = 42;

it('should build the expected extent using Coordinates', function () {
const withCoords = new Extent('EPSG:4326',
Expand Down Expand Up @@ -199,10 +201,42 @@ describe('Extent', function () {
assert.equal(maxY, withValues.north);
});

it('should set values from array', function () {
const extent = new Extent('EPSG:4326', 0, 0, 0, 0);
const array = [minX, maxX, minY, maxY, minZ, maxZ];

extent.setFromArray(array);
assert.deepEqual(
[minX, maxX, minY, maxY],
[extent.west, extent.east, extent.south, extent.north],
);

extent.setFromArray(array, 2);
assert.deepEqual(
[minY, maxY, minZ, maxZ],
[extent.west, extent.east, extent.south, extent.north],
);
});

it('sould set values from an extent-like object', function () {
const extent = new Extent('EPSG:4326', 0, 0, 0, 0);
extent.setFromExtent({
west: minX,
east: maxX,
south: minY,
north: maxY,
});
assert.equal(minX, extent.west);
assert.equal(maxX, extent.east);
assert.equal(minY, extent.south);
assert.equal(maxY, extent.north);
});

it('should copy extent', function () {
const toCopy = new Extent('EPSG:4326', [minX, maxX, minY, maxY]);
const toCopy = new Extent('EPSG:2154', [minX, maxX, minY, maxY]);
const withValues = new Extent('EPSG:4326', [0, 0, 0, 0]);
withValues.copy(toCopy);
assert.equal('EPSG:2154', withValues.crs);
assert.equal(minX, withValues.west);
assert.equal(maxX, withValues.east);
assert.equal(minY, withValues.south);
Expand Down

0 comments on commit f4b9270

Please sign in to comment.