Allow transforming geometries, lonlat, and bounds with strings.
This commit is contained in:
@@ -186,12 +186,19 @@ OpenLayers.Projection.addTransform = function(from, to, method) {
|
|||||||
* point - {object} A transformed coordinate. The original point is modified.
|
* point - {object} A transformed coordinate. The original point is modified.
|
||||||
*/
|
*/
|
||||||
OpenLayers.Projection.transform = function(point, source, dest) {
|
OpenLayers.Projection.transform = function(point, source, dest) {
|
||||||
if (source.proj && dest.proj) {
|
if (source && dest) {
|
||||||
point = Proj4js.transform(source.proj, dest.proj, point);
|
if (!(source instanceof OpenLayers.Projection)) {
|
||||||
} else if (source && dest &&
|
source = new OpenLayers.Projection(source);
|
||||||
OpenLayers.Projection.transforms[source.getCode()] &&
|
}
|
||||||
OpenLayers.Projection.transforms[source.getCode()][dest.getCode()]) {
|
if (!(dest instanceof OpenLayers.Projection)) {
|
||||||
OpenLayers.Projection.transforms[source.getCode()][dest.getCode()](point);
|
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;
|
return point;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -630,7 +630,7 @@
|
|||||||
|
|
||||||
}
|
}
|
||||||
function test_Bounds_transform(t) {
|
function test_Bounds_transform(t) {
|
||||||
t.plan( 3 );
|
t.plan( 5 );
|
||||||
bounds = new OpenLayers.Bounds(10, -10, 20, 10);
|
bounds = new OpenLayers.Bounds(10, -10, 20, 10);
|
||||||
bounds.transform(new OpenLayers.Projection("foo"), new OpenLayers.Projection("Bar"));
|
bounds.transform(new OpenLayers.Projection("foo"), new OpenLayers.Projection("Bar"));
|
||||||
t.eq(bounds.toBBOX(), "10,-10,20,10", "null transform okay");
|
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");
|
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"));
|
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");
|
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) {
|
function test_Bounds_add(t) {
|
||||||
|
|||||||
@@ -166,7 +166,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function test_LonLat_transform(t) {
|
function test_LonLat_transform(t) {
|
||||||
t.plan( 6 );
|
t.plan(10);
|
||||||
lonlat = new OpenLayers.LonLat(10, -10);
|
lonlat = new OpenLayers.LonLat(10, -10);
|
||||||
lonlat.transform(new OpenLayers.Projection("foo"), new OpenLayers.Projection("Bar"));
|
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.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"));
|
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(lonlat.lon, 10, "lon for inverse spherical mercator transform is correct");
|
||||||
t.eq(Math.round(lonlat.lat), -10, "lat for inverse spherical mercator 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) {
|
function test_LonLat_wrapDateLine(t) {
|
||||||
|
|||||||
@@ -51,6 +51,21 @@
|
|||||||
t.eq(point.bounds, null, "Point bounds cleared after transform");
|
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) {
|
function test_Point_distanceTo(t) {
|
||||||
t.plan(7);
|
t.plan(7);
|
||||||
|
|
||||||
|
|||||||
@@ -46,6 +46,19 @@
|
|||||||
t.eq(polygon.getBounds().toBBOX(), "556597.453889,334111.171355,1113194.907778,1574216.547942", "Bounds are correct")
|
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) {
|
function test_Polygon_getArea(t) {
|
||||||
t.plan( 5 );
|
t.plan( 5 );
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user