Base ol.Extent on ol.Rectangle instead of goog.math.Box

This commit is contained in:
Tom Payne
2012-07-15 18:54:05 +02:00
parent b4ee35954f
commit ac3d05421b
2 changed files with 31 additions and 29 deletions
+13 -24
View File
@@ -1,40 +1,29 @@
goog.provide('ol.Extent'); goog.provide('ol.Extent');
goog.require('goog.math.Box'); goog.require('ol.Rectangle');
goog.require('ol.TransformFunction'); goog.require('ol.TransformFunction');
/** /**
* @constructor * @constructor
* @extends {goog.math.Box} * @extends {ol.Rectangle}
* @param {number} top Top. * @param {number} minX Minimum X.
* @param {number} right Right. * @param {number} minY Minimum Y.
* @param {number} bottom Bottom. * @param {number} maxX Maximum X.
* @param {number} left Left. * @param {number} maxY Maximum Y.
*/ */
ol.Extent = function(top, right, bottom, left) { ol.Extent = function(minX, minY, maxX, maxY) {
goog.base(this, minX, minY, maxX, maxY);
goog.base(this, top, right, bottom, left);
}; };
goog.inherits(ol.Extent, goog.math.Box); goog.inherits(ol.Extent, ol.Rectangle);
/** /**
* @return {ol.Extent} Extent. * @return {ol.Extent} Extent.
*/ */
ol.Extent.prototype.clone = function() { ol.Extent.prototype.clone = function() {
return new ol.Extent(this.top, this.right, this.bottom, this.left); return new ol.Extent(this.minX, this.minY, this.maxX, this.maxY);
};
/**
* @return {goog.math.Coordinate} Center.
*/
ol.Extent.prototype.getCenter = function() {
return new goog.math.Coordinate(
(this.left + this.right) / 2, (this.top + this.bottom) / 2);
}; };
@@ -43,7 +32,7 @@ ol.Extent.prototype.getCenter = function() {
* @return {ol.Extent} Extent. * @return {ol.Extent} Extent.
*/ */
ol.Extent.prototype.transform = function(transform) { ol.Extent.prototype.transform = function(transform) {
var topRight = transform(new goog.math.Coordinate(this.right, this.top)); var min = transform(new goog.math.Coordinate(this.minX, this.minY));
var bottomLeft = transform(new goog.math.Coordinate(this.left, this.bottom)); var max = transform(new goog.math.Coordinate(this.maxX, this.maxY));
return new ol.Extent(topRight.y, topRight.x, bottomLeft.y, bottomLeft.x); return new ol.Extent(min.x, min.y, max.x, max.y);
}; };
+18 -5
View File
@@ -1,16 +1,29 @@
goog.require('goog.math.Coordinate');
goog.require('goog.testing.jsunit'); goog.require('goog.testing.jsunit');
goog.require('ol.Extent'); goog.require('ol.Extent');
goog.require('ol.Projection'); goog.require('ol.Projection');
function testClone() {
var extent = new ol.Extent(1, 2, 3, 4);
var clonedExtent = extent.clone();
assertTrue(clonedExtent instanceof ol.Extent);
assertFalse(clonedExtent === extent);
assertEquals(extent.minX, clonedExtent.minX);
assertEquals(extent.minY, clonedExtent.minY);
assertEquals(extent.maxX, clonedExtent.maxX);
assertEquals(extent.maxY, clonedExtent.maxY);
}
function testTransform() { function testTransform() {
var transform = ol.Projection.getTransformFromCodes('EPSG:4326', 'EPSG:3857'); var transform = ol.Projection.getTransformFromCodes('EPSG:4326', 'EPSG:3857');
var sourceExtent = new ol.Extent(60, 45, -30, -15); var sourceExtent = new ol.Extent(-15, -30, 45, 60);
var destinationExtent = sourceExtent.transform(transform); var destinationExtent = sourceExtent.transform(transform);
assertNotNullNorUndefined(destinationExtent); assertNotNullNorUndefined(destinationExtent);
// FIXME check values with third-party tool // FIXME check values with third-party tool
assertRoughlyEquals(8399737.889818361, destinationExtent.top, 1e-9); assertRoughlyEquals(-1669792.3618991037, destinationExtent.minX, 1e-9);
assertRoughlyEquals(5009377.085697311, destinationExtent.right, 1e-9); assertRoughlyEquals(-3503549.843504376, destinationExtent.minY, 1e-9);
assertRoughlyEquals(-3503549.843504376, destinationExtent.bottom, 1e-9); assertRoughlyEquals(5009377.085697311, destinationExtent.maxX, 1e-9);
assertRoughlyEquals(-1669792.3618991037, destinationExtent.left, 1e-9); assertRoughlyEquals(8399737.889818361, destinationExtent.maxY, 1e-9);
} }