~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/KaMap.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/Layer/Grid.js
 
8
 */
 
9
 
 
10
/**
 
11
 * Class: OpenLayers.Layer.KaMap
 
12
 * 
 
13
 * Inherits from:
 
14
 *  - <OpenLayers.Layer.Grid>
 
15
 */
 
16
OpenLayers.Layer.KaMap = OpenLayers.Class(OpenLayers.Layer.Grid, {
 
17
 
 
18
    /** 
 
19
     * APIProperty: isBaseLayer
 
20
     * {Boolean} KaMap Layer is always a base layer 
 
21
     */    
 
22
    isBaseLayer: true,
 
23
 
 
24
    /**
 
25
     * APIProperty: units
 
26
     * {?}
 
27
     */    
 
28
    units: null,
 
29
 
 
30
    /**
 
31
     * APIProperty: resolution
 
32
     * {Float}
 
33
     */
 
34
    resolution: OpenLayers.DOTS_PER_INCH,
 
35
    
 
36
    /**
 
37
     * Constant: DEFAULT_PARAMS
 
38
     * {Object} parameters set by default. The default parameters set 
 
39
     * the format via the 'i' parameter to 'jpeg'.    
 
40
     */
 
41
    DEFAULT_PARAMS: {
 
42
        i: 'jpeg',
 
43
        map: ''
 
44
    },
 
45
        
 
46
    /**
 
47
     * Constructor: OpenLayers.Layer.KaMap
 
48
     * 
 
49
     * Parameters:
 
50
     * name - {String}
 
51
     * url - {String}
 
52
     * params - {Object} Parameters to be sent to the HTTP server in the
 
53
     *    query string for the tile. The format can be set via the 'i'
 
54
     *    parameter (defaults to jpg) , and the map should be set via 
 
55
     *    the 'map' parameter. It has been reported that ka-Map may behave
 
56
     *    inconsistently if your format parameter does not match the format
 
57
     *    parameter configured in your config.php. (See ticket #327 for more
 
58
     *    information.)
 
59
     * options - {Object} Additional options for the layer. Any of the 
 
60
     *     APIProperties listed on this layer, and any layer types it
 
61
     *     extends, can be overridden through the options parameter. 
 
62
     */
 
63
    initialize: function(name, url, params, options) {
 
64
        var newArguments = [];
 
65
        newArguments.push(name, url, params, options);
 
66
        OpenLayers.Layer.Grid.prototype.initialize.apply(this, newArguments);
 
67
        this.params = OpenLayers.Util.applyDefaults(
 
68
            this.params, this.DEFAULT_PARAMS
 
69
        );
 
70
    },
 
71
 
 
72
    /**
 
73
     * Method: getURL
 
74
     * 
 
75
     * Parameters:
 
76
     * bounds - {<OpenLayers.Bounds>} 
 
77
     * 
 
78
     * Returns:
 
79
     * {String} A string with the layer's url and parameters and also the 
 
80
     *          passed-in bounds and appropriate tile size specified as 
 
81
     *          parameters
 
82
     */
 
83
    getURL: function (bounds) {
 
84
        bounds = this.adjustBounds(bounds);
 
85
        var mapRes = this.map.getResolution();
 
86
        var scale = Math.round((this.map.getScale() * 10000)) / 10000;
 
87
        var pX = Math.round(bounds.left / mapRes);
 
88
        var pY = -Math.round(bounds.top / mapRes);
 
89
        return this.getFullRequestString(
 
90
                      { t: pY, 
 
91
                        l: pX,
 
92
                        s: scale
 
93
                      });
 
94
    },
 
95
 
 
96
    /**
 
97
     * Method: addTile
 
98
     * 
 
99
     * Parameters:
 
100
     * bounds - {<OpenLayers.Bounds>}
 
101
     * position - {<OpenLayers.Pixel>}
 
102
     * 
 
103
     * Returns:
 
104
     * {<OpenLayers.Tile.Image>}
 
105
     */    
 
106
    addTile:function(bounds,position) {
 
107
        var url = this.getURL(bounds);
 
108
        return new OpenLayers.Tile.Image(this, position, bounds, 
 
109
                                             url, this.tileSize);
 
110
    },
 
111
 
 
112
    /** 
 
113
     * Method: calculateGridLayout
 
114
     * ka-Map uses the center point of the map as an origin for 
 
115
     * its tiles. Override calculateGridLayout to center tiles 
 
116
     * correctly for this case.
 
117
     *
 
118
     * Parameters:
 
119
     * bounds - {<OpenLayers.Bound>}
 
120
     * extent - {<OpenLayers.Bounds>}
 
121
     * resolution - {Number}
 
122
     *
 
123
     * Returns:
 
124
     * Object containing properties tilelon, tilelat, tileoffsetlat,
 
125
     * tileoffsetlat, tileoffsetx, tileoffsety
 
126
     */
 
127
    calculateGridLayout: function(bounds, extent, resolution) {
 
128
        var tilelon = resolution*this.tileSize.w;
 
129
        var tilelat = resolution*this.tileSize.h;
 
130
        
 
131
        var offsetlon = bounds.left;
 
132
        var tilecol = Math.floor(offsetlon/tilelon) - this.buffer;
 
133
        var tilecolremain = offsetlon/tilelon - tilecol;
 
134
        var tileoffsetx = -tilecolremain * this.tileSize.w;
 
135
        var tileoffsetlon = tilecol * tilelon;
 
136
        
 
137
        var offsetlat = bounds.top;  
 
138
        var tilerow = Math.ceil(offsetlat/tilelat) + this.buffer;
 
139
        var tilerowremain = tilerow - offsetlat/tilelat;
 
140
        var tileoffsety = -(tilerowremain+1) * this.tileSize.h;
 
141
        var tileoffsetlat = tilerow * tilelat;
 
142
        
 
143
        return { 
 
144
          tilelon: tilelon, tilelat: tilelat,
 
145
          tileoffsetlon: tileoffsetlon, tileoffsetlat: tileoffsetlat,
 
146
          tileoffsetx: tileoffsetx, tileoffsety: tileoffsety
 
147
        };
 
148
    },    
 
149
 
 
150
    /**
 
151
     * APIMethod: clone
 
152
     * 
 
153
     * Parameters: 
 
154
     * obj - {Object}
 
155
     * 
 
156
     * Returns:
 
157
     * {<OpenLayers.Layer.Kamap>} An exact clone of this OpenLayers.Layer.KaMap
 
158
     */
 
159
    clone: function (obj) {
 
160
        
 
161
        if (obj == null) {
 
162
            obj = new OpenLayers.Layer.KaMap(this.name,
 
163
                                            this.url,
 
164
                                            this.params,
 
165
                                            this.options);
 
166
        }
 
167
 
 
168
        //get all additions from superclasses
 
169
        obj = OpenLayers.Layer.Grid.prototype.clone.apply(this, [obj]);
 
170
 
 
171
        // copy/set any non-init, non-simple values here
 
172
        if (this.tileSize != null) {
 
173
            obj.tileSize = this.tileSize.clone();
 
174
        }
 
175
        
 
176
        // we do not want to copy reference to grid, so we make a new array
 
177
        obj.grid = [];
 
178
 
 
179
        return obj;
 
180
    },    
 
181
    
 
182
    /**
 
183
     * APIMethod: getTileBounds
 
184
     * Returns The tile bounds for a layer given a pixel location.
 
185
     *
 
186
     * Parameters:
 
187
     * viewPortPx - {<OpenLayers.Pixel>} The location in the viewport.
 
188
     *
 
189
     * Returns:
 
190
     * {<OpenLayers.Bounds>} Bounds of the tile at the given pixel location.
 
191
     */
 
192
    getTileBounds: function(viewPortPx) {
 
193
        var resolution = this.getResolution();
 
194
        var tileMapWidth = resolution * this.tileSize.w;
 
195
        var tileMapHeight = resolution * this.tileSize.h;
 
196
        var mapPoint = this.getLonLatFromViewPortPx(viewPortPx);
 
197
        var tileLeft = tileMapWidth * Math.floor(mapPoint.lon / tileMapWidth);
 
198
        var tileBottom = tileMapHeight * Math.floor(mapPoint.lat / tileMapHeight);
 
199
        return new OpenLayers.Bounds(tileLeft, tileBottom,
 
200
                                     tileLeft + tileMapWidth,
 
201
                                     tileBottom + tileMapHeight);
 
202
    },
 
203
 
 
204
    CLASS_NAME: "OpenLayers.Layer.KaMap"
 
205
});