summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Layer/SphericalMercator.html
blob: be94735c2436650703258ac0d0e38241ee55a8e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<html>
<head>
    <script src="../OLLoader.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");
       
        // be kind and stay within the world instead of having +/- infinity lat
        t.ok(ne.lat, 20037508.34, "NE lat is correct");
        t.eq(ne.lon, 20037508.34, "NE lon is correct");
        
        t.eq(sw.lat, -20037508.34, "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.toFixed(10), "-85.0511287798", "Southwest lat correct");
        t.eq(ne.lat.toFixed(10), "85.0511287798", "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_to4326(t) { 
        t.plan(1);
        var point = new OpenLayers.Geometry.Point(1113195, 2273031);
        point.transform("EPSG:900913", "EPSG:4326");

        t.eq(strToFixed(point.toString()), 
             strToFixed("POINT(10.000000828446318 20.000000618997227)"), 
             "point transforms from Spherical Mercator to EPSG:4326"); 
    }
    
    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(typeof(wgs84["EPSG:900913"]) === "function",
             "from EPSG:4326 to EPSG:900913 correctly defined");
        t.ok(typeof(smerc["EPSG:4326"]) === "function",
             "from EPSG:900913 to EPSG:4326 correctly defined");
    }
    
    function test_equivalence(t) {

        // list of equivalent codes for web mercator
        var codes = ["EPSG:900913", "EPSG:3857", "EPSG:102113", "EPSG:102100"];
        var len = codes.length;
        
        t.plan(len + (len * len));

        var ggPoint = new OpenLayers.Geometry.Point(10, 20);
        var smPoint = new OpenLayers.Geometry.Point(1113195, 2273031);
        
        var gg = new OpenLayers.Projection("EPSG:4326");
        
        var i, proj, forward, inverse, other, j, equiv;
        for (i=0, len=codes.length; i<len; ++i) {
            proj = new OpenLayers.Projection(codes[i]);
            
            // confirm that forward/inverse work
            forward = ggPoint.clone().transform(gg, proj);
            t.eq(
                strToFixed(forward.toString()), 
                strToFixed("POINT(1113194.9077777779 2273030.9266712805)"), 
                "transforms from EPSG:4326 to " + proj
            );
            inverse = smPoint.clone().transform(proj, gg);
            t.eq(
                strToFixed(inverse.toString()), 
                strToFixed("POINT(10.000000828446318 20.000000618997227)"), 
                "transforms from " + proj + " to EPSG:4326"
            ); 
            
            // confirm that null transform works
            for (j=i+1; j<len; ++j) {
                other = new OpenLayers.Projection(codes[j]);
                equiv = ggPoint.clone().transform(proj, other);
                t.ok(proj.equals(other),  proj + " and " + other + " are equivalent");
                t.ok(ggPoint.equals(equiv), "transform from " + proj + " to " + other + " preserves geometry");                
            }
        }

    }
    
  </script> 
</head>
<body>
</body>
</html>