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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for * full list of contributors). Published under the Clear BSD license. * See http://svn.openlayers.org/trunk/openlayers/license.txt for the * full text of the license. */ /** * @requires OpenLayers/Control.js * @requires OpenLayers/Feature/Vector.js */ /** * Class: OpenLayers.Control.Measure * Allows for drawing of features for measurements. * * Inherits from: * - <OpenLayers.Control> */ OpenLayers.Control.Measure = OpenLayers.Class(OpenLayers.Control, { /** * Constant: EVENT_TYPES * {Array(String)} Supported application event types. Register a listener * for a particular event with the following syntax: * (code) * control.events.register(type, obj, listener); * (end) * * Listeners will be called with a reference to an event object. The * properties of this event depends on exactly what happened. * * Supported control event types (in addition to those from <OpenLayers.Control>): * measure - Triggered when a measurement sketch is complete. Listeners * will receive an event with measure, units, order, and geometry * properties. * measurepartial - Triggered when a new point is added to the * measurement sketch. Listeners receive an event with measure, * units, order, and geometry. */ EVENT_TYPES: ['measure', 'measurepartial'], /** * APIProperty: handlerOptions * {Object} Used to set non-default properties on the control's handler */ handlerOptions: null, /** * Property: callbacks * {Object} The functions that are sent to the handler for callback */ callbacks: null, /** * Property: displaySystem * {String} Display system for output measurements. Supported values * are 'english', 'metric', and 'geographic'. Default is 'metric'. */ displaySystem: 'metric', /** * Property: geodesic * {Boolean} Calculate geodesic metrics instead of planar metrics. This * requires that geometries can be transformed into Geographic/WGS84 * (if that is not already the map projection). Default is false. */ geodesic: false, /** * Property: displaySystemUnits * {Object} Units for various measurement systems. Values are arrays * of unit abbreviations (from OpenLayers.INCHES_PER_UNIT) in decreasing * order of length. */ displaySystemUnits: { geographic: ['dd'], english: ['mi', 'ft', 'in'], metric: ['km', 'm'] }, /** * Property: delay * {Number} Number of milliseconds between clicks before the event is * considered a double-click. The "measurepartial" event will not * be triggered if the sketch is completed within this time. This * is required for IE where creating a browser reflow (if a listener * is modifying the DOM by displaying the measurement values) messes * with the dblclick listener in the sketch handler. */ partialDelay: 300, /** * Property: delayedTrigger * {Number} Timeout id of trigger for measurepartial. */ delayedTrigger: null, /** * APIProperty: persist * {Boolean} Keep the temporary measurement sketch drawn after the * measurement is complete. The geometry will persist until a new * measurement is started, the control is deactivated, or <cancel> is * called. */ persist: false, /** * Constructor: OpenLayers.Control.Measure * * Parameters: * handler - {<OpenLayers.Handler>} * options - {Object} */ initialize: function(handler, options) { // concatenate events specific to measure with those from the base this.EVENT_TYPES = OpenLayers.Control.Measure.prototype.EVENT_TYPES.concat( OpenLayers.Control.prototype.EVENT_TYPES ); OpenLayers.Control.prototype.initialize.apply(this, [options]); this.callbacks = OpenLayers.Util.extend( {done: this.measureComplete, point: this.measurePartial}, this.callbacks ); // let the handler options override, so old code that passes 'persist' // directly to the handler does not need an update this.handlerOptions = OpenLayers.Util.extend( {persist: this.persist}, this.handlerOptions ); this.handler = new handler(this, this.callbacks, this.handlerOptions); }, /** * APIMethod: cancel * Stop the control from measuring. If <persist> is true, the temporary * sketch will be erased. */ cancel: function() { this.handler.cancel(); }, /** * Method: updateHandler * * Parameters: * handler - {Function} One of the sketch handler constructors. * options - {Object} Options for the handler. */ updateHandler: function(handler, options) { var active = this.active; if(active) { this.deactivate(); } this.handler = new handler(this, this.callbacks, options); if(active) { this.activate(); } }, /** * Method: measureComplete * Called when the measurement sketch is done. * * Parameters: * geometry - {<OpenLayers.Geometry>} */ measureComplete: function(geometry) { if(this.delayedTrigger) { window.clearTimeout(this.delayedTrigger); } this.measure(geometry, "measure"); }, /** * Method: measurePartial * Called each time a new point is added to the measurement sketch. * * Parameters: * point - {<OpenLayers.Geometry.Point>} The last point added. * geometry - {<OpenLayers.Geometry>} The sketch geometry. */ measurePartial: function(point, geometry) { if (geometry.getLength() > 0) { geometry = geometry.clone(); this.delayedTrigger = window.setTimeout( OpenLayers.Function.bind(function() { this.measure(geometry, "measurepartial"); }, this), this.partialDelay ); } }, /** * Method: measure * * Parameters: * geometry - {<OpenLayers.Geometry>} * eventType - {String} */ measure: function(geometry, eventType) { var stat, order; if(geometry.CLASS_NAME.indexOf('LineString') > -1) { stat = this.getBestLength(geometry); order = 1; } else { stat = this.getBestArea(geometry); order = 2; } this.events.triggerEvent(eventType, { measure: stat[0], units: stat[1], order: order, geometry: geometry }); }, /** * Method: getBestArea * Based on the <displaySystem> returns the area of a geometry. * * Parameters: * geometry - {<OpenLayers.Geometry>} * * Returns: * {Array([Float, String])} Returns a two item array containing the * area and the units abbreviation. */ getBestArea: function(geometry) { var units = this.displaySystemUnits[this.displaySystem]; var unit, area; for(var i=0, len=units.length; i<len; ++i) { unit = units[i]; area = this.getArea(geometry, unit); if(area > 1) { break; } } return [area, unit]; }, /** * Method: getArea * * Parameters: * geometry - {<OpenLayers.Geometry>} * units - {String} Unit abbreviation * * Returns: * {Float} The geometry area in the given units. */ getArea: function(geometry, units) { var area, geomUnits; if(this.geodesic) { area = geometry.getGeodesicArea(this.map.getProjectionObject()); geomUnits = "m"; } else { area = geometry.getArea(); geomUnits = this.map.getUnits(); } var inPerDisplayUnit = OpenLayers.INCHES_PER_UNIT[units]; if(inPerDisplayUnit) { var inPerMapUnit = OpenLayers.INCHES_PER_UNIT[geomUnits]; area *= Math.pow((inPerMapUnit / inPerDisplayUnit), 2); } return area; }, /** * Method: getBestLength * Based on the <displaySystem> returns the length of a geometry. * * Parameters: * geometry - {<OpenLayers.Geometry>} * * Returns: * {Array([Float, String])} Returns a two item array containing the * length and the units abbreviation. */ getBestLength: function(geometry) { var units = this.displaySystemUnits[this.displaySystem]; var unit, length; for(var i=0, len=units.length; i<len; ++i) { unit = units[i]; length = this.getLength(geometry, unit); if(length > 1) { break; } } return [length, unit]; }, /** * Method: getLength * * Parameters: * geometry - {<OpenLayers.Geometry>} * units - {String} Unit abbreviation * * Returns: * {Float} The geometry length in the given units. */ getLength: function(geometry, units) { var length, geomUnits; if(this.geodesic) { length = geometry.getGeodesicLength(this.map.getProjectionObject()); geomUnits = "m"; } else { length = geometry.getLength(); geomUnits = this.map.getUnits(); } var inPerDisplayUnit = OpenLayers.INCHES_PER_UNIT[units]; if(inPerDisplayUnit) { var inPerMapUnit = OpenLayers.INCHES_PER_UNIT[geomUnits]; length *= (inPerMapUnit / inPerDisplayUnit); } return length; }, CLASS_NAME: "OpenLayers.Control.Measure" }); |