Allow transforming geometries, lonlat, and bounds with strings.

This commit is contained in:
tschaub
2011-10-26 16:23:48 -06:00
parent 98d6e74314
commit a606e565d3
5 changed files with 61 additions and 8 deletions

View File

@@ -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;
};

View File

@@ -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) {

View File

@@ -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) {

View File

@@ -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);

View File

@@ -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 );