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

« back to all changes in this revision

Viewing changes to gis/dhis-gis-geostat/mfbase/openlayers/lib/OpenLayers/Layer/Image.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
 * @requires OpenLayers/Layer.js
 
7
 * @requires OpenLayers/Tile/Image.js
 
8
 */
 
9
 
 
10
/**
 
11
 * Class: OpenLayers.Layer.Image
 
12
 * Instances of OpenLayers.Layer.Image are used to display data from a web
 
13
 * accessible image as a map layer.  Create a new image layer with the
 
14
 * <OpenLayers.Layer.Image> constructor.  Inherits from <OpenLayers.Layer>.
 
15
 */
 
16
OpenLayers.Layer.Image = OpenLayers.Class(OpenLayers.Layer, {
 
17
 
 
18
    /**
 
19
     * Property: isBaseLayer
 
20
     * {Boolean} The layer is a base layer.  Default is true.  Set this property
 
21
     * in the layer options
 
22
     */
 
23
    isBaseLayer: true,
 
24
    
 
25
    /**
 
26
     * Property: url
 
27
     * {String} URL of the image to use
 
28
     */
 
29
    url: null,
 
30
 
 
31
    /**
 
32
     * Property: extent
 
33
     * {<OpenLayers.Bounds>} The image bounds in map units
 
34
     */
 
35
    extent: null,
 
36
    
 
37
    /**
 
38
     * Property: size
 
39
     * {<OpenLayers.Size>} The image size in pixels
 
40
     */
 
41
    size: null,
 
42
 
 
43
    /**
 
44
     * Property: tile
 
45
     * {<OpenLayers.Tile.Image>}
 
46
     */
 
47
    tile: null,
 
48
 
 
49
    /**
 
50
     * Property: aspectRatio
 
51
     * {Float} The ratio of height/width represented by a single pixel in the
 
52
     * graphic
 
53
     */
 
54
    aspectRatio: null,
 
55
 
 
56
    /**
 
57
     * Constructor: OpenLayers.Layer.Image
 
58
     * Create a new image layer
 
59
     *
 
60
     * Parameters:
 
61
     * name - {String} A name for the layer.
 
62
     * url - {String} Relative or absolute path to the image
 
63
     * extent - {<OpenLayers.Bounds>} The extent represented by the image
 
64
     * size - {<OpenLayers.Size>} The size (in pixels) of the image
 
65
     * options - {Object} Hashtable of extra options to tag onto the layer
 
66
     */
 
67
    initialize: function(name, url, extent, size, options) {
 
68
        this.url = url;
 
69
        this.extent = extent;
 
70
        this.size = size;
 
71
        OpenLayers.Layer.prototype.initialize.apply(this, [name, options]);
 
72
 
 
73
        this.aspectRatio = (this.extent.getHeight() / this.size.h) /
 
74
                           (this.extent.getWidth() / this.size.w);
 
75
    },    
 
76
 
 
77
    /**
 
78
     * Method: destroy
 
79
     * Destroy this layer
 
80
     */
 
81
    destroy: function() {
 
82
        if (this.tile) {
 
83
            this.tile.destroy();
 
84
            this.tile = null;
 
85
        }
 
86
        OpenLayers.Layer.prototype.destroy.apply(this, arguments);
 
87
    },
 
88
    
 
89
    /**
 
90
     * Method: clone
 
91
     * Create a clone of this layer
 
92
     *
 
93
     * Paramters:
 
94
     * obj - {Object} An optional layer (is this ever used?)
 
95
     *
 
96
     * Returns:
 
97
     * {<OpenLayers.Layer.Image>} An exact copy of this layer
 
98
     */
 
99
    clone: function(obj) {
 
100
        
 
101
        if(obj == null) {
 
102
            obj = new OpenLayers.Layer.Image(this.name,
 
103
                                               this.url,
 
104
                                               this.extent,
 
105
                                               this.size,
 
106
                                               this.options);
 
107
        }
 
108
 
 
109
        //get all additions from superclasses
 
110
        obj = OpenLayers.Layer.prototype.clone.apply(this, [obj]);
 
111
 
 
112
        // copy/set any non-init, non-simple values here
 
113
 
 
114
        return obj;
 
115
    },    
 
116
    
 
117
    /**
 
118
     * APIMethod: setMap
 
119
     * 
 
120
     * Parameters:
 
121
     * map - {<OpenLayers.Map>}
 
122
     */
 
123
    setMap: function(map) {
 
124
        /**
 
125
         * If nothing to do with resolutions has been set, assume a single
 
126
         * resolution determined by ratio*extent/size - if an image has a
 
127
         * pixel aspect ratio different than one (as calculated above), the
 
128
         * image will be stretched in one dimension only.
 
129
         */
 
130
        if( this.options.maxResolution == null ) {
 
131
            this.options.maxResolution = this.aspectRatio *
 
132
                                         this.extent.getWidth() /
 
133
                                         this.size.w;
 
134
        }
 
135
        OpenLayers.Layer.prototype.setMap.apply(this, arguments);
 
136
    },
 
137
 
 
138
    /** 
 
139
     * Method: moveTo
 
140
     * Create the tile for the image or resize it for the new resolution
 
141
     * 
 
142
     * Parameters:
 
143
     * bounds - {<OpenLayers.Bounds>}
 
144
     * zoomChanged - {Boolean}
 
145
     * dragging - {Boolean}
 
146
     */
 
147
    moveTo:function(bounds, zoomChanged, dragging) {
 
148
        OpenLayers.Layer.prototype.moveTo.apply(this, arguments);
 
149
 
 
150
        var firstRendering = (this.tile == null);
 
151
 
 
152
        if(zoomChanged || firstRendering) {
 
153
 
 
154
            //determine new tile size
 
155
            this.setTileSize();
 
156
 
 
157
            //determine new position (upper left corner of new bounds)
 
158
            var ul = new OpenLayers.LonLat(this.extent.left, this.extent.top);
 
159
            var ulPx = this.map.getLayerPxFromLonLat(ul);
 
160
 
 
161
            if(firstRendering) {
 
162
                //create the new tile
 
163
                this.tile = new OpenLayers.Tile.Image(this, ulPx, this.extent, 
 
164
                                                      null, this.tileSize);
 
165
            } else {
 
166
                //just resize the tile and set it's new position
 
167
                this.tile.size = this.tileSize.clone();
 
168
                this.tile.position = ulPx.clone();
 
169
            }
 
170
            this.tile.draw();
 
171
        }
 
172
    }, 
 
173
 
 
174
    /**
 
175
     * Set the tile size based on the map size.
 
176
     */
 
177
    setTileSize: function() {
 
178
        var tileWidth = this.extent.getWidth() / this.map.getResolution();
 
179
        var tileHeight = this.extent.getHeight() / this.map.getResolution();
 
180
        this.tileSize = new OpenLayers.Size(tileWidth, tileHeight);
 
181
    },
 
182
 
 
183
    /**
 
184
     * APIMethod: setUrl
 
185
     * 
 
186
     * Parameters:
 
187
     * newUrl - {String}
 
188
     */
 
189
    setUrl: function(newUrl) {
 
190
        this.url = newUrl;
 
191
        this.tile.draw();
 
192
    },
 
193
 
 
194
    /** 
 
195
     * APIMethod: getURL
 
196
     * The url we return is always the same (the image itself never changes)
 
197
     *     so we can ignore the bounds parameter (it will always be the same, 
 
198
     *     anyways) 
 
199
     * 
 
200
     * Parameters:
 
201
     * bounds - {<OpenLayers.Bounds>}
 
202
     */
 
203
    getURL: function(bounds) {
 
204
        return this.url;
 
205
    },
 
206
 
 
207
    CLASS_NAME: "OpenLayers.Layer.Image"
 
208
});