~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to gis/dhis-gis-geostat/mfbase/openlayers/lib/OpenLayers/Geometry/Collection.js

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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. */
 
4
 
 
5
/**
 
6
 * @requires OpenLayers/Geometry.js
 
7
 */
 
8
 
 
9
/**
 
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). 
 
14
 * 
 
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).
 
18
 * 
 
19
 * The <getArea> and <getLength> functions here merely iterate through
 
20
 * the components, summing their respective areas and lengths.
 
21
 *
 
22
 * Create a new instance with the <OpenLayers.Geometry.Collection> constructor.
 
23
 *
 
24
 * Inerhits from:
 
25
 *  - <OpenLayers.Geometry> 
 
26
 */
 
27
OpenLayers.Geometry.Collection = OpenLayers.Class(OpenLayers.Geometry, {
 
28
 
 
29
    /**
 
30
     * APIProperty: components
 
31
     * {Array(<OpenLayers.Geometry>)} The component parts of this geometry
 
32
     */
 
33
    components: null,
 
34
    
 
35
    /**
 
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.
 
40
     */
 
41
    componentTypes: null,
 
42
 
 
43
    /**
 
44
     * Constructor: OpenLayers.Geometry.Collection
 
45
     * Creates a Geometry Collection -- a list of geoms.
 
46
     *
 
47
     * Parameters: 
 
48
     * components - {Array(<OpenLayers.Geometry>)} Optional array of geometries
 
49
     *
 
50
     */
 
51
    initialize: function (components) {
 
52
        OpenLayers.Geometry.prototype.initialize.apply(this, arguments);
 
53
        this.components = [];
 
54
        if (components != null) {
 
55
            this.addComponents(components);
 
56
        }
 
57
    },
 
58
 
 
59
    /**
 
60
     * APIMethod: destroy
 
61
     * Destroy this geometry.
 
62
     */
 
63
    destroy: function () {
 
64
        this.components.length = 0;
 
65
        this.components = null;
 
66
    },
 
67
 
 
68
    /**
 
69
     * APIMethod: clone
 
70
     * Clone this geometry.
 
71
     *
 
72
     * Returns:
 
73
     * {<OpenLayers.Geometry.Collection>} An exact clone of this collection
 
74
     */
 
75
    clone: function() {
 
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());
 
79
        }
 
80
        
 
81
        // catch any randomly tagged-on properties
 
82
        OpenLayers.Util.applyDefaults(geometry, this);
 
83
        
 
84
        return geometry;
 
85
    },
 
86
 
 
87
    /**
 
88
     * Method: getComponentsString
 
89
     * Get a string representing the components for this collection
 
90
     * 
 
91
     * Returns:
 
92
     * {String} A string representation of the components of this geometry
 
93
     */
 
94
    getComponentsString: function(){
 
95
        var strings = [];
 
96
        for(var i=0, len=this.components.length; i<len; i++) {
 
97
            strings.push(this.components[i].toShortString()); 
 
98
        }
 
99
        return strings.join(",");
 
100
    },
 
101
 
 
102
    /**
 
103
     * APIMethod: calculateBounds
 
104
     * Recalculate the bounds by iterating through the components and 
 
105
     * calling calling extendBounds() on each item.
 
106
     */
 
107
    calculateBounds: function() {
 
108
        this.bounds = null;
 
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());
 
113
            }
 
114
        }
 
115
    },
 
116
 
 
117
    /**
 
118
     * APIMethod: addComponents
 
119
     * Add components to this geometry.
 
120
     *
 
121
     * Parameters:
 
122
     * components - {Array(<OpenLayers.Geometry>)} An array of geometries to add
 
123
     */
 
124
    addComponents: function(components){
 
125
        if(!(components instanceof Array)) {
 
126
            components = [components];
 
127
        }
 
128
        for(var i=0, len=components.length; i<len; i++) {
 
129
            this.addComponent(components[i]);
 
130
        }
 
131
    },
 
132
 
 
133
    /**
 
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.
 
137
     *
 
138
     * The bounds cache is reset.
 
139
     * 
 
140
     * Parameters:
 
141
     * component - {<OpenLayers.Geometry>} A geometry to add
 
142
     * index - {int} Optional index into the array to insert the component
 
143
     *
 
144
     * Returns:
 
145
     * {Boolean} The component geometry was successfully added
 
146
     */    
 
147
    addComponent: function(component, index) {
 
148
        var added = false;
 
149
        if(component) {
 
150
            if(this.componentTypes == null ||
 
151
               (OpenLayers.Util.indexOf(this.componentTypes,
 
152
                                        component.CLASS_NAME) > -1)) {
 
153
 
 
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);
 
160
                } else {
 
161
                    this.components.push(component);
 
162
                }
 
163
                component.parent = this;
 
164
                this.clearBounds();
 
165
                added = true;
 
166
            }
 
167
        }
 
168
        return added;
 
169
    },
 
170
    
 
171
    /**
 
172
     * APIMethod: removeComponents
 
173
     * Remove components from this geometry.
 
174
     *
 
175
     * Parameters:
 
176
     * components - {Array(<OpenLayers.Geometry>)} The components to be removed
 
177
     */
 
178
    removeComponents: function(components) {
 
179
        if(!(components instanceof Array)) {
 
180
            components = [components];
 
181
        }
 
182
        for(var i=components.length-1; i>=0; --i) {
 
183
            this.removeComponent(components[i]);
 
184
        }
 
185
    },
 
186
    
 
187
    /**
 
188
     * Method: removeComponent
 
189
     * Remove a component from this geometry.
 
190
     *
 
191
     * Parameters:
 
192
     * component - {<OpenLayers.Geometry>} 
 
193
     */
 
194
    removeComponent: function(component) {
 
195
        
 
196
        OpenLayers.Util.removeItem(this.components, component);
 
197
        
 
198
        // clearBounds() so that it gets recalculated on the next call
 
199
        // to this.getBounds();
 
200
        this.clearBounds();
 
201
    },
 
202
 
 
203
    /**
 
204
     * APIMethod: getLength
 
205
     * Calculate the length of this geometry
 
206
     *
 
207
     * Returns:
 
208
     * {Float} The length of the geometry
 
209
     */
 
210
    getLength: function() {
 
211
        var length = 0.0;
 
212
        for (var i=0, len=this.components.length; i<len; i++) {
 
213
            length += this.components[i].getLength();
 
214
        }
 
215
        return length;
 
216
    },
 
217
    
 
218
    /**
 
219
     * APIMethod: getArea
 
220
     * Calculate the area of this geometry. Note how this function is overridden
 
221
     * in <OpenLayers.Geometry.Polygon>.
 
222
     *
 
223
     * Returns:
 
224
     * {Float} The area of the collection by summing its parts
 
225
     */
 
226
    getArea: function() {
 
227
        var area = 0.0;
 
228
        for (var i=0, len=this.components.length; i<len; i++) {
 
229
            area += this.components[i].getArea();
 
230
        }
 
231
        return area;
 
232
    },
 
233
 
 
234
    /**
 
235
     * APIMethod: move
 
236
     * Moves a collection in place
 
237
     *
 
238
     * Parameters:
 
239
     * x - {Float} The x-displacement (in map units)
 
240
     * y - {Float} The y-displacement (in map units)
 
241
     */
 
242
    move: function(x, y) {
 
243
        for(var i=0, len=this.components.length; i<len; i++) {
 
244
            this.components[i].move(x, y);
 
245
        }
 
246
    },
 
247
 
 
248
    /**
 
249
     * APIMethod: rotate
 
250
     * Rotate a geometry around some origin
 
251
     *
 
252
     * Parameters:
 
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
 
256
     */
 
257
    rotate: function(angle, origin) {
 
258
        for(var i=0, len=this.components.length; i<len; ++i) {
 
259
            this.components[i].rotate(angle, origin);
 
260
        }
 
261
    },
 
262
 
 
263
    /**
 
264
     * APIMethod: resize
 
265
     * Resize a geometry relative to some origin.  Use this method to apply
 
266
     *     a uniform scaling to a geometry.
 
267
     *
 
268
     * Parameters:
 
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.
 
275
     */
 
276
    resize: function(scale, origin, ratio) {
 
277
        for(var i=0; i<this.components.length; ++i) {
 
278
            this.components[i].resize(scale, origin, ratio);
 
279
        }
 
280
    },
 
281
 
 
282
    /**
 
283
     * APIMethod: equals
 
284
     * Tests for equivalent geometries
 
285
     *
 
286
     * Parameters:
 
287
     * geometry - {<OpenLayers.Geometry>}
 
288
     *
 
289
     * Returns:
 
290
     * {Boolean} The coordinates are equivalent
 
291
     */
 
292
    equals: function(geometry) {
 
293
        var equivalent = true;
 
294
        if(!geometry || !geometry.CLASS_NAME ||
 
295
           (this.CLASS_NAME != geometry.CLASS_NAME)) {
 
296
            equivalent = false;
 
297
        } else if(!(geometry.components instanceof Array) ||
 
298
                  (geometry.components.length != this.components.length)) {
 
299
            equivalent = false;
 
300
        } else {
 
301
            for(var i=0, len=this.components.length; i<len; ++i) {
 
302
                if(!this.components[i].equals(geometry.components[i])) {
 
303
                    equivalent = false;
 
304
                    break;
 
305
                }
 
306
            }
 
307
        }
 
308
        return equivalent;
 
309
    },
 
310
 
 
311
    /**
 
312
     * APIMethod: transform
 
313
     * Reproject the components geometry from source to dest.
 
314
     * 
 
315
     * Parameters:
 
316
     * source - {<OpenLayers.Projection>} 
 
317
     * dest - {<OpenLayers.Projection>}
 
318
     * 
 
319
     * Returns:
 
320
     * {<OpenLayers.Geometry>} 
 
321
     */
 
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);
 
327
            }
 
328
            this.bounds = null;
 
329
        }
 
330
        return this;
 
331
    },
 
332
 
 
333
    /**
 
334
     * APIMethod: intersects
 
335
     * Determine if the input geometry intersects this one.
 
336
     *
 
337
     * Parameters:
 
338
     * geometry - {<OpenLayers.Geometry>} Any type of geometry.
 
339
     *
 
340
     * Returns:
 
341
     * {Boolean} The input geometry intersects this one.
 
342
     */
 
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]);
 
347
            if(intersect) {
 
348
                break;
 
349
            }
 
350
        }
 
351
        return intersect;
 
352
    },
 
353
 
 
354
    CLASS_NAME: "OpenLayers.Geometry.Collection"
 
355
});