File: LonLat.html

package info (click to toggle)
openlayers 2.11%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 60,144 kB
  • ctags: 10,906
  • sloc: xml: 7,435; python: 778; sh: 68; makefile: 30
file content (225 lines) | stat: -rw-r--r-- 8,535 bytes parent folder | download
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">

    var lonlat; 

    function test_LonLat_constructor (t) {
        t.plan( 6 );
        lonlat = new OpenLayers.LonLat(6, 5);
        t.ok( lonlat instanceof OpenLayers.LonLat, "new OpenLayers.LonLat returns LonLat object" );
        t.eq( lonlat.CLASS_NAME, "OpenLayers.LonLat", "lonlat.CLASS_NAME is set correctly");
        t.eq( lonlat.lon, 6, "lonlat.lon is set correctly");
        t.eq( lonlat.lat, 5, "lonlat.lat is set correctly");

        // possible global Mercator projection values
        lonlat = new OpenLayers.LonLat(20037508.33999999, -20037508.33999999);
        t.eq( lonlat.lon, 20037508.34, "lonlat.lon rounds correctly");
        t.eq( lonlat.lat, -20037508.34, "lonlat.lat rounds correctly");
    }
    
    function test_LonLat_constructorFromStrings (t) {
        t.plan( 4 );
        lonlat = new OpenLayers.LonLat("6", "5");
        t.ok( lonlat instanceof OpenLayers.LonLat, "new OpenLayers.LonLat returns LonLat object" );
        t.eq( lonlat.CLASS_NAME, "OpenLayers.LonLat", "lonlat.CLASS_NAME is set correctly");
        t.eq( lonlat.lon, 6, "lonlat.lon is set correctly");
        t.eq( lonlat.lat, 5, "lonlat.lat is set correctly");
    }

    function test_LonLat_toString(t) {
        t.plan( 1 );
        lonlat = new OpenLayers.LonLat(5,6);
        t.eq( lonlat.toString(), "lon=5,lat=6", "lonlat.toString() returns correctly");
    }

    function test_LonLat_toShortString(t) {
        t.plan( 1 );
        lonlat = new OpenLayers.LonLat(5,6);
        t.eq( lonlat.toShortString(), "5, 6", "lonlat.toShortString() returns correctly");
    }

    function test_LonLat_clone(t) {
        t.plan( 3 );
        oldLonLat = new OpenLayers.LonLat(5,6);
        lonlat = oldLonLat.clone();
        t.ok( lonlat instanceof OpenLayers.LonLat, "clone returns new OpenLayers.LonLat object" );
        t.ok( lonlat.equals(oldLonLat), "lonlat is set correctly");
        
        oldLonLat.lon = 100;
        t.eq( lonlat.lon, 5, "changing oldLonLat.lon doesn't change lonlat.lon");
    }

    function test_LonLat_add(t) {
        t.plan(10);

        origLL = new OpenLayers.LonLat(10,100);
        lonlatA = origLL.clone();

        addpx = lonlatA.add(5, 50);
        t.ok( lonlatA.equals(origLL), "lonlatA is not modified by add operation");

        var ll = new OpenLayers.LonLat(15,150);
        t.ok( addpx.equals(ll), "addpx is set correctly");
        
    //null values
        OpenLayers.Lang.setCode('en');
        var desiredMsg = "You must pass both lon and lat values to the add function.";
        OpenLayers.Console.error = function(msg) {
            t.eq(msg, desiredMsg, "error correctly reported");
        }
    
        addpx = lonlatA.add(null, 50);
        t.ok( lonlatA.equals(origLL), "lonlatA is not modified by erroneous add operation (null lon)");
        t.ok(addpx == null, "returns null on erroneous add operation (null lon)");
 
        addpx = lonlatA.add(5, null);
        t.ok( lonlatA.equals(origLL), "lonlatA is not modified by erroneous add operation (null lat)");
        t.ok(addpx == null, "returns null on erroneous add operation (null lat)");

        // string values
        addpx = origLL.clone().add("5", "50");
        t.eq(addpx.lon, 15, "addpx.lon is set correctly");
        t.eq(addpx.lat, 150, "addpx.lat is set correctly");
    }
    
    function test_LonLat_equals(t) {
        t.plan( 5 );
        lonlat = new OpenLayers.LonLat(5,6);

        ll = new OpenLayers.LonLat(5,6);
        t.eq( lonlat.equals(ll), true, "(5,6) equals (5,6)");

        ll = new OpenLayers.LonLat(1,6);
        t.eq( lonlat.equals(ll), false, "(5,6) does not equal (1,6)");

        ll = new OpenLayers.LonLat(5,2);
        t.eq( lonlat.equals(ll), false, "(5,6) does not equal (5,2)");

        ll = new OpenLayers.LonLat(1,2);
        t.eq( lonlat.equals(ll), false, "(5,6) does not equal (1,2)");

        t.ok( !lonlat.equals(null), "equals() returns false on comparison to null");

    }

    function test_LonLat_fromString(t) {
        t.plan( 2 );
        lonlat = OpenLayers.LonLat.fromString("6,5");
        t.ok( lonlat instanceof OpenLayers.LonLat, "OpenLayers.LonLat.fromString() returns LonLat object" );

        var ll = new OpenLayers.LonLat(6, 5);
        t.ok( lonlat.equals(ll), "lonlat is set correctly");
    }
    
    function test_LonLat_fromArray(t) {
        t.plan( 3 );
        
        // (1 test) must return a OpenLayers.LonLat-instance 
        lonlat = OpenLayers.LonLat.fromArray([6,5]);
        t.ok( lonlat instanceof OpenLayers.LonLat, "OpenLayers.LonLat.fromArray returns LonLat object" );

        var ll = new OpenLayers.LonLat(6, 5);
        // (1 test) must return correct LonLat-object
        t.ok( lonlat.equals(ll), "lonlat is set correctly");
        
        
        // (1 test) check how function deals with illegal arguments, it should 
        // never throw an exception and always return an instance of 
        // OpenLayers.LonLat.
        var unexpectedResult = false,
            undef,
            checkArgs = [
                {},
                '',
                6,
                false,
                true,
                [undef, 5],
                [6, undef]
            ],
            returnedVal;
            
        try {
            for(var i = 0, len = checkArgs.length; i < len; i++ ){
                returnedVal = OpenLayers.LonLat.fromArray( checkArgs[i] );
                if (!(returnedVal instanceof OpenLayers.LonLat) ) {
                    unexpectedResult = true;
                    break;
                }
            }
            // no arguments at all
            returnedVal = OpenLayers.LonLat.fromArray();
            unexpectedResult = !(returnedVal instanceof OpenLayers.LonLat);
        } catch(e) {
            unexpectedResult = true;
        } finally {
            t.ok(!unexpectedResult, "OpenLayers.LonLat.fromArray always returns an instance of OpenLayers.LonLat and doesn't throw an exception when called with unexpected argument.");
        }
    }

    function test_LonLat_transform(t) {
        t.plan( 6 );
        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")
        t.eq(lonlat.lat, -10, "lat for null transform is the same")
        lonlat.transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913")); 
        t.eq(Math.round(lonlat.lon), 1113195, "lon for spherical mercator transform is correct");
        t.eq(Math.round(lonlat.lat), -1118890, "lat for spherical mercator correct")
        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")
    }
    
    function test_LonLat_wrapDateLine(t) {
        t.plan( 6 );

        var goodLL = new OpenLayers.LonLat(0,0);
        var testLL, wrappedLL;

  //bad maxextent
        var maxExtent = null;

        testLL = goodLL.clone();
        wrappedLL = testLL.wrapDateLine(maxExtent);
        t.ok(wrappedLL.equals(goodLL), "wrapping a ll with a bad maxextent does nothing");
        
        
  //good maxextent
        maxExtent = new OpenLayers.Bounds(-10,-10,10,10);

    //inside
        testLL = goodLL.clone();
        wrappedLL = testLL.wrapDateLine(maxExtent);
        t.ok(wrappedLL.equals(goodLL), "wrapping a ll within the maxextent does nothing");
        
    //left
        testLL = goodLL.add(-20,0);
        wrappedLL = testLL.wrapDateLine(maxExtent);
        t.ok(wrappedLL.equals(goodLL), "wrapping a ll once left of maxextent works");
        
    //way left
        testLL = goodLL.add(-200,0);
        wrappedLL = testLL.wrapDateLine(maxExtent);
        t.ok(wrappedLL.equals(goodLL), "wrapping a ll way left of maxextent works");

    //right
        testLL = goodLL.add(20,0);
        wrappedLL = testLL.wrapDateLine(maxExtent);
        t.ok(wrappedLL.equals(goodLL), "wrapping a ll once right of maxextent works");
        
    //way right
        testLL = goodLL.add(200,0);
        wrappedLL = testLL.wrapDateLine(maxExtent);
        t.ok(wrappedLL.equals(goodLL), "wrapping a ll way right of maxextent works");

    }


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