forked from openlayers/ol2
/
Image.js
206 lines (180 loc) · 5.96 KB
/
Image.js
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
/* Copyright (c) 2006-2007 MetaCarta, Inc., published under the BSD license.
* See http://svn.openlayers.org/trunk/openlayers/release-license.txt
* for the full text of the license. */
/**
* @requires OpenLayers/Layer.js
* @requires OpenLayers/Tile/Image.js
*
* Class: OpenLayers.Layer.Image
* Instances of OpenLayers.Layer.Image are used to display data from a web
* accessible image as a map layer. Create a new image layer with the
* <OpenLayers.Layer.Image> constructor. Inherits from <OpenLayers.Layer>.
*/
OpenLayers.Layer.Image = OpenLayers.Class(OpenLayers.Layer, {
/**
* Property: isBaseLayer
* {Boolean} The layer is a base layer. Default is true. Set this property
* in the layer options
*/
isBaseLayer: true,
/**
* Property: url
* {String} URL of the image to use
*/
url: null,
/**
* Property: extent
* {<OpenLayers.Bounds>} The image bounds in map units
*/
extent: null,
/**
* Property: size
* {<OpenLayers.Size>} The image size in pixels
*/
size: null,
/**
* Property: tile
* {<OpenLayers.Tile.Image>}
*/
tile: null,
/**
* Property: aspectRatio
* {Float} The ratio of height/width represented by a single pixel in the
* graphic
*/
aspectRatio: null,
/**
* Constructor: OpenLayers.Layer.Image
* Create a new image layer
*
* Parameters:
* name - {String} A name for the layer.
* url - {String} Relative or absolute path to the image
* extent - {<OpenLayers.Bounds>} The extent represented by the image
* size - {<OpenLayers.Size>} The size (in pixels) of the image
* options - {Object} Hashtable of extra options to tag onto the layer
*/
initialize: function(name, url, extent, size, options) {
this.url = url;
this.extent = extent;
this.size = size;
OpenLayers.Layer.prototype.initialize.apply(this, [name, options]);
this.aspectRatio = (this.extent.getHeight() / this.size.h) /
(this.extent.getWidth() / this.size.w);
},
/**
* Method: destroy
* Destroy this layer
*/
destroy: function() {
if (this.tile) {
this.tile.destroy();
this.tile = null;
}
OpenLayers.Layer.prototype.destroy.apply(this, arguments);
},
/**
* Method: clone
* Create a clone of this layer
*
* Paramters:
* obj - {Object} An optional layer (is this ever used?)
*
* Returns:
* {<OpenLayers.Layer.Image>} An exact copy of this layer
*/
clone: function(obj) {
if(obj == null) {
obj = new OpenLayers.Layer.Image(this.name,
this.url,
this.extent,
this.size,
this.options);
}
//get all additions from superclasses
obj = OpenLayers.Layer.prototype.clone.apply(this, [obj]);
// copy/set any non-init, non-simple values here
return obj;
},
/**
* APIMethod: setMap
*
* Parameters:
* map - {<OpenLayers.Map>}
*/
setMap: function(map) {
/**
* If nothing to do with resolutions has been set, assume a single
* resolution determined by ratio*extent/size - if an image has a
* pixel aspect ratio different than one (as calculated above), the
* image will be stretched in one dimension only.
*/
if( this.options.maxResolution == null ) {
this.options.maxResolution = this.aspectRatio *
this.extent.getWidth() /
this.size.w;
}
OpenLayers.Layer.prototype.setMap.apply(this, arguments);
},
/**
* Method: moveTo
* Create the tile for the image or resize it for the new resolution
*
* Parameters:
* bounds - {<OpenLayers.Bounds>}
* zoomChanged - {Boolean}
* dragging - {Boolean}
*/
moveTo:function(bounds, zoomChanged, dragging) {
OpenLayers.Layer.prototype.moveTo.apply(this, arguments);
var firstRendering = (this.tile == null);
if(zoomChanged || firstRendering) {
//determine new tile size
this.setTileSize();
//determine new position (upper left corner of new bounds)
var ul = new OpenLayers.LonLat(this.extent.left, this.extent.top);
var ulPx = this.map.getLayerPxFromLonLat(ul);
if(firstRendering) {
//create the new tile
this.tile = new OpenLayers.Tile.Image(this, ulPx, this.extent,
null, this.tileSize);
} else {
//just resize the tile and set it's new position
this.tile.size = this.tileSize.clone();
this.tile.position = ulPx.clone();
}
this.tile.draw();
}
},
/**
* Set the tile size based on the map size.
*/
setTileSize: function() {
var tileWidth = this.extent.getWidth() / this.map.getResolution();
var tileHeight = this.extent.getHeight() / this.map.getResolution();
this.tileSize = new OpenLayers.Size(tileWidth, tileHeight);
},
/**
* APIMethod: setUrl
*
* Parameters:
* newUrl - {String}
*/
setUrl: function(newUrl) {
this.url = newUrl;
this.tile.draw();
},
/**
* APIMethod: getURL
* The url we return is always the same (the image itself never changes)
* so we can ignore the bounds parameter (it will always be the same,
* anyways)
*
* Parameters:
* bounds - {<OpenLayers.Bounds>}
*/
getURL: function(bounds) {
return this.url;
},
CLASS_NAME: "OpenLayers.Layer.Image"
});