summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Geometry/Collection.html
blob: 7c9fd62e0597b5674bfa125174449ecf52c4cbed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">
    var coll;
        
    function test_Collection_constructor (t) {
        t.plan( 4 );
        
      //null param
        coll = new OpenLayers.Geometry.Collection();
        t.ok( coll instanceof OpenLayers.Geometry.Collection, "new OpenLayers.Geometry.Collection returns coll object" );
        t.eq( coll.CLASS_NAME, "OpenLayers.Geometry.Collection", "coll.CLASS_NAME is set correctly");
        t.eq( coll.components.length, 0, "coll.components is set correctly");
        
        OpenLayers.Geometry.Collection.prototype._addComponents = 
            OpenLayers.Geometry.Collection.prototype.addComponents;
        OpenLayers.Geometry.Collection.prototype.addComponents = 
            function(comps) { g_addcomponents = comps; };

      //valid param
        g_addcomponents = null;
        var components = {};
        coll = new OpenLayers.Geometry.Collection(components);
        t.ok(g_addcomponents, components, "addcomponents called on non-null param")
        
        OpenLayers.Geometry.Collection.prototype.addComponents = 
            OpenLayers.Geometry.Collection.prototype._addComponents;
    }

    function test_Collection_addComponents (t) {
        t.plan( 10 );

        coll = new OpenLayers.Geometry.Collection();

      //null
        coll.addComponents(null);
        t.ok(true, "doesn't break on add null components");

        OpenLayers.Geometry.Collection.prototype._addComponent = 
            OpenLayers.Geometry.Collection.prototype.addComponent;

        OpenLayers.Geometry.Collection.prototype.addComponent = 
            function(comp) { g_addComp = comp; g_added++};
            

      //nonarray argument
        var g_added = 0;
        var g_addComp = 0;
        var component = {};
        coll.addComponents(component);
        t.eq(g_added, 1, "added once");
        t.eq(g_addComp, component, "added component");
        
      //array arg
        var g_added = 0;
        var g_addComp = 0;
        var component1 = {};
        var component2 = {};
        coll.addComponents([component1, component2]);
        t.eq(g_added, 2, "added twice");
        t.eq(g_addComp, component2, "added component");

        OpenLayers.Geometry.Collection.prototype.addComponent = 
            OpenLayers.Geometry.Collection.prototype._addComponent;



        coll.addComponents(new OpenLayers.Geometry.Point(0,0));
        coll.addComponents(new OpenLayers.Geometry.Point(10,10));
        t.eq( coll.components.length, 2, "added two components to collection" );
        bounds = coll.getBounds();
        t.eq( bounds.left, 0, "left bound is 0" );
        t.eq( bounds.bottom, 0, "bottom bound is 0" );
        t.eq( bounds.right, 10, "right bound is 10" );
        t.eq( bounds.top, 10, "top bound is 10" );
    }

    function test_Collection_clone (t) {
        t.plan( 3 );
        coll = new OpenLayers.Geometry.Collection();
        coll.addComponents(new OpenLayers.Geometry.Point(0,0));
        coll.addComponents(new OpenLayers.Geometry.Point(10,10));
        coll2 = coll.clone(); 
        t.ok( coll2 instanceof OpenLayers.Geometry.Collection, "coll.clone() returns collection object" );
        t.eq( coll2.components.length, 2, "coll2.components.length is set correctly");
        t.ok( coll2.components[0] instanceof OpenLayers.Geometry.Point,
            "coll2.components.length is set correctly");
    }

    function test_Collection_removeComponents (t) {
        t.plan( 5 );
        coll = new OpenLayers.Geometry.Collection();
        point = new OpenLayers.Geometry.Point(0,0);
        coll.addComponents(point);
        coll.addComponents(new OpenLayers.Geometry.Point(10,10));
        coll.removeComponents(coll.components[0]);
        t.eq( coll.components.length, 1, "coll.components.length is smaller after removeComponent" );
        t.ok( coll.bounds == null, "bounds are nullified after call to remove (to trigger recalc on getBounds()");
        bounds = coll.getBounds();
        t.eq( bounds.left, 10, "left bound is 10 after removeComponent" );
        t.eq( bounds.bottom, 10, "bottom bound is 10 after removeComponent" );
        
        coll = new OpenLayers.Geometry.Collection();
        for(var i=0; i<5; ++i) {
            coll.addComponents(
                new OpenLayers.Geometry.Point(Math.random(), Math.random())
            );
        }
        coll.removeComponents(coll.components);
        t.eq(coll.components.length, 0,
             "remove components even works with multiple components");
        
    }
    
    function test_Collection_calculateBounds(t) {
        t.plan( 9 );
        
        var coll = new OpenLayers.Geometry.Collection();
        coll.calculateBounds();
        t.eq(coll.bounds, null, "null components list gives null bounds on calculation()");

        var p1 = new OpenLayers.Geometry.Point(10,20);
        var p2 = new OpenLayers.Geometry.Point(30,40);
        
        var components = [p1, p2];
        coll = new OpenLayers.Geometry.Collection(components);
        
        coll.calculateBounds();
        
        t.eq(coll.bounds.left, 10, "good left bounds");
        t.eq(coll.bounds.bottom, 20, "good bottom bounds");
        t.eq(coll.bounds.right, 30, "good right bounds");
        t.eq(coll.bounds.top, 40, "good top bounds");

        var newPoint = new OpenLayers.Geometry.Point(60,70);
        coll.addComponent(newPoint);
        coll.calculateBounds();
        
        t.eq(coll.bounds.left, 10, "good left bounds");
        t.eq(coll.bounds.bottom, 20, "good bottom bounds");
        t.eq(coll.bounds.right, 60, "good right bounds");
        t.eq(coll.bounds.top, 70, "good top bounds");
    }
    
    function test_Collection_equals(t) {
        t.plan(1);
        var geom = new OpenLayers.Geometry.Collection();
        t.ok(!geom.equals(), "collection.equals() returns false for undefined");
    }

    function test_Collection_addComponent(t)   {
        t.plan(10);
        
        var coll = new OpenLayers.Geometry.Collection();
        
        //null
        coll.addComponent(null);
        t.ok(!coll.addComponent(null),
             "addComponent returns false for bad component")        

        //good component
        var component = new OpenLayers.Geometry.Point(3,4);
        t.ok(coll.addComponent(component),
             "addComponent returns true for good component");
        t.ok(coll.bounds == null, "bounds cache correctly cleared");         
         
        var foundComponent = false;
        for(var i=0; i< coll.components.length; i++) {
            if (coll.components[i].equals(component)) {
                foundComponent = true;
            }
        }
        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_collection_getLength(t) {
        t.plan(2);
        
      //null
        var coll = new OpenLayers.Geometry.Collection();
        t.eq( coll.getLength(), 0, "null coll has 0 getlength");
            
      //valid
        coll.components = [
            { 'getLength': function() { return 50; } },
            { 'getLength': function() { return 15; } }
        ];
        t.eq( coll.getLength(), 65, "coll with valid components correctly sums getlength");
    }
    
    function test_collection_getArea(t) {
        t.plan(2);
        
      //null
        var coll = new OpenLayers.Geometry.Collection();
        t.eq( coll.getArea(), 0, "null coll has 0 getArea");
            
      //valid
        coll.components = [
            { 'getArea': function() { return 50; } },
            { 'getArea': function() { return 15; } }
        ];
        t.eq( coll.getArea(), 65, "coll with valid components correctly sums getArea");
    }
    
    function test_transform(t) {
        t.plan(5);
        var p1 = new OpenLayers.Geometry.Point(0,0);
        p1.bounds = "foo";
        var p2 = new OpenLayers.Geometry.Point(1,1);
        p2.bounds = "foo";
        var line = new OpenLayers.Geometry.LineString([p1, p2]);
        var multipoint = new OpenLayers.Geometry.MultiPoint([p1, p2]);
        var coll = new OpenLayers.Geometry.Collection([
            p1, p2, line, multipoint
        ]);
        coll.bounds = "foo";
        
        var wgs84 = new OpenLayers.Projection("EPSG:4326");
        var sm = new OpenLayers.Projection("EPSG:900913");
        coll.transform(wgs84, sm);
        
        t.eq(coll.bounds, null, "coll bounds cleared");
        t.eq(p1.bounds, null, "p1 component bounds cleared");
        t.eq(p2.bounds, null, "p2 component bounds cleared");
        t.eq(line.bounds, null, "line component bounds cleared");
        t.eq(multipoint.bounds, null, "multipoint component bounds cleared");

    }

    function test_getCentroid_pts_only(t) {
        t.plan(3);

        coll = new OpenLayers.Geometry.Collection();
        coll.addComponent(new OpenLayers.Geometry.Point(0,0));
        coll.addComponent(new OpenLayers.Geometry.Point(1,1));

        centroid = coll.getCentroid(true);
        t.ok(centroid != null, 'The centroid is not null.');
        t.eq(centroid.x, 0.5, 'The centroid x coordinate is good.');
        t.eq(centroid.y, 0.5, 'The centroid y coordinate is good.');

        coll.destroy();
    }

    function test_getCentroid_poly_nonrecursive(t) {
        t.plan(3);

        coll = new OpenLayers.Geometry.Collection();
        coll.addComponent(
            new OpenLayers.Geometry.Polygon([
                new OpenLayers.Geometry.LinearRing([
                    new OpenLayers.Geometry.Point(0,0),
                    new OpenLayers.Geometry.Point(0,1),
                    new OpenLayers.Geometry.Point(1,1),
                    new OpenLayers.Geometry.Point(1,0)
                ])
            ])
        );
        // performing non-recursive getCentroid means this next polygon
        // is excluded from the centroid computation
        coll.addComponent(
            new OpenLayers.Geometry.Polygon([
                new OpenLayers.Geometry.LinearRing([
                    new OpenLayers.Geometry.Point(2,2),
                    new OpenLayers.Geometry.Point(2,3),
                    new OpenLayers.Geometry.Point(3,3),
                    new OpenLayers.Geometry.Point(3,2)
                ])
            ])
        );

        centroid = coll.getCentroid();
        t.ok(centroid != null, 'The centroid is not null.');
        t.eq(centroid.x, 0.5, 'The centroid x coordinate is good.');
        t.eq(centroid.y, 0.5, 'The centroid y coordinate is good.');

        coll.destroy();
    }

    function test_getCentroid_poly_only(t) {
        t.plan(3);

        coll = new OpenLayers.Geometry.Collection();
        coll.addComponent(
            new OpenLayers.Geometry.Polygon([
                    new OpenLayers.Geometry.LinearRing([
                    new OpenLayers.Geometry.Point(0,0),
                    new OpenLayers.Geometry.Point(0,1),
                    new OpenLayers.Geometry.Point(1,1),
                    new OpenLayers.Geometry.Point(1,0)
                ])
            ])
        );

        centroid = coll.getCentroid(true);
        t.ok(centroid != null, 'The centroid is not null.');
        t.eq(centroid.x, 0.5, 'The centroid x coordinate is good.');
        t.eq(centroid.y, 0.5, 'The centroid y coordinate is good.');

        coll.destroy();
    }

    function test_getCentroid_poly_and_pts(t) {
        t.plan(3);

        coll = new OpenLayers.Geometry.Collection();
        coll.addComponent(
            new OpenLayers.Geometry.Polygon([
                new OpenLayers.Geometry.LinearRing([
                    new OpenLayers.Geometry.Point(0,0),
                    new OpenLayers.Geometry.Point(0,1),
                    new OpenLayers.Geometry.Point(1,1),
                    new OpenLayers.Geometry.Point(1,0)
                ])
            ])
        );

        // since the polygon above has an area of 1 and these
        // points have an area of 0, they should not change the centroid
        coll.addComponent( new OpenLayers.Geometry.Point(2,2) );
        coll.addComponent( new OpenLayers.Geometry.Point(4,4) );

        centroid = coll.getCentroid(true);
        t.ok(centroid != null, 'The centroid is not null.');
        t.eq(centroid.x, 0.5, 'The centroid x coordinate is good.');
        t.eq(centroid.y, 0.5, 'The centroid y coordinate is good.');

        coll.destroy();
    }

    function test_getCentroid_poly_big_and_small(t) {
        t.plan(3);

        coll = new OpenLayers.Geometry.Collection();
        // polygon w/area=1, centroid=0.5,0.5
        coll.addComponent(
            new OpenLayers.Geometry.Polygon([
                new OpenLayers.Geometry.LinearRing([
                    new OpenLayers.Geometry.Point(0,0),
                    new OpenLayers.Geometry.Point(0,1),
                    new OpenLayers.Geometry.Point(1,1),
                    new OpenLayers.Geometry.Point(1,0)
                ])
            ])
        );

        // since the polygon above has an area of 1 and this
        // polygon has an area of 4, the center is weighted 20% toward
        // the first polygon

        // polygon w/area=4, centroid=5.5,5.5
        coll.addComponent(
            new OpenLayers.Geometry.Polygon([
                new OpenLayers.Geometry.LinearRing([
                    new OpenLayers.Geometry.Point(4.5,-0.5),
                    new OpenLayers.Geometry.Point(4.5,1.5),
                    new OpenLayers.Geometry.Point(6.5,1.5),
                    new OpenLayers.Geometry.Point(6.5,-0.5)
                ])
            ])
        );

        centroid = coll.getCentroid(true);
        t.ok(centroid != null, 'The centroid is not null.');
        t.eq(centroid.x, 4.5, 'The centroid x coordinate is good.');
        t.eq(centroid.y, 0.5, 'The centroid y coordinate is good.');

        coll.destroy();
    }

    function test_avoid_infinite_recursion(t) {
        t.plan(1);

        var g = new OpenLayers.Geometry.Polygon([
            new OpenLayers.Geometry.LinearRing(),
            new OpenLayers.Geometry.LinearRing()
        ]);
        var bounds;
        try {
            bounds = g.getBounds();
            t.eq(bounds, null, "Polygon with empty linear ring has null bounds");
        } catch (err) {
            t.fail("Failed to get bounds of polygon with empty linear ring: " + err.message);
        }

    }

    
    function test_Collection_destroy(t) {
        t.plan( 3 );
        coll = new OpenLayers.Geometry.Collection();
        coll.addComponents(new OpenLayers.Geometry.Point(0,0));
        coll.addComponents(new OpenLayers.Geometry.Point(10,10));
        coll.getBounds();
        coll.destroy(); 
        
        t.ok(coll.components == null, "components array cleared");
        t.ok(coll.getBounds() == null, "bounds is cleared");
        t.ok(coll.id == null, "id is cleared");

    }

  </script>
</head>
<body>
</body>
</html>