summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Layer/ArcGIS93Rest.html
blob: ddca6ac7a8b7c7ffc4ae2fab305653df06da716d (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<html>
<head>
    <script type="text/javascript">var oldAlert = window.alert, gMess; window.alert = function(message) {gMess = message; return true;};</script>
    <script type="text/javascript">window.alert = oldAlert;</script>
<script src="../OLLoader.js"></script>
  <script type="text/javascript">
    var isMozilla = (navigator.userAgent.indexOf("compatible") == -1);
    var layer; 

    var name = 'Test Layer';
    var url = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer/export";
    var params = {layers: "show:0,2"};

    function test_Layer_AGS93_constructor (t) {
        var params = {layers: "show:0,2"};
        t.plan( 14 );

        var trans_format = "png";
        if (OpenLayers.Util.alphaHack()) { trans_format = "gif"; } 
        
        layer = new OpenLayers.Layer.ArcGIS93Rest(name, url, params);
        t.ok( layer instanceof OpenLayers.Layer.ArcGIS93Rest, "new OpenLayers.Layer.ArcGIS93Rest returns object" );
        t.eq( layer.url, url, "layer.url is correct (HTTPRequest inited)" );
        t.eq( layer.params.LAYERS, "show:0,2", "params passed in correctly uppercased" );

        t.eq( layer.params.FORMAT, "png", "default params correclty uppercased and copied");

        t.eq(layer.isBaseLayer, true, "no transparency setting, wms is baselayer");

        params.format = 'jpg';
        layer = new OpenLayers.Layer.ArcGIS93Rest(name, url, params);
        t.eq( layer.params.FORMAT, "jpg", "default params correclty uppercased and overridden");

        params.TRANSPARENT = "true";
        var layer2 = new OpenLayers.Layer.ArcGIS93Rest(name, url, params);
        t.eq(layer2.isBaseLayer, false, "transparency == 'true', wms is not baselayer");

        params.TRANSPARENT = "TRUE";
        var layer3 = new OpenLayers.Layer.ArcGIS93Rest(name, url, params);
        t.eq(layer3.isBaseLayer, false, "transparency == 'TRUE', wms is not baselayer");
        t.eq(layer3.params.FORMAT, trans_format, "transparent = TRUE causes non-image/jpeg format");

        params.TRANSPARENT = "TRuE";
        var layer4 = new OpenLayers.Layer.ArcGIS93Rest(name, url, params);
        t.eq(layer4.isBaseLayer, false, "transparency == 'TRuE', wms is not baselayer");
        t.eq(layer4.params.FORMAT, trans_format, "transparent = TRuE causes non-image/jpeg format");

        params.TRANSPARENT = true;
        var layer5 = new OpenLayers.Layer.ArcGIS93Rest(name, url, params);
        t.eq(layer5.isBaseLayer, false, "transparency == true, wms is not baselayer");
        t.eq(layer5.params.FORMAT, trans_format, "transparent = true causes non-image/jpeg format");

        params.TRANSPARENT = false;
        var layer6 = new OpenLayers.Layer.ArcGIS93Rest(name, url, params);
        t.eq(layer6.isBaseLayer, true, "transparency == false, wms is baselayer");
    }
    
    function test_Layer_AGS93_addtile (t) {
        var params = {layers: "show:0,2"};
        t.plan( 6 );
    
        layer = new OpenLayers.Layer.ArcGIS93Rest(name, url, params);
        var map = new OpenLayers.Map('map', {tileManager: null});
        map.addLayer(layer);
        var pixel = new OpenLayers.Pixel(5,6);
        var tile = layer.addTile(new OpenLayers.Bounds(1,2,3,4), pixel);
        tile.draw();

        var img = tile.imgDiv;
        var tParams = OpenLayers.Util.extend({},
                        OpenLayers.Util.upperCaseObject(params));
        tParams = OpenLayers.Util.extend(tParams, {
            FORMAT: "png", BBOX: "1,2,3,4", SIZE: "256,256", F: "image", BBOXSR: "4326", IMAGESR: "4326"
        });
        t.eq( tile.url,
             url + "?" + OpenLayers.Util.getParameterString(tParams),
             "image src is created correctly via addtile" );
        t.eq( tile.getTile().style.top, "6px", "image top is set correctly via addtile" );
        t.eq( tile.getTile().style.left, "5px", "image top is set correctly via addtile" );

        var firstChild = layer.div.firstChild;
        t.eq( firstChild.nodeName.toLowerCase(), "img", "div first child is an image object" );
        t.ok( firstChild == img, "div first child is correct image object" );
        t.eq( tile.position.toString(), "x=5,y=6", "Position of tile is set correctly." );
        map.destroy();
    }
    
    function test_Layer_AGS93_inittiles (t) {
        var params = {layers: "show:0,2"};
        t.plan( 2 );
        var map = new OpenLayers.Map('map');
        layer = new OpenLayers.Layer.ArcGIS93Rest(name, url, params, {buffer: 2});
        map.addLayer(layer);
        map.setCenter(new OpenLayers.LonLat(0,0),5);
        t.eq( layer.grid.length, 8, "Grid rows is correct." );
        t.eq( layer.grid[0].length, 7, "Grid cols is correct." );
        map.destroy();
    }


    function test_Layer_AGS93_clone (t) {
        var params = {layers: "show:0,2"};
        t.plan(4);
        
        var options = {tileSize: new OpenLayers.Size(500,50)};
        var map = new OpenLayers.Map('map', options);
        layer = new OpenLayers.Layer.ArcGIS93Rest(name, url, params);
        map.addLayer(layer);

        layer.grid = [ [6, 7], 
                       [8, 9]];

        var clone = layer.clone();

        t.ok( clone.grid != layer.grid, "clone does not copy grid");

        t.ok( clone.tileSize.equals(layer.tileSize), "tileSize correctly cloned");

        layer.tileSize.w += 40;

        t.eq( clone.tileSize.w, 500, "changing layer.tileSize does not change clone.tileSize -- a fresh copy was made, not just copied reference");

        t.eq( clone.alpha, layer.alpha, "alpha copied correctly");

        layer.grid = null;
        map.destroy();
    }

    function test_Layer_AGS93_isBaseLayer(t) {
        var params = {layers: "show:0,2"};
        t.plan(3);
        
        layer = new OpenLayers.Layer.ArcGIS93Rest(name, url, params);
        t.ok( layer.isBaseLayer, "baselayer is true by default");

        var newParams = OpenLayers.Util.extend({}, params);
        newParams.transparent = "true";
        layer = new OpenLayers.Layer.ArcGIS93Rest(name, url, newParams);
        t.ok( !layer.isBaseLayer, "baselayer is false when transparent is set to true");

        layer = new OpenLayers.Layer.ArcGIS93Rest(name, url, params, {isBaseLayer: false});
        t.ok( !layer.isBaseLayer, "baselayer is false when option is set to false" );
    }

    function test_Layer_AGS93_mergeNewParams (t) {
        var params = {layers: "show:0,2"};
        t.plan( 4 );

        var map = new OpenLayers.Map("map");
        layer = new OpenLayers.Layer.ArcGIS93Rest(name, url, params);
        
        var newParams = { layers: 'sooper', 
                          chickpeas: 'png'};

        map.addLayer(layer);
        map.zoomToMaxExtent();

        layer.redraw = function() {
            t.ok(true, "layer is redrawn after new params merged");
        }

        layer.mergeNewParams(newParams);
        
        t.eq( layer.params.LAYERS, "sooper", "mergeNewParams() overwrites well");
        t.eq( layer.params.CHICKPEAS, "png", "mergeNewParams() adds well");
    
        newParams.CHICKPEAS = 151;

        t.eq( layer.params.CHICKPEAS, "png", "mergeNewParams() makes clean copy of hashtable");
        map.destroy();
    }

    function test_Layer_AGS93_getFullRequestString (t) {
        var params = {layers: "show:0,2"};
        t.plan( 1 );
        var map = new OpenLayers.Map('map');
        map.projection = "xx";
        tParams = { layers: 'show:0,2', 
                   format: 'png'};
        var tLayer = new OpenLayers.Layer.ArcGIS93Rest(name, url, tParams);
        map.addLayer(tLayer);
        str = tLayer.getFullRequestString();
        var tParams = {
            LAYERS: "show:0,2", FORMAT: "png"
        };
        t.eq(str,
             url + "?" + OpenLayers.Util.getParameterString(tParams),
             "getFullRequestString() adds SRS value");
        map.destroy();
 
    }

    function test_Layer_AGS93_noGutters (t) { 
        t.plan(2);
        var map = new OpenLayers.Map('map');
        var layer = new OpenLayers.Layer.ArcGIS93Rest("no gutter layer", url, params, {gutter: 0});
        map.addLayer(layer);
        map.setCenter(new OpenLayers.LonLat(0,0), 5);
        var tile = layer.grid[0][0];
        var request = layer.getURL(tile.bounds);
        var args = OpenLayers.Util.getParameters(request);
        t.eq(parseInt(args['SIZE'][0]),
             tile.size.w,
             "layer without gutter requests images that are as wide as the tile");
        t.eq(parseInt(args['SIZE'][1]),
             tile.size.h,
             "layer without gutter requests images that are as tall as the tile");
        
        layer.destroy();
        map.destroy();
    }

    function test_Layer_AGS93_gutters (t) { 
        var params = {layers: "show:0,2"};
        t.plan(2);
        var gutter = 15;
        var map = new OpenLayers.Map('map');
        var layer = new OpenLayers.Layer.ArcGIS93Rest("gutter layer", url, params, {gutter: gutter});
        map.addLayer(layer);
        map.setCenter(new OpenLayers.LonLat(0,0), 5);
        var tile = layer.grid[0][0];
        var request = layer.getURL(tile.bounds);
        var args = OpenLayers.Util.getParameters(request);
        t.eq(parseInt(args['SIZE'][0]),
             tile.size.w + (2 * gutter),
             "layer with gutter requests images that are wider by twice the gutter");
        t.eq(parseInt(args['SIZE'][1]),
             tile.size.h + (2 * gutter),
             "layer with gutter requests images that are taller by twice the gutter");

        layer.destroy();
        map.destroy();

    }

    function test_Layer_AGS93_destroy (t) {

        t.plan( 1 );

        var map = new OpenLayers.Map('map');
        layer = new OpenLayers.Layer.ArcGIS93Rest(name, url, params);
        map.addLayer(layer);

        map.setCenter(new OpenLayers.LonLat(0,0), 5);

        //grab a reference to one of the tiles
        var tile = layer.grid[0][0];        

        layer.destroy();
        
    // checks to make sure superclass (grid) destroy() was called    
        
        t.ok( layer.grid == null, "grid set to null");
    }
    
    function test_Layer_ADG93_Filter(t) {
        var params = {layers: "show:0,2"};
        t.plan( 9 );
    
        layer = new OpenLayers.Layer.ArcGIS93Rest(name, url, params);
        var map = new OpenLayers.Map('map', {tileManager: null});
        map.addLayer(layer);
        var pixel = new OpenLayers.Pixel(5,6);
        var tile = layer.addTile(new OpenLayers.Bounds(1,2,3,4), pixel);
        // Set up basic params.
        var tParams = OpenLayers.Util.extend({}, OpenLayers.Util.upperCaseObject(params));
        tParams = OpenLayers.Util.extend(tParams, {
            FORMAT: "png", BBOX: "1,2,3,4", SIZE: "256,256", F: "image", BBOXSR: "4326", IMAGESR: "4326"
        });
        
        // We need to actually set the "correct" url on a dom element, because doing so encodes things not encoded by getParameterString.
        var encodingHack = document.createElement("img");
        
        tile.draw();
        t.eq( tile.url, url + "?" + OpenLayers.Util.getParameterString(tParams), "image src no filter" );
        
        layer.setLayerFilter('1', "MR_TOAD = 'FLYING'");
        tParams["LAYERDEFS"] = "1:MR_TOAD = 'FLYING';";
        tile.draw();
        t.eq( tile.url, url + "?" + OpenLayers.Util.getParameterString(tParams), "image src one filter" );

        layer.setLayerFilter('1', "MR_TOAD = 'NOT FLYING'");
        tParams["LAYERDEFS"] = "1:MR_TOAD = 'NOT FLYING';";
        tile.draw();
        t.eq( tile.url, url + "?" + OpenLayers.Util.getParameterString(tParams), "image src change one filter" );

        layer.setLayerFilter('2', "true = false");
        tParams["LAYERDEFS"] = "1:MR_TOAD = 'NOT FLYING';2:true = false;";
        tile.draw();
        t.eq( tile.url, url + "?" + OpenLayers.Util.getParameterString(tParams), "image src two filters" );

        layer.setLayerFilter('99', "some_col > 5");
        tParams["LAYERDEFS"] = "1:MR_TOAD = 'NOT FLYING';2:true = false;99:some_col > 5;";
        tile.draw();
        t.eq( tile.url, url + "?" + OpenLayers.Util.getParameterString(tParams), "image src three filters" );
        
        layer.clearLayerFilter('2');
        tParams["LAYERDEFS"] = "1:MR_TOAD = 'NOT FLYING';99:some_col > 5;";
        tile.draw();
        t.eq( tile.url, url + "?" + OpenLayers.Util.getParameterString(tParams), "image src removed middle filter" );
        
        layer.clearLayerFilter('2');
        tParams["LAYERDEFS"] = "1:MR_TOAD = 'NOT FLYING';99:some_col > 5;";
        tile.draw();
        t.eq( tile.url, url + "?" + OpenLayers.Util.getParameterString(tParams), "image src removed missing filter (no change)" );
        
        layer.clearLayerFilter();
        delete tParams["LAYERDEFS"];
        tile.draw();
        t.eq( tile.url, url + "?" + OpenLayers.Util.getParameterString(tParams), "image src removed all filters" );
        
        layer.clearLayerFilter();
        tile.draw();
        t.eq( tile.url, url + "?" + OpenLayers.Util.getParameterString(tParams), "image src removed all (no) filters" );
    }
    
    

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