Skip to content

Commit

Permalink
chore(Extent): deprecate array and extent constructor parameters
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
- Deprecate Extent#constructor with array and extent parameters
- Deprecate Extent#set with array and extent parameters
  • Loading branch information
Desplandis committed Nov 27, 2024
1 parent f4b9270 commit 8e6f479
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 89 deletions.
2 changes: 1 addition & 1 deletion src/Converter/textureConverter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as THREE from 'three';
import Feature2Texture from 'Converter/Feature2Texture';
import Extent from 'Core/Geographic/Extent';

const extentTexture = new Extent('EPSG:4326', [0, 0, 0, 0]);
const extentTexture = new Extent('EPSG:4326');

const textureLayer = (texture, layer) => {
texture.generateMipmaps = false;
Expand Down
43 changes: 16 additions & 27 deletions src/Core/Geographic/Extent.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Extent {
* @param {number} [v2] south value
* @param {number} [v3] north value
*/
constructor(crs, v0, v1, v2, v3) {
constructor(crs, v0 = 0, v1 = 0, v2 = 0, v3 = 0) {
if (CRS.isGeocentric(crs)) {
throw new Error(`${crs} is a geocentric projection, it doesn't make sense with a geographical extent`);
}
Expand Down Expand Up @@ -76,7 +76,7 @@ class Extent {
*/
as(crs, target) {
CRS.isValid(crs);
target = target || new Extent('EPSG:4326', [0, 0, 0, 0]);
target = target || new Extent('EPSG:4326');
if (this.crs != crs) {
// Compute min/max in x/y by projecting 8 cardinal points,
// and then taking the min/max of each coordinates.
Expand Down Expand Up @@ -272,7 +272,7 @@ class Extent {
*/
intersect(extent) {
if (!this.intersectsExtent(extent)) {
return new Extent(this.crs, 0, 0, 0, 0);
return new Extent(this.crs);
}
if (extent.crs != this.crs) {
extent = extent.as(this.crs, _extent);
Expand Down Expand Up @@ -303,28 +303,17 @@ class Extent {
throw new Error('No values to set in the extent');
}
if (v0.isExtent) {
v1 = v0.east;
v2 = v0.south;
v3 = v0.north;
v0 = v0.west;
}

if (v0.isCoordinates) {
// seem never used
this.west = v0.x;
this.east = v1.x;
this.south = v0.y;
this.north = v1.y;
} else if (v0.west !== undefined) {
this.west = v0.west;
this.east = v0.east;
this.south = v0.south;
this.north = v0.north;
console.warn(
'Deprecated Extent#constructor(string, Extent) and Extent#set(Extent),',
'use new Extent(string).setFromExtent(Extent) instead.',
);
this.setFromExtent(v0);
} else if (v0.length == 4) {
this.west = v0[0];
this.east = v0[1];
this.south = v0[2];
this.north = v0[3];
console.warn(
'Deprecated Extent#constructor(string, number[]) and Extent#set(number[]),',
'use new Extent(string).setFromArray(number[]) instead.',
);
this.setFromArray(v0);
} else if (v3 !== undefined) {
this.west = v0;
this.east = v1;
Expand Down Expand Up @@ -372,7 +361,7 @@ class Extent {
*/
copy(extent) {
this.crs = extent.crs;
return this.set(extent);
return this.setFromExtent(extent);
}

/**
Expand Down Expand Up @@ -463,7 +452,7 @@ class Extent {
cNorthEast.setFromVector3(box.max).as(crs, cNorthEast).toVector3(box.max);
}

return new Extent(crs, {
return new Extent(crs).setFromExtent({
west: box.min.x,
east: box.max.x,
south: box.min.y,
Expand Down Expand Up @@ -576,6 +565,6 @@ class Extent {
}
}

_extent = new Extent('EPSG:4326', [0, 0, 0, 0]);
_extent = new Extent('EPSG:4326');

export default Extent;
6 changes: 3 additions & 3 deletions src/Core/Tile/Tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ function _rowColfromParent(/** @type {Tile} */ tile, /** @type {number} */ zoom)
return r;
}

const _extent = new Extent('EPSG:4326', [0, 0, 0, 0]);
const _extent2 = new Extent('EPSG:4326', [0, 0, 0, 0]);
const _extent = new Extent('EPSG:4326');
const _extent2 = new Extent('EPSG:4326');

const _c = new Coordinates('EPSG:4326', 0, 0);

Expand Down Expand Up @@ -57,7 +57,7 @@ class Tile {
*/
toExtent(crs, target) {
CRS.isValid(crs);
target = target || new Extent('EPSG:4326', [0, 0, 0, 0]);
target = target || new Extent('EPSG:4326');
const { epsg, globalExtent, globalDimension } = getInfoTms(this.crs);
const countTiles = getCountTiles(this.crs, this.zoom);

Expand Down
2 changes: 1 addition & 1 deletion src/Source/Source.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class Source extends InformationsData {
this.whenReady = Promise.resolve();
this._featuresCaches = {};
if (source.extent && !(source.extent.isExtent)) {
this.extent = new Extent(this.crs, source.extent);
this.extent = new Extent(this.crs).setFromExtent(source.extent);
} else {
this.extent = source.extent;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Source/WFSSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Source from 'Source/Source';
import URLBuilder from 'Provider/URLBuilder';
import Extent from 'Core/Geographic/Extent';

const _extent = new Extent('EPSG:4326', [0, 0, 0, 0]);
const _extent = new Extent('EPSG:4326');

/**
* An object defining the source of resources to get from a
Expand Down
2 changes: 1 addition & 1 deletion src/Source/WMSSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Source from 'Source/Source';
import URLBuilder from 'Provider/URLBuilder';
import Extent from 'Core/Geographic/Extent';

const _extent = new Extent('EPSG:4326', [0, 0, 0, 0]);
const _extent = new Extent('EPSG:4326');

/**
* An object defining the source of images to get from a
Expand Down
79 changes: 24 additions & 55 deletions test/unit/extent.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,6 @@ describe('Extent', function () {
const minZ = -50;
const maxZ = 42;

it('should build the expected extent using Coordinates', function () {
const withCoords = new Extent('EPSG:4326',
new Coordinates('EPSG:4326', minX, minY),
new Coordinates('EPSG:4326', maxX, maxY));
assert.equal(minX, withCoords.west);
assert.equal(maxX, withCoords.east);
assert.equal(minY, withCoords.south);
assert.equal(maxY, withCoords.north);
});

it('should build the expected extent using keywords', function () {
const withKeywords = new Extent('EPSG:4326', {
south: minY,
east: maxX,
north: maxY,
west: minX,
});
assert.equal(minX, withKeywords.west);
assert.equal(maxX, withKeywords.east);
assert.equal(minY, withKeywords.south);
assert.equal(maxY, withKeywords.north);
});

it('should build the expected extent using values', function () {
const withValues = new Extent('EPSG:4326',
minX,
Expand All @@ -49,14 +26,6 @@ describe('Extent', function () {
assert.equal(maxY, withValues.north);
});

it('should build the expected extent using Array', function () {
const withValues = new Extent('EPSG:4326', [minX, maxX, minY, maxY]);
assert.equal(minX, withValues.west);
assert.equal(maxX, withValues.east);
assert.equal(minY, withValues.south);
assert.equal(maxY, withValues.north);
});

it('should build the expected extent from box3', function () {
const box = new Box3(
new Vector3(Math.random(), Math.random()),
Expand Down Expand Up @@ -125,7 +94,7 @@ describe('Extent', function () {
});

it('should clone extent like expected', function () {
const withValues = new Extent('EPSG:4326', [minX, maxX, minY, maxY]);
const withValues = new Extent('EPSG:4326', minX, maxX, minY, maxY);
const clonedExtent = withValues.clone();
assert.equal(clonedExtent.west, withValues.west);
assert.equal(clonedExtent.east, withValues.east);
Expand All @@ -134,41 +103,41 @@ describe('Extent', function () {
});

it('should convert extent EPSG:4326 like expected', function () {
const withValues = new Extent('EPSG:4326', [minX, maxX, minY, maxY]).as('EPSG:3857');
const withValues = new Extent('EPSG:4326', minX, maxX, minY, maxY).as('EPSG:3857');
assert.equal(0, withValues.west);
assert.equal(1113194.9079327357, withValues.east);
assert.equal(-111325.14286638597, withValues.south);
assert.equal(334111.1714019597, withValues.north);
});

it('should return center of extent expected', function () {
const withValues = new Extent('EPSG:4326', [minX, maxX, minY, maxY]);
const withValues = new Extent('EPSG:4326', minX, maxX, minY, maxY);
const center = withValues.center();
assert.equal(5, center.longitude);
assert.equal(1, center.latitude);
});
it('should return dimensions of extent expected', function () {
const withValues = new Extent('EPSG:4326', [minX, maxX, minY, maxY]);
const withValues = new Extent('EPSG:4326', minX, maxX, minY, maxY);
const dimensions = withValues.planarDimensions();
assert.equal(10, dimensions.x);
assert.equal(4, dimensions.y);
});

it('should return true is point is inside extent expected', function () {
const withValues = new Extent('EPSG:4326', [minX, maxX, minY, maxY]);
const withValues = new Extent('EPSG:4326', minX, maxX, minY, maxY);
const coord = new Coordinates('EPSG:4326', minX + 1, minY + 2);
assert.ok(withValues.isPointInside(coord));
});

it('should return true is extent is inside extent expected', function () {
const withValues = new Extent('EPSG:4326', [minX, maxX, minY, maxY]);
const inside = new Extent('EPSG:4326', [minX + 1, maxX - 1, minY + 1, maxY - 1]);
const withValues = new Extent('EPSG:4326', minX, maxX, minY, maxY);
const inside = new Extent('EPSG:4326', minX + 1, maxX - 1, minY + 1, maxY - 1);
assert.ok(withValues.isInside(inside, 1));
});

it('should return expected offset', function () {
const withValues = new Extent('EPSG:4326', [minX, maxX, minY, maxY]);
const inside = new Extent('EPSG:4326', [minX + 1, maxX - 1, minY + 1, maxY - 1]);
const withValues = new Extent('EPSG:4326', minX, maxX, minY, maxY);
const inside = new Extent('EPSG:4326', minX + 1, maxX - 1, minY + 1, maxY - 1);
const offset = withValues.offsetToParent(inside);
assert.equal(offset.x, -0.125);
assert.equal(offset.y, -0.5);
Expand All @@ -177,14 +146,14 @@ describe('Extent', function () {
});

it('should return true if intersect other extent', function () {
const withValues = new Extent('EPSG:4326', [minX, maxX, minY, maxY]);
const inter = new Extent('EPSG:4326', [minX + 1, maxX - 1, maxY - 1, maxY + 2]);
const withValues = new Extent('EPSG:4326', minX, maxX, minY, maxY);
const inter = new Extent('EPSG:4326', minX + 1, maxX - 1, maxY - 1, maxY + 2);
assert.ok(withValues.intersectsExtent(inter));
});

it('should intersect like expected', function () {
const withValues = new Extent('EPSG:4326', [minX, maxX, minY, maxY]);
const extent = new Extent('EPSG:4326', [minX + 1, maxX - 1, maxY - 1, maxY + 2]);
const withValues = new Extent('EPSG:4326', minX, maxX, minY, maxY);
const extent = new Extent('EPSG:4326', minX + 1, maxX - 1, maxY - 1, maxY + 2);
const inter = withValues.intersect(extent);
assert.equal(1, inter.west);
assert.equal(9, inter.east);
Expand All @@ -193,7 +162,7 @@ describe('Extent', function () {
});

it('should set values', function () {
const withValues = new Extent('EPSG:4326', [0, 0, 0, 0]);
const withValues = new Extent('EPSG:4326');
withValues.set(minX, maxX, minY, maxY);
assert.equal(minX, withValues.west);
assert.equal(maxX, withValues.east);
Expand All @@ -202,7 +171,7 @@ describe('Extent', function () {
});

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

extent.setFromArray(array);
Expand All @@ -219,7 +188,7 @@ describe('Extent', function () {
});

it('sould set values from an extent-like object', function () {
const extent = new Extent('EPSG:4326', 0, 0, 0, 0);
const extent = new Extent('EPSG:4326');
extent.setFromExtent({
west: minX,
east: maxX,
Expand All @@ -233,8 +202,8 @@ describe('Extent', function () {
});

it('should copy extent', function () {
const toCopy = new Extent('EPSG:2154', [minX, maxX, minY, maxY]);
const withValues = new Extent('EPSG:4326', [0, 0, 0, 0]);
const toCopy = new Extent('EPSG:2154', minX, maxX, minY, maxY);
const withValues = new Extent('EPSG:4326');
withValues.copy(toCopy);
assert.equal('EPSG:2154', withValues.crs);
assert.equal(minX, withValues.west);
Expand All @@ -244,8 +213,8 @@ describe('Extent', function () {
});

it('should union like expected', function () {
const withValues = new Extent('EPSG:4326', [minX, maxX, minY, maxY]);
const extent = new Extent('EPSG:4326', [minX + 1, maxX - 1, maxY - 1, maxY + 2]);
const withValues = new Extent('EPSG:4326', minX, maxX, minY, maxY);
const extent = new Extent('EPSG:4326', minX + 1, maxX - 1, maxY - 1, maxY + 2);
withValues.union(extent);
assert.equal(0, withValues.west);
assert.equal(10, withValues.east);
Expand All @@ -254,7 +223,7 @@ describe('Extent', function () {
});

it('should expand by point', function () {
const withValues = new Extent('EPSG:4326', [minX, maxX, minY, maxY]);
const withValues = new Extent('EPSG:4326', minX, maxX, minY, maxY);
const coord = new Coordinates('EPSG:4326', maxX + 1, maxY + 2);
withValues.expandByCoordinates(coord);
assert.equal(0, withValues.west);
Expand All @@ -264,7 +233,7 @@ describe('Extent', function () {
});

it('should convert EPSG extent values to string', function () {
const withValues = new Extent('EPSG:4326', [minX, maxX, minY, maxY]);
const withValues = new Extent('EPSG:4326', minX, maxX, minY, maxY);
const tostring = withValues.toString(',');
const toValues = tostring.split(',').map(s => Number(s));
assert.equal(toValues[0], withValues.east);
Expand All @@ -274,8 +243,8 @@ describe('Extent', function () {
});

it('should copy and transform extent', function () {
const withValues = new Extent('EPSG:4326', [0, 0, 0, 0]);
const extent = new Extent('EPSG:4326', [minX + 1, maxX - 1, maxY - 1, maxY + 2]);
const withValues = new Extent('EPSG:4326', 0, 0, 0, 0);
const extent = new Extent('EPSG:4326', minX + 1, maxX - 1, maxY - 1, maxY + 2);
const position = new Vector3(1, 2, 0);
const scale = new Vector3(2, -2, 1);
const quaternion = new Quaternion();
Expand Down

0 comments on commit 8e6f479

Please sign in to comment.