~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to gis/dhis-gis-geostat/mfbase/openlayers/lib/OpenLayers/Tile.js

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
 
2
 * license.  See http://svn.openlayers.org/trunk/openlayers/license.txt for the
 
3
 * full text of the license. */
 
4
 
 
5
 
 
6
/*
 
7
 * @requires OpenLayers/Util.js
 
8
 */
 
9
 
 
10
/*
 
11
 * Class: OpenLayers.Tile 
 
12
 * This is a class designed to designate a single tile, however
 
13
 *     it is explicitly designed to do relatively little. Tiles store 
 
14
 *     information about themselves -- such as the URL that they are related
 
15
 *     to, and their size - but do not add themselves to the layer div 
 
16
 *     automatically, for example. Create a new tile with the 
 
17
 *     <OpenLayers.Tile> constructor, or a subclass. 
 
18
 * 
 
19
 * TBD 3.0 - remove reference to url in above paragraph
 
20
 * 
 
21
 */
 
22
OpenLayers.Tile = OpenLayers.Class({
 
23
    
 
24
    /** 
 
25
     * Constant: EVENT_TYPES
 
26
     * {Array(String)} Supported application event types
 
27
     */
 
28
    EVENT_TYPES: [ "loadstart", "loadend", "reload", "unload"],
 
29
    
 
30
    /**
 
31
     * APIProperty: events
 
32
     * {<OpenLayers.Events>} An events object that handles all 
 
33
     *                       events on the tile.
 
34
     */
 
35
    events: null,
 
36
 
 
37
    /**
 
38
     * Property: id 
 
39
     * {String} null
 
40
     */
 
41
    id: null,
 
42
    
 
43
    /** 
 
44
     * Property: layer 
 
45
     * {<OpenLayers.Layer>} layer the tile is attached to 
 
46
     */
 
47
    layer: null,
 
48
    
 
49
    /**
 
50
     * Property: url
 
51
     * {String} url of the request.
 
52
     *
 
53
     * TBD 3.0 
 
54
     * Deprecated. The base tile class does not need an url. This should be 
 
55
     * handled in subclasses. Does not belong here.
 
56
     */
 
57
    url: null,
 
58
 
 
59
    /** 
 
60
     * APIProperty: bounds 
 
61
     * {<OpenLayers.Bounds>} null
 
62
     */
 
63
    bounds: null,
 
64
    
 
65
    /** 
 
66
     * Property: size 
 
67
     * {<OpenLayers.Size>} null
 
68
     */
 
69
    size: null,
 
70
    
 
71
    /** 
 
72
     * Property: position 
 
73
     * {<OpenLayers.Pixel>} Top Left pixel of the tile
 
74
     */    
 
75
    position: null,
 
76
 
 
77
    /**
 
78
     * Property: isLoading
 
79
     * {Boolean} Is the tile loading?
 
80
     */
 
81
    isLoading: false,
 
82
        
 
83
    /** TBD 3.0 -- remove 'url' from the list of parameters to the constructor.
 
84
     *             there is no need for the base tile class to have a url.
 
85
     * 
 
86
     * Constructor: OpenLayers.Tile
 
87
     * Constructor for a new <OpenLayers.Tile> instance.
 
88
     * 
 
89
     * Parameters:
 
90
     * layer - {<OpenLayers.Layer>} layer that the tile will go in.
 
91
     * position - {<OpenLayers.Pixel>}
 
92
     * bounds - {<OpenLayers.Bounds>}
 
93
     * url - {<String>}
 
94
     * size - {<OpenLayers.Size>}
 
95
     */   
 
96
    initialize: function(layer, position, bounds, url, size) {
 
97
        this.layer = layer;
 
98
        this.position = position.clone();
 
99
        this.bounds = bounds.clone();
 
100
        this.url = url;
 
101
        this.size = size.clone();
 
102
 
 
103
        //give the tile a unique id based on its BBOX.
 
104
        this.id = OpenLayers.Util.createUniqueID("Tile_");
 
105
        
 
106
        this.events = new OpenLayers.Events(this, null, this.EVENT_TYPES);
 
107
    },
 
108
 
 
109
    /**
 
110
     * Method: unload
 
111
     * Call immediately before destroying if you are listening to tile
 
112
     * events, so that counters are properly handled if tile is still
 
113
     * loading at destroy-time. Will only fire an event if the tile is
 
114
     * still loading.
 
115
     */
 
116
    unload: function() {
 
117
       if (this.isLoading) { 
 
118
           this.isLoading = false; 
 
119
           this.events.triggerEvent("unload"); 
 
120
       }
 
121
    },
 
122
    
 
123
    /** 
 
124
     * APIMethod: destroy
 
125
     * Nullify references to prevent circular references and memory leaks.
 
126
     */
 
127
    destroy:function() {
 
128
        this.layer  = null;
 
129
        this.bounds = null;
 
130
        this.size = null;
 
131
        this.position = null;
 
132
        
 
133
        this.events.destroy();
 
134
        this.events = null;
 
135
    },
 
136
    
 
137
    /**
 
138
     * Method: clone
 
139
     *
 
140
     * Parameters:
 
141
     * obj - {<OpenLayers.Tile>} The tile to be cloned
 
142
     *
 
143
     * Returns:
 
144
     * {<OpenLayers.Tile>} An exact clone of this <OpenLayers.Tile>
 
145
     */
 
146
    clone: function (obj) {
 
147
        if (obj == null) {
 
148
            obj = new OpenLayers.Tile(this.layer, 
 
149
                                      this.position, 
 
150
                                      this.bounds, 
 
151
                                      this.url, 
 
152
                                      this.size);
 
153
        } 
 
154
        
 
155
        // catch any randomly tagged-on properties
 
156
        OpenLayers.Util.applyDefaults(obj, this);
 
157
        
 
158
        return obj;
 
159
    },
 
160
 
 
161
    /**
 
162
     * Method: draw
 
163
     * Clear whatever is currently in the tile, then return whether or not 
 
164
     *     it should actually be re-drawn.
 
165
     * 
 
166
     * Returns:
 
167
     * {Boolean} Whether or not the tile should actually be drawn. Note that 
 
168
     *     this is not really the best way of doing things, but such is 
 
169
     *     the way the code has been developed. Subclasses call this and
 
170
     *     depend on the return to know if they should draw or not.
 
171
     */
 
172
    draw: function() {
 
173
        var maxExtent = this.layer.maxExtent;
 
174
        var withinMaxExtent = (maxExtent &&
 
175
                               this.bounds.intersectsBounds(maxExtent, false));
 
176
 
 
177
        // The only case where we *wouldn't* want to draw the tile is if the 
 
178
        // tile is outside its layer's maxExtent.
 
179
        this.shouldDraw = (withinMaxExtent || this.layer.displayOutsideMaxExtent);
 
180
                
 
181
        //clear tile's contents and mark as not drawn
 
182
        this.clear();
 
183
        
 
184
        return this.shouldDraw;
 
185
    },
 
186
    
 
187
    /** 
 
188
     * Method: moveTo
 
189
     * Reposition the tile.
 
190
     *
 
191
     * Parameters:
 
192
     * bounds - {<OpenLayers.Bounds>}
 
193
     * position - {<OpenLayers.Pixel>}
 
194
     * redraw - {Boolean} Call draw method on tile after moving.
 
195
     *     Default is true
 
196
     */
 
197
    moveTo: function (bounds, position, redraw) {
 
198
        if (redraw == null) {
 
199
            redraw = true;
 
200
        }
 
201
 
 
202
        this.bounds = bounds.clone();
 
203
        this.position = position.clone();
 
204
        if (redraw) {
 
205
            this.draw();
 
206
        }
 
207
    },
 
208
 
 
209
    /** 
 
210
     * Method: clear
 
211
     * Clear the tile of any bounds/position-related data so that it can 
 
212
     *     be reused in a new location. To be implemented by subclasses.
 
213
     */
 
214
    clear: function() {
 
215
        // to be implemented by subclasses
 
216
    },
 
217
    
 
218
    /**   
 
219
     * Method: getBoundsFromBaseLayer
 
220
     * Take the pixel locations of the corner of the tile, and pass them to 
 
221
     *     the base layer and ask for the location of those pixels, so that 
 
222
     *     displaying tiles over Google works fine.
 
223
     *
 
224
     * Parameters:
 
225
     * position - {<OpenLayers.Pixel>}
 
226
     *
 
227
     * Returns:
 
228
     * bounds - {<OpenLayers.Bounds>} 
 
229
     */
 
230
    getBoundsFromBaseLayer: function(position) {
 
231
        var msg = OpenLayers.i18n('reprojectDeprecated',
 
232
                                              {'layerName':this.layer.name});
 
233
        OpenLayers.Console.warn(msg);
 
234
        var topLeft = this.layer.map.getLonLatFromLayerPx(position); 
 
235
        var bottomRightPx = position.clone();
 
236
        bottomRightPx.x += this.size.w;
 
237
        bottomRightPx.y += this.size.h;
 
238
        var bottomRight = this.layer.map.getLonLatFromLayerPx(bottomRightPx); 
 
239
        // Handle the case where the base layer wraps around the date line.
 
240
        // Google does this, and it breaks WMS servers to request bounds in 
 
241
        // that fashion.  
 
242
        if (topLeft.lon > bottomRight.lon) {
 
243
            if (topLeft.lon < 0) {
 
244
                topLeft.lon = -180 - (topLeft.lon+180);
 
245
            } else {
 
246
                bottomRight.lon = 180+bottomRight.lon+180;
 
247
            }        
 
248
        }
 
249
        var bounds = new OpenLayers.Bounds(topLeft.lon, 
 
250
                                       bottomRight.lat, 
 
251
                                       bottomRight.lon, 
 
252
                                       topLeft.lat);  
 
253
        return bounds;
 
254
    },        
 
255
        
 
256
    /** 
 
257
     * Method: showTile
 
258
     * Show the tile only if it should be drawn.
 
259
     */
 
260
    showTile: function() { 
 
261
        if (this.shouldDraw) {
 
262
            this.show();
 
263
        }
 
264
    },
 
265
    
 
266
    /** 
 
267
     * Method: show
 
268
     * Show the tile.  To be implemented by subclasses.
 
269
     */
 
270
    show: function() { },
 
271
    
 
272
    /** 
 
273
     * Method: hide
 
274
     * Hide the tile.  To be implemented by subclasses.
 
275
     */
 
276
    hide: function() { },
 
277
    
 
278
    CLASS_NAME: "OpenLayers.Tile"
 
279
});