[76] | 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/Strategy.js |
---|
| 8 | * @requires OpenLayers/Filter/Spatial.js |
---|
| 9 | */ |
---|
| 10 | |
---|
| 11 | /** |
---|
| 12 | * Class: OpenLayers.Strategy.BBOX |
---|
| 13 | * A simple strategy that reads new features when the viewport invalidates |
---|
| 14 | * some bounds. |
---|
| 15 | * |
---|
| 16 | * Inherits from: |
---|
| 17 | * - <OpenLayers.Strategy> |
---|
| 18 | */ |
---|
| 19 | OpenLayers.Strategy.BBOX = OpenLayers.Class(OpenLayers.Strategy, { |
---|
| 20 | |
---|
| 21 | /** |
---|
| 22 | * Property: bounds |
---|
| 23 | * {<OpenLayers.Bounds>} The current data bounds (in the same projection |
---|
| 24 | * as the layer - not always the same projection as the map). |
---|
| 25 | */ |
---|
| 26 | bounds: null, |
---|
| 27 | |
---|
| 28 | /** |
---|
| 29 | * Property: resolution |
---|
| 30 | * {Float} The current data resolution. |
---|
| 31 | */ |
---|
| 32 | resolution: null, |
---|
| 33 | |
---|
| 34 | /** |
---|
| 35 | * APIProperty: ratio |
---|
| 36 | * {Float} The ratio of the data bounds to the viewport bounds (in each |
---|
| 37 | * dimension). Default is 2. |
---|
| 38 | */ |
---|
| 39 | ratio: 2, |
---|
| 40 | |
---|
| 41 | /** |
---|
| 42 | * Property: resFactor |
---|
| 43 | * {Float} Optional factor used to determine when previously requested |
---|
| 44 | * features are invalid. If set, the resFactor will be compared to the |
---|
| 45 | * resolution of the previous request to the current map resolution. |
---|
| 46 | * If resFactor > (old / new) and 1/resFactor < (old / new). If you |
---|
| 47 | * set a resFactor of 1, data will be requested every time the |
---|
| 48 | * resolution changes. If you set a resFactor of 3, data will be |
---|
| 49 | * requested if the old resolution is 3 times the new, or if the new is |
---|
| 50 | * 3 times the old. If the old bounds do not contain the new bounds |
---|
| 51 | * new data will always be requested (with or without considering |
---|
| 52 | * resFactor). |
---|
| 53 | */ |
---|
| 54 | resFactor: null, |
---|
| 55 | |
---|
| 56 | /** |
---|
| 57 | * Property: response |
---|
| 58 | * {<OpenLayers.Protocol.Response>} The protocol response object returned |
---|
| 59 | * by the layer protocol. |
---|
| 60 | */ |
---|
| 61 | response: null, |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | * Constructor: OpenLayers.Strategy.BBOX |
---|
| 65 | * Create a new BBOX strategy. |
---|
| 66 | * |
---|
| 67 | * Parameters: |
---|
| 68 | * options - {Object} Optional object whose properties will be set on the |
---|
| 69 | * instance. |
---|
| 70 | */ |
---|
| 71 | initialize: function(options) { |
---|
| 72 | OpenLayers.Strategy.prototype.initialize.apply(this, [options]); |
---|
| 73 | }, |
---|
| 74 | |
---|
| 75 | /** |
---|
| 76 | * Method: activate |
---|
| 77 | * Set up strategy with regard to reading new batches of remote data. |
---|
| 78 | * |
---|
| 79 | * Returns: |
---|
| 80 | * {Boolean} The strategy was successfully activated. |
---|
| 81 | */ |
---|
| 82 | activate: function() { |
---|
| 83 | var activated = OpenLayers.Strategy.prototype.activate.call(this); |
---|
| 84 | if(activated) { |
---|
| 85 | this.layer.events.on({ |
---|
| 86 | "moveend": this.update, |
---|
| 87 | scope: this |
---|
| 88 | }); |
---|
| 89 | this.layer.events.on({ |
---|
| 90 | "refresh": this.update, |
---|
| 91 | scope: this |
---|
| 92 | }); |
---|
| 93 | } |
---|
| 94 | return activated; |
---|
| 95 | }, |
---|
| 96 | |
---|
| 97 | /** |
---|
| 98 | * Method: deactivate |
---|
| 99 | * Tear down strategy with regard to reading new batches of remote data. |
---|
| 100 | * |
---|
| 101 | * Returns: |
---|
| 102 | * {Boolean} The strategy was successfully deactivated. |
---|
| 103 | */ |
---|
| 104 | deactivate: function() { |
---|
| 105 | var deactivated = OpenLayers.Strategy.prototype.deactivate.call(this); |
---|
| 106 | if(deactivated) { |
---|
| 107 | this.layer.events.un({ |
---|
| 108 | "moveend": this.update, |
---|
| 109 | scope: this |
---|
| 110 | }); |
---|
| 111 | this.layer.events.un({ |
---|
| 112 | "refresh": this.update, |
---|
| 113 | scope: this |
---|
| 114 | }); |
---|
| 115 | } |
---|
| 116 | return deactivated; |
---|
| 117 | }, |
---|
| 118 | |
---|
| 119 | /** |
---|
| 120 | * Method: update |
---|
| 121 | * Callback function called on "moveend" or "refresh" layer events. |
---|
| 122 | * |
---|
| 123 | * Parameters: |
---|
| 124 | * options - {Object} An object with a property named "force", this |
---|
| 125 | * property references a boolean value indicating if new data |
---|
| 126 | * must be incondtionally read. |
---|
| 127 | */ |
---|
| 128 | update: function(options) { |
---|
| 129 | var mapBounds = this.getMapBounds(); |
---|
| 130 | if ((options && options.force) || this.invalidBounds(mapBounds)) { |
---|
| 131 | this.calculateBounds(mapBounds); |
---|
| 132 | this.resolution = this.layer.map.getResolution(); |
---|
| 133 | this.triggerRead(); |
---|
| 134 | } |
---|
| 135 | }, |
---|
| 136 | |
---|
| 137 | /** |
---|
| 138 | * Method: getMapBounds |
---|
| 139 | * Get the map bounds expressed in the same projection as this layer. |
---|
| 140 | * |
---|
| 141 | * Returns: |
---|
| 142 | * {<OpenLayers.Bounds>} Map bounds in the projection of the layer. |
---|
| 143 | */ |
---|
| 144 | getMapBounds: function() { |
---|
| 145 | var bounds = this.layer.map.getExtent(); |
---|
| 146 | if(!this.layer.projection.equals(this.layer.map.getProjectionObject())) { |
---|
| 147 | bounds = bounds.clone().transform( |
---|
| 148 | this.layer.map.getProjectionObject(), this.layer.projection |
---|
| 149 | ); |
---|
| 150 | } |
---|
| 151 | return bounds; |
---|
| 152 | }, |
---|
| 153 | |
---|
| 154 | /** |
---|
| 155 | * Method: invalidBounds |
---|
| 156 | * Determine whether the previously requested set of features is invalid. |
---|
| 157 | * This occurs when the new map bounds do not contain the previously |
---|
| 158 | * requested bounds. In addition, if <resFactor> is set, it will be |
---|
| 159 | * considered. |
---|
| 160 | * |
---|
| 161 | * Parameters: |
---|
| 162 | * mapBounds - {<OpenLayers.Bounds>} the current map extent, will be |
---|
| 163 | * retrieved from the map object if not provided |
---|
| 164 | * |
---|
| 165 | * Returns: |
---|
| 166 | * {Boolean} |
---|
| 167 | */ |
---|
| 168 | invalidBounds: function(mapBounds) { |
---|
| 169 | if(!mapBounds) { |
---|
| 170 | mapBounds = this.getMapBounds(); |
---|
| 171 | } |
---|
| 172 | var invalid = !this.bounds || !this.bounds.containsBounds(mapBounds); |
---|
| 173 | if(!invalid && this.resFactor) { |
---|
| 174 | var ratio = this.resolution / this.layer.map.getResolution(); |
---|
| 175 | invalid = (ratio >= this.resFactor || ratio <= (1 / this.resFactor)); |
---|
| 176 | } |
---|
| 177 | return invalid; |
---|
| 178 | }, |
---|
| 179 | |
---|
| 180 | /** |
---|
| 181 | * Method: calculateBounds |
---|
| 182 | * |
---|
| 183 | * Parameters: |
---|
| 184 | * mapBounds - {<OpenLayers.Bounds>} the current map extent, will be |
---|
| 185 | * retrieved from the map object if not provided |
---|
| 186 | */ |
---|
| 187 | calculateBounds: function(mapBounds) { |
---|
| 188 | if(!mapBounds) { |
---|
| 189 | mapBounds = this.getMapBounds(); |
---|
| 190 | } |
---|
| 191 | var center = mapBounds.getCenterLonLat(); |
---|
| 192 | var dataWidth = mapBounds.getWidth() * this.ratio; |
---|
| 193 | var dataHeight = mapBounds.getHeight() * this.ratio; |
---|
| 194 | this.bounds = new OpenLayers.Bounds( |
---|
| 195 | center.lon - (dataWidth / 2), |
---|
| 196 | center.lat - (dataHeight / 2), |
---|
| 197 | center.lon + (dataWidth / 2), |
---|
| 198 | center.lat + (dataHeight / 2) |
---|
| 199 | ); |
---|
| 200 | }, |
---|
| 201 | |
---|
| 202 | /** |
---|
| 203 | * Method: triggerRead |
---|
| 204 | * |
---|
| 205 | * Returns: |
---|
| 206 | * {<OpenLayers.Protocol.Response>} The protocol response object |
---|
| 207 | * returned by the layer protocol. |
---|
| 208 | */ |
---|
| 209 | triggerRead: function() { |
---|
| 210 | if (this.response) { |
---|
| 211 | this.layer.protocol.abort(this.response); |
---|
| 212 | this.layer.events.triggerEvent("loadend"); |
---|
| 213 | } |
---|
| 214 | this.layer.events.triggerEvent("loadstart"); |
---|
| 215 | this.response = this.layer.protocol.read({ |
---|
| 216 | filter: this.createFilter(), |
---|
| 217 | callback: this.merge, |
---|
| 218 | scope: this |
---|
| 219 | }); |
---|
| 220 | }, |
---|
| 221 | |
---|
| 222 | /** |
---|
| 223 | * Method: createFilter |
---|
| 224 | * Creates a spatial BBOX filter. If the layer that this strategy belongs |
---|
| 225 | * to has a filter property, this filter will be combined with the BBOX |
---|
| 226 | * filter. |
---|
| 227 | * |
---|
| 228 | * Returns |
---|
| 229 | * {<OpenLayers.Filter>} The filter object. |
---|
| 230 | */ |
---|
| 231 | createFilter: function() { |
---|
| 232 | var filter = new OpenLayers.Filter.Spatial({ |
---|
| 233 | type: OpenLayers.Filter.Spatial.BBOX, |
---|
| 234 | value: this.bounds, |
---|
| 235 | projection: this.layer.projection |
---|
| 236 | }); |
---|
| 237 | if (this.layer.filter) { |
---|
| 238 | filter = new OpenLayers.Filter.Logical({ |
---|
| 239 | type: OpenLayers.Filter.Logical.AND, |
---|
| 240 | filters: [this.layer.filter, filter] |
---|
| 241 | }); |
---|
| 242 | } |
---|
| 243 | return filter; |
---|
| 244 | }, |
---|
| 245 | |
---|
| 246 | /** |
---|
| 247 | * Method: merge |
---|
| 248 | * Given a list of features, determine which ones to add to the layer. |
---|
| 249 | * If the layer projection differs from the map projection, features |
---|
| 250 | * will be transformed from the layer projection to the map projection. |
---|
| 251 | * |
---|
| 252 | * Parameters: |
---|
| 253 | * resp - {<OpenLayers.Protocol.Response>} The response object passed |
---|
| 254 | * by the protocol. |
---|
| 255 | */ |
---|
| 256 | merge: function(resp) { |
---|
| 257 | this.layer.destroyFeatures(); |
---|
| 258 | var features = resp.features; |
---|
| 259 | if(features && features.length > 0) { |
---|
| 260 | var remote = this.layer.projection; |
---|
| 261 | var local = this.layer.map.getProjectionObject(); |
---|
| 262 | if(!local.equals(remote)) { |
---|
| 263 | var geom; |
---|
| 264 | for(var i=0, len=features.length; i<len; ++i) { |
---|
| 265 | geom = features[i].geometry; |
---|
| 266 | if(geom) { |
---|
| 267 | geom.transform(remote, local); |
---|
| 268 | } |
---|
| 269 | } |
---|
| 270 | } |
---|
| 271 | this.layer.addFeatures(features); |
---|
| 272 | } |
---|
| 273 | this.response = null; |
---|
| 274 | this.layer.events.triggerEvent("loadend"); |
---|
| 275 | }, |
---|
| 276 | |
---|
| 277 | CLASS_NAME: "OpenLayers.Strategy.BBOX" |
---|
| 278 | }); |
---|