OpenLayers OpenLayers

Changeset 6362

Show
Ignore:
Timestamp:
02/24/08 21:48:18 (11 months ago)
Author:
achipa
Message:

A more elegant interval implementation through the Date class extension + various time related cleanups

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • sandbox/achipa/openlayers/lib/OpenLayers.js

    r6310 r6362  
    7979            "OpenLayers/BaseTypes/Pixel.js", 
    8080            "OpenLayers/BaseTypes/Size.js", 
    81             "OpenLayers/BaseTypes/DateTime.js", 
     81            "OpenLayers/BaseTypes/ExtDate.js", 
    8282            "OpenLayers/Console.js", 
    8383            "Rico/Corner.js", 
  • sandbox/achipa/openlayers/lib/OpenLayers/BaseTypes/ExtDate.js

    r6328 r6362  
    33 * Instances of this class represent moments or intervals of time. This allows us to 
    44 * give the spatial data a temporal dimension and act accordingly. The default timezone 
    5  * is UTC and the precision is in seconds. The minimum representable time is 
    6  * 1970-01-01T00:00:00Z, and the maximum representable time is 2038-01-19T03:14:07Z 
     5 * is UTC and the precision is in seconds.   
     6 */ 
     7 
     8 
     9Date.prototype.duration = 0; 
     10 
     11/** 
     12 * Method: contains 
     13 * Return whether the parameter date is fully contained within the interval of the Date method 
     14 * in whose context this function is called 
    715 * 
     16 * Parameters: 
     17 * {Date} targetdate 
     18 * 
     19 * Returns: 
     20 * {bool} 
    821 */ 
    9 OpenLayers.DateTime = OpenLayers.Class(Date, { 
     22Date.prototype.contains = function(targetdate) { 
     23    if (targetdate >= this && targetdate.getTime() + targetdate.duration <= this.getTime() + this.duration)  
     24            return true; 
    1025 
    11     /** 
    12      * Property: to 
    13      * {Number} 
    14      */ 
    15     to: null, 
     26    return false; 
     27}; 
    1628 
    17     /** 
    18      * Property: zone 
    19      * {Number} 
    20      */ 
    21     zone: 'UTC', 
     29/** 
     30 * Method: getEndDate 
     31 * Returns a {Date} object with the value of the end of the interval represented in this object 
     32 * 
     33 * Returns: 
     34 * {Date} 
     35 */ 
     36Date.prototype.getEndDate = function() { 
     37    return new Date(this.getTime() + this.duration); 
     38}; 
    2239 
    23     /** 
    24      * Constructor: OpenLayers.DateTime 
    25      * Construct a new time object. 
    26      * 
    27      * Parameters: 
    28      * begin - {Date} The start bounds. 
    29      * end - {Date} The end bounds. 
    30      * zone - {String} Time zone. 
    31      */ 
    32     initialize: function(begin, end, zone) { 
    33         if (begin != null) { 
    34             this.prototype.setTime(begin); 
    35         } 
    36         if (end != null) { 
    37         if (begin > end) { // we got them swapped 
    38                 this.to = begin; 
    39             this.from = end; 
    40             } else { 
    41                 this.to = end; 
    42             } 
    43         } 
    44         if (this.from == this.to) { 
    45             this.interval = false; 
    46         } 
    47     }, 
     40/** 
     41 * Method: getPlaceInInterval 
     42 * Returns a float value between 0 and 1 depending on where the targetdate parameter is in the 
     43 * date objects' time interval. If targetdate starts earlier than the date object, 0 is returned, 
     44 * if it starts after the end of the date object, 1 is returned. 
     45 * 
     46 * The optional second {bool} parameter defines whether the center of the  
     47 * targetdate's interval should be used (default is false). 
     48 * 
     49 * Parameters: 
     50 * {Date} targetdate 
     51 * {bool} us e 
     52 * 
     53 * Returns: 
     54 * {float} 0.0 - 1.0 
     55 */ 
     56Date.prototype.getPlaceInInterval = function(targetdate, center) { 
     57    var centerdate = targetdate; 
     58    if (center && targetdate.duration != 0) 
     59        centerdate = new Date(targetdate.getTime() + targetdate.duration/2); 
    4860 
    49     /** 
    50      * Method: setTime 
    51      * Set time 
    52      * 
    53      */ 
    54     setTime:function(targetdate) { 
    55         this.prototype.setTime(targetdate.getTime()); 
    56         if (targetdate.isInterval()) 
    57         this.setEnd(targetdate.to); 
    58     else 
    59         this.to = null; 
    60     }, 
     61    if (centerdate.getTime() > this.getTime() + this.duration) 
     62        return 1; 
     63    else if (targetdate <= centerdate) // using <= to accomodate for non-interval datetimes 
     64        return 0; 
     65    else 
     66        return (centerdate - this) / this.duration ; 
     67     
     68}; 
    6169 
    62     /** 
    63      * Method: setEnd 
    64      * Set time 
    65      * 
    66      */ 
    67     setEnd:function(targetdate) { 
    68         if (targetdate.isInterval()) 
    69         this.to = new Date(targetdate.to); 
    70     else 
    71         this.to = new Date(targetdate); 
     70/** 
     71 * Method: equals 
     72 * Returns whether the targetdate is equal by both start times and duration to the objects' 
     73 * 
     74 * Parameters: 
     75 * {Date} targetdate 
     76 * 
     77 * Returns: 
     78 * {bool} 
     79 */ 
     80Date.prototype.equals = function(targetdate) { 
     81    if (this == targetdate && this.duration == targetduration.duration) return true; 
     82    return false; 
     83}; 
    7284 
    73     if (this > this.to) { // we got them swapped 
    74         this.tmp = this.to.getTime(); 
    75             this.to.setTime(this.getTime()); 
    76             this.prototype.setTime(this.tmp); 
    77             delete this.tmp; 
    78         } 
    79     }, 
    80  
    81     /** 
    82      * Method: contains 
    83      * Set time 
    84      * 
    85      */ 
    86     contains:function(targetdate) { 
    87         if (targetdate >= this && targetdate <= this.to && // start date within range 
    88         (!targetdate.isInterval()) || (targetdate.to >= this && targetdate.to <= this.to)) // and both dates in range if it's an interval 
    89                 return true; 
    90  
    91         return false; 
    92     }, 
    93  
    94     /** 
    95      * Method: contains 
    96      * Set time 
    97      * 
    98      */ 
    99     getPlaceInInterval:function(targetdate) { 
    100         if (targetdate) { 
    101             if (targetdate > this.to) 
    102         return 1; 
    103             else if (targetdate <= this) // using <= to accomodate for non-interval datetimes 
    104                 return 0; 
    105  
    106         return (targetdate - this) / (this.to - this) ; 
    107         } 
    108         return -1; 
    109     }, 
    110  
    111     equal: function(dateTime) { 
    112         if (this.from == dateTime.from && this.to == dateTime.to) return true; 
    113         return false; 
    114     }, 
    115  
    116     isInterval: function() { 
    117         if (to) 
    118         return true; 
    119      
    120     return false; 
    121     }, 
    122  
    123     CLASS_NAME: "OpenLayers.DateTime" 
    124  
    125 }); 
    12685 
    12786/** 
     
    13089 * 
    13190 */ 
    132 OpenLayers.DateTime.fromRFC3339 = function(datestr) { 
     91Date.fromRFC3339 = function(datestr) { 
    13392    var targetdate = new Date(); 
    13493    if (datestr) { 
  • sandbox/achipa/openlayers/lib/OpenLayers/Control/PanTimeBar.js

    r6310 r6362  
    383383            has = false; 
    384384            for (j=0; j < this.time.length; j++) { 
    385                 if (this.time[j].equal(features[i].dateTime)) { 
     385                if (this.time[j].equals(features[i].dateTime)) { 
    386386                    has = true 
    387387                    continue; 
     
    397397        else if (this.now >= this.time.length) this.now = this.time.length-1; 
    398398        for (var i=0; i < this.map.layers.length; i++) { 
    399             if (this.map.layers[i].isTemporalLayer()) this.map.layers[i].setDateTime(this.time[this.now].from); 
     399            if (this.map.layers[i].isTemporalLayer()) this.map.layers[i].setTime(this.time[this.now]); 
    400400        } 
    401401    }, 
  • sandbox/achipa/openlayers/lib/OpenLayers/Layer.js

    r6310 r6362  
    10441044 
    10451045    getDateTimeIntervals: function () { 
    1046         alert(this.datetimeIntervals); 
    10471046        return this.datetimeIntervals; 
    10481047    }, 
     
    10501049    /** 
    10511050     * Method: setDateTime 
    1052      * {Date} or {<OpenLayers.DateTime>} Sets the current date/time in the layers 
    1053      * 
    1054      */ 
    1055     setDateTime: function (dt) { 
     1051     * {Date} Sets the current date/time in the layers 
     1052     * 
     1053     */ 
     1054    setTime: function (dt) { 
    10561055        if (!this.isTemporalLayer()) 
    10571056            return; 
    10581057 
    1059         if (typeof dt == "OpenLayers.DateTime") 
     1058        if (!this.currentDateTime.equals(dt)){ 
    10601059            this.currentDateTime = dt; 
    1061         else 
    1062             this.currentDateTime = new OpenLayers.DateTime(dt); 
    1063  
    1064         this.events.triggerEvent("datetimechanged"); 
    1065     }, 
    1066  
    1067     /** 
    1068      * Method: getStartDateTime 
     1060            this.events.triggerEvent("datetimechanged"); 
     1061        } 
     1062    }, 
     1063 
     1064    /** 
     1065     * Method: getStartTime 
    10691066     * {Date} Returns the first date/time the layer is available for, null 
    10701067     *     if the layer has no temporal conotation 
    10711068     * 
    10721069     */ 
    1073     getStartDateTime: function () { 
     1070    getStartTime: function () { 
    10741071    if (!this.isTemporalLayer()) 
    10751072            return null; 
     
    10781075        return null; 
    10791076 
    1080         return this.datetimeIntervals[0].from
    1081     }, 
    1082  
    1083     /** 
    1084      * Method: getStopDateTime 
     1077        return this.datetimeIntervals[0]
     1078    }, 
     1079 
     1080    /** 
     1081     * Method: getStopTime 
    10851082     * {Date} Returns the last date/time the layer is available for, null 
    10861083     *     if the layer has no temporal conotation 
    10871084     * 
    10881085     */ 
    1089     getStopDateTime: function () { 
     1086    getStopTime: function () { 
    10901087    if (!this.isTemporalLayer()) 
    10911088            return null; 
     
    10941091        return null; 
    10951092 
    1096         return this.datetimeIntervals[this.datetimeIntervals.length -1].to
     1093        return new Date(this.datetimeIntervals[this.datetimeIntervals.length -1].getTime() + this.datetimeIntervals[this.datetimeIntervals.length -1].duration)
    10971094    }, 
    10981095 
  • sandbox/achipa/openlayers/lib/OpenLayers/Layer/TimedPointTrack.js

    r6311 r6362  
    9191    updateHighlight: function(){ 
    9292        this.renderer.eraseFeatures([this.highlight]); 
    93         if (!this.permanentHighlight && (this.getStartDateTime() > this.currentDateTime.to || this.getStopDateTime() < this.currentDateTime.from)) 
     93        if (!this.permanentHighlight && (this.getStartTime() > this.currentDateTime.getEndDate() || this.getStopTime() < this.currentDateTime)) 
    9494            return; 
    95        
     95       
    9696        var closestpoint; 
    9797        var closesttime = 0; 
    9898        var closestfeature = 0; 
    9999        for (var i=0; i < this.features.length; i++){ 
    100             if (Math.abs( closesttime - this.currentDateTime.from ) >  // our best result so far is worse 
     100            if (Math.abs( closesttime - this.currentDateTime ) >  // our best result so far is worse 
    101101               Math.min(                                           // than the result to the closer point in time 
    102                    Math.abs( this.features[i].dateTime.from - this.currentDateTime.from ), 
    103                    Math.abs( this.features[i].dateTime.to - this.currentDateTime.from
     102                   Math.abs( this.features[i].dateTime - this.currentDateTime ), 
     103                   Math.abs( this.features[i].dateTime.getEndDate() - this.currentDateTime
    104104                   ) 
    105            && (this.features[i].dateTime.contains(this.currentDateTime.from)  || !this.features[i].dateTime.interval)    // allow Points to match as they might not have intervals 
    106  
    107105        ){ 
    108                 closesttime = this.features[i].dateTime.from
     106                closesttime = this.features[i].dateTime
    109107                closestpoint = this.features[i].geometry.components[0]; 
    110108                closestfeature = this.features[i]; 
     
    143141        for(var i = 0; i < this.features.length; i++) { 
    144142            if (this.features[i].attributes["pubdate"]) { 
    145                 pubdate = OpenLayers.DateTime.fromRFC3339(this.features[i].attributes["pubdate"]); 
     143                pubdate = Date.fromRFC3339(this.features[i].attributes["pubdate"]); 
    146144                if (pubdate <= begin) 
    147145                    begin = pubdate; 
     
    150148            } 
    151149        } 
    152         this.datetimeIntervals = [new OpenLayers.DateTime(begin, end)]; 
     150        this.datetimeIntervals = [new Date(begin)]; 
     151        this.datetimeIntervals[0].duration = end - begin; 
    153152    }, 
    154153 
     
    179178 
    180179        if (pointFeature.data) 
    181             pubDate = OpenLayers.DateTime.fromRFC3339(pointFeature.data["pubdate"]); 
     180            pubDate = Date.fromRFC3339(pointFeature.data["pubdate"]); 
    182181        else if (pointFeature.attributes) 
    183             pubDate = OpenLayers.DateTime.fromRFC3339(pointFeature.attributes["pubdate"]); 
     182            pubDate = Date.fromRFC3339(pointFeature.attributes["pubdate"]); 
    184183             
    185184            if (!endPoint) { 
     
    202201                lines[i-1] = new OpenLayers.Feature.Vector(line, attributes); 
    203202        if (attributes["pubdate"]) { 
    204                 lines[i-1].dateTime = new OpenLayers.DateTime(prevDate, pubDate); 
     203                lines[i-1].dateTime = new Date(prevDate); 
     204                    lines[i-1].dateTime.duration = pubDate-prevDate; 
    205205        } 
    206206            } 
  • sandbox/achipa/openlayers/lib/OpenLayers/Map.js

    r5982 r6362  
    19211921    }, 
    19221922 
     1923    /** 
     1924     * Method: setTime 
     1925     *  
     1926     * Parameters: 
     1927     * {Date} Sets the current date/time in all layers 
     1928     * 
     1929     */ 
     1930    setTime: function (dt) { 
     1931        for(var i=0; i < this.layers.length; i++)  
     1932            if (this.layers[i].isTemporalLayer()) 
     1933                this.layers[i].setTime(dt); 
     1934    }, 
     1935 
     1936 
    19231937    CLASS_NAME: "OpenLayers.Map" 
    19241938});