Skip to content

Commit

Permalink
Merge pull request openlayers#14962 from mike-000/fromExtent
Browse files Browse the repository at this point in the history
Cannot create polygon from empty extent
  • Loading branch information
ahocevar authored Aug 1, 2023
2 parents e532713 + 78aa961 commit 0b90e81
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/ol/geom/Polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import LinearRing from './LinearRing.js';
import Point from './Point.js';
import SimpleGeometry from './SimpleGeometry.js';
import {arrayMaxSquaredDelta, assignClosestArrayPoint} from './flat/closest.js';
import {closestSquaredDistanceXY, getCenter} from '../extent.js';
import {closestSquaredDistanceXY, getCenter, isEmpty} from '../extent.js';
import {deflateCoordinatesArray} from './flat/deflate.js';
import {extend} from '../array.js';
import {getInteriorPointOfArray} from './flat/interiorpoint.js';
Expand Down Expand Up @@ -438,6 +438,9 @@ export function circular(center, radius, n, sphereRadius) {
* @api
*/
export function fromExtent(extent) {
if (isEmpty(extent)) {
throw new Error('Cannot create polygon from empty extent');
}
const minX = extent[0];
const minY = extent[1];
const maxX = extent[2];
Expand Down
12 changes: 11 additions & 1 deletion test/node/ol/geom/Polygon.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import Polygon, {
fromExtent,
} from '../../../../src/ol/geom/Polygon.js';
import expect from '../../expect.js';
import {boundingExtent, isEmpty} from '../../../../src/ol/extent.js';
import {
boundingExtent,
createEmpty,
isEmpty,
} from '../../../../src/ol/extent.js';

describe('ol/geom/Polygon.js', function () {
it('cannot be constructed with a null geometry', function () {
Expand Down Expand Up @@ -693,6 +697,12 @@ describe('ol/geom/Polygon.js', function () {
const orientedFlatCoordinates = polygon.getOrientedFlatCoordinates();
expect(orientedFlatCoordinates).to.eql([1, 2, 1, 5, 3, 5, 3, 2, 1, 2]);
});

it('throws on empty extent', function () {
expect(function () {
fromExtent(createEmpty());
}).to.throwException();
});
});

describe('fromCircle()', function () {
Expand Down

0 comments on commit 0b90e81

Please sign in to comment.