[OpenLayers-Commits] r2386 - in sandbox/vector-2.4: lib/OpenLayers lib/OpenLayers/Geometry tests

commits at openlayers.org commits at openlayers.org
Tue Mar 6 11:51:14 EST 2007


Author: euzuro
Date: 2007-03-06 11:51:13 -0500 (Tue, 06 Mar 2007)
New Revision: 2386

Modified:
   sandbox/vector-2.4/lib/OpenLayers/Geometry.js
   sandbox/vector-2.4/lib/OpenLayers/Geometry/Collection.js
   sandbox/vector-2.4/lib/OpenLayers/Geometry/Curve.js
   sandbox/vector-2.4/lib/OpenLayers/Geometry/Point.js
   sandbox/vector-2.4/tests/test_Geometry.html
Log:
split the getBounds() into getBounds() and calculateBounds() to perfect the cacheing mechanism. also add the clone() call in several important parts of the code to protect klobbering amongst itself

Modified: sandbox/vector-2.4/lib/OpenLayers/Geometry/Collection.js
===================================================================
--- sandbox/vector-2.4/lib/OpenLayers/Geometry/Collection.js	2007-03-06 16:47:30 UTC (rev 2385)
+++ sandbox/vector-2.4/lib/OpenLayers/Geometry/Collection.js	2007-03-06 16:51:13 UTC (rev 2386)
@@ -62,16 +62,15 @@
         return this.components;
     },
 
-    /**
-     * @type OpenLayers.Bounds
+    /** Recalculate the bounds by iterating through each item in the collection
+     *   and calling extendBounds()
+     * 
      */
-    getBounds: function () {
-        if (!this.bounds) {
-            for (var i = 0; i < this.components.length; i++) {
-                this.extendBounds(this.components[i].getBounds());
-            }
+    calculateBounds: function() {
+        this.bounds = null;
+        for (var i = 0; i < this.components.length; i++) {
+            this.extendBounds(this.components[i].getBounds());
         }
-        return this.bounds;
     },
 
     /**

Modified: sandbox/vector-2.4/lib/OpenLayers/Geometry/Curve.js
===================================================================
--- sandbox/vector-2.4/lib/OpenLayers/Geometry/Curve.js	2007-03-06 16:47:30 UTC (rev 2385)
+++ sandbox/vector-2.4/lib/OpenLayers/Geometry/Curve.js	2007-03-06 16:51:13 UTC (rev 2386)
@@ -27,12 +27,10 @@
         
         if (points && points instanceof Array) {
             for (var i = 0; i < points.length; i++) {
-                this.addPoint(points[i]);
+                this.addPoint(points[i].clone());
             }
         }
         // TODO: check to see if the path has more than 1 point?
-                
-        this.bounds = this.getBounds();
     },
     
     /**
@@ -51,35 +49,15 @@
         return obj;
     },
 
-    /**
-     * Get the bounds for this Geometry. The bounds will be null if has
-     * not been set.
-     * Once the bounds is set, it is not calculated again, this makes queries
-     * faster.
-     * @return {OpenLayers.Bounds}
+    /** Recalculate the bounds by iterating through each item in the path
+     *   and calling extendBounds()
+     * 
      */
-    getBounds: function(){
-        var bounds = null;
-        if (this.path.length > 0) {
-            var xmin, ymin, xmax, ymax = null;
-            for (var i=0; i < this.path.length; i++) {
-                var point = this.path[i];
-                if (point.lon < xmin || xmin == null) {
-                    xmin = point.lon;
-                }
-                if (point.lon > xmax || xmax == null) {
-                    xmax = point.lon;
-                }
-                if (point.lat < ymin || ymin == null) {
-                    ymin = point.lat;
-                }
-                if (point.lat > ymax || ymax == null) {
-                    ymax = point.lat;
-                }
-            }
-            bounds = new OpenLayers.Bounds(xmin, ymin, xmax, ymax);
+    calculateBounds: function(){
+        this.bounds = null;
+        for (var i = 0; i < this.path.length; i++) {
+            this.extendBounds(this.path[i].getBounds());
         }
-        return bounds;
     },
 
     /**
@@ -110,6 +88,11 @@
      */
     removePoint: function(point){
         this.path = OpenLayers.Util.removeItem(this.path, point);
+
+        // reset this.bounds so that it gets recalculated on the next call
+        // to this.getBounds();
+        this.bounds = null;
+
         // TODO: does the simple equality test in removeItem() actually make sense here?
         // TODO: check to see if the path has more than 1 point?
     },

Modified: sandbox/vector-2.4/lib/OpenLayers/Geometry/Point.js
===================================================================
--- sandbox/vector-2.4/lib/OpenLayers/Geometry/Point.js	2007-03-06 16:47:30 UTC (rev 2385)
+++ sandbox/vector-2.4/lib/OpenLayers/Geometry/Point.js	2007-03-06 16:51:13 UTC (rev 2386)
@@ -28,7 +28,6 @@
     	OpenLayers.LonLat.prototype.initialize.apply(this, arguments);
     	OpenLayers.Geometry.prototype.initialize.apply(this, arguments);
     	
-    	this.id = OpenLayers.Util.createUniqueID(this.CLASS_NAME + "_");
     	this.x = this.lon;
     	this.y = this.lat;
     },
@@ -82,12 +81,12 @@
 	   return this.lat;
     },
 
-    /**
-    * @type OpenLayers.Bounds
-    */
-    getBounds: function () {
-        return new OpenLayers.Bounds( this.lon, this.lat,
-                                      this.lon, this.lat );
+    /** Create a new Bounds based on the lon/lat
+     * 
+     */
+    calculateBounds: function () {
+        this.bounds = new OpenLayers.Bounds(this.lon, this.lat,
+                                            this.lon, this.lat);
     },
 
     /**

Modified: sandbox/vector-2.4/lib/OpenLayers/Geometry.js
===================================================================
--- sandbox/vector-2.4/lib/OpenLayers/Geometry.js	2007-03-06 16:47:30 UTC (rev 2385)
+++ sandbox/vector-2.4/lib/OpenLayers/Geometry.js	2007-03-06 16:51:13 UTC (rev 2386)
@@ -53,7 +53,9 @@
      * @param {OpenLayers.Bounds} object
      */
     setBounds: function(bounds) {
-        this.bounds = bounds;
+        if (bounds) {
+            this.bounds = bounds.clone();
+        }
     },
     
     /**
@@ -72,16 +74,27 @@
     
     /**
      * Get the bounds for this Geometry. The bounds will be null if not it has
-     * not been set.
-     * Once the bounds is set, it is not calculated again, this makes queries
-     * faster.
+     * not been set. Once the bounds is set, it is not calculated again, 
+     * this makes queries faster.
      * 
      * @type OpenLayers.Bounds
      */
     getBounds: function() {
+        if (this.bounds == null) {
+            this.calculateBounds();
+        }
         return this.bounds;
     },
     
+    /** Recalculate the bounds for the geometry. 
+     * 
+     */
+    calculateBounds: function() {
+        //
+        // This should be overridden by subclasses.
+        //
+    },
+    
     /**
      * Note: This is only an approximation based on the bounds of the 
      * geometry.

Modified: sandbox/vector-2.4/tests/test_Geometry.html
===================================================================
--- sandbox/vector-2.4/tests/test_Geometry.html	2007-03-06 16:47:30 UTC (rev 2385)
+++ sandbox/vector-2.4/tests/test_Geometry.html	2007-03-06 16:51:13 UTC (rev 2386)
@@ -24,11 +24,12 @@
         t.ok(g.bounds == null, "setbounds with null value does not crash or set bounds");
       
       //no classname object
-        var object = new Object();
-        
+        g_clone = {};
+        var object = {
+            'clone': function() { return g_clone; }
+        };
         g.setBounds(object);
-        t.ok(g.bounds == object, "setbounds with valid object sets bounds");
-      
+        t.ok(g.bounds == g_clone, "setbounds with valid object sets bounds, calls clone");
     }
 
     function test_03_Geometry_extendBounds(t) {



More information about the Commits mailing list