LonLat.initialize() to 14 significant figures. Added an OpenLayers.Util.toFloat() function, and changed the LonLat constructor and the Bounds constructor to truncate all float parameters to 14 significant figures to avoid numeric comparison errors caused by floating point precision loss. See the comments around the definition of OpenLayers.Util.DEFAULT_PRECISION for how it works. Also refactored Bounds.intersectBounds() for readability, and added a Bounds.touchesBounds() in the process. After tweaking the tests for Layer.SphericalMercator and Format.WKT (because they were expecting too many significant figures), all tests pass. git-svn-id: http://svn.openlayers.org/trunk/openlayers@9022 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
96 lines
3.9 KiB
HTML
96 lines
3.9 KiB
HTML
<html>
|
|
<head>
|
|
<script src="../../lib/OpenLayers.js"></script>
|
|
<script type="text/javascript">
|
|
function test_SphericalMercator_forwardMercator(t) {
|
|
t.plan(12);
|
|
var arctic = OpenLayers.Layer.SphericalMercator.forwardMercator(0, 85);
|
|
var antarctic = OpenLayers.Layer.SphericalMercator.forwardMercator(0, -85);
|
|
var hawaii = OpenLayers.Layer.SphericalMercator.forwardMercator(-180, 0);
|
|
var phillipines = OpenLayers.Layer.SphericalMercator.forwardMercator(180, 0);
|
|
var ne = OpenLayers.Layer.SphericalMercator.forwardMercator(180, 90);
|
|
var sw = OpenLayers.Layer.SphericalMercator.forwardMercator(-180, -90);
|
|
|
|
t.eq(arctic.lon, 0, "Arctic longitude is correct");
|
|
t.eq(Math.round(arctic.lat), 19971869, "Arctic latitude is correct");
|
|
|
|
t.eq(antarctic.lon, 0, "Antarctic longitude is correct");
|
|
t.eq(Math.round(antarctic.lat), -19971869, "Antarctic latitude is correct");
|
|
|
|
t.eq(Math.round(hawaii.lat), 0, "Hawaiian lat is correct");
|
|
t.eq(hawaii.lon, -20037508.34, "Hawaiian lon is correct");
|
|
|
|
t.eq(Math.round(phillipines.lat), 0, "Phillipines lat is correct");
|
|
t.eq(phillipines.lon, 20037508.340, "Phillipines lon is correct");
|
|
|
|
// Rounding errors make this not infinity
|
|
t.ok(ne.lat > 50000000, "NE lat is correct");
|
|
t.eq(ne.lon, 20037508.34, "NE lon is correct");
|
|
|
|
t.eq(sw.lat, -Infinity, "SW lat is correct");
|
|
t.eq(sw.lon, -20037508.34, "SW lon is correct");
|
|
}
|
|
|
|
function test_sphericalMercator_inverseMercator(t) {
|
|
t.plan(4);
|
|
var sw = OpenLayers.Layer.SphericalMercator.inverseMercator(-20037508.34, -20037508.34);
|
|
var ne = OpenLayers.Layer.SphericalMercator.inverseMercator(20037508.34, 20037508.34);
|
|
t.eq(sw.lon, -180, "Southwest lon correct");
|
|
t.eq(ne.lon, 180, "Northeast lon correct");
|
|
|
|
t.eq(sw.lat, -85.051128779807, "Southwest lat correct");
|
|
t.eq(ne.lat, 85.051128779807, "Northeast lat correct");
|
|
}
|
|
|
|
function strToFixed(str, dig) {
|
|
if(dig == undefined) {
|
|
dig = 5;
|
|
}
|
|
return str.replace(/(\d+\.\d+)/g, function(match) {
|
|
return parseFloat(match).toFixed(dig);
|
|
});
|
|
}
|
|
|
|
function test_SphericalMercator_projectForward(t) {
|
|
t.plan(1);
|
|
var point = new OpenLayers.Geometry.Point(10, 20);
|
|
OpenLayers.Layer.SphericalMercator.projectForward(point);
|
|
|
|
t.eq(strToFixed(point.toString()),
|
|
strToFixed("POINT(1113194.9077777779 2273030.9266712805)"),
|
|
"point transforms from EPSG:4326 to Spherical Mercator");
|
|
}
|
|
|
|
function test_SphericalMercator_to4326(t) {
|
|
t.plan(1);
|
|
var point = new OpenLayers.Geometry.Point(1113195, 2273031);
|
|
|
|
OpenLayers.Layer.SphericalMercator.projectInverse(point);
|
|
|
|
t.eq(strToFixed(point.toString()),
|
|
strToFixed("POINT(10.000000828446318 20.000000618997227)"),
|
|
"point transforms from EPSG:4326 to Spherical Mercator");
|
|
}
|
|
|
|
function test_SphericalMercator_addTransform(t) {
|
|
// this class should add two methods to the
|
|
// OpenLayers.Projection.transforms object
|
|
t.plan(4);
|
|
var wgs84 = OpenLayers.Projection.transforms["EPSG:4326"];
|
|
t.ok(wgs84 instanceof Object, "EPSG:4326 exists in table");
|
|
|
|
var smerc = OpenLayers.Projection.transforms["EPSG:900913"];
|
|
t.ok(smerc instanceof Object, "EPSG:900913 exists in table");
|
|
|
|
t.ok(wgs84["EPSG:900913"] === OpenLayers.Layer.SphericalMercator.projectForward,
|
|
"from EPSG:4326 to EPSG:900913 correctly defined");
|
|
t.ok(smerc["EPSG:4326"] === OpenLayers.Layer.SphericalMercator.projectInverse,
|
|
"from EPSG:900913 to EPSG:4326 correctly defined");
|
|
}
|
|
|
|
</script>
|
|
</head>
|
|
<body>
|
|
</body>
|
|
</html>
|