Project

General

Profile

Download (7.65 KB) Statistics
| Branch: | Tag: | Revision:
1
/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
2
 * full list of contributors). Published under the 2-clause BSD license.
3
 * See license.txt in the OpenLayers distribution or repository for the
4
 * full text of the license. */
5
 
6
/**
7
 * @requires OpenLayers/Layer.js
8
 * @requires OpenLayers/Tile/Image.js
9
 */
10

    
11
/**
12
 * Class: OpenLayers.Layer.Image
13
 * Instances of OpenLayers.Layer.Image are used to display data from a web
14
 * accessible image as a map layer.  Create a new image layer with the
15
 * <OpenLayers.Layer.Image> constructor.
16
 *
17
 * Inherits from:
18
 *  - <OpenLayers.Layer>
19
 */
20
OpenLayers.Layer.Image = OpenLayers.Class(OpenLayers.Layer, {
21

    
22
    /**
23
     * Property: isBaseLayer
24
     * {Boolean} The layer is a base layer.  Default is true.  Set this property
25
     * in the layer options
26
     */
27
    isBaseLayer: true,
28
    
29
    /**
30
     * Property: url
31
     * {String} URL of the image to use
32
     */
33
    url: null,
34

    
35
    /**
36
     * Property: extent
37
     * {<OpenLayers.Bounds>} The image bounds in map units.  This extent will
38
     *     also be used as the default maxExtent for the layer.  If you wish
39
     *     to have a maxExtent that is different than the image extent, set the
40
     *     maxExtent property of the options argument (as with any other layer).
41
     */
42
    extent: null,
43
    
44
    /**
45
     * Property: size
46
     * {<OpenLayers.Size>} The image size in pixels
47
     */
48
    size: null,
49

    
50
    /**
51
     * Property: tile
52
     * {<OpenLayers.Tile.Image>}
53
     */
54
    tile: null,
55

    
56
    /**
57
     * Property: aspectRatio
58
     * {Float} The ratio of height/width represented by a single pixel in the
59
     * graphic
60
     */
61
    aspectRatio: null,
62

    
63
    /**
64
     * Constructor: OpenLayers.Layer.Image
65
     * Create a new image layer
66
     *
67
     * Parameters:
68
     * name - {String} A name for the layer.
69
     * url - {String} Relative or absolute path to the image
70
     * extent - {<OpenLayers.Bounds>} The extent represented by the image
71
     * size - {<OpenLayers.Size>} The size (in pixels) of the image
72
     * options - {Object} Hashtable of extra options to tag onto the layer
73
     */
74
    initialize: function(name, url, extent, size, options) {
75
        this.url = url;
76
        this.extent = extent;
77
        this.maxExtent = extent;
78
        this.size = size;
79
        OpenLayers.Layer.prototype.initialize.apply(this, [name, options]);
80

    
81
        this.aspectRatio = (this.extent.getHeight() / this.size.h) /
82
                           (this.extent.getWidth() / this.size.w);
83
    },    
84

    
85
    /**
86
     * Method: destroy
87
     * Destroy this layer
88
     */
89
    destroy: function() {
90
        if (this.tile) {
91
            this.removeTileMonitoringHooks(this.tile);
92
            this.tile.destroy();
93
            this.tile = null;
94
        }
95
        OpenLayers.Layer.prototype.destroy.apply(this, arguments);
96
    },
97
    
98
    /**
99
     * Method: clone
100
     * Create a clone of this layer
101
     *
102
     * Paramters:
103
     * obj - {Object} An optional layer (is this ever used?)
104
     *
105
     * Returns:
106
     * {<OpenLayers.Layer.Image>} An exact copy of this layer
107
     */
108
    clone: function(obj) {
109
        
110
        if(obj == null) {
111
            obj = new OpenLayers.Layer.Image(this.name,
112
                                               this.url,
113
                                               this.extent,
114
                                               this.size,
115
                                               this.getOptions());
116
        }
117

    
118
        //get all additions from superclasses
119
        obj = OpenLayers.Layer.prototype.clone.apply(this, [obj]);
120

    
121
        // copy/set any non-init, non-simple values here
122

    
123
        return obj;
124
    },    
125
    
126
    /**
127
     * APIMethod: setMap
128
     * 
129
     * Parameters:
130
     * map - {<OpenLayers.Map>}
131
     */
132
    setMap: function(map) {
133
        /**
134
         * If nothing to do with resolutions has been set, assume a single
135
         * resolution determined by ratio*extent/size - if an image has a
136
         * pixel aspect ratio different than one (as calculated above), the
137
         * image will be stretched in one dimension only.
138
         */
139
        if( this.options.maxResolution == null ) {
140
            this.options.maxResolution = this.aspectRatio *
141
                                         this.extent.getWidth() /
142
                                         this.size.w;
143
        }
144
        OpenLayers.Layer.prototype.setMap.apply(this, arguments);
145
    },
146

    
147
    /** 
148
     * Method: moveTo
149
     * Create the tile for the image or resize it for the new resolution
150
     * 
151
     * Parameters:
152
     * bounds - {<OpenLayers.Bounds>}
153
     * zoomChanged - {Boolean}
154
     * dragging - {Boolean}
155
     */
156
    moveTo:function(bounds, zoomChanged, dragging) {
157
        OpenLayers.Layer.prototype.moveTo.apply(this, arguments);
158

    
159
        var firstRendering = (this.tile == null);
160

    
161
        if(zoomChanged || firstRendering) {
162

    
163
            //determine new tile size
164
            this.setTileSize();
165

    
166
            //determine new position (upper left corner of new bounds)
167
            var ulPx = this.map.getLayerPxFromLonLat({
168
                lon: this.extent.left,
169
                lat: this.extent.top
170
            });
171

    
172
            if(firstRendering) {
173
                //create the new tile
174
                this.tile = new OpenLayers.Tile.Image(this, ulPx, this.extent, 
175
                                                      null, this.tileSize);
176
                this.addTileMonitoringHooks(this.tile);
177
            } else {
178
                //just resize the tile and set it's new position
179
                this.tile.size = this.tileSize.clone();
180
                this.tile.position = ulPx.clone();
181
            }
182
            this.tile.draw();
183
        }
184
    }, 
185

    
186
    /**
187
     * Set the tile size based on the map size.
188
     */
189
    setTileSize: function() {
190
        var tileWidth = this.extent.getWidth() / this.map.getResolution();
191
        var tileHeight = this.extent.getHeight() / this.map.getResolution();
192
        this.tileSize = new OpenLayers.Size(tileWidth, tileHeight);
193
    },
194

    
195
    /** 
196
     * Method: addTileMonitoringHooks
197
     * This function takes a tile as input and adds the appropriate hooks to 
198
     *     the tile so that the layer can keep track of the loading tiles.
199
     * 
200
     * Parameters: 
201
     * tile - {<OpenLayers.Tile>}
202
     */
203
    addTileMonitoringHooks: function(tile) {
204
        tile.onLoadStart = function() {
205
            this.events.triggerEvent("loadstart");
206
        };
207
        tile.events.register("loadstart", this, tile.onLoadStart);
208
      
209
        tile.onLoadEnd = function() {
210
            this.events.triggerEvent("loadend");
211
        };
212
        tile.events.register("loadend", this, tile.onLoadEnd);
213
        tile.events.register("unload", this, tile.onLoadEnd);
214
    },
215

    
216
    /** 
217
     * Method: removeTileMonitoringHooks
218
     * This function takes a tile as input and removes the tile hooks 
219
     *     that were added in <addTileMonitoringHooks>.
220
     * 
221
     * Parameters: 
222
     * tile - {<OpenLayers.Tile>}
223
     */
224
    removeTileMonitoringHooks: function(tile) {
225
        tile.unload();
226
        tile.events.un({
227
            "loadstart": tile.onLoadStart,
228
            "loadend": tile.onLoadEnd,
229
            "unload": tile.onLoadEnd,
230
            scope: this
231
        });
232
    },
233
    
234
    /**
235
     * APIMethod: setUrl
236
     * 
237
     * Parameters:
238
     * newUrl - {String}
239
     */
240
    setUrl: function(newUrl) {
241
        this.url = newUrl;
242
        this.tile.draw();
243
    },
244

    
245
    /** 
246
     * APIMethod: getURL
247
     * The url we return is always the same (the image itself never changes)
248
     *     so we can ignore the bounds parameter (it will always be the same, 
249
     *     anyways) 
250
     * 
251
     * Parameters:
252
     * bounds - {<OpenLayers.Bounds>}
253
     */
254
    getURL: function(bounds) {
255
        return this.url;
256
    },
257

    
258
    CLASS_NAME: "OpenLayers.Layer.Image"
259
});
(12-12/31)