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

@@ -24,6 +24,14 @@ OpenLayers.Geometry.Collection.prototype =
/** @type Array(OpenLayers.Geometry) */
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
@@ -59,19 +67,16 @@ OpenLayers.Geometry.Collection.prototype =
* @returns An exact clone of this collection
* @type OpenLayers.Geometry.Collection
*/
clone: function (obj) {
if (obj == null) {
obj = eval("new " + this.CLASS_NAME + "()");
}
for (var i = 0; i < this.components.length; i++) {
obj.addComponent(this.components[i].clone());
clone: function() {
var geometry = eval("new " + this.CLASS_NAME + "()");
for(var i=0; i<this.components.length; i++) {
geometry.addComponent(this.components[i].clone());
}
// catch any randomly tagged-on properties
OpenLayers.Util.applyDefaults(obj, this);
OpenLayers.Util.applyDefaults(geometry, this);
return obj;
return geometry;
},
/**
@@ -121,28 +126,39 @@ 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.
*
* @param {OpenLayers.Geometry} component
* @param {int} index Index into the array to insert the component
* @type Boolean
* @return Component was successfully added
*/
addComponent: function(component, index) {
if (component) {
var added = false;
if(component) {
if(this.componentTypes == null ||
(OpenLayers.Util.indexOf(this.componentTypes,
component.CLASS_NAME) > -1)) {
if (index) {
var components1 = this.components.slice(0, index);
var components2 = this.components.slice(index,
this.components.length);
components1.push(component);
this.components = components1.concat(components2);
} else {
this.components.push(component);
if(index != null && (index < this.components.length)) {
var components1 = this.components.slice(0, index);
var components2 = this.components.slice(index,
this.components.length);
components1.push(component);
this.components = components1.concat(components2);
} else {
this.components.push(component);
}
component.parent = this;
this.clearBounds();
added = true;
}
component.parent = this;
this.clearBounds();
}
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 */
CLASS_NAME: "OpenLayers.Geometry.Collection"
});

View File

@@ -15,6 +15,14 @@ OpenLayers.Geometry.Curve = OpenLayers.Class.create();
OpenLayers.Geometry.Curve.prototype =
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
*
@@ -25,20 +33,6 @@ OpenLayers.Geometry.Curve.prototype =
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
* @type float

View File

@@ -20,24 +20,7 @@ OpenLayers.Geometry.LineString.prototype =
* @param {Array(OpenLayers.Geometry.Point)} points
*/
initialize: function(points) {
OpenLayers.Geometry.Curve.prototype.initialize.apply(this,
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;
OpenLayers.Geometry.Curve.prototype.initialize.apply(this, arguments);
},
/** 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) {
if ( this.components && (this.components.length > 2)) {
OpenLayers.Geometry.Curve.prototype.removeComponent.apply(this,
OpenLayers.Geometry.Collection.prototype.removeComponent.apply(this,
arguments);
}
},

View File

@@ -19,50 +19,62 @@ OpenLayers.Geometry.LinearRing.prototype =
OpenLayers.Class.inherit(OpenLayers.Geometry.LineString, {
/**
* 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
*/
initialize: function(points) {
OpenLayers.Geometry.LineString.prototype.initialize.apply(this,
arguments);
},
/**
* @returns An exact clone of this OpenLayers.Geometry.LinearRing
* @type OpenLayers.Geometry.LinearRing
*/
clone: function (obj) {
if (obj == null) {
obj = new OpenLayers.Geometry.LinearRing();
}
for (var i = 0; i < this.components.length; i++) {
obj.addComponent(this.components[i].clone());
}
return obj;
},
/**
* Adds a point to geometry components
* Adds a point to geometry components. If the point is to be added to
* 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
* effect of closing the ring if it is not already closed, and doing the
* right thing if it is already closed. This behavior can be overridden
* by calling the method with a non-null index as the second argument.
*
* @param {OpenLayers.Geometry.Point} point
* @param {int} index Index into the array to insert the component
*/
* @type Boolean
* @return Point was successfully added
*/
addComponent: function(point, index) {
var added = false;
//remove last point
var lastPoint = this.components[this.components.length-1];
OpenLayers.Geometry.Curve.prototype.removeComponent.apply(this,
[lastPoint]);
//add our point
OpenLayers.Geometry.LineString.prototype.addComponent.apply(this,
arguments);
OpenLayers.Geometry.Collection.prototype.removeComponent.apply(this,
[lastPoint]);
// given an index, add the point
// without an index only add non-duplicate points
if(index != null || !point.equals(lastPoint)) {
added = OpenLayers.Geometry.Collection.prototype.addComponent.apply(this,
arguments);
}
//append copy of first point
var firstPoint = this.components[0];
OpenLayers.Geometry.Curve.prototype.addComponent.apply(this,
OpenLayers.Geometry.Collection.prototype.addComponent.apply(this,
[firstPoint.clone()]);
return added;
},
/**
@@ -75,15 +87,15 @@ OpenLayers.Geometry.LinearRing.prototype =
//remove last point
var lastPoint = this.components[this.components.length-1];
OpenLayers.Geometry.Curve.prototype.removeComponent.apply(this,
OpenLayers.Geometry.Collection.prototype.removeComponent.apply(this,
[lastPoint]);
//remove our point
OpenLayers.Geometry.LineString.prototype.removeComponent.apply(this,
OpenLayers.Geometry.Collection.prototype.removeComponent.apply(this,
arguments);
//append copy of first point
var firstPoint = this.components[0];
OpenLayers.Geometry.Curve.prototype.addComponent.apply(this,
OpenLayers.Geometry.Collection.prototype.addComponent.apply(this,
[firstPoint.clone()]);
}
},

View File

@@ -13,6 +13,14 @@ OpenLayers.Geometry.MultiLineString = OpenLayers.Class.create();
OpenLayers.Geometry.MultiLineString.prototype =
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
*
@@ -23,20 +31,6 @@ OpenLayers.Geometry.MultiLineString.prototype =
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 */
CLASS_NAME: "OpenLayers.Geometry.MultiLineString"
});

View File

@@ -13,6 +13,14 @@ OpenLayers.Geometry.MultiPoint = OpenLayers.Class.create();
OpenLayers.Geometry.MultiPoint.prototype =
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
*
@@ -23,20 +31,6 @@ OpenLayers.Geometry.MultiPoint.prototype =
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()
*

View File

@@ -13,6 +13,14 @@ OpenLayers.Geometry.MultiPolygon = OpenLayers.Class.create();
OpenLayers.Geometry.MultiPolygon.prototype =
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
*
@@ -22,23 +30,6 @@ OpenLayers.Geometry.MultiPolygon.prototype =
OpenLayers.Geometry.Collection.prototype.initialize.apply(this,
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 */
CLASS_NAME: "OpenLayers.Geometry.MultiPolygon"

View File

@@ -16,6 +16,14 @@ OpenLayers.Geometry.Polygon = OpenLayers.Class.create();
OpenLayers.Geometry.Polygon.prototype =
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
*
@@ -25,24 +33,6 @@ OpenLayers.Geometry.Polygon.prototype =
OpenLayers.Geometry.Collection.prototype.initialize.apply(this,
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
* area of the outer hole.