[OpenLayers-Commits] r12458 - in addins/timedpointtrack/trunk: examples examples/json lib lib/OpenLayers lib/OpenLayers/Layer lib/OpenLayers/Strategy

commits-20090109 at openlayers.org commits-20090109 at openlayers.org
Mon Jan 30 18:08:55 EST 2012


Author: ahocevar
Date: 2012-01-30 15:08:54 -0800 (Mon, 30 Jan 2012)
New Revision: 12458

Added:
   addins/timedpointtrack/trunk/examples/json/
   addins/timedpointtrack/trunk/examples/json/track1.json
   addins/timedpointtrack/trunk/lib/OpenLayers/Strategy/
   addins/timedpointtrack/trunk/lib/OpenLayers/Strategy/TimeFeatures.js
Modified:
   addins/timedpointtrack/trunk/lib/OpenLayers/Layer/TimedPointTrack.js
   addins/timedpointtrack/trunk/lib/TimedPointTrack.js
Log:
Making addin work with GeoJSON linestrings that have a time attribute with an array of timestamps for each linestring vertex

Added: addins/timedpointtrack/trunk/examples/json/track1.json
===================================================================
--- addins/timedpointtrack/trunk/examples/json/track1.json	                        (rev 0)
+++ addins/timedpointtrack/trunk/examples/json/track1.json	2012-01-30 23:08:54 UTC (rev 12458)
@@ -0,0 +1,11 @@
+{
+    "type": "Feature",
+    "geometry": {
+    "type": "LineString",
+    "coordinates": [[-78.58,33.13,3],[-78.68,33.23,3],[-78.78,33.43,3],[-78.38,33.83,3],[-78.18,33.63,3],[-78.28,33.83,3],[-78.68,33.73,3]]
+    },
+    "properties": {
+        "title": "DATASET_2342",
+        "time": ["2012-01-29 07:00:00Z","2012-01-29 08:00:00Z","2012-01-29 09:00:00Z","2012-01-29 10:00:00Z","2012-01-29 11:00:00Z","2012-01-29 12:00:00Z","2012-01-29 13:00:00Z"]
+    }
+}

Modified: addins/timedpointtrack/trunk/lib/OpenLayers/Layer/TimedPointTrack.js
===================================================================
--- addins/timedpointtrack/trunk/lib/OpenLayers/Layer/TimedPointTrack.js	2012-01-26 15:20:30 UTC (rev 12457)
+++ addins/timedpointtrack/trunk/lib/OpenLayers/Layer/TimedPointTrack.js	2012-01-30 23:08:54 UTC (rev 12458)
@@ -81,76 +81,21 @@
      *            instance.
      */    
     initialize: function(name, url, options) {
+        options = options || {};
         
         var feedCategories = this.feedCategories;
         
         var defaultOptions = {
-            strategies: [new OpenLayers.Strategy.Fixed()],
+            strategies: [
+                new OpenLayers.Strategy.Fixed(),
+                new OpenLayers.Strategy.TimeFeatures({
+                    createFeatures: options.createFeatures
+                })
+            ],
             protocol: new OpenLayers.Protocol.HTTP({
                 url: url,
-                format: new OpenLayers.Format.GeoRSS({
-                    georssns: "http://www.georss.org/georss/10",
-                    read: function(doc) {
-                        if(typeof doc == "string") { 
-                            doc = OpenLayers.Format.XML.prototype.read.apply(
-                                this, [doc]);
-                        }
-
-                        var feedCats = this.getElementsByTagNameNS(doc, '*', 'category');
-                        var cat;
-                        for(var i=0; i<feedCats.length; ++i) {
-                            cat = feedCats[i];
-                            if(cat.parentNode.nodeName == "feed") {
-                                feedCategories[cat.getAttribute("scheme")] = {
-                                    term: cat.getAttribute("term"),
-                                    label: cat.getAttribute("label")
-                                }
-                            }
-                        }
-
-                        var itemlist = this.getElementsByTagNameNS(doc, '*', 'item');
-                        if(itemlist.length == 0) {
-                            itemlist = this.getElementsByTagNameNS(doc, '*', 'entry');
-                        }
-
-                        // create an empty linestring geometry for the track
-                        var pointtrack = new OpenLayers.Geometry.LineString();
-                        var len = itemlist.length;
-                        pointtrack.components = new Array(len);
-                        // Create the features array with an empty feature.
-                        // This feature will get the pointtrack geometry when
-                        // we are done with parsing the rss feed. Doing it
-                        // this way will ensure that the track is on bottom of
-                        // the markers.
-                        var features = [new OpenLayers.Feature.Vector()];
-                        var feature, component, pubDate, prevDate;
-                        for(var i=0; i<len; ++i) {
-                            feature = this.createFeatureFromItem(itemlist[i]);
-                            // add the point to the track
-                            component = feature.geometry;
-                            pubDate = OpenLayers.Date.smartParse(feature.attributes["pubDate"]);
-                            component.dateTime = pubDate;
-                            pointtrack.components[i] = component;
-
-
-                            if (i > 1 && feature.attributes["pubDate"]) {
-                                pointtrack.components[i-1].dateTime.duration = pubDate-prevDate;
-                            }
-                            prevDate = pubDate;
-
-                            // only add marker if it has something to say.
-                            if(options.createFeatures !== false && feature.attributes.title != "Untitled") {
-                                features.push(feature);
-                            }
-                        }
-
-                        features[0].geometry = pointtrack;
-                        features[0].attributes = {
-                            title: "The Track",
-                            description: "This track has been created by connecting the feed entries to a line."
-                        }
-                        return features;
-                    }
+                format: options.format || new OpenLayers.Format.GeoRSS({
+                    georssns: "http://www.georss.org/georss/10"
                 })
             })
         };

Added: addins/timedpointtrack/trunk/lib/OpenLayers/Strategy/TimeFeatures.js
===================================================================
--- addins/timedpointtrack/trunk/lib/OpenLayers/Strategy/TimeFeatures.js	                        (rev 0)
+++ addins/timedpointtrack/trunk/lib/OpenLayers/Strategy/TimeFeatures.js	2012-01-30 23:08:54 UTC (rev 12458)
@@ -0,0 +1,136 @@
+/**
+ * Class: OpenLayers.Strategy.TimeFeatures
+ * Strategy for processing features from time enabled linestring or point
+ * features.
+ *
+ * Inherits from:
+ *  - <OpenLayers.Strategy> 
+ */
+OpenLayers.Strategy.TimeFeatures = OpenLayers.Class(OpenLayers.Strategy, {
+    
+    /**
+     * APIProperty: createFeatures
+     * {Boolean} true if point features should be created in addition to a
+     *     linestring. Default is true.
+     */
+    createFeatures: true,
+    
+    /**
+     * APIMethod: activate
+     * Activate the strategy.  Register any listeners, do appropriate setup.
+     * 
+     * Returns:
+     * {Boolean} The strategy was successfully activated.
+     */
+    activate: function() {
+        var activated = OpenLayers.Strategy.prototype.activate.call(this);
+        if(activated) {
+            this.layer.events.on({
+                "beforefeaturesadded": this.processFeatures,
+                scope: this
+            });
+        }
+        return activated;
+    },
+    
+    /**
+     * APIMethod: deactivate
+     * Deactivate the strategy.  Unregister any listeners, do appropriate
+     *     tear-down.
+     * 
+     * Returns:
+     * {Boolean} The strategy was successfully deactivated.
+     */
+    deactivate: function() {
+        var deactivated = OpenLayers.Strategy.prototype.deactivate.call(this);
+        if(deactivated) {
+            this.clearCache();
+            this.layer.events.un({
+                "beforefeaturesadded": this.processFeatures,
+                scope: this
+            });
+        }
+        return deactivated;
+    },
+    
+    /**
+     * Method: processFeatures
+     * Process features before they are added to the layer.
+     *
+     * Parameters:
+     * event - {Object} The event that this was listening for.  This will come
+     *     with a batch of features to be processed.
+     */
+    processFeatures: function(event) {
+        var features = event.features;
+        
+        if (features[0].geometry instanceof OpenLayers.Geometry.LineString) {
+            this.processLineString(features);
+        } else {
+            this.processPoints(features);
+        }
+    },
+    
+    /**
+     * Method: processLineString
+     * Add time stamps from the feature's time attribute to each geometry
+     *     component of the linestring
+     *
+     * Parameters:
+     * features - {Array(<OpenLayers.Feature.Vector>)} Array of exactly one
+     *     feature with a linestring geometry and a time attribute.
+     */
+    processLineString: function(features) {
+        var geom = features[0].geometry,
+            time = features[0].attributes["time"],
+            component, i, prevDate, pubData;
+        for (i=0, ii=geom.components.length; i<ii; ++i) {
+            component = geom.components[i];
+            pubDate = OpenLayers.Date.smartParse(time[i]);
+            component.dateTime = pubDate;
+            if (i > 1) {
+                geom.components[i-1].dateTime.duration = pubDate - prevDate;
+            }
+            prevDate = pubDate;
+        }
+    },
+    
+    /**
+     * Method: processPoints
+     * Process a feed of point features, and create a linestring that connects
+     * the points. The linestring will be added as first feature in the array.
+     *
+     * Parameters:
+     * features - {Array(<OpenLayers.Feature.Vector)} Array of point features
+     *     with a pubDate attribute.
+     */
+    processPoints: function(features) {
+        var i, len = features.length,
+            feature,
+            components = new Array(len);
+        for (i=len-1; i>=0; --i) {
+            feature = features[i];
+            component = feature.geometry;
+            pubDate = OpenLayers.Date.smartParse(feature.attributes["pubDate"]);
+            component.dateTime = pubDate;
+            if (i < len-1) {
+                component.dateTime.duration = components[i+1].dateTime - pubDate;
+            }
+            components[i] = component;
+            // only add marker if it has something to say.
+            if(this.createFeatures === false || feature.attributes.title === "Untitled") {
+                features.splice(i, 1);
+            }
+        }
+        features.unshift(new OpenLayers.Feature.Vector(
+            new OpenLayers.Geometry.LineString(components),
+            {
+                title: "The Track",
+                description: "This track has been created by connecting the feed entries to a line."
+            }
+        ));
+    },
+    
+    CLASS_NAME: "OpenLayers.Strategy.TimeFeatures"
+    
+});
\ No newline at end of file

Modified: addins/timedpointtrack/trunk/lib/TimedPointTrack.js
===================================================================
--- addins/timedpointtrack/trunk/lib/TimedPointTrack.js	2012-01-26 15:20:30 UTC (rev 12457)
+++ addins/timedpointtrack/trunk/lib/TimedPointTrack.js	2012-01-30 23:08:54 UTC (rev 12458)
@@ -2,6 +2,7 @@
  * @requires OpenLayers/Map.js
  * @requires OpenLayers/Layer.js
  * @requires OpenLayers/Format/GeoRSS.js
+ * @requires OpenLayers/Format/GeoJSON.js
  */
 
 /**
@@ -29,7 +30,8 @@
             "OpenLayers/Control/TimeDisplay.js",
             "OpenLayers/Control/TimeSlider.js",
             "OpenLayers/Control/TimeSliderBar.js",
-            "OpenLayers/Layer/TimedPointTrack.js"
+            "OpenLayers/Layer/TimedPointTrack.js",
+            "OpenLayers/Strategy/TimeFeatures.js"
         );
 
         var allScriptTags = new Array(jsfiles.length);
@@ -230,6 +232,16 @@
         feature.fid = id;
         return feature;
     });
+    
+    var geoJsonRead = OpenLayers.Format.GeoJSON.prototype.read;
+    add(OpenLayers.Format.GeoJSON, "read", function(json, type, filter) {
+        if (typeof json == "string") {
+            json = OpenLayers.Format.JSON.prototype.read.apply(this,
+                                                              [json, filter]);
+        }
+        var features = geoJsonRead.apply(this, [json, type || json.type, filter]);
+        return (!type && json.type === "Feature") ? [features] : features;            
+    });
 
     /**
      * APIMethod: OpenLayers.Date.contains



More information about the Commits mailing list