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.

git-svn-id: http://svn.openlayers.org/trunk/openlayers@2931 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Schuyler Erle
2007-03-30 21:42:32 +00:00
parent 91ec16c81a
commit 313704b844
16 changed files with 374 additions and 177 deletions

View File

@@ -25,6 +25,14 @@ OpenLayers.Geometry.Collection.prototype =
/** @type Array(OpenLayers.Geometry) */ /** @type Array(OpenLayers.Geometry) */
components: null, components: null,
/**
* An array of class names representing the types of components that
* the collection can include. A null value means the component types
* are not restricted.
* @type Array(String)
*/
componentTypes: null,
/** /**
* @constructor * @constructor
* *
@@ -59,19 +67,16 @@ OpenLayers.Geometry.Collection.prototype =
* @returns An exact clone of this collection * @returns An exact clone of this collection
* @type OpenLayers.Geometry.Collection * @type OpenLayers.Geometry.Collection
*/ */
clone: function (obj) { clone: function() {
if (obj == null) { var geometry = eval("new " + this.CLASS_NAME + "()");
obj = eval("new " + this.CLASS_NAME + "()");
}
for(var i=0; i<this.components.length; i++) { for(var i=0; i<this.components.length; i++) {
obj.addComponent(this.components[i].clone()); geometry.addComponent(this.components[i].clone());
} }
// catch any randomly tagged-on properties // catch any randomly tagged-on properties
OpenLayers.Util.applyDefaults(obj, this); OpenLayers.Util.applyDefaults(geometry, this);
return obj; return geometry;
}, },
/** /**
@@ -121,17 +126,25 @@ OpenLayers.Geometry.Collection.prototype =
} }
}, },
/** Add a new component (geometry) to the collection. /**
* Add a new component (geometry) to the collection. If this.componentTypes
* is set, then the component class name must be in the componentTypes array.
* *
* The bounds cache is reset. * The bounds cache is reset.
* *
* @param {OpenLayers.Geometry} component * @param {OpenLayers.Geometry} component
* @param {int} index Index into the array to insert the component * @param {int} index Index into the array to insert the component
* @type Boolean
* @return Component was successfully added
*/ */
addComponent: function(component, index) { addComponent: function(component, index) {
var added = false;
if(component) { if(component) {
if(this.componentTypes == null ||
(OpenLayers.Util.indexOf(this.componentTypes,
component.CLASS_NAME) > -1)) {
if (index) { if(index != null && (index < this.components.length)) {
var components1 = this.components.slice(0, index); var components1 = this.components.slice(0, index);
var components2 = this.components.slice(index, var components2 = this.components.slice(index,
this.components.length); this.components.length);
@@ -142,7 +155,10 @@ OpenLayers.Geometry.Collection.prototype =
} }
component.parent = this; component.parent = this;
this.clearBounds(); this.clearBounds();
added = true;
} }
}
return added;
}, },
/** /**
@@ -205,6 +221,30 @@ OpenLayers.Geometry.Collection.prototype =
} }
}, },
/**
* Tests for equivalent geometries
* @param {OpenLayers.Geometry}
* @type Boolean
* @return The coordinates are equivalent
*/
equals: function(geometry) {
var equivalent = true;
if(!geometry.CLASS_NAME || (this.CLASS_NAME != geometry.CLASS_NAME)) {
equivalent = false;
} else if(!(geometry.components instanceof Array) ||
(geometry.components.length != this.components.length)) {
equivalent = false;
} else {
for(var i=0; i<this.components.length; ++i) {
if(!this.components[i].equals(geometry.components[i])) {
equivalent = false;
break;
}
}
}
return equivalent;
},
/** @final @type String */ /** @final @type String */
CLASS_NAME: "OpenLayers.Geometry.Collection" CLASS_NAME: "OpenLayers.Geometry.Collection"
}); });

View File

@@ -15,6 +15,14 @@ OpenLayers.Geometry.Curve = OpenLayers.Class.create();
OpenLayers.Geometry.Curve.prototype = OpenLayers.Geometry.Curve.prototype =
OpenLayers.Class.inherit(OpenLayers.Geometry.MultiPoint, { OpenLayers.Class.inherit(OpenLayers.Geometry.MultiPoint, {
/**
* An array of class names representing the types of components that
* the collection can include. A null value means the component types
* are not restricted.
* @type Array(String)
*/
componentTypes: ["OpenLayers.Geometry.Point"],
/** /**
* @constructor * @constructor
* *
@@ -25,20 +33,6 @@ OpenLayers.Geometry.Curve.prototype =
arguments); arguments);
}, },
/**
* @returns An exact clone of this OpenLayers.Feature
* @type OpenLayers.Feature
*/
clone: function (obj) {
if (obj == null) {
obj = new OpenLayers.Geometry.Curve();
}
obj = OpenLayers.Geometry.Collection.prototype.clone.apply(this,
[obj]);
return obj;
},
/** /**
* @returns The length of the curve * @returns The length of the curve
* @type float * @type float

View File

@@ -20,24 +20,7 @@ OpenLayers.Geometry.LineString.prototype =
* @param {Array(OpenLayers.Geometry.Point)} points * @param {Array(OpenLayers.Geometry.Point)} points
*/ */
initialize: function(points) { initialize: function(points) {
OpenLayers.Geometry.Curve.prototype.initialize.apply(this, OpenLayers.Geometry.Curve.prototype.initialize.apply(this, arguments);
arguments);
},
/**
* @returns An exact clone of this OpenLayers.Feature
* @type OpenLayers.Feature
*/
clone: function (obj) {
if (obj == null) {
obj = new OpenLayers.Geometry.LineString();
}
for (var i = 0; i < this.components.length; i++) {
obj.addComponent(this.components[i].clone());
}
return obj;
}, },
/** Only allows removal of a point if there are three or more points in /** Only allows removal of a point if there are three or more points in
@@ -47,7 +30,7 @@ OpenLayers.Geometry.LineString.prototype =
*/ */
removeComponent: function(point) { removeComponent: function(point) {
if ( this.components && (this.components.length > 2)) { if ( this.components && (this.components.length > 2)) {
OpenLayers.Geometry.Curve.prototype.removeComponent.apply(this, OpenLayers.Geometry.Collection.prototype.removeComponent.apply(this,
arguments); arguments);
} }
}, },

View File

@@ -19,8 +19,21 @@ OpenLayers.Geometry.LinearRing.prototype =
OpenLayers.Class.inherit(OpenLayers.Geometry.LineString, { OpenLayers.Class.inherit(OpenLayers.Geometry.LineString, {
/** /**
* @constructor * An array of class names representing the types of components that
* the collection can include. A null value means the component types
* are not restricted.
* @type Array(String)
*/
componentTypes: ["OpenLayers.Geometry.Point"],
/**
* Linear rings are constructed with an array of points. This array
* can represent a closed or open ring. If the ring is open (the last
* point does not equal the first point), the constructor will close
* the ring. If the ring is already closed (the last point does equal
* the first point), it will be left closed.
* *
* @constructor
* @param {Array(OpenLayers.Geometry.Point)} points * @param {Array(OpenLayers.Geometry.Point)} points
*/ */
initialize: function(points) { initialize: function(points) {
@@ -29,40 +42,39 @@ OpenLayers.Geometry.LinearRing.prototype =
}, },
/** /**
* @returns An exact clone of this OpenLayers.Geometry.LinearRing * Adds a point to geometry components. If the point is to be added to
* @type OpenLayers.Geometry.LinearRing * the end of the components array and it is the same as the last point
*/ * already in that array, the duplicate point is not added. This has the
clone: function (obj) { * effect of closing the ring if it is not already closed, and doing the
if (obj == null) { * right thing if it is already closed. This behavior can be overridden
obj = new OpenLayers.Geometry.LinearRing(); * by calling the method with a non-null index as the second argument.
}
for (var i = 0; i < this.components.length; i++) {
obj.addComponent(this.components[i].clone());
}
return obj;
},
/**
* Adds a point to geometry components
* *
* @param {OpenLayers.Geometry.Point} point * @param {OpenLayers.Geometry.Point} point
* @param {int} index Index into the array to insert the component * @param {int} index Index into the array to insert the component
* @type Boolean
* @return Point was successfully added
*/ */
addComponent: function(point, index) { addComponent: function(point, index) {
var added = false;
//remove last point //remove last point
var lastPoint = this.components[this.components.length-1]; var lastPoint = this.components[this.components.length-1];
OpenLayers.Geometry.Curve.prototype.removeComponent.apply(this, OpenLayers.Geometry.Collection.prototype.removeComponent.apply(this,
[lastPoint]); [lastPoint]);
//add our point // given an index, add the point
OpenLayers.Geometry.LineString.prototype.addComponent.apply(this, // without an index only add non-duplicate points
if(index != null || !point.equals(lastPoint)) {
added = OpenLayers.Geometry.Collection.prototype.addComponent.apply(this,
arguments); arguments);
}
//append copy of first point //append copy of first point
var firstPoint = this.components[0]; var firstPoint = this.components[0];
OpenLayers.Geometry.Curve.prototype.addComponent.apply(this, OpenLayers.Geometry.Collection.prototype.addComponent.apply(this,
[firstPoint.clone()]); [firstPoint.clone()]);
return added;
}, },
/** /**
@@ -75,15 +87,15 @@ OpenLayers.Geometry.LinearRing.prototype =
//remove last point //remove last point
var lastPoint = this.components[this.components.length-1]; var lastPoint = this.components[this.components.length-1];
OpenLayers.Geometry.Curve.prototype.removeComponent.apply(this, OpenLayers.Geometry.Collection.prototype.removeComponent.apply(this,
[lastPoint]); [lastPoint]);
//remove our point //remove our point
OpenLayers.Geometry.LineString.prototype.removeComponent.apply(this, OpenLayers.Geometry.Collection.prototype.removeComponent.apply(this,
arguments); arguments);
//append copy of first point //append copy of first point
var firstPoint = this.components[0]; var firstPoint = this.components[0];
OpenLayers.Geometry.Curve.prototype.addComponent.apply(this, OpenLayers.Geometry.Collection.prototype.addComponent.apply(this,
[firstPoint.clone()]); [firstPoint.clone()]);
} }
}, },

View File

@@ -13,6 +13,14 @@ OpenLayers.Geometry.MultiLineString = OpenLayers.Class.create();
OpenLayers.Geometry.MultiLineString.prototype = OpenLayers.Geometry.MultiLineString.prototype =
OpenLayers.Class.inherit(OpenLayers.Geometry.Collection, { OpenLayers.Class.inherit(OpenLayers.Geometry.Collection, {
/**
* An array of class names representing the types of components that
* the collection can include. A null value means the component types
* are not restricted.
* @type Array(String)
*/
componentTypes: ["OpenLayers.Geometry.LineString"],
/** /**
* @constructor * @constructor
* *
@@ -23,20 +31,6 @@ OpenLayers.Geometry.MultiLineString.prototype =
arguments); arguments);
}, },
/**
* adds a component to the MultiPoint, checking type
*
* @param {OpenLayers.Geometry.LineString} component lineString to add
* @param {int} index Index into the array to insert the component
*/
addComponent: function(component, index) {
if (!(component instanceof OpenLayers.Geometry.LineString)) {
throw "component should be an OpenLayers.Geometry.LineString";
}
OpenLayers.Geometry.Collection.prototype.addComponent.apply(this,
arguments);
},
/** @final @type String */ /** @final @type String */
CLASS_NAME: "OpenLayers.Geometry.MultiLineString" CLASS_NAME: "OpenLayers.Geometry.MultiLineString"
}); });

View File

@@ -13,6 +13,14 @@ OpenLayers.Geometry.MultiPoint = OpenLayers.Class.create();
OpenLayers.Geometry.MultiPoint.prototype = OpenLayers.Geometry.MultiPoint.prototype =
OpenLayers.Class.inherit(OpenLayers.Geometry.Collection, { OpenLayers.Class.inherit(OpenLayers.Geometry.Collection, {
/**
* An array of class names representing the types of components that
* the collection can include. A null value means the component types
* are not restricted.
* @type Array(String)
*/
componentTypes: ["OpenLayers.Geometry.Point"],
/** /**
* @constructor * @constructor
* *
@@ -23,20 +31,6 @@ OpenLayers.Geometry.MultiPoint.prototype =
arguments); arguments);
}, },
/**
* adds component to the MultiPoint, checking type
*
* @param {OpenLayers.Geometry.Point} component point to add
* @param {int} index Index into the array to insert the component
*/
addComponent: function(component, index) {
if (!(component instanceof OpenLayers.Geometry.Point)) {
throw "component should be an OpenLayers.Geometry.Point";
}
OpenLayers.Geometry.Collection.prototype.addComponent.apply(this,
arguments);
},
/** /**
* Wrapper for addComponent() * Wrapper for addComponent()
* *

View File

@@ -13,6 +13,14 @@ OpenLayers.Geometry.MultiPolygon = OpenLayers.Class.create();
OpenLayers.Geometry.MultiPolygon.prototype = OpenLayers.Geometry.MultiPolygon.prototype =
OpenLayers.Class.inherit(OpenLayers.Geometry.Collection, { OpenLayers.Class.inherit(OpenLayers.Geometry.Collection, {
/**
* An array of class names representing the types of components that
* the collection can include. A null value means the component types
* are not restricted.
* @type Array(String)
*/
componentTypes: ["OpenLayers.Geometry.Polygon"],
/** /**
* @constructor * @constructor
* *
@@ -23,23 +31,6 @@ OpenLayers.Geometry.MultiPolygon.prototype =
arguments); arguments);
}, },
/**
* adds component to the MultiPolygon, checking type
*
* @param {OpenLayers.Geometry.Polygon} component Polygon to add
* @param {int} index Index into the array to insert the component
*/
addComponent: function(component, index) {
if (!(component instanceof OpenLayers.Geometry.Polygon)) {
var throwStr = "component should be an " +
"OpenLayers.Geometry.Polygon but is an " +
component.CLASS_NAME;
throw throwStr;
}
OpenLayers.Geometry.Collection.prototype.addComponent.apply(this,
arguments);
},
/** @final @type String */ /** @final @type String */
CLASS_NAME: "OpenLayers.Geometry.MultiPolygon" CLASS_NAME: "OpenLayers.Geometry.MultiPolygon"
}); });

View File

@@ -16,6 +16,14 @@ OpenLayers.Geometry.Polygon = OpenLayers.Class.create();
OpenLayers.Geometry.Polygon.prototype = OpenLayers.Geometry.Polygon.prototype =
OpenLayers.Class.inherit(OpenLayers.Geometry.Collection, { OpenLayers.Class.inherit(OpenLayers.Geometry.Collection, {
/**
* An array of class names representing the types of components that
* the collection can include. A null value means the component types
* are not restricted.
* @type Array(String)
*/
componentTypes: ["OpenLayers.Geometry.LinearRing"],
/** /**
* @constructor * @constructor
* *
@@ -26,24 +34,6 @@ OpenLayers.Geometry.Polygon.prototype =
arguments); arguments);
}, },
/**
* adds a component to the Polygon, checking type
*
* @param {OpenLayers.Geometry.LinearRing} point to add
* @param {int} index Index into the array to insert the component
*/
addComponent: function(component, index) {
if (!(component instanceof OpenLayers.Geometry.LinearRing)) {
var throwStr = "component should be an " +
"OpenLayers.Geometry.LinearRing but is a " +
component.CLASS_NAME;
throw throwStr;
}
OpenLayers.Geometry.Collection.prototype.addComponent.apply(this,
arguments);
},
/** Calculated by subtracting the areas of the internal holes from the /** Calculated by subtracting the areas of the internal holes from the
* area of the outer hole. * area of the outer hole.
* *

View File

@@ -79,10 +79,11 @@ OpenLayers.Handler.Path.prototype =
}, },
/** /**
* Add point to geometry * Add point to geometry. Send the point index to override
* the behavior of LinearRing that disregards adding duplicate points.
*/ */
addPoint: function() { addPoint: function() {
this.line.addComponent(this.point.clone()); this.line.addComponent(this.point.clone(), this.line.components.length);
}, },
/** /**

View File

@@ -86,6 +86,22 @@ OpenLayers.Handler.Polygon.prototype =
return this.polygon.clone(); return this.polygon.clone();
}, },
/**
* Handle double-clicks. Finish the geometry and send it back
* to the control.
*
* @param {Event} evt
*/
dblclick: function(evt) {
if(!this.freehandMode(evt)) {
// remove the penultimate point
var index = this.line.components.length - 2;
this.line.removeComponent(this.line.components[index]);
this.finalize(this.line);
}
return false;
},
/** @final @type String */ /** @final @type String */
CLASS_NAME: "OpenLayers.Handler.Polygon" CLASS_NAME: "OpenLayers.Handler.Polygon"
}); });

View File

@@ -142,18 +142,19 @@
} }
function test_07_Collection_addComponent(t) { function test_07_Collection_addComponent(t) {
t.plan(3); t.plan(10);
var coll = new OpenLayers.Geometry.Collection(); var coll = new OpenLayers.Geometry.Collection();
//null //null
coll.addComponent(null); coll.addComponent(null);
t.ok(true, "no breakage, no executage from null input") t.ok(!coll.addComponent(null),
"addComponent returns false for bad component")
//good component //good component
var component = new OpenLayers.Geometry.Point(3,4); var component = new OpenLayers.Geometry.Point(3,4);
coll.addComponent(component); t.ok(coll.addComponent(component),
"addComponent returns true for good component");
t.ok(coll.bounds == null, "bounds cache correctly cleared"); t.ok(coll.bounds == null, "bounds cache correctly cleared");
var foundComponent = false; var foundComponent = false;
@@ -164,6 +165,27 @@
} }
t.ok(foundComponent, "component added to internal array"); t.ok(foundComponent, "component added to internal array");
// restricted components
coll.componentTypes = ["OpenLayers.Geometry.Point",
"OpenLayers.Geometry.LineString"];
var point1 = new OpenLayers.Geometry.Point(0,0);
var point2 = new OpenLayers.Geometry.Point(1,1);
var line = new OpenLayers.Geometry.LineString([point1, point2]);
var multipoint = new OpenLayers.Geometry.MultiPoint([point1, point2]);
t.ok(coll.addComponent(point1),
"addComponent returns true for 1st geometry type in componentTypes");
t.ok(OpenLayers.Util.indexOf(coll.components, point1) > -1,
"addComponent adds 1st restricted type to components array");
t.ok(coll.addComponent(line),
"addComponent returns true for 2nd geometry type in componentTypes");
t.ok(OpenLayers.Util.indexOf(coll.components, point1) > -1,
"addComponent adds 2nd restricted type to components array");
t.ok(!coll.addComponent(multipoint),
"addComponent returns false for geometry type not in componentTypes");
t.ok(OpenLayers.Util.indexOf(coll.components, multipoint) == -1,
"addComponent doesn't add restricted type to component array");
} }
function test_08_collection_getLength(t) { function test_08_collection_getLength(t) {

View File

@@ -34,9 +34,9 @@
function test_03_LineString_removeComponent(t) { function test_03_LineString_removeComponent(t) {
t.plan(2); t.plan(2);
OpenLayers.Geometry.Curve.prototype._removeComponent = OpenLayers.Geometry.Collection.prototype._removeComponent =
OpenLayers.Geometry.Curve.prototype.removeComponent; OpenLayers.Geometry.Collection.prototype.removeComponent;
OpenLayers.Geometry.Curve.prototype.removeComponent = OpenLayers.Geometry.Collection.prototype.removeComponent =
function(point) { g_removeComponent = point; }; function(point) { g_removeComponent = point; };
line = new OpenLayers.Geometry.LineString(components); line = new OpenLayers.Geometry.LineString(components);
@@ -49,8 +49,8 @@
line.removeComponent(components[0]); line.removeComponent(components[0]);
t.ok(g_removeComponent, components[0], "point removed if 3 points in components"); t.ok(g_removeComponent, components[0], "point removed if 3 points in components");
OpenLayers.Geometry.Curve.prototype.removeComponent = OpenLayers.Geometry.Collection.prototype.removeComponent =
OpenLayers.Geometry.Curve.prototype._removeComponent; OpenLayers.Geometry.Collection.prototype._removeComponent;
} }
function test_04_LineString_move(t) { function test_04_LineString_move(t) {
@@ -73,6 +73,46 @@
t.eq(line.components[1].y, y1 + dy, "move() correctly modifies second y"); t.eq(line.components[1].y, y1 + dy, "move() correctly modifies second y");
} }
function test_LineString_equals(t) {
t.plan(3);
var x0 = Math.random() * 100;
var y0 = Math.random() * 100;
var x1 = Math.random() * 100;
var y1 = Math.random() * 100;
var point0 = new OpenLayers.Geometry.Point(x0, y0);
var point1 = new OpenLayers.Geometry.Point(x1, y1);
var geometry = new OpenLayers.Geometry.LineString([point0, point1]);
var equal = new OpenLayers.Geometry.LineString([point0, point1]);
var offX = new OpenLayers.Geometry.LineString([
new OpenLayers.Geometry.Point(x0 + 1, y0),
new OpenLayers.Geometry.Point(x1 + 1, y1)]);
var offY = new OpenLayers.Geometry.LineString([
new OpenLayers.Geometry.Point(x0, y0 + 1),
new OpenLayers.Geometry.Point(x1, y1 + 1)]);
t.ok(geometry.equals(equal),
"equals() returns true for a geometry with equivalent coordinates");
t.ok(!geometry.equals(offX),
"equals() returns false for a geometry with offset x");
t.ok(!geometry.equals(offY),
"equals() returns false for a geometry with offset y");
}
function test_LineString_clone(t) {
t.plan(2);
var x0 = Math.random() * 100;
var y0 = Math.random() * 100;
var x1 = Math.random() * 100;
var y1 = Math.random() * 100;
var point0 = new OpenLayers.Geometry.Point(x0, y0);
var point1 = new OpenLayers.Geometry.Point(x1, y1);
var geometry = new OpenLayers.Geometry.LineString([point0, point1]);
var clone = geometry.clone();
t.ok(clone instanceof OpenLayers.Geometry.LineString,
"clone() creates an OpenLayers.Geometry.LineString");
t.ok(geometry.equals(clone), "clone has equivalent coordinates");
}
// --> // -->
</script> </script>

View File

@@ -23,29 +23,36 @@
} }
function test_02_LinearRing_addComponent(t) { function test_02_LinearRing_addComponent(t) {
t.plan(12); t.plan(13);
var ring = new OpenLayers.Geometry.LinearRing(); var ring = new OpenLayers.Geometry.LinearRing();
var point = new OpenLayers.Geometry.Point(0,0); var point = new OpenLayers.Geometry.Point(0,0);
ring.addComponent( point ); t.ok(ring.addComponent(point),
"addComponent returns true for 1st point");
t.eq(ring.components.length, 2, "add first point, correct length"); t.eq(ring.components.length, 2, "add first point, correct length");
t.ok(ring.components[0].equals(point), "point one correct"); t.ok(ring.components[0].equals(point), "point one correct");
t.ok(ring.components[0].equals(ring.components[ring.components.length - 1]), "first and last point equal"); t.ok(ring.components[0].equals(ring.components[ring.components.length - 1]), "first and last point equal");
ring.addComponent( point );
t.eq(ring.components.length, 3, "add second point, correct length");
t.ok(ring.components[0].equals(point), "point one correct");
t.ok(ring.components[1].equals(point), "point two correct");
t.ok(ring.components[0].equals(ring.components[ring.components.length - 1]), "first and last point equal");
newPoint = new OpenLayers.Geometry.Point(10,10); newPoint = new OpenLayers.Geometry.Point(10,10);
ring.addComponent( newPoint ); t.ok(ring.addComponent( newPoint ),
t.eq(ring.components.length, 4, "correctly adds 3rd point"); "addComponent returns true for unique point");
t.eq(ring.components.length, 3, "correctly adds 3rd point");
t.ok(ring.components[0].equals(point), "point one correct"); t.ok(ring.components[0].equals(point), "point one correct");
t.ok(ring.components[1].equals(point), "point two correct"); t.ok(ring.components[1].equals(newPoint), "point one correct");
t.ok(ring.components[2].equals(newPoint), "point one correct"); t.ok(ring.components[2].equals(ring.components[0]), "first and last point equal");
t.ok(ring.components[0].equals(ring.components[ring.components.length - 1]), "first and last point equal");
var length = ring.components.length;
var clone = ring.components[length - 1].clone();
t.ok(!ring.addComponent(clone),
"addComponent returns false for adding a duplicate last point");
t.eq(ring.components.length, length,
"components remains unchanged after trying to add duplicate point");
t.ok(ring.addComponent(clone, length - 1),
"addComponent returns true when adding a duplicate with an index");
t.eq(ring.components.length, length + 1,
"components increase in length after adding a duplicate point with index");
} }
function test_03_LinearRing_removeComponent(t) { function test_03_LinearRing_removeComponent(t) {

View File

@@ -7,14 +7,14 @@
function test_01_MultiPoint_constructor (t) { function test_01_MultiPoint_constructor (t) {
t.plan( 2 ); t.plan( 2 );
multipoint = new OpenLayers.Geometry.MultiPoint(); var multipoint = new OpenLayers.Geometry.MultiPoint();
t.ok( multipoint instanceof OpenLayers.Geometry.MultiPoint, "new OpenLayers.Geometry.MultiPoint returns multipoint object" ); t.ok( multipoint instanceof OpenLayers.Geometry.MultiPoint, "new OpenLayers.Geometry.MultiPoint returns multipoint object" );
t.eq( multipoint.CLASS_NAME, "OpenLayers.Geometry.MultiPoint", "multipoint.CLASS_NAME is set correctly"); t.eq( multipoint.CLASS_NAME, "OpenLayers.Geometry.MultiPoint", "multipoint.CLASS_NAME is set correctly");
} }
function test_01a_MultiPoint_constructor (t) { function test_01a_MultiPoint_constructor (t) {
t.plan( 3 ); t.plan( 3 );
multipoint = new OpenLayers.Geometry.MultiPoint([point]); var multipoint = new OpenLayers.Geometry.MultiPoint([point]);
t.ok( multipoint instanceof OpenLayers.Geometry.MultiPoint, "new OpenLayers.Geometry.MultiPoint returns multipoint object" ); t.ok( multipoint instanceof OpenLayers.Geometry.MultiPoint, "new OpenLayers.Geometry.MultiPoint returns multipoint object" );
t.eq( multipoint.CLASS_NAME, "OpenLayers.Geometry.MultiPoint", "multipoint.CLASS_NAME is set correctly"); t.eq( multipoint.CLASS_NAME, "OpenLayers.Geometry.MultiPoint", "multipoint.CLASS_NAME is set correctly");
t.eq( multipoint.components.length, 1, "multipolygon.components.length is set correctly"); t.eq( multipoint.components.length, 1, "multipolygon.components.length is set correctly");
@@ -23,7 +23,7 @@
function test_02_MultiPoint_move(t) { function test_02_MultiPoint_move(t) {
t.plan(4); t.plan(4);
multipoint = new OpenLayers.Geometry.MultiPoint([point]); var multipoint = new OpenLayers.Geometry.MultiPoint([point]);
var x = point.x; var x = point.x;
var y = point.y; var y = point.y;
@@ -36,6 +36,40 @@
t.eq(multipoint.components[0].lat, y + dy, "move() correctly modifies lat"); t.eq(multipoint.components[0].lat, y + dy, "move() correctly modifies lat");
} }
function test_MultiPoint_equals(t) {
t.plan(3);
var x = Math.random() * 100;
var y = Math.random() * 100;
var geometry = new OpenLayers.Geometry.MultiPoint(
[new OpenLayers.Geometry.Point(x, y)]);
var equal = new OpenLayers.Geometry.MultiPoint(
[new OpenLayers.Geometry.Point(x, y)]);
var offX = new OpenLayers.Geometry.MultiPoint(
[new OpenLayers.Geometry.Point(x + 1, y)]);
var offY = new OpenLayers.Geometry.MultiPoint(
[new OpenLayers.Geometry.Point(x, y + 1)]);
t.ok(geometry.equals(equal),
"equals() returns true for a geometry with equivalent coordinates");
t.ok(!geometry.equals(offX),
"equals() returns false for a geometry with offset x");
t.ok(!geometry.equals(offY),
"equals() returns false for a geometry with offset y");
}
function test_MultiPoint_clone(t) {
t.plan(2);
var x = Math.random() * 100;
var y = Math.random() * 100;
var geometry = new OpenLayers.Geometry.MultiPoint(
[new OpenLayers.Geometry.Point(x, y)]);
var clone = geometry.clone();
t.ok(clone instanceof OpenLayers.Geometry.MultiPoint,
"clone() creates an OpenLayers.Geometry.MultiPoint");
t.ok(geometry.equals(clone), "clone has equivalent coordinates");
}
// --> // -->
</script> </script>
</head> </head>

View File

@@ -104,6 +104,35 @@
t.eq(point.lat, y + dy, "move() correctly modifies lat"); t.eq(point.lat, y + dy, "move() correctly modifies lat");
} }
function test_Point_equals(t) {
t.plan(3);
var x = Math.random() * 100;
var y = Math.random() * 100;
var geometry = new OpenLayers.Geometry.Point(x, y);
var equal = new OpenLayers.Geometry.Point(x, y);
var offX = new OpenLayers.Geometry.Point(x + 1, y);
var offY = new OpenLayers.Geometry.Point(x, y + 1);
t.ok(geometry.equals(equal),
"equals() returns true for a geometry with equivalent coordinates");
t.ok(!geometry.equals(offX),
"equals() returns false for a geometry with offset x");
t.ok(!geometry.equals(offY),
"equals() returns false for a geometry with offset y");
}
function test_Point_clone(t) {
t.plan(2);
var x = Math.random() * 100;
var y = Math.random() * 100;
var geometry = new OpenLayers.Geometry.Point(x, y);
var clone = geometry.clone();
t.ok(clone instanceof OpenLayers.Geometry.Point,
"clone() creates an OpenLayers.Geometry.Point");
t.ok(geometry.equals(clone), "clone has equivalent coordinates");
}
// --> // -->
</script> </script>
</head> </head>

View File

@@ -106,6 +106,56 @@
t.eq(polygon.components[1].components[0].y, y2 + dy, "move() correctly modifies second y"); t.eq(polygon.components[1].components[0].y, y2 + dy, "move() correctly modifies second y");
} }
function test_Polygon_equals(t) {
t.plan(3);
var x0 = Math.random() * 100;
var y0 = Math.random() * 100;
var x1 = Math.random() * 100;
var y1 = Math.random() * 100;
var x2 = Math.random() * 100;
var y2 = Math.random() * 100;
var point0 = new OpenLayers.Geometry.Point(x0, y0);
var point1 = new OpenLayers.Geometry.Point(x1, y1);
var point2 = new OpenLayers.Geometry.Point(x2, y2);
var pointX = new OpenLayers.Geometry.Point(x0 + 1, y0);
var pointY = new OpenLayers.Geometry.Point(x0, y0 + 1);
var geometry = new OpenLayers.Geometry.Polygon([
new OpenLayers.Geometry.LinearRing([point0, point1, point2])]);
var equal = new OpenLayers.Geometry.Polygon([
new OpenLayers.Geometry.LinearRing([point0, point1, point2])]);
var offX = new OpenLayers.Geometry.Polygon([
new OpenLayers.Geometry.LinearRing([pointX, point1, point2])]);
var offY = new OpenLayers.Geometry.Polygon([
new OpenLayers.Geometry.LinearRing([pointY, point1, point2])]);
t.ok(geometry.equals(equal),
"equals() returns true for a geometry with equivalent coordinates");
t.ok(!geometry.equals(offX),
"equals() returns false for a geometry with offset x");
t.ok(!geometry.equals(offY),
"equals() returns false for a geometry with offset y");
}
function test_Polygon_clone(t) {
t.plan(2);
var x0 = Math.random() * 100;
var y0 = Math.random() * 100;
var x1 = Math.random() * 100;
var y1 = Math.random() * 100;
var x2 = Math.random() * 100;
var y2 = Math.random() * 100;
var point0 = new OpenLayers.Geometry.Point(x0, y0);
var point1 = new OpenLayers.Geometry.Point(x1, y1);
var point2 = new OpenLayers.Geometry.Point(x2, y2);
var geometry = new OpenLayers.Geometry.Polygon([
new OpenLayers.Geometry.LinearRing([point0, point1, point2])]);
var clone = geometry.clone();
t.ok(clone instanceof OpenLayers.Geometry.Polygon,
"clone() creates an OpenLayers.Geometry.Polygon");
t.ok(geometry.equals(clone), "clone has equivalent coordinates");
}
// --> // -->
</script> </script>
</head> </head>