3 <script src="../../lib/OpenLayers.js"></script>
4 <script type="text/javascript">
5 function test_SphericalMercator_forwardMercator(t) {
7 var arctic = OpenLayers.Layer.SphericalMercator.forwardMercator(0, 85);
8 var antarctic = OpenLayers.Layer.SphericalMercator.forwardMercator(0, -85);
9 var hawaii = OpenLayers.Layer.SphericalMercator.forwardMercator(-180, 0);
10 var phillipines = OpenLayers.Layer.SphericalMercator.forwardMercator(180, 0);
11 var ne = OpenLayers.Layer.SphericalMercator.forwardMercator(180, 90);
12 var sw = OpenLayers.Layer.SphericalMercator.forwardMercator(-180, -90);
14 t.eq(arctic.lon, 0, "Arctic longitude is correct");
15 t.eq(Math.round(arctic.lat), 19971869, "Arctic latitude is correct");
17 t.eq(antarctic.lon, 0, "Antarctic longitude is correct");
18 t.eq(Math.round(antarctic.lat), -19971869, "Antarctic latitude is correct");
20 t.eq(Math.round(hawaii.lat), 0, "Hawaiian lat is correct");
21 t.eq(hawaii.lon, -20037508.34, "Hawaiian lon is correct");
23 t.eq(Math.round(phillipines.lat), 0, "Phillipines lat is correct");
24 t.eq(phillipines.lon, 20037508.340, "Phillipines lon is correct");
26 // Rounding errors make this not infinity
27 t.ok(ne.lat > 50000000, "NE lat is correct");
28 t.eq(ne.lon, 20037508.34, "NE lon is correct");
30 t.eq(sw.lat, -Infinity, "SW lat is correct");
31 t.eq(sw.lon, -20037508.34, "SW lon is correct");
34 function test_sphericalMercator_inverseMercator(t) {
36 var sw = OpenLayers.Layer.SphericalMercator.inverseMercator(-20037508.34, -20037508.34);
37 var ne = OpenLayers.Layer.SphericalMercator.inverseMercator(20037508.34, 20037508.34);
38 t.eq(sw.lon, -180, "Southwest lon correct");
39 t.eq(ne.lon, 180, "Northeast lon correct");
41 t.eq(sw.lat, -85.051128779807, "Southwest lat correct");
42 t.eq(ne.lat, 85.051128779807, "Northeast lat correct");
45 function strToFixed(str, dig) {
46 if(dig == undefined) {
49 return str.replace(/(\d+\.\d+)/g, function(match) {
50 return parseFloat(match).toFixed(dig);
54 function test_SphericalMercator_projectForward(t) {
56 var point = new OpenLayers.Geometry.Point(10, 20);
57 OpenLayers.Layer.SphericalMercator.projectForward(point);
59 t.eq(strToFixed(point.toString()),
60 strToFixed("POINT(1113194.9077777779 2273030.9266712805)"),
61 "point transforms from EPSG:4326 to Spherical Mercator");
64 function test_SphericalMercator_to4326(t) {
66 var point = new OpenLayers.Geometry.Point(1113195, 2273031);
68 OpenLayers.Layer.SphericalMercator.projectInverse(point);
70 t.eq(strToFixed(point.toString()),
71 strToFixed("POINT(10.000000828446318 20.000000618997227)"),
72 "point transforms from EPSG:4326 to Spherical Mercator");
75 function test_SphericalMercator_addTransform(t) {
76 // this class should add two methods to the
77 // OpenLayers.Projection.transforms object
79 var wgs84 = OpenLayers.Projection.transforms["EPSG:4326"];
80 t.ok(wgs84 instanceof Object, "EPSG:4326 exists in table");
82 var smerc = OpenLayers.Projection.transforms["EPSG:900913"];
83 t.ok(smerc instanceof Object, "EPSG:900913 exists in table");
85 t.ok(wgs84["EPSG:900913"] === OpenLayers.Layer.SphericalMercator.projectForward,
86 "from EPSG:4326 to EPSG:900913 correctly defined");
87 t.ok(smerc["EPSG:4326"] === OpenLayers.Layer.SphericalMercator.projectInverse,
88 "from EPSG:900913 to EPSG:4326 correctly defined");