OpenLayers OpenLayers

Ticket #1241: measure.2.patch

File measure.2.patch, 8.2 kB (added by tschaub, 2 years ago)

basic measure control

  • lib/OpenLayers/Control/Measure.js

    old new  
     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/Control.js 
     7 * @requires OpenLayers/Feature/Vector.js 
     8 */ 
     9 
     10/** 
     11 * Class: OpenLayers.Control.Measure 
     12 * Allows for drawing of features for measurements. 
     13 * 
     14 * Inherits from: 
     15 *  - <OpenLayers.Control> 
     16 */ 
     17OpenLayers.Control.Measure = OpenLayers.Class(OpenLayers.Control, { 
     18 
     19    /** 
     20     * Constant: EVENT_TYPES 
     21     * {Array(String)} Supported application event types.  Register a listener 
     22     *     for a particular event with the following syntax: 
     23     * (code) 
     24     * control.events.register(type, obj, listener); 
     25     * (end) 
     26     * 
     27     * Listeners will be called with a reference to an event object.  The 
     28     *     properties of this event depends on exactly what happened. 
     29     * 
     30     * Supported control event types (in addition to those from <OpenLayers.Control>): 
     31     *  - *measure* Triggered when a measurement sketch is complete.  Listeners 
     32     *      will receive an event with measure, units, order, and geometry 
     33     *      properties. 
     34     *  - *measurepartial* Triggered when a new point is added to the 
     35     *      measurement sketch.  Listeners receive an event with measure, 
     36     *      units, order, and geometry. 
     37     */ 
     38    EVENT_TYPES: ['measure', 'measurepartial'], 
     39 
     40    /** 
     41     * APIProperty: handlerOptions 
     42     * {Object} Used to set non-default properties on the control's handler 
     43     */ 
     44    handlerOptions: null, 
     45     
     46    /** 
     47     * Property: callbacks 
     48     * {Object} The functions that are sent to the handler for callback 
     49     */ 
     50    callbacks: null, 
     51     
     52    /** 
     53     * Property: displaySystem 
     54     * {String} Display system for output measurements.  Supported values 
     55     *     are 'english', 'metric', and 'geographic'.  Default is 'metric'. 
     56     */ 
     57    displaySystem: 'metric', 
     58     
     59    /** 
     60     * Property: displaySystemUnits 
     61     * {Object} Units for various measurement systems.  Values are arrays 
     62     *     of unit abbreviations (from OpenLayers.INCHES_PER_UNIT) in decreasing 
     63     *     order of length. 
     64     */ 
     65    displaySystemUnits: { 
     66        geographic: ['dd'], 
     67        english: ['mi', 'ft', 'in'], 
     68        metric: ['km', 'm'] 
     69    }, 
     70 
     71    /** 
     72     * Constructor: OpenLayers.Control.Measure 
     73     *  
     74     * Parameters: 
     75     * handler - {<OpenLayers.Handler>}  
     76     * options - {Object}  
     77     */ 
     78    initialize: function(handler, options) { 
     79        // concatenate events specific to measure with those from the base 
     80        this.EVENT_TYPES = 
     81            OpenLayers.Control.Measure.prototype.EVENT_TYPES.concat( 
     82            OpenLayers.Control.prototype.EVENT_TYPES 
     83        ); 
     84        OpenLayers.Control.prototype.initialize.apply(this, [options]); 
     85        this.callbacks = OpenLayers.Util.extend( 
     86            {done: this.measureComplete, point: this.measurePartial}, 
     87            this.callbacks 
     88        ); 
     89        this.handler = new handler(this, this.callbacks, this.handlerOptions); 
     90    }, 
     91     
     92    /** 
     93     * Method: updateHandler 
     94     * 
     95     * Parameters: 
     96     * handler - {Function} One of the sketch handler constructors. 
     97     * options - {Object} Options for the handler. 
     98     */ 
     99    updateHandler: function(handler, options) { 
     100        var active = this.active; 
     101        if(active) { 
     102            this.deactivate(); 
     103        } 
     104        this.handler = new handler(this, this.callbacks, options); 
     105        if(active) { 
     106            this.activate(); 
     107        } 
     108    }, 
     109 
     110    /** 
     111     * Method: measureComplete 
     112     * Called when the measurement sketch is done. 
     113     * 
     114     * Parameters: 
     115     * geometry - {<OpenLayers.Geometry>} 
     116     */ 
     117    measureComplete: function(geometry) { 
     118        this.measure(geometry, "measure"); 
     119    }, 
     120     
     121    /** 
     122     * Method: measurePartial 
     123     * Called each time a new point is added to the measurement sketch. 
     124     * 
     125     * Parameters: 
     126     * point - {<OpenLayers.Geometry.Point>} The last point added. 
     127     * geometry - {<OpenLayers.Geometry>} The sketch geometry. 
     128     */ 
     129    measurePartial: function(point, geometry) { 
     130        this.measure(geometry, "measurepartial"); 
     131    }, 
     132 
     133    /** 
     134     * Method: measure 
     135     * 
     136     * Parameters: 
     137     * geometry - {<OpenLayers.Geometry>} 
     138     * eventType - {String} 
     139     */ 
     140    measure: function(geometry, eventType) { 
     141        var stat, order; 
     142        if(geometry.CLASS_NAME.indexOf('LineString') > -1) { 
     143            stat = this.getBestLength(geometry); 
     144            order = 1; 
     145        } else { 
     146            stat = this.getBestArea(geometry); 
     147            order = 2; 
     148        } 
     149        this.events.triggerEvent(eventType, { 
     150            measure: stat[0], 
     151            units: stat[1], 
     152            order: order, 
     153            geometry: geometry 
     154        }); 
     155    }, 
     156     
     157    /** 
     158     * Method: getBestArea 
     159     * Based on the <displaySystem> returns the area of a geometry. 
     160     * 
     161     * Parameters: 
     162     * geometry - {<OpenLayers.Geometry>} 
     163     * 
     164     * Returns: 
     165     * {Array([Float, String])}  Returns a two item array containing the 
     166     *     area and the units abbreviation. 
     167     */ 
     168    getBestArea: function(geometry) { 
     169        var units = this.displaySystemUnits[this.displaySystem]; 
     170        var unit, area; 
     171        for(var i=0, len=units.length; i<len; ++i) { 
     172            unit = units[i]; 
     173            area = this.getArea(geometry, unit); 
     174            if(area > 1) { 
     175                break; 
     176            } 
     177        } 
     178        return [area, unit]; 
     179    }, 
     180     
     181    /** 
     182     * Method: getArea 
     183     * 
     184     * Parameters: 
     185     * geometry - {<OpenLayers.Geometry>} 
     186     * units - {String} Unit abbreviation 
     187     * 
     188     * Returns: 
     189     * {Float} The geometry area in the given units. 
     190     */ 
     191    getArea: function(geometry, units) { 
     192        var area = geometry.getArea(); 
     193        var inPerDisplayUnit = OpenLayers.INCHES_PER_UNIT[units]; 
     194        if(inPerDisplayUnit) { 
     195            var inPerMapUnit = OpenLayers.INCHES_PER_UNIT[this.map.getUnits()]; 
     196            area *= Math.pow((inPerMapUnit / inPerDisplayUnit), 2); 
     197        } 
     198        return area; 
     199    }, 
     200     
     201    /** 
     202     * Method: getBestLength 
     203     * Based on the <displaySystem> returns the length of a geometry. 
     204     * 
     205     * Parameters: 
     206     * geometry - {<OpenLayers.Geometry>} 
     207     * 
     208     * Returns: 
     209     * {Array([Float, String])}  Returns a two item array containing the 
     210     *     length and the units abbreviation. 
     211     */ 
     212    getBestLength: function(geometry) { 
     213        var units = this.displaySystemUnits[this.displaySystem]; 
     214        var unit, length; 
     215        for(var i=0, len=units.length; i<len; ++i) { 
     216            unit = units[i]; 
     217            length = this.getLength(geometry, unit); 
     218            if(length > 1) { 
     219                break; 
     220            } 
     221        } 
     222        return [length, unit]; 
     223    }, 
     224 
     225    /** 
     226     * Method: getLength 
     227     * 
     228     * Parameters: 
     229     * geometry - {<OpenLayers.Geometry>} 
     230     * units - {String} Unit abbreviation 
     231     * 
     232     * Returns: 
     233     * {Float} The geometry length in the given units. 
     234     */ 
     235    getLength: function(geometry, units) { 
     236        var length = geometry.getLength(); 
     237        var inPerDisplayUnit = OpenLayers.INCHES_PER_UNIT[units]; 
     238        if(inPerDisplayUnit) { 
     239            var inPerMapUnit = OpenLayers.INCHES_PER_UNIT[this.map.getUnits()]; 
     240            length *= (inPerMapUnit / inPerDisplayUnit); 
     241        } 
     242        return length; 
     243    }, 
     244 
     245    CLASS_NAME: "OpenLayers.Control.Measure" 
     246}); 
  • lib/OpenLayers.js

    old new  
    160160            "OpenLayers/Control/DrawFeature.js", 
    161161            "OpenLayers/Control/DragFeature.js", 
    162162            "OpenLayers/Control/ModifyFeature.js", 
     163            "OpenLayers/Control/Measure.js", 
    163164            "OpenLayers/Control/Panel.js", 
    164165            "OpenLayers/Control/SelectFeature.js", 
    165166            "OpenLayers/Control/NavigationHistory.js",