diff --git a/lib/OpenLayers/Projection.js b/lib/OpenLayers/Projection.js index 7e42b8459b..97eb2f63ba 100644 --- a/lib/OpenLayers/Projection.js +++ b/lib/OpenLayers/Projection.js @@ -186,12 +186,19 @@ OpenLayers.Projection.addTransform = function(from, to, method) { * point - {object} A transformed coordinate. The original point is modified. */ OpenLayers.Projection.transform = function(point, source, dest) { - if (source.proj && dest.proj) { - point = Proj4js.transform(source.proj, dest.proj, point); - } else if (source && dest && - OpenLayers.Projection.transforms[source.getCode()] && - OpenLayers.Projection.transforms[source.getCode()][dest.getCode()]) { - OpenLayers.Projection.transforms[source.getCode()][dest.getCode()](point); + if (source && dest) { + if (!(source instanceof OpenLayers.Projection)) { + source = new OpenLayers.Projection(source); + } + if (!(dest instanceof OpenLayers.Projection)) { + dest = new OpenLayers.Projection(dest); + } + if (source.proj && dest.proj) { + point = Proj4js.transform(source.proj, dest.proj, point); + } else if (OpenLayers.Projection.transforms[source.getCode()] && + OpenLayers.Projection.transforms[source.getCode()][dest.getCode()]) { + OpenLayers.Projection.transforms[source.getCode()][dest.getCode()](point); + } } return point; }; diff --git a/tests/BaseTypes/Bounds.html b/tests/BaseTypes/Bounds.html index b8ff57eceb..a1497cf6da 100644 --- a/tests/BaseTypes/Bounds.html +++ b/tests/BaseTypes/Bounds.html @@ -630,7 +630,7 @@ } function test_Bounds_transform(t) { - t.plan( 3 ); + t.plan( 5 ); 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"); @@ -638,6 +638,14 @@ 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"); + + // transform with string + bounds = new OpenLayers.Bounds(10, -10, 20, 10); + bounds.transform("EPSG:4326", "EPSG:900913"); + t.eq(bounds.toBBOX(), "1113194.907778,-1118889.974702,2226389.815556,1118889.974702", "(string) bounds for spherical mercator transform are correct"); + bounds.transform("EPSG:900913", "EPSG:4326"); + t.eq(bounds.toBBOX(), "10,-10,20,10", "(string) bounds for inverse spherical mercator transform are correct"); + } function test_Bounds_add(t) { diff --git a/tests/BaseTypes/LonLat.html b/tests/BaseTypes/LonLat.html index d5283cf286..a1801f6c6b 100644 --- a/tests/BaseTypes/LonLat.html +++ b/tests/BaseTypes/LonLat.html @@ -166,7 +166,7 @@ } function test_LonLat_transform(t) { - t.plan( 6 ); + t.plan(10); 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") @@ -177,6 +177,16 @@ 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") + + // transform with string + lonlat = new OpenLayers.LonLat(10, -10); + lonlat.transform("EPSG:4326", "EPSG:900913"); + t.eq(Math.round(lonlat.lon), 1113195, "(string) lon for spherical mercator transform is correct"); + t.eq(Math.round(lonlat.lat), -1118890, "(string) lat for spherical mercator correct") + lonlat.transform("EPSG:900913", "EPSG:4326"); + t.eq(lonlat.lon, 10, "(string) lon for inverse spherical mercator transform is correct"); + t.eq(Math.round(lonlat.lat), -10, "(string) lat for inverse spherical mercator correct") + } function test_LonLat_wrapDateLine(t) { diff --git a/tests/Geometry/Point.html b/tests/Geometry/Point.html index 9baaf5c72b..838a587521 100644 --- a/tests/Geometry/Point.html +++ b/tests/Geometry/Point.html @@ -50,6 +50,21 @@ new OpenLayers.Projection("EPSG:900913")); t.eq(point.bounds, null, "Point bounds cleared after transform"); } + + function test_Point_transform_string(t) { + t.plan(4); + + var x = 10; + var y = 20; + point = new OpenLayers.Geometry.Point(x, y); + point.calculateBounds(); + t.ok( point.bounds != null, "bounds calculated by calcBounds" ); + point.transform("EPSG:4326", "EPSG:900913"); + t.eq(point.bounds, null, "Point bounds cleared after transform"); + t.eq(point.x.toFixed(2), "1113194.91", "transformed x"); + t.eq(point.y.toFixed(2), "2273030.93", "transformed y"); + + } function test_Point_distanceTo(t) { t.plan(7); diff --git a/tests/Geometry/Polygon.html b/tests/Geometry/Polygon.html index 598b68800e..9f5cd95358 100644 --- a/tests/Geometry/Polygon.html +++ b/tests/Geometry/Polygon.html @@ -46,6 +46,19 @@ t.eq(polygon.getBounds().toBBOX(), "556597.453889,334111.171355,1113194.907778,1574216.547942", "Bounds are correct") } + function test_Polygon_transform_string (t) { + t.plan(3); + + var components = [new OpenLayers.Geometry.Point(10,14), new OpenLayers.Geometry.Point(5,3)]; + var linearRing = new OpenLayers.Geometry.LinearRing(components); + polygon = new OpenLayers.Geometry.Polygon([linearRing.clone()]); + polygon.calculateBounds(); + t.ok( polygon.bounds != null, "bounds calculated by calcBounds" ); + polygon.transform("EPSG:4326", "EPSG:900913"); + t.eq(polygon.bounds, null, "Point bounds cleared after transform"); + t.eq(polygon.getBounds().toBBOX(), "556597.453889,334111.171355,1113194.907778,1574216.547942", "Bounds are correct") + } + function test_Polygon_getArea(t) { t.plan( 5 );