summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Layer/FixedZoomLevels.html
blob: 133571bcb899c822bf1d6c7671235de693a8df32 (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
<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">
    var layer; 

    function test_Layer_FixedZoomLevels (t) {
        t.plan( 39 );
        
        var layer = { 'MIN_ZOOM_LEVEL': 5,
                      'MAX_ZOOM_LEVEL': 10 };        


    //defaults 

        layer = p_createLayer(layer);
        p_minMaxNum(t, layer, layer.MIN_ZOOM_LEVEL, layer.MAX_ZOOM_LEVEL, "nothing specified");

    
    //layer.options

        // min,num
        layer = p_createLayer(layer, {}, { minZoomLevel: 3, numZoomLevels: 12});
        p_minMaxNum(t, layer, layer.MIN_ZOOM_LEVEL, layer.MAX_ZOOM_LEVEL, "min too low num too high(layer.options)");

        layer = p_createLayer(layer, {}, { minZoomLevel: 6, numZoomLevels: 3 });
        p_minMaxNum(t, layer, 6, 8, "valid min,num(layer.options)");


        // max
        layer = p_createLayer(layer, {}, { maxZoomLevel: 9 });
        p_minMaxNum(t, layer, layer.MIN_ZOOM_LEVEL, 9, "valid max(layer.options)");

        layer = p_createLayer(layer, {}, { maxZoomLevel: 12 });
        p_minMaxNum(t, layer, layer.MIN_ZOOM_LEVEL, layer.MAX_ZOOM_LEVEL, "invalid max(layer.options)");



    //map

        // min,num
        layer = p_createLayer(layer, { minZoomLevel: 3, numZoomLevels: 12});
        p_minMaxNum(t, layer, layer.MIN_ZOOM_LEVEL, layer.MAX_ZOOM_LEVEL, "min too low num too high(map)");

        layer = p_createLayer(layer, { minZoomLevel: 6, numZoomLevels: 3 });
        p_minMaxNum(t, layer, 6, 8, "valid min,num(map)");


        // max
        layer = p_createLayer(layer, { maxZoomLevel: 9 });
        p_minMaxNum(t, layer, layer.MIN_ZOOM_LEVEL, 9, "valid max(map)");

        layer = p_createLayer(layer, { maxZoomLevel: 12 });
        p_minMaxNum(t, layer, layer.MIN_ZOOM_LEVEL, layer.MAX_ZOOM_LEVEL, "invalid max(map)");

    //map vs. options

        layer = p_createLayer(layer, {minZoomLevel: 6, numZoomLevels: 2}, { minZoomLevel: 7, numZoomLevels: 3});
        p_minMaxNum(t, layer, 7, 9, "min,num(layer.options) wins over (map)");

        layer = p_createLayer(layer, {minZoomLevel: 6, maxZoomLevel: 8}, { minZoomLevel: 7, maxZoomLevel: 9});
        p_minMaxNum(t, layer, 7, 9, "min,max(layer.options) wins over (map)");


    // numZoomLevels vs. maxZoomLevel
    
        layer = p_createLayer(layer, {maxZoomLevel: 8, numZoomLevels: 6});
        p_minMaxNum(t, layer, layer.MIN_ZOOM_LEVEL, 10, "min,max(layer.options) wins over (map)");

    // resolutions array

        var resolutions = Array(20);
        for (var i = 0; i < 20; i++) {
            resolutions[i] = Math.random();
        }
        OpenLayers.Util.extend(layer, {RESOLUTIONS:resolutions});
        var minZoomLevel = 6;
        var numZoomLevels = 2;
        layer = p_createLayer(layer, {}, {minZoomLevel: minZoomLevel, numZoomLevels: numZoomLevels});
        t.eq( layer.resolutions.length, numZoomLevels, "length of resolutions array ok");
        for (var i = 0; i < numZoomLevels; i++) {
            t.eq( layer.resolutions[i], resolutions[i + minZoomLevel], "resolutions array at index " + i + " ok");
        }
    }
    
    function test_getMapObjectZoomFromOLZoom(t) {
        t.plan(4);
        
        var map = new OpenLayers.Map("map", {allOverlays: true});
        var xyz = new OpenLayers.Layer.XYZ("xyz", "${x}${y}${z}", {
            sphericalMercator: true,
            resolutions: [39135.7584765625, 19567.87923828125, 9783.939619140625]
        });
        var fixed = new (OpenLayers.Class(OpenLayers.Layer, OpenLayers.Layer.FixedZoomLevels, {
             initialize: function() {
                OpenLayers.Layer.prototype.initialize.apply(this, arguments);
            }
        }))("fixed", {
            resolutions: [156543.03390625, 78271.516953125, 39135.7584765625, 19567.87923828125, 9783.939619140625],
            minZoomLevel: 1
        });
        map.addLayers([xyz, fixed]);
        map.setCenter(new OpenLayers.LonLat(0, 0), 2);
        // map.getZoom() returns 2
        t.eq(fixed.getMapObjectZoomFromOLZoom(map.getZoom()), 4, "correct return value from getMapObjectZoomFromOLZoom");
        t.eq(fixed.getOLZoomFromMapObjectZoom(4), map.getZoom() - fixed.minZoomLevel, "correct return value from getOLZoomFromMapObjectZoom");

        map.setBaseLayer(fixed);
        // map.getZoom() returns 4 now
        t.eq(fixed.getMapObjectZoomFromOLZoom(map.getZoom()), 5, "correct return value from getMapObjectZoomFromOLZoom");
        t.eq(fixed.getOLZoomFromMapObjectZoom(5), map.getZoom(), "correct return value from getOLZoomFromMapObjectZoom");
    }

    function p_createLayer(layer, mapOptions, layerOptions) {

        layer.map = mapOptions || {};
        layer.options = layerOptions || {};
        OpenLayers.Layer.FixedZoomLevels.prototype.initResolutions.apply(layer);

        return layer;
    }

    function p_minMaxNum(t, layer, min, max, msg) {

        t.eq( layer.minZoomLevel, min, "min zoom level inherited from layer constant: " + msg);
        t.eq( layer.maxZoomLevel, max, "max zoom level inherited from layer constant: " + msg);
        t.eq( layer.numZoomLevels, max - min + 1, "num zoom levels correctly calcuated: " + msg);
        
    }


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