Changeset 7789


Ignore:
Timestamp:
Aug 21, 2008 10:37:42 AM (8 years ago)
Author:
tschaub
Message:

Measure control updates.

Location:
sandbox/topp/trimet
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • sandbox/topp/trimet/examples/measure.html

    r7541 r7789  
    3434            var options = {
    3535                displayUnits: 'km',
     36                eventListeners: {
     37                    'measure': handleMeasurements
     38                },
    3639                handlerOptions: {
    3740                    persist: true
     
    4043            var circleOptions = {
    4144                displayUnits: 'km',
     45                eventListeners: {
     46                    'measure': handleMeasurements
     47                },
    4248                handlerOptions: {
    4349                    sides: 35,
     
    5763            };
    5864           
    59             var control;
    6065            for(var key in measureControls) {
    61                 control = measureControls[key];
    62                 control.onMeasure = handleMeasurements;
    63                 map.addControl(control);
     66                map.addControl(measureControls[key]);
    6467            }
    6568           
     
    6972        }
    7073       
    71         function handleMeasurements(geometry, length, area, units) {
     74        function handleMeasurements(event) {
     75            var geometry = event.geometry;
     76            var length = event.length;
     77            var area = event.area;
     78            var units = event.units;
    7279            var element = document.getElementById('output');
    7380            var out = "";
     
    8188        }
    8289
    83         function toggleControl(element) {
    84             for(key in measureControls) {
    85                 var control = measureControls[key];
    86                 if(element.value == key && element.checked) {
    87                     control.activate();
    88                 } else {
    89                     control.deactivate();
    90                 }
    91             }
    92         }
    9390    </script>
    9491  </head>
  • sandbox/topp/trimet/lib/OpenLayers/Control/Measure.js

    r7541 r7789  
    1818
    1919    /**
     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    /**
    2041     * APIProperty: handlerOptions
    2142     * {Object} Used to set non-default properties on the control's handler
     
    2445   
    2546    /**
    26      * APIProperty: onMeasure
    27      * {Function} After a geometry is drawn, onMeasure is called with three
    28      *     arguments: the geometry, its length, and its area.
    29      */
    30     onMeasure: function() {},
    31    
    32     /**
    3347     * Property: callbacks
    3448     * {Object} The functions that are sent to the handler for callback
     
    3751   
    3852    /**
    39      * Property: displayUnits
    40      * {String} Units for output.  Must be one of 'in', 'ft', 'mi', 'm', 'km',
    41      *     or 'dd'.  If null, displayUnits will be assumed to be the same as
    42      *     map units.
    43      */
    44     displayUnits: null,
     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    },
    4570
    4671    /**
     
    5277     */
    5378    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        );
    5484        OpenLayers.Control.prototype.initialize.apply(this, [options]);
    5585        this.callbacks = OpenLayers.Util.extend(
    56             {done: this.measureGeometry, point: this.partialMeasure},
     86            {done: this.measureComplete, point: this.measurePartial},
    5787            this.callbacks
    5888        );
    5989        this.handler = new handler(this, this.callbacks, this.handlerOptions);
    6090    },
    61 
    62     /**
    63      * Method: measureGeometry
    64      */
    65     measureGeometry: function(geometry) {
    66         var area = this.getArea(geometry);
    67         var length = this.getLength(geometry);
    68         this.onMeasure(geometry, length, area, this.displayUnits);
     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];
    69179    },
    70180   
    71181    /**
    72182     * Method: getArea
    73      */
    74     getArea: function(geometry) {
     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) {
    75192        var area = geometry.getArea();
    76         var inPerDisplayUnit = OpenLayers.INCHES_PER_UNIT[this.displayUnits];
     193        var inPerDisplayUnit = OpenLayers.INCHES_PER_UNIT[units];
    77194        if(inPerDisplayUnit) {
    78             var inPerMapUnit = OpenLayers.INCHES_PER_UNIT[this.map.units];
     195            var inPerMapUnit = OpenLayers.INCHES_PER_UNIT[this.map.getUnits()];
    79196            area *= Math.pow((inPerMapUnit / inPerDisplayUnit), 2);
    80197        }
     
    83200   
    84201    /**
     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    /**
    85226     * Method: getLength
    86      */
    87     getLength: function(geometry) {
     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) {
    88236        var length = geometry.getLength();
    89         var inPerDisplayUnit = OpenLayers.INCHES_PER_UNIT[this.displayUnits];
     237        var inPerDisplayUnit = OpenLayers.INCHES_PER_UNIT[units];
    90238        if(inPerDisplayUnit) {
    91             var inPerMapUnit = OpenLayers.INCHES_PER_UNIT[this.map.units];
     239            var inPerMapUnit = OpenLayers.INCHES_PER_UNIT[this.map.getUnits()];
    92240            length *= (inPerMapUnit / inPerDisplayUnit);
    93241        }
  • sandbox/topp/trimet/lib/OpenLayers/Handler/Path.js

    r7541 r7789  
    104104        this.line.geometry.addComponent(this.point.geometry.clone(),
    105105                                        this.line.geometry.components.length);
    106         this.callback("point", [this.point.geometry]);
     106        this.callback("point", [this.point.geometry, this.getGeometry()]);
    107107    },
    108108   
     
    140140
    141141    /**
    142      * Method: geometryClone
    143      * Return a clone of the relevant geometry.
     142     * Method: getGeometry
     143     * Return the sketch geometry.  If <multi> is true, this will return
     144     *     a multi-part geometry.
    144145     *
    145146     * Returns:
    146147     * {<OpenLayers.Geometry.LineString>}
    147148     */
    148     geometryClone: function() {
    149         var geometry = this.line.geometry.clone();
     149    getGeometry: function() {
     150        var geometry = this.line.geometry;
    150151        if(this.multi) {
    151152            geometry = new OpenLayers.Geometry.MultiLineString([geometry]);
  • sandbox/topp/trimet/lib/OpenLayers/Handler/Point.js

    r7541 r7789  
    233233   
    234234    /**
     235     * Method: getGeometry
     236     * Return the sketch geometry.  If <multi> is true, this will return
     237     *     a multi-part geometry.
     238     *
     239     * Returns:
     240     * {<OpenLayers.Geometry.Point>}
     241     */
     242    getGeometry: function() {
     243        var geometry = this.point.geometry;
     244        if(this.multi) {
     245            geometry = new OpenLayers.Geometry.MultiPoint([geometry]);
     246        }
     247        return geometry;
     248    },
     249
     250    /**
    235251     * Method: geometryClone
    236252     * Return a clone of the relevant geometry.
    237253     *
    238254     * Returns:
    239      * {<OpenLayers.Geometry.Point>}
     255     * {<OpenLayers.Geometry>}
    240256     */
    241257    geometryClone: function() {
    242         var geometry = this.point.geometry.clone();
    243         if(this.multi) {
    244             geometry = new OpenLayers.Geometry.MultiPoint([geometry]);
    245         }
    246         return geometry;
     258        return this.getGeometry().clone();
    247259    },
    248260 
  • sandbox/topp/trimet/lib/OpenLayers/Handler/Polygon.js

    r7541 r7789  
    9292        this.layer.drawFeature(this.point, this.style);
    9393    },
    94 
     94   
    9595    /**
    96      * Method: geometryClone
    97      * Return a clone of the relevant geometry.
     96     * Method: getGeometry
     97     * Return the sketch geometry.  If <multi> is true, this will return
     98     *     a multi-part geometry.
    9899     *
    99100     * Returns:
    100101     * {<OpenLayers.Geometry.Polygon>}
    101102     */
    102     geometryClone: function() {
    103         var geometry = this.polygon.geometry.clone();
     103    getGeometry: function() {
     104        var geometry = this.polygon.geometry;
    104105        if(this.multi) {
    105106            geometry = new OpenLayers.Geometry.MultiPolygon([geometry]);
Note: See TracChangeset for help on using the changeset viewer.