1
/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2
* license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3
* full text of the license. */
6
* @requires OpenLayers/Geometry.js
10
* Class: OpenLayers.Geometry.Collection
11
* A Collection is exactly what it sounds like: A collection of different
12
* Geometries. These are stored in the local parameter <components> (which
13
* can be passed as a parameter to the constructor).
15
* As new geometries are added to the collection, they are NOT cloned.
16
* When removing geometries, they need to be specified by reference (ie you
17
* have to pass in the *exact* geometry to be removed).
19
* The <getArea> and <getLength> functions here merely iterate through
20
* the components, summing their respective areas and lengths.
22
* Create a new instance with the <OpenLayers.Geometry.Collection> constructor.
25
* - <OpenLayers.Geometry>
27
OpenLayers.Geometry.Collection = OpenLayers.Class(OpenLayers.Geometry, {
30
* APIProperty: components
31
* {Array(<OpenLayers.Geometry>)} The component parts of this geometry
36
* Property: componentTypes
37
* {Array(String)} An array of class names representing the types of
38
* components that the collection can include. A null value means the
39
* component types are not restricted.
44
* Constructor: OpenLayers.Geometry.Collection
45
* Creates a Geometry Collection -- a list of geoms.
48
* components - {Array(<OpenLayers.Geometry>)} Optional array of geometries
51
initialize: function (components) {
52
OpenLayers.Geometry.prototype.initialize.apply(this, arguments);
54
if (components != null) {
55
this.addComponents(components);
61
* Destroy this geometry.
63
destroy: function () {
64
this.components.length = 0;
65
this.components = null;
70
* Clone this geometry.
73
* {<OpenLayers.Geometry.Collection>} An exact clone of this collection
76
var geometry = eval("new " + this.CLASS_NAME + "()");
77
for(var i=0, len=this.components.length; i<len; i++) {
78
geometry.addComponent(this.components[i].clone());
81
// catch any randomly tagged-on properties
82
OpenLayers.Util.applyDefaults(geometry, this);
88
* Method: getComponentsString
89
* Get a string representing the components for this collection
92
* {String} A string representation of the components of this geometry
94
getComponentsString: function(){
96
for(var i=0, len=this.components.length; i<len; i++) {
97
strings.push(this.components[i].toShortString());
99
return strings.join(",");
103
* APIMethod: calculateBounds
104
* Recalculate the bounds by iterating through the components and
105
* calling calling extendBounds() on each item.
107
calculateBounds: function() {
109
if ( this.components && this.components.length > 0) {
110
this.setBounds(this.components[0].getBounds());
111
for (var i=1, len=this.components.length; i<len; i++) {
112
this.extendBounds(this.components[i].getBounds());
118
* APIMethod: addComponents
119
* Add components to this geometry.
122
* components - {Array(<OpenLayers.Geometry>)} An array of geometries to add
124
addComponents: function(components){
125
if(!(components instanceof Array)) {
126
components = [components];
128
for(var i=0, len=components.length; i<len; i++) {
129
this.addComponent(components[i]);
134
* Method: addComponent
135
* Add a new component (geometry) to the collection. If this.componentTypes
136
* is set, then the component class name must be in the componentTypes array.
138
* The bounds cache is reset.
141
* component - {<OpenLayers.Geometry>} A geometry to add
142
* index - {int} Optional index into the array to insert the component
145
* {Boolean} The component geometry was successfully added
147
addComponent: function(component, index) {
150
if(this.componentTypes == null ||
151
(OpenLayers.Util.indexOf(this.componentTypes,
152
component.CLASS_NAME) > -1)) {
154
if(index != null && (index < this.components.length)) {
155
var components1 = this.components.slice(0, index);
156
var components2 = this.components.slice(index,
157
this.components.length);
158
components1.push(component);
159
this.components = components1.concat(components2);
161
this.components.push(component);
163
component.parent = this;
172
* APIMethod: removeComponents
173
* Remove components from this geometry.
176
* components - {Array(<OpenLayers.Geometry>)} The components to be removed
178
removeComponents: function(components) {
179
if(!(components instanceof Array)) {
180
components = [components];
182
for(var i=components.length-1; i>=0; --i) {
183
this.removeComponent(components[i]);
188
* Method: removeComponent
189
* Remove a component from this geometry.
192
* component - {<OpenLayers.Geometry>}
194
removeComponent: function(component) {
196
OpenLayers.Util.removeItem(this.components, component);
198
// clearBounds() so that it gets recalculated on the next call
199
// to this.getBounds();
204
* APIMethod: getLength
205
* Calculate the length of this geometry
208
* {Float} The length of the geometry
210
getLength: function() {
212
for (var i=0, len=this.components.length; i<len; i++) {
213
length += this.components[i].getLength();
220
* Calculate the area of this geometry. Note how this function is overridden
221
* in <OpenLayers.Geometry.Polygon>.
224
* {Float} The area of the collection by summing its parts
226
getArea: function() {
228
for (var i=0, len=this.components.length; i<len; i++) {
229
area += this.components[i].getArea();
236
* Moves a collection in place
239
* x - {Float} The x-displacement (in map units)
240
* y - {Float} The y-displacement (in map units)
242
move: function(x, y) {
243
for(var i=0, len=this.components.length; i<len; i++) {
244
this.components[i].move(x, y);
250
* Rotate a geometry around some origin
253
* angle - {Float} Rotation angle in degrees (measured counterclockwise
254
* from the positive x-axis)
255
* origin - {<OpenLayers.Geometry.Point>} Center point for the rotation
257
rotate: function(angle, origin) {
258
for(var i=0, len=this.components.length; i<len; ++i) {
259
this.components[i].rotate(angle, origin);
265
* Resize a geometry relative to some origin. Use this method to apply
266
* a uniform scaling to a geometry.
269
* scale - {Float} Factor by which to scale the geometry. A scale of 2
270
* doubles the size of the geometry in each dimension
271
* (lines, for example, will be twice as long, and polygons
272
* will have four times the area).
273
* origin - {<OpenLayers.Geometry.Point>} Point of origin for resizing
274
* ratio - {Float} Optional x:y ratio for resizing. Default ratio is 1.
276
resize: function(scale, origin, ratio) {
277
for(var i=0; i<this.components.length; ++i) {
278
this.components[i].resize(scale, origin, ratio);
284
* Tests for equivalent geometries
287
* geometry - {<OpenLayers.Geometry>}
290
* {Boolean} The coordinates are equivalent
292
equals: function(geometry) {
293
var equivalent = true;
294
if(!geometry || !geometry.CLASS_NAME ||
295
(this.CLASS_NAME != geometry.CLASS_NAME)) {
297
} else if(!(geometry.components instanceof Array) ||
298
(geometry.components.length != this.components.length)) {
301
for(var i=0, len=this.components.length; i<len; ++i) {
302
if(!this.components[i].equals(geometry.components[i])) {
312
* APIMethod: transform
313
* Reproject the components geometry from source to dest.
316
* source - {<OpenLayers.Projection>}
317
* dest - {<OpenLayers.Projection>}
320
* {<OpenLayers.Geometry>}
322
transform: function(source, dest) {
323
if (source && dest) {
324
for (var i=0, len=this.components.length; i<len; i++) {
325
var component = this.components[i];
326
component.transform(source, dest);
334
* APIMethod: intersects
335
* Determine if the input geometry intersects this one.
338
* geometry - {<OpenLayers.Geometry>} Any type of geometry.
341
* {Boolean} The input geometry intersects this one.
343
intersects: function(geometry) {
344
var intersect = false;
345
for(var i=0, len=this.components.length; i<len; ++ i) {
346
intersect = geometry.intersects(this.components[i]);
354
CLASS_NAME: "OpenLayers.Geometry.Collection"