Skip to content

Commit

Permalink
Merge pull request openlayers#2821 from elemoine/polygon-from-extent
Browse files Browse the repository at this point in the history
Add ol.geom.Polygon.fromExtent
  • Loading branch information
Éric Lemoine committed Oct 15, 2014
2 parents 017703f + 1ce43e3 commit 74fd55a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/ol/geom/polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,23 @@ ol.geom.Polygon.circular = function(sphere, center, radius, opt_n) {
ol.geom.GeometryLayout.XY, flatCoordinates, [flatCoordinates.length]);
return polygon;
};


/**
* Create a polygon from an extent. The layout used is `XY`.
* @param {ol.Extent} extent The extent.
* @return {ol.geom.Polygon} The polygon.
* @api
*/
ol.geom.Polygon.fromExtent = function(extent) {
var minX = extent[0];
var minY = extent[1];
var maxX = extent[2];
var maxY = extent[3];
var flatCoordinates =
[minX, minY, minX, maxY, maxX, maxY, maxX, minY, minX, minY];
var polygon = new ol.geom.Polygon(null);
polygon.setFlatCoordinates(
ol.geom.GeometryLayout.XY, flatCoordinates, [flatCoordinates.length]);
return polygon;
};
13 changes: 13 additions & 0 deletions test/spec/ol/geom/polygon.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,19 @@ describe('ol.geom.Polygon', function() {
});
});

describe('ol.geom.Polygon.fromExtent', function() {
it('creates the correct polygon', function() {
var extent = [1, 2, 3, 5];
var polygon = ol.geom.Polygon.fromExtent(extent);
var flatCoordinates = polygon.getFlatCoordinates();
expect(flatCoordinates).to.eql(
[1, 2, 1, 5, 3, 5, 3, 2, 1, 2]);
var orientedFlatCoordinates = polygon.getOrientedFlatCoordinates();
expect(orientedFlatCoordinates).to.eql(
[1, 2, 1, 5, 3, 5, 3, 2, 1, 2]);
});
});

});


Expand Down

0 comments on commit 74fd55a

Please sign in to comment.