OpenLayers OpenLayers

Changeset 2931

Show
Ignore:
Timestamp:
03/30/07 17:42:32 (2 years ago)
Author:
sderle
Message:

Tim's big refactoring of the Geometry modules. Fixes #590. All tests pass in FF (except the PanZoomBar stuff, which wasn't touched by this patch) and IE.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/lib/OpenLayers/Geometry/Collection.js

    r2901 r2931  
    2525    /** @type Array(OpenLayers.Geometry) */ 
    2626    components: null, 
     27     
     28    /** 
     29     * An array of class names representing the types of components that 
     30     * the collection can include.  A null value means the component types 
     31     * are not restricted. 
     32     * @type Array(String) 
     33     */ 
     34    componentTypes: null, 
    2735 
    2836    /** 
     
    6068     * @type OpenLayers.Geometry.Collection 
    6169     */ 
    62     clone: function (obj) { 
    63         if (obj == null) { 
    64             obj = eval("new " + this.CLASS_NAME + "()"); 
    65         } 
    66          
    67         for (var i = 0; i < this.components.length; i++) { 
    68             obj.addComponent(this.components[i].clone()); 
     70    clone: function() { 
     71        var geometry = eval("new " + this.CLASS_NAME + "()"); 
     72        for(var i=0; i<this.components.length; i++) { 
     73            geometry.addComponent(this.components[i].clone()); 
    6974        } 
    7075         
    7176        // catch any randomly tagged-on properties 
    72         OpenLayers.Util.applyDefaults(obj, this); 
    73          
    74         return obj
     77        OpenLayers.Util.applyDefaults(geometry, this); 
     78         
     79        return geometry
    7580    }, 
    7681 
     
    122127    }, 
    123128 
    124     /** Add a new component (geometry) to the collection.  
     129    /** 
     130     * Add a new component (geometry) to the collection.  If this.componentTypes 
     131     * is set, then the component class name must be in the componentTypes array. 
    125132     *  
    126133     * The bounds cache is reset. 
     
    128135     * @param {OpenLayers.Geometry} component 
    129136     * @param {int} index Index into the array to insert the component 
     137     * @type Boolean 
     138     * @return Component was successfully added 
    130139    */     
    131140    addComponent: function(component, index) { 
    132         if (component) { 
    133  
    134             if (index) { 
    135                 var components1 = this.components.slice(0, index); 
    136                 var components2 = this.components.slice(index,  
    137                                                        this.components.length); 
    138                 components1.push(component); 
    139                 this.components = components1.concat(components2); 
    140             } else { 
    141                 this.components.push(component); 
     141        var added = false; 
     142        if(component) { 
     143            if(this.componentTypes == null || 
     144               (OpenLayers.Util.indexOf(this.componentTypes, 
     145                                        component.CLASS_NAME) > -1)) { 
     146 
     147                if(index != null && (index < this.components.length)) { 
     148                    var components1 = this.components.slice(0, index); 
     149                    var components2 = this.components.slice(index,  
     150                                                           this.components.length); 
     151                    components1.push(component); 
     152                    this.components = components1.concat(components2); 
     153                } else { 
     154                    this.components.push(component); 
     155                } 
     156                component.parent = this; 
     157                this.clearBounds(); 
     158                added = true; 
    142159            } 
    143             component.parent = this; 
    144             this.clearBounds(); 
    145         } 
     160        } 
     161        return added; 
    146162    }, 
    147163     
     
    206222    }, 
    207223 
     224    /** 
     225     * Tests for equivalent geometries 
     226     * @param {OpenLayers.Geometry} 
     227     * @type Boolean 
     228     * @return The coordinates are equivalent 
     229     */ 
     230    equals: function(geometry) { 
     231        var equivalent = true; 
     232        if(!geometry.CLASS_NAME || (this.CLASS_NAME != geometry.CLASS_NAME)) { 
     233            equivalent = false; 
     234        } else if(!(geometry.components instanceof Array) || 
     235                  (geometry.components.length != this.components.length)) { 
     236            equivalent = false; 
     237        } else { 
     238            for(var i=0; i<this.components.length; ++i) { 
     239                if(!this.components[i].equals(geometry.components[i])) { 
     240                    equivalent = false; 
     241                    break; 
     242                } 
     243            } 
     244        } 
     245        return equivalent; 
     246    }, 
     247 
    208248    /** @final @type String */ 
    209249    CLASS_NAME: "OpenLayers.Geometry.Collection" 
  • trunk/openlayers/lib/OpenLayers/Geometry/Curve.js

    r2803 r2931  
    1717 
    1818    /** 
     19     * An array of class names representing the types of components that 
     20     * the collection can include.  A null value means the component types 
     21     * are not restricted. 
     22     * @type Array(String) 
     23     */ 
     24    componentTypes: ["OpenLayers.Geometry.Point"], 
     25 
     26    /** 
    1927     * @constructor 
    2028     * 
     
    2432        OpenLayers.Geometry.MultiPoint.prototype.initialize.apply(this,  
    2533                                                                  arguments); 
    26     }, 
    27      
    28     /** 
    29      * @returns An exact clone of this OpenLayers.Feature 
    30      * @type OpenLayers.Feature 
    31      */ 
    32     clone: function (obj) { 
    33         if (obj == null) { 
    34             obj = new OpenLayers.Geometry.Curve(); 
    35         } 
    36          
    37         obj = OpenLayers.Geometry.Collection.prototype.clone.apply(this,  
    38                                                                    [obj]); 
    39         return obj; 
    4034    }, 
    4135     
  • trunk/openlayers/lib/OpenLayers/Geometry/LineString.js

    r2920 r2931  
    2121     */ 
    2222    initialize: function(points) { 
    23         OpenLayers.Geometry.Curve.prototype.initialize.apply(this,  
    24                                                                   arguments);         
    25     }, 
    26  
    27     /** 
    28      * @returns An exact clone of this OpenLayers.Feature 
    29      * @type OpenLayers.Feature 
    30      */ 
    31     clone: function (obj) { 
    32         if (obj == null) { 
    33             obj = new OpenLayers.Geometry.LineString(); 
    34         } 
    35          
    36         for (var i = 0; i < this.components.length; i++) { 
    37             obj.addComponent(this.components[i].clone()); 
    38         } 
    39          
    40         return obj; 
     23        OpenLayers.Geometry.Curve.prototype.initialize.apply(this, arguments);         
    4124    }, 
    4225 
     
    4831    removeComponent: function(point) { 
    4932        if ( this.components && (this.components.length > 2)) { 
    50             OpenLayers.Geometry.Curve.prototype.removeComponent.apply(this,  
     33            OpenLayers.Geometry.Collection.prototype.removeComponent.apply(this,  
    5134                                                                  arguments); 
    5235        } 
  • trunk/openlayers/lib/OpenLayers/Geometry/LinearRing.js

    r2901 r2931  
    2020 
    2121    /** 
     22     * An array of class names representing the types of components that 
     23     * the collection can include.  A null value means the component types 
     24     * are not restricted. 
     25     * @type Array(String) 
     26     */ 
     27    componentTypes: ["OpenLayers.Geometry.Point"], 
     28 
     29    /** 
     30     * Linear rings are constructed with an array of points.  This array 
     31     * can represent a closed or open ring.  If the ring is open (the last 
     32     * point does not equal the first point), the constructor will close 
     33     * the ring.  If the ring is already closed (the last point does equal 
     34     * the first point), it will be left closed. 
     35     *  
    2236     * @constructor 
    23      * 
    2437     * @param {Array(OpenLayers.Geometry.Point)} points 
    2538     */ 
     
    2841                                                                  arguments); 
    2942    }, 
    30      
     43 
    3144    /** 
    32      * @returns An exact clone of this OpenLayers.Geometry.LinearRing 
    33      * @type OpenLayers.Geometry.LinearRing 
    34      */ 
    35     clone: function (obj) { 
    36         if (obj == null) { 
    37             obj = new OpenLayers.Geometry.LinearRing(); 
    38         } 
    39          
    40         for (var i = 0; i < this.components.length; i++) { 
    41             obj.addComponent(this.components[i].clone()); 
    42         } 
    43          
    44         return obj; 
    45     }, 
    46      
    47     /** 
    48      * Adds a point to geometry components 
     45     * Adds a point to geometry components.  If the point is to be added to 
     46     * the end of the components array and it is the same as the last point 
     47     * already in that array, the duplicate point is not added.  This has the 
     48     * effect of closing the ring if it is not already closed, and doing the 
     49     * right thing if it is already closed.  This behavior can be overridden 
     50     * by calling the method with a non-null index as the second argument. 
    4951     * 
    5052     * @param {OpenLayers.Geometry.Point} point 
    5153     * @param {int} index Index into the array to insert the component 
    52      */  
     54     * @type Boolean 
     55     * @return Point was successfully added 
     56     */ 
    5357    addComponent: function(point, index) { 
     58        var added = false; 
     59 
    5460        //remove last point 
    5561        var lastPoint = this.components[this.components.length-1]; 
    56         OpenLayers.Geometry.Curve.prototype.removeComponent.apply(this,  
    57                                                                   [lastPoint]); 
    58   
    59         //add our point 
    60         OpenLayers.Geometry.LineString.prototype.addComponent.apply(this,  
    61                                                                     arguments); 
     62        OpenLayers.Geometry.Collection.prototype.removeComponent.apply(this,  
     63                                                              [lastPoint]); 
     64 
     65        // given an index, add the point 
     66        // without an index only add non-duplicate points 
     67        if(index != null || !point.equals(lastPoint)) { 
     68            added = OpenLayers.Geometry.Collection.prototype.addComponent.apply(this,  
     69                                                                        arguments); 
     70        } 
     71 
    6272        //append copy of first point 
    6373        var firstPoint = this.components[0]; 
    64         OpenLayers.Geometry.Curve.prototype.addComponent.apply(this,  
     74        OpenLayers.Geometry.Collection.prototype.addComponent.apply(this,  
    6575                                                         [firstPoint.clone()]); 
     76 
     77        return added; 
    6678    }, 
    6779     
     
    7688            //remove last point 
    7789            var lastPoint = this.components[this.components.length-1]; 
    78             OpenLayers.Geometry.Curve.prototype.removeComponent.apply(this,  
     90            OpenLayers.Geometry.Collection.prototype.removeComponent.apply(this,  
    7991                                                                 [lastPoint]); 
    8092             
    8193            //remove our point 
    82             OpenLayers.Geometry.LineString.prototype.removeComponent.apply(this,  
     94            OpenLayers.Geometry.Collection.prototype.removeComponent.apply(this,  
    8395                                                                    arguments); 
    8496            //append copy of first point 
    8597            var firstPoint = this.components[0]; 
    86             OpenLayers.Geometry.Curve.prototype.addComponent.apply(this,  
     98            OpenLayers.Geometry.Collection.prototype.addComponent.apply(this,  
    8799                                                         [firstPoint.clone()]); 
    88100        } 
  • trunk/openlayers/lib/OpenLayers/Geometry/MultiLineString.js

    r2920 r2931  
    1515 
    1616    /** 
     17     * An array of class names representing the types of components that 
     18     * the collection can include.  A null value means the component types 
     19     * are not restricted. 
     20     * @type Array(String) 
     21     */ 
     22    componentTypes: ["OpenLayers.Geometry.LineString"], 
     23 
     24    /** 
    1725     * @constructor 
    1826     * 
     
    2432    }, 
    2533 
    26     /** 
    27      * adds a component to the MultiPoint, checking type 
    28      * 
    29      * @param {OpenLayers.Geometry.LineString} component lineString to add 
    30      * @param {int} index Index into the array to insert the component 
    31      */ 
    32     addComponent: function(component, index) { 
    33         if (!(component instanceof OpenLayers.Geometry.LineString)) { 
    34             throw "component should be an OpenLayers.Geometry.LineString"; 
    35         } 
    36         OpenLayers.Geometry.Collection.prototype.addComponent.apply(this,  
    37                                                                     arguments);         
    38     }, 
    39  
    4034    /** @final @type String */ 
    4135    CLASS_NAME: "OpenLayers.Geometry.MultiLineString" 
  • trunk/openlayers/lib/OpenLayers/Geometry/MultiPoint.js

    r2920 r2931  
    1515 
    1616    /** 
     17     * An array of class names representing the types of components that 
     18     * the collection can include.  A null value means the component types 
     19     * are not restricted. 
     20     * @type Array(String) 
     21     */ 
     22    componentTypes: ["OpenLayers.Geometry.Point"], 
     23 
     24    /** 
    1725     * @constructor 
    1826     * 
     
    2230        OpenLayers.Geometry.Collection.prototype.initialize.apply(this,  
    2331                                                                  arguments); 
    24     }, 
    25  
    26     /** 
    27      * adds component to the MultiPoint, checking type 
    28      * 
    29      * @param {OpenLayers.Geometry.Point} component point to add 
    30      * @param {int} index Index into the array to insert the component 
    31      */ 
    32     addComponent: function(component, index) { 
    33         if (!(component instanceof OpenLayers.Geometry.Point)) { 
    34             throw "component should be an OpenLayers.Geometry.Point"; 
    35         } 
    36         OpenLayers.Geometry.Collection.prototype.addComponent.apply(this,  
    37                                                                     arguments); 
    3832    }, 
    3933 
  • trunk/openlayers/lib/OpenLayers/Geometry/MultiPolygon.js

    r2920 r2931  
    1515 
    1616    /** 
     17     * An array of class names representing the types of components that 
     18     * the collection can include.  A null value means the component types 
     19     * are not restricted. 
     20     * @type Array(String) 
     21     */ 
     22    componentTypes: ["OpenLayers.Geometry.Polygon"], 
     23 
     24    /** 
    1725    * @constructor 
    1826    * 
     
    2331                                                                  arguments); 
    2432    }, 
    25  
    26     /** 
    27      * adds component to the MultiPolygon, checking type 
    28      * 
    29      * @param {OpenLayers.Geometry.Polygon} component Polygon to add 
    30      * @param {int} index Index into the array to insert the component 
    31      */ 
    32     addComponent: function(component, index) { 
    33         if (!(component instanceof OpenLayers.Geometry.Polygon)) { 
    34             var throwStr = "component should be an " + 
    35                            "OpenLayers.Geometry.Polygon but is an " +  
    36                            component.CLASS_NAME; 
    37             throw throwStr; 
    38         } 
    39         OpenLayers.Geometry.Collection.prototype.addComponent.apply(this,  
    40                                                                     arguments); 
    41     }, 
    4233     
    4334    /** @final @type String */ 
  • trunk/openlayers/lib/OpenLayers/Geometry/Polygon.js

    r2920 r2931  
    1818 
    1919    /** 
     20     * An array of class names representing the types of components that 
     21     * the collection can include.  A null value means the component types 
     22     * are not restricted. 
     23     * @type Array(String) 
     24     */ 
     25    componentTypes: ["OpenLayers.Geometry.LinearRing"], 
     26 
     27    /** 
    2028     * @constructor 
    2129     * 
     
    2533        OpenLayers.Geometry.Collection.prototype.initialize.apply(this,  
    2634                                                                  arguments); 
    27     }, 
    28  
    29     /** 
    30      * adds a component to the Polygon, checking type 
    31      * 
    32      * @param {OpenLayers.Geometry.LinearRing} point to add 
    33      * @param {int} index Index into the array to insert the component 
    34      */ 
    35     addComponent: function(component, index) { 
    36         if (!(component instanceof OpenLayers.Geometry.LinearRing)) { 
    37             var throwStr = "component should be an " + 
    38                            "OpenLayers.Geometry.LinearRing but is a " +  
    39                            component.CLASS_NAME; 
    40             throw throwStr; 
    41                             
    42         } 
    43         OpenLayers.Geometry.Collection.prototype.addComponent.apply(this,  
    44                                                                     arguments); 
    4535    }, 
    4636     
  • trunk/openlayers/lib/OpenLayers/Handler/Path.js

    r2803 r2931  
    8080     
    8181    /** 
    82      * Add point to geometry 
     82     * Add point to geometry.  Send the point index to override 
     83     * the behavior of LinearRing that disregards adding duplicate points. 
    8384     */ 
    8485    addPoint: function() { 
    85         this.line.addComponent(this.point.clone()); 
     86        this.line.addComponent(this.point.clone(), this.line.components.length); 
    8687    }, 
    8788     
  • trunk/openlayers/lib/OpenLayers/Handler/Polygon.js

    r2803 r2931  
    8787    }, 
    8888 
     89    /** 
     90     * Handle double-clicks.  Finish the geometry and send it back 
     91     * to the control. 
     92     *  
     93     * @param {Event} evt 
     94     */ 
     95    dblclick: function(evt) { 
     96        if(!this.freehandMode(evt)) { 
     97            // remove the penultimate point 
     98            var index = this.line.components.length - 2; 
     99            this.line.removeComponent(this.line.components[index]); 
     100            this.finalize(this.line); 
     101        } 
     102        return false; 
     103    }, 
     104 
    89105    /** @final @type String */ 
    90106    CLASS_NAME: "OpenLayers.Handler.Polygon" 
  • trunk/openlayers/tests/Geometry/test_Collection.html

    r2803 r2931  
    143143     
    144144    function test_07_Collection_addComponent(t)   { 
    145         t.plan(3); 
    146          
    147         var coll = new OpenLayers.Geometry.Collection(); 
    148          
    149       //null 
     145        t.plan(10); 
     146         
     147        var coll = new OpenLayers.Geometry.Collection(); 
     148         
     149        //null 
    150150        coll.addComponent(null); 
    151         t.ok(true, "no breakage, no executage from null input")         
    152  
    153       //good component 
     151        t.ok(!coll.addComponent(null), 
     152             "addComponent returns false for bad component")         
     153 
     154        //good component 
    154155        var component = new OpenLayers.Geometry.Point(3,4); 
    155         coll.addComponent(component);       
    156           
     156        t.ok(coll.addComponent(component), 
     157             "addComponent returns true for good component"); 
    157158        t.ok(coll.bounds == null, "bounds cache correctly cleared");          
    158159          
     
    164165        } 
    165166        t.ok(foundComponent, "component added to internal array"); 
     167         
     168        // restricted components 
     169        coll.componentTypes = ["OpenLayers.Geometry.Point", 
     170                               "OpenLayers.Geometry.LineString"]; 
     171        var point1 = new OpenLayers.Geometry.Point(0,0); 
     172        var point2 = new OpenLayers.Geometry.Point(1,1); 
     173        var line = new OpenLayers.Geometry.LineString([point1, point2]); 
     174        var multipoint = new OpenLayers.Geometry.MultiPoint([point1, point2]); 
     175         
     176        t.ok(coll.addComponent(point1), 
     177             "addComponent returns true for 1st geometry type in componentTypes"); 
     178        t.ok(OpenLayers.Util.indexOf(coll.components, point1) > -1, 
     179             "addComponent adds 1st restricted type to components array"); 
     180        t.ok(coll.addComponent(line), 
     181             "addComponent returns true for 2nd geometry type in componentTypes"); 
     182        t.ok(OpenLayers.Util.indexOf(coll.components, point1) > -1, 
     183             "addComponent adds 2nd restricted type to components array"); 
     184        t.ok(!coll.addComponent(multipoint), 
     185             "addComponent returns false for geometry type not in componentTypes"); 
     186        t.ok(OpenLayers.Util.indexOf(coll.components, multipoint) == -1, 
     187             "addComponent doesn't add restricted type to component array"); 
    166188 
    167189    } 
  • trunk/openlayers/tests/Geometry/test_LineString.html

    r2803 r2931  
    3535        t.plan(2); 
    3636         
    37         OpenLayers.Geometry.Curve.prototype._removeComponent =  
    38             OpenLayers.Geometry.Curve.prototype.removeComponent; 
    39         OpenLayers.Geometry.Curve.prototype.removeComponent =  
     37        OpenLayers.Geometry.Collection.prototype._removeComponent =  
     38            OpenLayers.Geometry.Collection.prototype.removeComponent; 
     39        OpenLayers.Geometry.Collection.prototype.removeComponent =  
    4040            function(point) { g_removeComponent = point; }; 
    4141         
     
    5050        t.ok(g_removeComponent, components[0], "point removed if 3 points in components"); 
    5151         
    52         OpenLayers.Geometry.Curve.prototype.removeComponent =  
    53             OpenLayers.Geometry.Curve.prototype._removeComponent; 
     52        OpenLayers.Geometry.Collection.prototype.removeComponent =  
     53            OpenLayers.Geometry.Collection.prototype._removeComponent; 
    5454    } 
    5555     
     
    7474    } 
    7575 
     76    function test_LineString_equals(t) { 
     77        t.plan(3); 
     78         
     79        var x0 = Math.random() * 100; 
     80        var y0 = Math.random() * 100; 
     81        var x1 = Math.random() * 100; 
     82        var y1 = Math.random() * 100; 
     83        var point0 = new OpenLayers.Geometry.Point(x0, y0); 
     84        var point1 = new OpenLayers.Geometry.Point(x1, y1); 
     85        var geometry = new OpenLayers.Geometry.LineString([point0, point1]); 
     86        var equal = new OpenLayers.Geometry.LineString([point0, point1]); 
     87        var offX =  new OpenLayers.Geometry.LineString([ 
     88                            new OpenLayers.Geometry.Point(x0 + 1, y0), 
     89                            new OpenLayers.Geometry.Point(x1 + 1, y1)]); 
     90        var offY =  new OpenLayers.Geometry.LineString([ 
     91                            new OpenLayers.Geometry.Point(x0, y0 + 1), 
     92                            new OpenLayers.Geometry.Point(x1, y1 + 1)]); 
     93        t.ok(geometry.equals(equal), 
     94             "equals() returns true for a geometry with equivalent coordinates"); 
     95        t.ok(!geometry.equals(offX), 
     96             "equals() returns false for a geometry with offset x"); 
     97        t.ok(!geometry.equals(offY), 
     98             "equals() returns false for a geometry with offset y"); 
     99    } 
     100     
     101    function test_LineString_clone(t) { 
     102        t.plan(2); 
     103         
     104        var x0 = Math.random() * 100; 
     105        var y0 = Math.random() * 100; 
     106        var x1 = Math.random() * 100; 
     107        var y1 = Math.random() * 100; 
     108        var point0 = new OpenLayers.Geometry.Point(x0, y0); 
     109        var point1 = new OpenLayers.Geometry.Point(x1, y1); 
     110        var geometry = new OpenLayers.Geometry.LineString([point0, point1]); 
     111        var clone = geometry.clone(); 
     112        t.ok(clone instanceof OpenLayers.Geometry.LineString, 
     113             "clone() creates an OpenLayers.Geometry.LineString"); 
     114        t.ok(geometry.equals(clone), "clone has equivalent coordinates"); 
     115    } 
    76116         
    77117  // --> 
  • trunk/openlayers/tests/Geometry/test_LinearRing.html

    r2899 r2931  
    2424     
    2525    function test_02_LinearRing_addComponent(t) { 
    26         t.plan(12); 
     26        t.plan(13); 
    2727         
    2828        var ring = new OpenLayers.Geometry.LinearRing(); 
    2929  
    3030        var point = new OpenLayers.Geometry.Point(0,0); 
    31         ring.addComponent( point ); 
     31        t.ok(ring.addComponent(point), 
     32             "addComponent returns true for 1st point"); 
    3233        t.eq(ring.components.length, 2, "add first point, correct length"); 
    3334        t.ok(ring.components[0].equals(point), "point one correct"); 
    3435        t.ok(ring.components[0].equals(ring.components[ring.components.length - 1]), "first and last point equal"); 
     36     
     37        newPoint = new OpenLayers.Geometry.Point(10,10); 
     38        t.ok(ring.addComponent( newPoint ), 
     39             "addComponent returns true for unique point"); 
     40        t.eq(ring.components.length, 3, "correctly adds 3rd point"); 
     41        t.ok(ring.components[0].equals(point), "point one correct"); 
     42        t.ok(ring.components[1].equals(newPoint), "point one correct"); 
     43        t.ok(ring.components[2].equals(ring.components[0]), "first and last point equal"); 
    3544         
    36         ring.addComponent( point ); 
    37         t.eq(ring.components.length, 3, "add second point, correct length"); 
    38         t.ok(ring.components[0].equals(point), "point one correct"); 
    39         t.ok(ring.components[1].equals(point), "point two correct"); 
    40         t.ok(ring.components[0].equals(ring.components[ring.components.length - 1]), "first and last point equal"); 
     45        var length = ring.components.length; 
     46        var clone = ring.components[length - 1].clone(); 
     47        t.ok(!ring.addComponent(clone), 
     48             "addComponent returns false for adding a duplicate last point"); 
     49        t.eq(ring.components.length, length, 
     50             "components remains unchanged after trying to add duplicate point"); 
     51        t.ok(ring.addComponent(clone, length - 1), 
     52             "addComponent returns true when adding a duplicate with an index"); 
     53        t.eq(ring.components.length, length + 1, 
     54             "components increase in length after adding a duplicate point with index"); 
    4155         
    42         newPoint = new OpenLayers.Geometry.Point(10,10); 
    43         ring.addComponent( newPoint ); 
    44         t.eq(ring.components.length, 4, "correctly adds 3rd point"); 
    45         t.ok(ring.components[0].equals(point), "point one correct"); 
    46         t.ok(ring.components[1].equals(point), "point two correct"); 
    47         t.ok(ring.components[2].equals(newPoint), "point one correct"); 
    48         t.ok(ring.components[0].equals(ring.components[ring.components.length - 1]), "first and last point equal"); 
    4956    } 
    5057     
  • trunk/openlayers/tests/Geometry/test_MultiPoint.html

    r2803 r2931  
    88    function test_01_MultiPoint_constructor (t) { 
    99        t.plan( 2 ); 
    10         multipoint = new OpenLayers.Geometry.MultiPoint(); 
     10        var multipoint = new OpenLayers.Geometry.MultiPoint(); 
    1111        t.ok( multipoint instanceof OpenLayers.Geometry.MultiPoint, "new OpenLayers.Geometry.MultiPoint returns multipoint object" ); 
    1212        t.eq( multipoint.CLASS_NAME, "OpenLayers.Geometry.MultiPoint", "multipoint.CLASS_NAME is set correctly"); 
     
    1515    function test_01a_MultiPoint_constructor (t) { 
    1616        t.plan( 3 ); 
    17         multipoint = new OpenLayers.Geometry.MultiPoint([point]); 
     17        var multipoint = new OpenLayers.Geometry.MultiPoint([point]); 
    1818        t.ok( multipoint instanceof OpenLayers.Geometry.MultiPoint, "new OpenLayers.Geometry.MultiPoint returns multipoint object" ); 
    1919        t.eq( multipoint.CLASS_NAME, "OpenLayers.Geometry.MultiPoint", "multipoint.CLASS_NAME is set correctly"); 
     
    2424        t.plan(4); 
    2525         
    26         multipoint = new OpenLayers.Geometry.MultiPoint([point]); 
     26        var multipoint = new OpenLayers.Geometry.MultiPoint([point]); 
    2727        var x = point.x; 
    2828        var y = point.y; 
     
    3737    } 
    3838 
     39    function test_MultiPoint_equals(t) { 
     40        t.plan(3); 
     41         
     42        var x = Math.random() * 100; 
     43        var y = Math.random() * 100; 
     44        var geometry = new OpenLayers.Geometry.MultiPoint( 
     45                                        [new OpenLayers.Geometry.Point(x, y)]); 
     46        var equal =  new OpenLayers.Geometry.MultiPoint( 
     47                                        [new OpenLayers.Geometry.Point(x, y)]);         
     48        var offX =  new OpenLayers.Geometry.MultiPoint( 
     49                                        [new OpenLayers.Geometry.Point(x + 1, y)]);         
     50        var offY =  new OpenLayers.Geometry.MultiPoint( 
     51                                        [new OpenLayers.Geometry.Point(x, y + 1)]);         
     52        t.ok(geometry.equals(equal), 
     53             "equals() returns true for a geometry with equivalent coordinates"); 
     54        t.ok(!geometry.equals(offX), 
     55             "equals() returns false for a geometry with offset x"); 
     56        t.ok(!geometry.equals(offY), 
     57             "equals() returns false for a geometry with offset y"); 
     58    } 
     59     
     60    function test_MultiPoint_clone(t) { 
     61        t.plan(2); 
     62         
     63        var x = Math.random() * 100; 
     64        var y = Math.random() * 100; 
     65        var geometry = new OpenLayers.Geometry.MultiPoint( 
     66                                        [new OpenLayers.Geometry.Point(x, y)]); 
     67        var clone = geometry.clone(); 
     68        t.ok(clone instanceof OpenLayers.Geometry.MultiPoint, 
     69             "clone() creates an OpenLayers.Geometry.MultiPoint"); 
     70        t.ok(geometry.equals(clone), "clone has equivalent coordinates"); 
     71    } 
     72 
    3973  // --> 
    4074  </script> 
  • trunk/openlayers/tests/Geometry/test_Point.html

    r2803 r2931  
    104104        t.eq(point.lat, y + dy, "move() correctly modifies lat"); 
    105105    } 
     106 
     107    function test_Point_equals(t) { 
     108        t.plan(3); 
     109         
     110        var x = Math.random() * 100; 
     111        var y = Math.random() * 100; 
     112        var geometry = new OpenLayers.Geometry.Point(x, y); 
     113        var equal = new OpenLayers.Geometry.Point(x, y); 
     114        var offX = new OpenLayers.Geometry.Point(x + 1, y); 
     115        var offY = new OpenLayers.Geometry.Point(x, y + 1); 
     116        t.ok(geometry.equals(equal), 
     117             "equals() returns true for a geometry with equivalent coordinates"); 
     118        t.ok(!geometry.equals(offX), 
     119             "equals() returns false for a geometry with offset x"); 
     120        t.ok(!geometry.equals(offY), 
     121             "equals() returns false for a geometry with offset y"); 
     122    } 
     123     
     124    function test_Point_clone(t) { 
     125        t.plan(2); 
     126         
     127        var x = Math.random() * 100; 
     128        var y = Math.random() * 100; 
     129        var geometry = new OpenLayers.Geometry.Point(x, y); 
     130        var clone = geometry.clone(); 
     131        t.ok(clone instanceof OpenLayers.Geometry.Point, 
     132             "clone() creates an OpenLayers.Geometry.Point"); 
     133        t.ok(geometry.equals(clone), "clone has equivalent coordinates"); 
     134    } 
    106135     
    107136  // --> 
  • trunk/openlayers/tests/Geometry/test_Polygon.html

    r2862 r2931  
    106106        t.eq(polygon.components[1].components[0].y, y2 + dy, "move() correctly modifies second y"); 
    107107    } 
     108 
     109    function test_Polygon_equals(t) { 
     110        t.plan(3); 
     111         
     112        var x0 = Math.random() * 100; 
     113        var y0 = Math.random() * 100; 
     114        var x1 = Math.random() * 100; 
     115        var y1 = Math.random() * 100; 
     116        var x2 = Math.random() * 100; 
     117        var y2 = Math.random() * 100; 
     118        var point0 = new OpenLayers.Geometry.Point(x0, y0); 
     119        var point1 = new OpenLayers.Geometry.Point(x1, y1); 
     120        var point2 = new OpenLayers.Geometry.Point(x2, y2); 
     121        var pointX = new OpenLayers.Geometry.Point(x0 + 1, y0); 
     122        var pointY = new OpenLayers.Geometry.Point(x0, y0 + 1); 
     123        var geometry = new OpenLayers.Geometry.Polygon([ 
     124                            new OpenLayers.Geometry.LinearRing([point0, point1, point2])]); 
     125        var equal = new OpenLayers.Geometry.Polygon([ 
     126                            new OpenLayers.Geometry.LinearRing([point0, point1, point2])]); 
     127        var offX = new OpenLayers.Geometry.Polygon([ 
     128                            new OpenLayers.Geometry.LinearRing([pointX, point1, point2])]); 
     129        var offY = new OpenLayers.Geometry.Polygon([ 
     130                            new OpenLayers.Geometry.LinearRing([pointY, point1, point2])]); 
     131        t.ok(geometry.equals(equal), 
     132             "equals() returns true for a geometry with equivalent coordinates"); 
     133        t.ok(!geometry.equals(offX), 
     134             "equals() returns false for a geometry with offset x"); 
     135        t.ok(!geometry.equals(offY), 
     136             "equals() returns false for a geometry with offset y"); 
     137    } 
     138     
     139    function test_Polygon_clone(t) { 
     140        t.plan(2); 
     141         
     142        var x0 = Math.random() * 100; 
     143        var y0 = Math.random() * 100; 
     144        var x1 = Math.random() * 100; 
     145        var y1 = Math.random() * 100; 
     146        var x2 = Math.random() * 100; 
     147        var y2 = Math.random() * 100; 
     148        var point0 = new OpenLayers.Geometry.Point(x0, y0); 
     149        var point1 = new OpenLayers.Geometry.Point(x1, y1); 
     150        var point2 = new OpenLayers.Geometry.Point(x2, y2); 
     151        var geometry = new OpenLayers.Geometry.Polygon([ 
     152                            new OpenLayers.Geometry.LinearRing([point0, point1, point2])]); 
     153        var clone = geometry.clone(); 
     154        t.ok(clone instanceof OpenLayers.Geometry.Polygon, 
     155             "clone() creates an OpenLayers.Geometry.Polygon"); 
     156        t.ok(geometry.equals(clone), "clone has equivalent coordinates"); 
     157    } 
    108158     
    109159  // -->