summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Layer/PointGrid.html
blob: 6fb6ae2012481136c423ceca40da2cb1eae1b120 (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
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
226
227
228
229
230
231
232
<!DOCTYPE html>
<html>
<head>
<script src="../OLLoader.js"></script>
<script type="text/javascript">

    function test_initialize(t) {
        t.plan(1);
        var layer = new OpenLayers.Layer.PointGrid();
        t.ok(layer instanceof OpenLayers.Layer.PointGrid, "instance created");
        layer.destroy();
    }
    
    function test_name(t) {
        t.plan(1);
        var layer = new OpenLayers.Layer.PointGrid({name: "foo"});
        t.eq(layer.name, "foo", "name set like every other property");
        layer.destroy();
    }
    
    function test_spacing(t) {
        t.plan(7);
        
        var layer = new OpenLayers.Layer.PointGrid({
            isBaseLayer: true,
            resolutions: [1],
            maxExtent: new OpenLayers.Bounds(-100, -50, 100, 50),
            dx: 10,
            dy: 10,
            ratio: 1
        });
        
        var map = new OpenLayers.Map({
            div: "map",
            layers: [layer],
            center: new OpenLayers.LonLat(0, 0),
            zoom: 0
        });
        
        t.eq(layer.features.length, 200, "200 features");
        
        // set dx/dy together
        layer.setSpacing(20);
        t.eq(layer.dx, 20, "dx 20");
        t.eq(layer.dy, 20, "dy 20");
        t.eq(layer.features.length, 50, "50 features");
        
        // set dx/dy independently
        layer.setSpacing(50, 25);
        t.eq(layer.dx, 50, "dx 50");
        t.eq(layer.dy, 25, "dy 25");
        t.eq(layer.features.length, 16, "16 features");
        
        map.destroy();
    }
    
    function test_ratio(t) {
        t.plan(3);
        
        var layer = new OpenLayers.Layer.PointGrid({
            isBaseLayer: true,
            resolutions: [1],
            maxExtent: new OpenLayers.Bounds(-100, -50, 100, 50),
            dx: 25,
            dy: 25,
            ratio: 1
        });
        
        var map = new OpenLayers.Map({
            div: "map",
            layers: [layer],
            center: new OpenLayers.LonLat(0, 0),
            zoom: 0
        });
        
        t.eq(layer.features.length, 32, "32 features");
        
        // increase ratio (1.5 -> 300 x 150)
        layer.setRatio(1.5);
        t.eq(layer.ratio, 1.5, "ratio 1.5");
        t.eq(layer.features.length, 72, "72 features");
        
        map.destroy();
    }

    function test_maxFeatures(t) {
        t.plan(3);
        
        var layer = new OpenLayers.Layer.PointGrid({
            isBaseLayer: true,
            resolutions: [1],
            maxExtent: new OpenLayers.Bounds(-100, -50, 100, 50),
            dx: 10,
            dy: 10,
            ratio: 1
        });
        
        var map = new OpenLayers.Map({
            div: "map",
            layers: [layer],
            center: new OpenLayers.LonLat(0, 0),
            zoom: 0
        });
        
        t.eq(layer.features.length, 200, "200 features");
        
        // limit maxFeatures
        layer.setMaxFeatures(150);
        t.eq(layer.maxFeatures, 150, "maxFeatures 150");
        t.ok(layer.features.length <= 150, "<= 150 features");
        
        map.destroy();
    }

    function test_rotation(t) {
        t.plan(6);
        
        var layer = new OpenLayers.Layer.PointGrid({
            isBaseLayer: true,
            resolutions: [1],
            maxExtent: new OpenLayers.Bounds(-100, -50, 100, 50),
            dx: 10,
            dy: 10,
            ratio: 1
        });
        
        var map = new OpenLayers.Map({
            div: "map",
            layers: [layer],
            center: new OpenLayers.LonLat(0, 0),
            zoom: 0
        });

        function getRotation(layer) {
            // grid starts at bottom left and goes up
            var g0 = layer.features[0].geometry;
            var g1 = layer.features[1].geometry;
            // subtract 90 to get rotation of grid
            return Math.atan2(g1.y - g0.y, g1.x - g0.x) * (180 / Math.PI) - 90;
        }
        
        t.eq(layer.rotation, 0, "0 rotation");
        t.eq(getRotation(layer).toFixed(3), (0).toFixed(3), "0 grid")
        
        // rotate grid 25 degrees counter-clockwise
        layer.setRotation(25);
        t.eq(layer.rotation, 25, "25 rotation");
        t.eq(getRotation(layer).toFixed(3), (25).toFixed(3), "25 grid");

        // rotate grid 45 degrees clockwise
        layer.setRotation(-45);
        t.eq(layer.rotation, -45, "-45 rotation");
        t.eq(getRotation(layer).toFixed(3), (-45).toFixed(3), "-45 grid");
        
        map.destroy();
    }

    function test_origin(t) {
        t.plan(7);
        
        var layer = new OpenLayers.Layer.PointGrid({
            isBaseLayer: true,
            resolutions: [1],
            maxExtent: new OpenLayers.Bounds(-100, -50, 100, 50),
            dx: 10,
            dy: 10,
            ratio: 1
        });
        
        var map = new OpenLayers.Map({
            div: "map",
            layers: [layer],
            center: new OpenLayers.LonLat(0, 0),
            zoom: 0
        });

        var origin = layer.getOrigin();
        t.ok(map.getExtent().getCenterLonLat().equals(origin), "default is center of map extent");
        
        var g0 = layer.features[0].geometry;
        
        t.eq((g0.x - origin.lon) % layer.dx, 0, "a) lattice aligned with origin x");
        t.eq((g0.y - origin.lat) % layer.dy, 0, "a) lattice aligned with origin y");
        
        // set origin
        layer.setOrigin(new OpenLayers.LonLat(-5, 12));
        origin = layer.getOrigin();
        t.eq(origin.lon, -5, "-5 origin x");
        t.eq(origin.lat, 12, "12 origin y");

        g0 = layer.features[0].geometry;        
        t.eq((g0.x - origin.lon) % layer.dx, 0, "b) lattice aligned with origin x");
        t.eq((g0.y - origin.lat) % layer.dy, 0, "b) lattice aligned with origin y");

        map.destroy();
    }

    function test_zoom(t) {
        t.plan(2);
        
        var layer = new OpenLayers.Layer.PointGrid({
            isBaseLayer: true,
            resolutions: [2, 1],
            maxExtent: new OpenLayers.Bounds(-200, -100, 200, 100),
            dx: 20,
            dy: 20,
            ratio: 1
        });
        
        var map = new OpenLayers.Map({
            div: "map",
            layers: [layer],
            center: new OpenLayers.LonLat(0, 0),
            zoom: 1,
            zoomMethod: null
        });
        
        t.eq(layer.features.length, 50, "50 features at zoom 1");
        
        map.zoomTo(0);
        t.eq(layer.features.length, 200, "200 features at zoom 0")

        map.destroy();
    }


</script>
</head>
<body>
<div id="map" style="width:200px;height:100px"></div>
</body>
</html>