[OpenLayers-Commits] r7484 - in sandbox/vector-behavior/lib/OpenLayers: Layer Strategy

commits at openlayers.org commits at openlayers.org
Wed Jul 9 11:29:15 EDT 2008


Author: elemoine
Date: 2008-07-09 11:29:15 -0400 (Wed, 09 Jul 2008)
New Revision: 7484

Modified:
   sandbox/vector-behavior/lib/OpenLayers/Layer/Vector.js
   sandbox/vector-behavior/lib/OpenLayers/Strategy/Grid.js
Log:
deal with features spanning multiple tiles


Modified: sandbox/vector-behavior/lib/OpenLayers/Layer/Vector.js
===================================================================
--- sandbox/vector-behavior/lib/OpenLayers/Layer/Vector.js	2008-07-09 14:50:32 UTC (rev 7483)
+++ sandbox/vector-behavior/lib/OpenLayers/Layer/Vector.js	2008-07-09 15:29:15 UTC (rev 7484)
@@ -112,6 +112,12 @@
      * {Array(<OpenLayers.Feature.Vector>)} 
      */
     features: null,
+
+    /**
+     * APIProperty: featureMap
+     * {Object} A feature fid to <OpenLayers.Feature.Vector> feature map.
+     */
+    featureMap: null,
     
     /** 
      * Property: selectedFeatures
@@ -205,6 +211,7 @@
         }
 
         this.features = [];
+        this.featureMap = {};
         this.selectedFeatures = [];
 
         // assumes if layer has strategy and protocol, then it has a format
@@ -229,6 +236,7 @@
     destroy: function() {
         this.destroyFeatures();
         this.features = null;
+        this.featureMap = null;
         this.selectedFeatures = null;
         if (this.renderer) {
             this.renderer.destroy();
@@ -413,6 +421,7 @@
               }
 
             this.features.push(feature);
+            this.featureMap[feature.fid] = feature;
             
             //give feature reference to its layer
             feature.layer = this;
@@ -474,6 +483,7 @@
             }
 
             this.features = OpenLayers.Util.removeItem(this.features, feature);
+            delete this.featureMap[feature.fid];
 
             if (feature.geometry) {
                 this.renderer.eraseGeometry(feature.geometry);

Modified: sandbox/vector-behavior/lib/OpenLayers/Strategy/Grid.js
===================================================================
--- sandbox/vector-behavior/lib/OpenLayers/Strategy/Grid.js	2008-07-09 14:50:32 UTC (rev 7483)
+++ sandbox/vector-behavior/lib/OpenLayers/Strategy/Grid.js	2008-07-09 15:29:15 UTC (rev 7484)
@@ -31,6 +31,18 @@
     tileSize: null,
 
     /**
+     * Property: safe
+     * {Boolean} If true the strategy makes sure it does not add multiple
+     * features with the same fid to the layer, this can occur if features
+     * span multiple grid cells. If true the strategy also makes sure it
+     * does not remove a feature that should still be visible because it
+     * spans multiple grid cells. If false, no such care is taken. For
+     * point features you may want to set it to false and get better
+     * performance. Defaults to true.
+     */
+    safe: true,
+
+    /**
      * Class: tileClass
      * Inner class to represent tiles
      */
@@ -72,7 +84,22 @@
          */
         destroyFeatures: function() {
             if (this.features.length > 0) {
-                this.strategy.layer.destroyFeatures(this.features);
+                var toDestroy = null;
+                if (this.strategy.safe) {
+                    var tileFeature;
+                    for (var i = 0; i < this.features.length; i++) {
+                        tileFeature = this.features[i];
+                        if (--tileFeature.refCnt == 0) {
+                            toDestroy = toDestroy || [];
+                            toDestroy.push(tileFeature);
+                        }
+                    }
+                } else {
+                    toDestroy = this.features;
+                }
+                if (toDestroy && toDestroy.length > 0) {
+                    this.strategy.layer.destroyFeatures(this.features);
+                }
             }
             this.features = [];
         },
@@ -100,8 +127,26 @@
                     if (resp.code == OpenLayers.Protocol.Response.SUCCESS) {
                         var features = resp.features;
                         if (features && features.length > 0) {
+                            var toAdd = null;
+                            if (this.strategy.safe) {
+                                var fid, inFeature;
+                                for (var i = 0; i < features.length; i++) {
+                                    inFeature = this.strategy.layer.featureMap[features[i].fid];
+                                    if (!inFeature) {
+                                        toAdd = toAdd || [];
+                                        inFeature = features[i];
+                                        toAdd.push(inFeature);
+                                        inFeature.refCnt = 0;
+                                    }
+                                    inFeature.refCnt++;
+                                }
+                            } else {
+                                toAdd = features;
+                            }
                             this.features = features;
-                            this.strategy.layer.addFeatures(features);
+                            if (toAdd && toAdd.length > 0) {
+                                this.strategy.layer.addFeatures(toAdd);
+                            }
                         }
                     } else {
                         // FIXME



More information about the Commits mailing list