[OpenLayers-Commits] r12426 - in
sandbox/mpriour/temporal_map/openlayers/lib/OpenLayers: .
Control Format/WMSCapabilities TimeAgent
commits-20090109 at openlayers.org
commits-20090109 at openlayers.org
Mon Oct 3 02:37:12 EDT 2011
Author: mpriour
Date: 2011-10-02 23:37:11 -0700 (Sun, 02 Oct 2011)
New Revision: 12426
Modified:
sandbox/mpriour/temporal_map/openlayers/lib/OpenLayers/Control/TimeManager.js
sandbox/mpriour/temporal_map/openlayers/lib/OpenLayers/Format/WMSCapabilities/v1.js
sandbox/mpriour/temporal_map/openlayers/lib/OpenLayers/TimeAgent.js
sandbox/mpriour/temporal_map/openlayers/lib/OpenLayers/TimeAgent/WMS.js
Log:
Modify to better handle existing WMS 1.1 Dimension & resolutions data from GetCapabilities
Self-config control based on time agents
Modified: sandbox/mpriour/temporal_map/openlayers/lib/OpenLayers/Control/TimeManager.js
===================================================================
--- sandbox/mpriour/temporal_map/openlayers/lib/OpenLayers/Control/TimeManager.js 2011-09-29 15:10:08 UTC (rev 12425)
+++ sandbox/mpriour/temporal_map/openlayers/lib/OpenLayers/Control/TimeManager.js 2011-10-03 06:37:11 UTC (rev 12426)
@@ -158,6 +158,16 @@
this.timeAgents = this.buildTimeAgents(options.layers);
if(this.timeAgents.length){this.fixedLayers=true;}
}
+ this.events.on({
+ 'play': function(){
+ if (!this.units) {
+ this.guessPlaybackRate()
+ }
+ else {
+ this.events.un({'play': arguments.caller.callee,scope: this})
+ }
+ },scope:this
+ })
},
/**
* APIMethod: destroy
@@ -184,7 +194,7 @@
if (!this.timeAgents) {
for (var i = 0, len = map.layers.length; i < len; i++) {
var lyr = map.layers[i];
- if (lyr.metadata.timeInterval && lyr.metadata.timeInterval.length) {
+ if ((lyr.dimensions && lyr.dimensions.time) || (lyr.metadata.timeInterval && lyr.metadata.timeInterval.length)) {
if (!this.layers) this.layers = [];
this.layers.push(lyr);
}
@@ -210,6 +220,9 @@
},
onAddLayer: function(evt){
var lyr = evt.layer;
+ if(lyr.dimensions && lyr.dimensions.time){
+ lyr.metadata.timeInterval = this.timeExtentsToIntervals(lyr.dimensions.time)
+ }
var added=false;
if (lyr.metadata.timeInterval && !this.fixedLayers) {
this.timeAgents || (this.timeAgents = [])
@@ -457,14 +470,11 @@
//categorize layers and separate into arrays for use in subclasses
for(var i=0,len=layers.length;i<len;i++){
var lyr = layers[i];
+ if(lyr.dimensions && lyr.dimensions.time){lyr.metadata.timeInterval=this.timeExtentsToIntervals(lyr.dimensions.time)}
//allow user specified overrides and custom behavior
if (lyr.timeAgent) {
var agent;
- if(lyr.timeAgent instanceof OpenLayers.TimeAgent){
- agent = lyr.timeAgent;
- agent.layers = (agent.layers && agent.layers instanceof Array)?(agent.layers.push(lyr)):agent.layers=[lyr];
- agent.timeManager=this;
- }else if(lyr.timeAgent instanceof Function){
+ if(lyr.timeAgent instanceof Function){
agent = new OpenLayers.TimeAgent({
onTick:lyr.timeAgent,
layers:[lyr],
@@ -543,6 +553,101 @@
}
return (range.length)?range:null;
},
+ guessPlaybackRate:function(){
+ var timeSpans=this.getValidTimeSpans();
+ if (timeSpans) {
+ timeSpans.sort(function(a, b){
+ //sort by most restrictive range
+ var arange = a.end - a.start, brange = b.end - b.start;
+ if (arange != brange) {
+ return (arange < brange) ? 1 : -1
+ }
+ else if (a.resolution.units != b.resolution.units) {
+ //same range find biggest step unit
+ switch (a.resolution.units) {
+ case OpenLayers.TimeUnit.YEARS:
+ return 1
+ case OpenLayers.TimeUnit.SECONDS:
+ return -1
+ case OpenLayers.TimeUnit.MONTHS:
+ return (b.resolution.units == OpenLayers.TimeUnit.YEARS) ? -1 : 1
+ case OpenLayers.TimeUnit.MINUTES:
+ return (b.resolution.units == OpenLayers.TimeUnit.SECONDS) ? 1 : -1
+ case OpenLayers.TimeUnit.HOURS:
+ if (b.resolution.units == OpenLayers.TimeUnit.MINUTES || b.resolution.units == OpenLayers.TimeUnit.SECONDS) {
+ return 1
+ }
+ else {
+ return -1
+ }
+ case OpenLayers.TimeUnit.DAYS:
+ if (b.resolution.units == OpenLayers.TimeUnit.MONTHS || b.resolution.units == OpenLayers.TimeUnit.YEARS) {
+ return -1
+ }
+ else {
+ return 1
+ }
+ }
+ }
+ else {
+ //same range and units, pick largest step
+ return a.resolution.step - b.resolution.step
+ }
+ });
+ this.setRange([timeSpans[0].start, timeSpans[0].end]);
+ this.units = timeSpans[0].resolution.units;
+ this.step = timeSpans[0].resolution.step;
+ }
+ else if (this.intervals) {
+ this.snapToIntervals = true;
+ }
+ else {
+ //guess based on range, keep step at 1
+ var diff = this.range[1] - this.range[0];
+ if (diff < 6e3) {
+ this.units = OpenLayers.TimeUnit.SECONDS;
+ }
+ else if (diff < 36e5) {
+ this.units = OpenLayers.TimeUnit.MINUTES;
+ }
+ else if (diff < 864e5) {
+ this.units = OpenLayers.TimeUnit.HOURS;
+ }
+ else if (diff < 2628e6) {
+ this.units = OpenLayers.TimeUnit.DAYS;
+ }
+ else if (diff < 31536e6) {
+ this.units = OpenLayers.TimeUnit.MONTHS;
+ }
+ else {
+ this.units = OpenLayers.TimeUnit.YEARS;
+ }
+ }
+ },
+ getValidTimeSpans:function(agents){
+ agents = agents || this.timeAgents || [];
+ var validTimes = []
+ for(var i=0,len=agents.length;i<len;i++){
+ if(agents[i].timeSpans){
+ validTimes=validTimes.concat(agents[i].timeSpans)
+ }
+ }
+ return (validTimes.length)?validTimes:null;
+ },
+ timeExtentsToIntervals: function(timeExtents){
+ var intervals=[];
+ for (var i = 0; i < timeExtents.length; ++i) {
+ var timeParts = timeExtents[i].split("/");
+ if (timeParts.length > 1) {
+ var min = timeParts[0], max = timeParts[1], res = timeParts[2];
+ intervals.push([min, max, res])
+ }
+ else {
+ intervals.push(timeParts[0])
+ }
+ }
+ return (intervals.length) ? intervals : null;
+ },
getUniqueDates:function(dates){
//sort the times
dates.sort(function(a, b){return a - b;});
Modified: sandbox/mpriour/temporal_map/openlayers/lib/OpenLayers/Format/WMSCapabilities/v1.js
===================================================================
--- sandbox/mpriour/temporal_map/openlayers/lib/OpenLayers/Format/WMSCapabilities/v1.js 2011-09-29 15:10:08 UTC (rev 12425)
+++ sandbox/mpriour/temporal_map/openlayers/lib/OpenLayers/Format/WMSCapabilities/v1.js 2011-10-03 06:37:11 UTC (rev 12426)
@@ -428,6 +428,32 @@
},
"SRS": function(node, obj) {
obj.srs[this.getChildValue(node)] = true;
+ },
+ "Dimension": function(node,obj){
+ var dimension = node.getAttribute("name");
+ var units = node.getAttribute("units");
+ obj.dimensions = obj.dimensions || [];
+ obj.dimensions.push({name:dimension,"units":unit})
+ },
+ "Extent": function(node,obj){
+ var type = node.getAttribute("name");
+ if(type == "time"){
+ if(!obj.metadata){obj.metadata={}}
+ var timeExtents = this.getChildValue(node),
+ intervals=[];
+ timeExtents = timeExtents.split(',');
+ for (var i = 0; i < timeExtents.length; ++i) {
+ var timeParts = timeExtents[i].split("/");
+ if (timeParts.length > 1) {
+ var min = timeParts[0], max = timeParts[1], res = timeParts[2];
+ intervals.push([min, max, res])
+ }
+ else {
+ intervals.push(timeParts[0])
+ }
+ }
+ obj.metadata.timeInterval=intervals;
+ }
}
}
},
Modified: sandbox/mpriour/temporal_map/openlayers/lib/OpenLayers/TimeAgent/WMS.js
===================================================================
--- sandbox/mpriour/temporal_map/openlayers/lib/OpenLayers/TimeAgent/WMS.js 2011-09-29 15:10:08 UTC (rev 12425)
+++ sandbox/mpriour/temporal_map/openlayers/lib/OpenLayers/TimeAgent/WMS.js 2011-10-03 06:37:11 UTC (rev 12426)
@@ -130,8 +130,28 @@
}
isotime=OpenLayers.Date.toISOString(minTime)+'/'+OpenLayers.Date.toISOString(time)
}
- else if(layer.metadata.timeInterval.length>2 && this.intervalMode != "exact"){
- //TODO implement special case handling
+ else if(layer.metadata.timeInterval[0] instanceof Date && this.intervalMode != "exact"){
+ //find where this time fits into
+ var intervals = layer.metadata.timeInterval;
+ //first check that this time is in the array
+ for (var i = 0,len=intervals.length; i < len; i++) {
+ if (time.getTime() == intervals[i].getTime()) {
+ isotime = OpenLayers.Date.toISOString(intervals[i])
+ break;
+ }
+ else{
+ var diff = time-intervals[i];
+ if(diff<0){
+ if(this.intervalMode=="lastValid"){
+ isotime = OpenLayers.Date.toISOString(intervals[i-1])
+ }else{
+ var useTime = diff>(time-intervals[i-1]) ? intervals[i-1] : intervals[i];
+ isotime = OpenLayers.Date.toISOString(useTime);
+ }
+ break;
+ }
+ }
+ }
}else{
//format time in ISO:8601 format
isotime = OpenLayers.Date.toISOString(time);
Modified: sandbox/mpriour/temporal_map/openlayers/lib/OpenLayers/TimeAgent.js
===================================================================
--- sandbox/mpriour/temporal_map/openlayers/lib/OpenLayers/TimeAgent.js 2011-09-29 15:10:08 UTC (rev 12425)
+++ sandbox/mpriour/temporal_map/openlayers/lib/OpenLayers/TimeAgent.js 2011-10-03 06:37:11 UTC (rev 12426)
@@ -58,8 +58,9 @@
}
if (this.layers) {
var timeConfig = this.buildRangeAndIntervals(this.layers);
- this.range = timeConfig.range;
- this.intervals = timeConfig.intervals;
+ this.range = timeConfig.range,
+ this.intervals = timeConfig.intervals,
+ this.timeSpans = timeConfig.timeSpans;
}
},
destroy:function(){
@@ -79,8 +80,9 @@
}
else {
var timeConfig = this.buildRangeAndIntervals(this.layers);
- this.range = timeConfig.range;
- this.intervals = timeConfig.intervals;
+ this.range = timeConfig.range,
+ this.intervals = timeConfig.intervals,
+ this.timeSpans = timeConfig.timeSpans;
}
},
removeLayer:function(layer){
@@ -96,29 +98,36 @@
the range & intervals*/
if(this.intervals || timeInterval[0].getTime() == this.range[0].getTime() || timeInterval[1].getTime() == this.range[1].getTime()){
var timeConfig = this.buildRangeAndIntervals(this.layers);
- this.range = timeConfig.range;
- this.intervals = timeConfig.intervals;
+ this.range = timeConfig.range,
+ this.intervals = timeConfig.intervals,
+ this.timeSpans = timeConfig.timeSpans;
}
},
buildRangeAndIntervals:function(layers){
- var range = [], intervals=[];
+ var range = [], intervals=[], validTimes=[];
for(var i=0,len=layers.length;i<len;i++){
var timeInterval = (layers[i].metadata)?layers[i].metadata.timeInterval:null;
if (timeInterval) {
for (var j = 0; j < timeInterval.length; j++) {
- if (!(timeInterval[j] instanceof Date)) {
- timeInterval[j] = OpenLayers.Date.parse(timeInterval[j])
+ if(timeInterval[j] instanceof Array){
+ var min = OpenLayers.Date.parse(timeInterval[j][0]),
+ max = OpenLayers.Date.parse(timeInterval[j][1]),
+ resolution = timeInterval[j][2] ? this.parseISOPeriod(timeInterval[j][2]) : null;
+ var timeRangeObj = {
+ start: min,
+ end: max,
+ 'resolution': (resolution) ? resolution : null
+ }
+ validTimes.push(timeRangeObj);
+ timeInterval[j] = timeRangeObj;
+ if (!range[0] || min < range[0]) {range[0] = min;}
+ if (!range[1] || max > range[1]) {range[1] = max;}
}
- }
- if (timeInterval.length == 2) {
- if (!range[0] || timeInterval[0] < range[0]) {range[0] = timeInterval[0];}
- if (!range[1] || timeInterval[1] > range[1]) {range[1] = timeInterval[1];}
- }
- else if (timeInterval.length > 2) {
- for (var j = 0; j < timeInterval.length; j++) {
- if (OpenLayers.Util.indexOf(intervals, timeInterval[j]) == -1) {
- intervals.push(timeInterval[j])
+ else{
+ if (!(timeInterval[j] instanceof Date)) {
+ timeInterval[j] = OpenLayers.Date.parse(timeInterval[j])
}
+ intervals.push(timeInterval[j])
}
}
}
@@ -127,12 +136,41 @@
intervals = this.timeManager.getUniqueDates(intervals);
var last = intervals.length-1;
if (!range[0] || intervals[0] < range[0]) {range[0] = intervals[0];}
- if (!range[1] || intervals[last] > range[1]) {range[1] = intervals[last];}
+ if (!range[1] || intervals[last] > range[1]) {range[1] = intervals[last];}
}else{
intervals=null;
}
- return {'range':range,'intervals':intervals}
+ return {'range':range,'intervals':intervals,'timeSpans':(validTimes.length)? validTimes : null}
},
-
+ parseISOPeriod:function(period){
+ var dateRE = [/(\d+)Y/,/(\d+)M/,/(\d+)D/],
+ timeRE = [/(\d+)H/,/(\d+)M/,/(\d+)S/],
+ periods = period.split('P')[1].split('T'),
+ intervalPeriod,d={};
+ if (periods[0]) {
+ var dt = periods[0];
+ d.years = dt.match(dateRE[0]) ? dt.match(dateRE[0])[1] : null;
+ d.months = dt.match(dateRE[1]) ? dt.match(dateRE[1])[1] * ((d.years) ? 12 * d.years : 1) : null;
+ d.days = dt.match(dateRE[2]) ? dt.match(dateRE[2])[1] * ((d.months) ? 30 * d.months : 1) : null;
+ }
+ if (periods[1]) {
+ var tm = periods[1];
+ d.hours = tm.match(timeRE[0]) ? tm.match(timeRE[0])[1] * ((d.days) ? 24 * d.days : 1) : null;
+ d.minutes = tm.match(timeRE[1]) ? tm.match(timeRE[1])[1] * ((d.hours) ? 60 * d.hours : 1) : null;
+ d.seconds = tm.match(timeRE[2]) ? tm.match(timeRE[2])[1] * ((d.hours) ? 60 * 60 * d.hours : (d.minutes) ? 60 * d.minutes : 1) : null;
+ }
+ var unitTest = ['seconds','minutes','hours','days','months','years']
+ for(var i=0;i<unitTest.length;++i){
+ var u = unitTest[i];
+ if(d[u]){
+ intervalPeriod = {
+ step: d[u],
+ units: OpenLayers.TimeUnit[u.toUpperCase()]
+ };
+ break;
+ }
+ }
+ return intervalPeriod;
+ },
CLASS_NAME:'OpenLayers.TimeAgent'
});
\ No newline at end of file
More information about the Commits
mailing list