1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
2 | * full list of contributors). Published under the Clear BSD license. |
---|
3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt 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. Inherits from <OpenLayers.Layer>. |
---|
16 | */ |
---|
17 | OpenLayers.Layer.Image = OpenLayers.Class(OpenLayers.Layer, { |
---|
18 | |
---|
19 | /** |
---|
20 | * Property: isBaseLayer |
---|
21 | * {Boolean} The layer is a base layer. Default is true. Set this property |
---|
22 | * in the layer options |
---|
23 | */ |
---|
24 | isBaseLayer: true, |
---|
25 | |
---|
26 | /** |
---|
27 | * Property: url |
---|
28 | * {String} URL of the image to use |
---|
29 | */ |
---|
30 | url: null, |
---|
31 | |
---|
32 | /** |
---|
33 | * Property: extent |
---|
34 | * {<OpenLayers.Bounds>} The image bounds in map units. This extent will |
---|
35 | * also be used as the default maxExtent for the layer. If you wish |
---|
36 | * to have a maxExtent that is different than the image extent, set the |
---|
37 | * maxExtent property of the options argument (as with any other layer). |
---|
38 | */ |
---|
39 | extent: null, |
---|
40 | |
---|
41 | /** |
---|
42 | * Property: size |
---|
43 | * {<OpenLayers.Size>} The image size in pixels |
---|
44 | */ |
---|
45 | size: null, |
---|
46 | |
---|
47 | /** |
---|
48 | * Property: tile |
---|
49 | * {<OpenLayers.Tile.Image>} |
---|
50 | */ |
---|
51 | tile: null, |
---|
52 | |
---|
53 | /** |
---|
54 | * Property: aspectRatio |
---|
55 | * {Float} The ratio of height/width represented by a single pixel in the |
---|
56 | * graphic |
---|
57 | */ |
---|
58 | aspectRatio: null, |
---|
59 | |
---|
60 | /** |
---|
61 | * Constructor: OpenLayers.Layer.Image |
---|
62 | * Create a new image layer |
---|
63 | * |
---|
64 | * Parameters: |
---|
65 | * name - {String} A name for the layer. |
---|
66 | * url - {String} Relative or absolute path to the image |
---|
67 | * extent - {<OpenLayers.Bounds>} The extent represented by the image |
---|
68 | * size - {<OpenLayers.Size>} The size (in pixels) of the image |
---|
69 | * options - {Object} Hashtable of extra options to tag onto the layer |
---|
70 | */ |
---|
71 | initialize: function(name, url, extent, size, options) { |
---|
72 | this.url = url; |
---|
73 | this.extent = extent; |
---|
74 | this.maxExtent = extent; |
---|
75 | this.size = size; |
---|
76 | OpenLayers.Layer.prototype.initialize.apply(this, [name, options]); |
---|
77 | |
---|
78 | this.aspectRatio = (this.extent.getHeight() / this.size.h) / |
---|
79 | (this.extent.getWidth() / this.size.w); |
---|
80 | }, |
---|
81 | |
---|
82 | /** |
---|
83 | * Method: destroy |
---|
84 | * Destroy this layer |
---|
85 | */ |
---|
86 | destroy: function() { |
---|
87 | if (this.tile) { |
---|
88 | this.removeTileMonitoringHooks(this.tile); |
---|
89 | this.tile.destroy(); |
---|
90 | this.tile = null; |
---|
91 | } |
---|
92 | OpenLayers.Layer.prototype.destroy.apply(this, arguments); |
---|
93 | }, |
---|
94 | |
---|
95 | /** |
---|
96 | * Method: clone |
---|
97 | * Create a clone of this layer |
---|
98 | * |
---|
99 | * Paramters: |
---|
100 | * obj - {Object} An optional layer (is this ever used?) |
---|
101 | * |
---|
102 | * Returns: |
---|
103 | * {<OpenLayers.Layer.Image>} An exact copy of this layer |
---|
104 | */ |
---|
105 | clone: function(obj) { |
---|
106 | |
---|
107 | if(obj == null) { |
---|
108 | obj = new OpenLayers.Layer.Image(this.name, |
---|
109 | this.url, |
---|
110 | this.extent, |
---|
111 | this.size, |
---|
112 | this.getOptions()); |
---|
113 | } |
---|
114 | |
---|
115 | //get all additions from superclasses |
---|
116 | obj = OpenLayers.Layer.prototype.clone.apply(this, [obj]); |
---|
117 | |
---|
118 | // copy/set any non-init, non-simple values here |
---|
119 | |
---|
120 | return obj; |
---|
121 | }, |
---|
122 | |
---|
123 | /** |
---|
124 | * APIMethod: setMap |
---|
125 | * |
---|
126 | * Parameters: |
---|
127 | * map - {<OpenLayers.Map>} |
---|
128 | */ |
---|
129 | setMap: function(map) { |
---|
130 | /** |
---|
131 | * If nothing to do with resolutions has been set, assume a single |
---|
132 | * resolution determined by ratio*extent/size - if an image has a |
---|
133 | * pixel aspect ratio different than one (as calculated above), the |
---|
134 | * image will be stretched in one dimension only. |
---|
135 | */ |
---|
136 | if( this.options.maxResolution == null ) { |
---|
137 | this.options.maxResolution = this.aspectRatio * |
---|
138 | this.extent.getWidth() / |
---|
139 | this.size.w; |
---|
140 | } |
---|
141 | OpenLayers.Layer.prototype.setMap.apply(this, arguments); |
---|
142 | }, |
---|
143 | |
---|
144 | /** |
---|
145 | * Method: moveTo |
---|
146 | * Create the tile for the image or resize it for the new resolution |
---|
147 | * |
---|
148 | * Parameters: |
---|
149 | * bounds - {<OpenLayers.Bounds>} |
---|
150 | * zoomChanged - {Boolean} |
---|
151 | * dragging - {Boolean} |
---|
152 | */ |
---|
153 | moveTo:function(bounds, zoomChanged, dragging) { |
---|
154 | OpenLayers.Layer.prototype.moveTo.apply(this, arguments); |
---|
155 | |
---|
156 | var firstRendering = (this.tile == null); |
---|
157 | |
---|
158 | if(zoomChanged || firstRendering) { |
---|
159 | |
---|
160 | //determine new tile size |
---|
161 | this.setTileSize(); |
---|
162 | |
---|
163 | //determine new position (upper left corner of new bounds) |
---|
164 | var ul = new OpenLayers.LonLat(this.extent.left, this.extent.top); |
---|
165 | var ulPx = this.map.getLayerPxFromLonLat(ul); |
---|
166 | |
---|
167 | if(firstRendering) { |
---|
168 | //create the new tile |
---|
169 | this.tile = new OpenLayers.Tile.Image(this, ulPx, this.extent, |
---|
170 | null, this.tileSize); |
---|
171 | this.addTileMonitoringHooks(this.tile); |
---|
172 | } else { |
---|
173 | //just resize the tile and set it's new position |
---|
174 | this.tile.size = this.tileSize.clone(); |
---|
175 | this.tile.position = ulPx.clone(); |
---|
176 | } |
---|
177 | this.tile.draw(); |
---|
178 | } |
---|
179 | }, |
---|
180 | |
---|
181 | /** |
---|
182 | * Set the tile size based on the map size. |
---|
183 | */ |
---|
184 | setTileSize: function() { |
---|
185 | var tileWidth = this.extent.getWidth() / this.map.getResolution(); |
---|
186 | var tileHeight = this.extent.getHeight() / this.map.getResolution(); |
---|
187 | this.tileSize = new OpenLayers.Size(tileWidth, tileHeight); |
---|
188 | }, |
---|
189 | |
---|
190 | /** |
---|
191 | * Method: addTileMonitoringHooks |
---|
192 | * This function takes a tile as input and adds the appropriate hooks to |
---|
193 | * the tile so that the layer can keep track of the loading tiles. |
---|
194 | * |
---|
195 | * Parameters: |
---|
196 | * tile - {<OpenLayers.Tile>} |
---|
197 | */ |
---|
198 | addTileMonitoringHooks: function(tile) { |
---|
199 | tile.onLoadStart = function() { |
---|
200 | this.events.triggerEvent("loadstart"); |
---|
201 | }; |
---|
202 | tile.events.register("loadstart", this, tile.onLoadStart); |
---|
203 | |
---|
204 | tile.onLoadEnd = function() { |
---|
205 | this.events.triggerEvent("loadend"); |
---|
206 | }; |
---|
207 | tile.events.register("loadend", this, tile.onLoadEnd); |
---|
208 | tile.events.register("unload", this, tile.onLoadEnd); |
---|
209 | }, |
---|
210 | |
---|
211 | /** |
---|
212 | * Method: removeTileMonitoringHooks |
---|
213 | * This function takes a tile as input and removes the tile hooks |
---|
214 | * that were added in <addTileMonitoringHooks>. |
---|
215 | * |
---|
216 | * Parameters: |
---|
217 | * tile - {<OpenLayers.Tile>} |
---|
218 | */ |
---|
219 | removeTileMonitoringHooks: function(tile) { |
---|
220 | tile.unload(); |
---|
221 | tile.events.un({ |
---|
222 | "loadstart": tile.onLoadStart, |
---|
223 | "loadend": tile.onLoadEnd, |
---|
224 | "unload": tile.onLoadEnd, |
---|
225 | scope: this |
---|
226 | }); |
---|
227 | }, |
---|
228 | |
---|
229 | /** |
---|
230 | * APIMethod: setUrl |
---|
231 | * |
---|
232 | * Parameters: |
---|
233 | * newUrl - {String} |
---|
234 | */ |
---|
235 | setUrl: function(newUrl) { |
---|
236 | this.url = newUrl; |
---|
237 | this.tile.draw(); |
---|
238 | }, |
---|
239 | |
---|
240 | /** |
---|
241 | * APIMethod: getURL |
---|
242 | * The url we return is always the same (the image itself never changes) |
---|
243 | * so we can ignore the bounds parameter (it will always be the same, |
---|
244 | * anyways) |
---|
245 | * |
---|
246 | * Parameters: |
---|
247 | * bounds - {<OpenLayers.Bounds>} |
---|
248 | */ |
---|
249 | getURL: function(bounds) { |
---|
250 | return this.url; |
---|
251 | }, |
---|
252 | |
---|
253 | CLASS_NAME: "OpenLayers.Layer.Image" |
---|
254 | }); |
---|