diff --git a/lib/OpenLayers/BaseTypes/Bounds.js b/lib/OpenLayers/BaseTypes/Bounds.js index 7eefceee4e..000cf7822f 100644 --- a/lib/OpenLayers/BaseTypes/Bounds.js +++ b/lib/OpenLayers/BaseTypes/Bounds.js @@ -425,6 +425,29 @@ OpenLayers.Bounds = OpenLayers.Class({ return quadrant; }, + + /** + * APIMethod: transform + * Transform the Bounds object from source to dest. + * + * Parameters: + * source - {} Source projection. + * dest - {} Destination projection. + * + * Returns: + * {} Itself, for use in chaining operations. + */ + transform: function(source, dest) { + var ll = OpenLayers.Projection.transform( + {'x': this.left, 'y': this.bottom}, source, dest); + var ur = OpenLayers.Projection.transform( + {'x': this.right, 'y': this.top}, source, dest); + this.left = ll.x; + this.bottom = ll.y; + this.right = ur.x; + this.top = ur.y; + return this; + }, /** * APIMethod: wrapDateLine diff --git a/lib/OpenLayers/BaseTypes/LonLat.js b/lib/OpenLayers/BaseTypes/LonLat.js index 51c2132471..0f195e3d91 100644 --- a/lib/OpenLayers/BaseTypes/LonLat.js +++ b/lib/OpenLayers/BaseTypes/LonLat.js @@ -112,6 +112,25 @@ OpenLayers.LonLat = OpenLayers.Class({ } return equals; }, + + /** + * APIMethod: transform + * Transform the LonLat object from source to dest. + * + * Parameters: + * source - {} Source projection. + * dest - {} Destination projection. + * + * Returns: + * {} Itself, for use in chaining operations. + */ + transform: function(source, dest) { + var point = OpenLayers.Projection.transform( + {'x': this.lon, 'y': this.lat}, source, dest); + this.lon = point.x; + this.lat = point.y; + return this; + }, /** * APIMethod: wrapDateLine diff --git a/tests/BaseTypes/test_Bounds.html b/tests/BaseTypes/test_Bounds.html index b1073d9b09..17b70e0908 100644 --- a/tests/BaseTypes/test_Bounds.html +++ b/tests/BaseTypes/test_Bounds.html @@ -494,6 +494,16 @@ } + function test_Bounds_transform(t) { + t.plan( 3 ); + bounds = new OpenLayers.Bounds(10, -10, 20, 10); + bounds.transform(new OpenLayers.Projection("foo"), new OpenLayers.Projection("Bar")); + t.eq(bounds.toBBOX(), "10,-10,20,10", "null transform okay"); + bounds.transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913")); + t.eq(bounds.toBBOX(), "1113194.907778,-1118889.974702,2226389.815556,1118889.974702", "bounds for spherical mercator transform are correct"); + bounds.transform(new OpenLayers.Projection("EPSG:900913"), new OpenLayers.Projection("EPSG:4326")); + t.eq(bounds.toBBOX(), "10,-10,20,10", "bounds for inverse spherical mercator transform are correct"); + } function test_17_Bounds_add(t) { t.plan( 8 ); diff --git a/tests/BaseTypes/test_LonLat.html b/tests/BaseTypes/test_LonLat.html index 5478a90193..fdfa39dc85 100644 --- a/tests/BaseTypes/test_LonLat.html +++ b/tests/BaseTypes/test_LonLat.html @@ -102,6 +102,20 @@ t.ok( lonlat.equals(ll), "lonlat is set correctly"); } + function test_LonLat_transform(t) { + t.plan( 6 ); + lonlat = new OpenLayers.LonLat(10, -10); + lonlat.transform(new OpenLayers.Projection("foo"), new OpenLayers.Projection("Bar")); + t.eq(lonlat.lon, 10, "lon for null transform is the same") + t.eq(lonlat.lat, -10, "lat for null transform is the same") + lonlat.transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913")); + t.eq(Math.round(lonlat.lon), 1113195, "lon for spherical mercator transform is correct"); + t.eq(Math.round(lonlat.lat), -1118890, "lat for spherical mercator correct") + lonlat.transform(new OpenLayers.Projection("EPSG:900913"), new OpenLayers.Projection("EPSG:4326")); + t.eq(lonlat.lon, 10, "lon for inverse spherical mercator transform is correct"); + t.eq(Math.round(lonlat.lat), -10, "lat for inverse spherical mercator correct") + } + function test_08_LonLat_wrapDateLine(t) { t.plan( 6 );