Skip to content

Commit

Permalink
Care with transform
Browse files Browse the repository at this point in the history
Since the transform method takes an arbitrary transform function, new coordinates may not be ordered in the same way as the originals.
  • Loading branch information
tschaub committed Jan 24, 2013
1 parent d6c96c0 commit 0845dea
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/ol/extent.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ ol.Extent.prototype.getTopRight = function() {
* @return {ol.Extent} Extent.
*/
ol.Extent.prototype.transform = function(transformFn) {
var min = transformFn(new ol.Coordinate(this.minX, this.minY));
var max = transformFn(new ol.Coordinate(this.maxX, this.maxY));
return new ol.Extent(min.x, min.y, max.x, max.y);
var a = transformFn(new ol.Coordinate(this.minX, this.minY));
var b = transformFn(new ol.Coordinate(this.maxX, this.maxY));
return new ol.Extent(Math.min(a.x, b.x), Math.min(a.y, b.y),
Math.max(a.x, b.x), Math.max(a.y, b.y));
};
17 changes: 17 additions & 0 deletions test/spec/ol/extent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ describe('ol.Extent', function() {
expect(destinationExtent.maxX).toRoughlyEqual(5009377.085697311, 1e-9);
expect(destinationExtent.maxY).toRoughlyEqual(8399737.889818361, 1e-9);
});

it('takes arbitrary function', function() {
var transformFn = function(coordinate) {
return new ol.Coordinate(-coordinate.x, -coordinate.y);
}
var sourceExtent = new ol.Extent(-15, -30, 45, 60);
var destinationExtent = sourceExtent.transform(transformFn);
expect(destinationExtent).not.toBeUndefined();
expect(destinationExtent).not.toBeNull();
// FIXME check values with third-party tool
expect(destinationExtent.minX).toBe(-45);
expect(destinationExtent.minY).toBe(-60);
expect(destinationExtent.maxX).toBe(15);
expect(destinationExtent.maxY).toBe(30);
});


});
});

Expand Down

0 comments on commit 0845dea

Please sign in to comment.