Project

General

Profile

1
/* Copyright (c) 2006 MetaCarta, Inc., published under the BSD license.
2
 * See http://svn.openlayers.org/trunk/openlayers/license.txt for the full
3
 * text of the license. */
4

    
5

    
6
/*********************
7
 *                   *
8
 *      PIXEL        * 
9
 *                   * 
10
 *********************/
11

    
12

    
13
/**
14
 * @class 
15
 * 
16
 * This class represents a screen coordinate, in x and y coordinates
17
 */
18
OpenLayers.Pixel = Class.create();
19
OpenLayers.Pixel.prototype = {
20
    
21
    /** @type float */
22
    x: 0.0,
23

    
24
    /** @type float */
25
    y: 0.0,
26
    
27
    /** 
28
    * @constructor
29
    *
30
    * @param {float} x
31
    * @param {float} y
32
    */
33
    initialize: function(x, y) {
34
        this.x = x;
35
        this.y = y;
36
    },
37
    
38
    /**
39
    * @return string representation of Pixel. ex: "x=200.4,y=242.2"
40
    * @type str
41
    */
42
    toString:function() {
43
        return ("x=" + this.x + ",y=" + this.y);
44
    },
45

    
46
    /**
47
     * @type OpenLayers.Pixel
48
     */
49
    clone:function() {
50
        return new OpenLayers.Pixel(this.x, this.y); 
51
    },
52
    
53
    /** 
54
    * @param {OpenLayers.Pixel} px
55
    * 
56
    * @return whether or not the point passed in as parameter is equal to this
57
    *          note that if px passed in is null, returns false
58
    * @type bool
59
    */
60
    equals:function(px) {
61
        var equals = false;
62
        if (px != null) {
63
            equals = ((this.x == px.x && this.y == px.y) ||
64
                      (isNaN(this.x) && isNaN(this.y) && isNaN(px.x) && isNaN(px.y)));
65
        }
66
        return equals;
67
    },
68

    
69
    /**
70
    * @param {int} x
71
    * @param {int} y
72
    * 
73
    * @return a new Pixel with this pixel's x&y augmented by the 
74
    *         values passed in.
75
    * @type OpenLayers.Pixel
76
    */
77
    add:function(x, y) {
78
        return new OpenLayers.Pixel(this.x + x, this.y + y);
79
    },
80

    
81
    /**
82
    * @param {OpenLayers.Pixel} px
83
    * 
84
    * @return a new Pixel with this pixel's x&y augmented by the 
85
    *         x&y values of the pixel passed in.
86
    * @type OpenLayers.Pixel
87
    */
88
    offset:function(px) {
89
        return this.add(px.x, px.y);                
90
    },
91
    
92
    /** @final @type str */
93
    CLASS_NAME: "OpenLayers.Pixel"
94
};
95

    
96

    
97
/*********************
98
 *                   *
99
 *      SIZE         * 
100
 *                   * 
101
 *********************/
102

    
103

    
104
/**
105
* @class 
106
* 
107
* This class represents a width and height pair
108
*/
109
OpenLayers.Size = Class.create();
110
OpenLayers.Size.prototype = {
111

    
112
    /** @type float */
113
    w: 0.0,
114
    
115
    /** @type float */
116
    h: 0.0,
117

    
118

    
119
    /** 
120
    * @constructor
121
    * 
122
    * @param {float} w 
123
    * @param {float} h 
124
    */
125
    initialize: function(w, h) {
126
        this.w = w;
127
        this.h = h;
128
    },
129

    
130
    /** 
131
    * @return String representation of OpenLayers.Size object. 
132
    *         (ex. <i>"w=55,h=66"</i>)
133
    * @type String
134
    */
135
    toString:function() {
136
        return ("w=" + this.w + ",h=" + this.h);
137
    },
138

    
139
    /** 
140
     * @return New OpenLayers.Size object with the same w and h values
141
     * @type OpenLayers.Size
142
     */
143
    clone:function() {
144
        return new OpenLayers.Size(this.w, this.h);
145
    },
146

    
147
    /** 
148
    * @param {OpenLayers.Size} sz
149
    * @returns Boolean value indicating whether the passed-in OpenLayers.Size 
150
    *          object has the same w and h components as this
151
    *          note that if sz passed in is null, returns false
152
    *
153
    * @type bool
154
    */
155
    equals:function(sz) {
156
        var equals = false;
157
        if (sz != null) {
158
            equals = ((this.w == sz.w && this.h == sz.h) ||
159
                      (isNaN(this.w) && isNaN(this.h) && isNaN(sz.w) && isNaN(sz.h)));
160
        }
161
        return equals;
162
    },
163
    
164
    /** @final @type String */
165
    CLASS_NAME: "OpenLayers.Size"
166
};
167

    
168
/*********************
169
 *                   *
170
 *      LONLAT       * 
171
 *                   * 
172
 *********************/
173

    
174

    
175
/**
176
* @class 
177
* 
178
* This class represents a longitude and latitude pair
179
*/
180
OpenLayers.LonLat = Class.create();
181
OpenLayers.LonLat.prototype = {
182

    
183
    /** @type float */
184
    lon: 0.0,
185
    
186
    /** @type float */
187
    lat: 0.0,
188

    
189
    /**
190
    * @constructor
191
    * 
192
    * @param {float} lon
193
    * @param {float} lat
194
    */
195
    initialize: function(lon, lat) {
196
        this.lon = lon;
197
        this.lat = lat;
198
    },
199
    
200
    /** 
201
    * @return String representation of OpenLayers.LonLat object. 
202
    *         (ex. <i>"lon=5,lat=42"</i>)
203
    * @type String
204
    */
205
    toString:function() {
206
        return ("lon=" + this.lon + ",lat=" + this.lat);
207
    },
208

    
209
    /** 
210
    * @return Shortened String representation of OpenLayers.LonLat object. 
211
    *         (ex. <i>"5, 42"</i>)
212
    * @type String
213
    */
214
    toShortString:function() {
215
        return (this.lon + ", " + this.lat);
216
    },
217

    
218
    /** 
219
     * @return New OpenLayers.LonLat object with the same lon and lat values
220
     * @type OpenLayers.LonLat
221
     */
222
    clone:function() {
223
        return new OpenLayers.LonLat(this.lon, this.lat);
224
    },
225

    
226
    /** 
227
    * @param {float} lon
228
    * @param {float} lat
229
    *
230
    * @return A new OpenLayers.LonLat object with the lon and lat passed-in
231
    *         added to this's. 
232
    * @type OpenLayers.LonLat
233
    */
234
    add:function(lon, lat) {
235
        return new OpenLayers.LonLat(this.lon + lon, this.lat + lat);
236
    },
237

    
238
    /** 
239
    * @param {OpenLayers.LonLat} ll
240
    * @returns Boolean value indicating whether the passed-in OpenLayers.LonLat
241
    *          object has the same lon and lat components as this
242
    *          note that if ll passed in is null, returns false
243
    *
244
    * @type bool
245
    */
246
    equals:function(ll) {
247
        var equals = false;
248
        if (ll != null) {
249
            equals = ((this.lon == ll.lon && this.lat == ll.lat) ||
250
                      (isNaN(this.lon) && isNaN(this.lat) && isNaN(ll.lon) && isNaN(ll.lat)));
251
        }
252
        return equals;
253
    },
254
    
255
    /** @final @type String */
256
    CLASS_NAME: "OpenLayers.LonLat"
257
};
258

    
259
/** Alternative constructor that builds a new OpenLayers.LonLat from a 
260
*    parameter string
261
* 
262
* @constructor
263
* 
264
* @param {String} str Comma-separated Lon,Lat coordinate string. 
265
*                     (ex. <i>"5,40"</i>)
266
*
267
* @returns New OpenLayers.LonLat object built from the passed-in String.
268
* @type OpenLayers.LonLat
269
*/
270
OpenLayers.LonLat.fromString = function(str) {
271
    var pair = str.split(",");
272
    return new OpenLayers.LonLat(parseFloat(pair[0]), 
273
                                 parseFloat(pair[1]));
274
};
275

    
276

    
277

    
278
/*********************
279
 *                   *
280
 *      BOUNDS       * 
281
 *                   * 
282
 *********************/
283

    
284

    
285

    
286

    
287
/**
288
* @class 
289
* 
290
* This class represents a bounding box. 
291
* Data stored as left, bottom, right, top floats
292
*/
293
OpenLayers.Bounds = Class.create();
294
OpenLayers.Bounds.prototype = {
295

    
296
    /** @type float */
297
    left: 0.0,
298

    
299
    /** @type float */
300
    bottom: 0.0,
301

    
302
    /** @type float */
303
    right: 0.0,
304

    
305
    /** @type float */
306
    top: 0.0,    
307

    
308
    /**
309
    * @constructor
310
    *
311
    * @param {float} left
312
    * @param {float} bottom
313
    * @param {float} right
314
    * @param {float} top
315
    *
316
    */
317
    initialize: function(left, bottom, right, top) {
318
        this.left = left;
319
        this.bottom = bottom;
320
        this.right = right;
321
        this.top = top;
322
    },
323

    
324
    /**
325
     * @returns A fresh copy of the bounds
326
     * @type OpenLayers.Bounds
327
     */
328
    clone:function() {
329
        return new OpenLayers.Bounds(this.left, this.bottom, 
330
                                     this.right, this.top);
331
    },
332

    
333
    /** 
334
    * @param {OpenLayers.Bounds} bounds
335
    * @returns Boolean value indicating whether the passed-in OpenLayers.Bounds
336
    *          object has the same left, right, top, bottom components as this
337
    *           note that if bounds passed in is null, returns false
338
    *
339
    * @type bool
340
    */
341
    equals:function(bounds) {
342
        var equals = false;
343
        if (bounds != null) {
344
            equals = ((this.left == bounds.left) && 
345
                      (this.right == bounds.right) &&
346
                      (this.top == bounds.top) && 
347
                      (this.bottom == bounds.bottom));
348
        }
349
        return equals;
350
    },
351

    
352
    /** 
353
    * @return String representation of OpenLayers.Bounds object. 
354
    *         (ex.<i>"left-bottom=(5,42) right-top=(10,45)"</i>)
355
    * @type String
356
    */
357
    toString:function(){
358
        return ( "left-bottom=(" + this.left + "," + this.bottom + ")"
359
                 + " right-top=(" + this.right + "," + this.top + ")" );
360
    },
361

    
362
    /** 
363
    * @return Simple String representation of OpenLayers.Bounds object.
364
    *         (ex. <i>"5,42,10,45"</i>)
365
    * @type String
366
    */
367
    toBBOX:function() {
368
        return (this.left + "," + this.bottom + ","
369
                + this.right + "," + this.top);
370
    },
371
    
372
    /**
373
    * @returns The width of the bounds
374
    * @type float
375
    */
376
    getWidth:function() {
377
        return (this.right - this.left);
378
    },
379

    
380
    /**
381
    * @returns The height of the bounds
382
    * @type float
383
    */
384
    getHeight:function() {
385
        return (this.top - this.bottom);
386
    },
387

    
388
    /**
389
    * @returns An OpenLayers.Size which represents the size of the box
390
    * @type OpenLayers.Size
391
    */
392
    getSize:function() {
393
        return new OpenLayers.Size(this.getWidth(), this.getHeight());
394
    },
395

    
396
    /**
397
    * @returns An OpenLayers.Pixel which represents the center of the bounds
398
    * @type OpenLayers.Pixel
399
    */
400
    getCenterPixel:function() {
401
        return new OpenLayers.Pixel( (this.left + this.right) / 2,
402
                                     (this.bottom + this.top) / 2);
403
    },
404

    
405
    /**
406
    * @returns An OpenLayers.LonLat which represents the center of the bounds
407
    * @type OpenLayers.LonLat
408
    */
409
    getCenterLonLat:function() {
410
        return new OpenLayers.LonLat( (this.left + this.right) / 2,
411
                                      (this.bottom + this.top) / 2);
412
    },
413

    
414
    /**
415
    * @param {float} x
416
    * @param {float} y
417
    *
418
    * @returns A new OpenLayers.Bounds whose coordinates are the same as this, 
419
    *          but shifted by the passed-in x and y values
420
    * @type OpenLayers.Bounds
421
    */
422
    add:function(x, y){
423
        return new OpenLayers.Bounds(this.left + x, this.bottom + y,
424
                                     this.right + x, this.top + y);
425
    },
426

    
427
    /**
428
    * @param {float} x
429
    * @param {float} y
430
    * @param {Boolean} inclusive Whether or not to include the border. 
431
    *                            Default is true
432
    *
433
    * @return Whether or not the passed-in coordinates are within this bounds
434
    * @type Boolean
435
    */
436
    contains:function(x, y, inclusive) {
437
    
438
        //set default
439
        if (inclusive == null) {
440
            inclusive = true;
441
        }
442
        
443
        var contains = false;
444
        if (inclusive) {
445
            contains = ((x >= this.left) && (x <= this.right) && 
446
                        (y >= this.bottom) && (y <= this.top));
447
        } else {
448
            contains = ((x > this.left) && (x < this.right) && 
449
                        (y > this.bottom) && (y < this.top));
450
        }              
451
        return contains;
452
    },
453
 
454
    /**
455
    * @param {OpenLayers.Bounds} bounds
456
    * @param {Boolean} partial If true, only part of passed-in 
457
    *                          OpenLayers.Bounds needs be within this bounds. 
458
    *                          If false, the entire passed-in bounds must be
459
    *                          within. Default is false
460
    * @param {Boolean} inclusive Whether or not to include the border. 
461
    *                            Default is true
462
    *
463
    * @return Whether or not the passed-in OpenLayers.Bounds object is 
464
    *         contained within this bounds. 
465
    * @type Boolean
466
    */
467
    containsBounds:function(bounds, partial, inclusive) {
468

    
469
        //set defaults
470
        if (partial == null) {
471
            partial = false;
472
        }
473
        if (inclusive == null) {
474
            inclusive = true;
475
        }
476

    
477
        var inLeft;
478
        var inTop;
479
        var inRight;
480
        var inBottom;
481
        
482
        if (inclusive) {
483
            inLeft = (bounds.left >= this.left) && (bounds.left <= this.right);
484
            inTop = (bounds.top >= this.bottom) && (bounds.top <= this.top);
485
            inRight= (bounds.right >= this.left) && (bounds.right <= this.right);
486
            inBottom = (bounds.bottom >= this.bottom) && (bounds.bottom <= this.top);
487
        } else {
488
            inLeft = (bounds.left > this.left) && (bounds.left < this.right);
489
            inTop = (bounds.top > this.bottom) && (bounds.top < this.top);
490
            inRight= (bounds.right > this.left) && (bounds.right < this.right);
491
            inBottom = (bounds.bottom > this.bottom) && (bounds.bottom < this.top);
492
        }
493
        
494
        return (partial) ? (inTop || inBottom) && (inLeft || inRight )
495
                         : (inTop && inLeft && inBottom && inRight);
496
    },
497

    
498
    /** 
499
     * @param {OpenLayers.LonLat} lonlat
500
     *
501
     * @returns The quadrant ("br" "tr" "tl" "bl") of the bounds in which 
502
     *           the coordinate lies.
503
     * @type String
504
     */
505
    determineQuadrant: function(lonlat) {
506
    
507
        var quadrant = "";
508
        var center = this.getCenterLonLat();
509
        
510
        quadrant += (lonlat.lat < center.lat) ? "b" : "t";
511
        quadrant += (lonlat.lon < center.lon) ? "l" : "r";
512
    
513
        return quadrant; 
514
    },
515

    
516
    /** @final @type String */
517
    CLASS_NAME: "OpenLayers.Bounds"
518
};
519

    
520
/** Alternative constructor that builds a new OpenLayers.Bounds from a 
521
*    parameter string
522
* 
523
* @constructor
524
* 
525
* @param {String} str Comma-separated bounds string. (ex. <i>"5,42,10,45"</i>)
526
*
527
* @returns New OpenLayers.Bounds object built from the passed-in String.
528
* @type OpenLayers.Bounds
529
*/
530
OpenLayers.Bounds.fromString = function(str) {
531
    var bounds = str.split(",");
532
    return OpenLayers.Bounds.fromArray(bounds);
533
};
534

    
535
/** Alternative constructor that builds a new OpenLayers.Bounds
536
*    from an array
537
* 
538
* @constructor
539
* 
540
* @param {Array} bbox Array of bounds values (ex. <i>[5,42,10,45]</i>)
541
*
542
* @returns New OpenLayers.Bounds object built from the passed-in Array.
543
* @type OpenLayers.Bounds
544
*/
545
OpenLayers.Bounds.fromArray = function(bbox) {
546
    return new OpenLayers.Bounds(parseFloat(bbox[0]),
547
                                 parseFloat(bbox[1]),
548
                                 parseFloat(bbox[2]),
549
                                 parseFloat(bbox[3]));
550
};
551

    
552
/** Alternative constructor that builds a new OpenLayers.Bounds
553
*    from an OpenLayers.Size
554
* 
555
* @constructor
556
* 
557
* @param {OpenLayers.Size} size
558
*            
559
* @returns New OpenLayers.Bounds object built with top and left set to 0 and
560
*           bottom right taken from the passed-in OpenLayers.Size.
561
* @type OpenLayers.Bounds
562
*/
563
OpenLayers.Bounds.fromSize = function(size) {
564
    return new OpenLayers.Bounds(0,
565
                                 size.h,
566
                                 size.w,
567
                                 0);
568
};
569
/**
570
 * @param {String} quadrant 
571
 * 
572
 * @returns The opposing quadrant ("br" "tr" "tl" "bl"). For Example, if 
573
 *           you pass in "bl" it returns "tr", if you pass in "br" it 
574
 *           returns "tl", etc.
575
 * @type String
576
 */
577
OpenLayers.Bounds.oppositeQuadrant = function(quadrant) {
578
    var opp = "";
579
    
580
    opp += (quadrant.charAt(0) == 't') ? 'b' : 't';
581
    opp += (quadrant.charAt(1) == 'l') ? 'r' : 'l';
582
    
583
    return opp;
584
};
585

    
586

    
587

    
588

    
589
/*********************
590
 *                   *
591
 *      STRING       * 
592
 *                   * 
593
 *********************/
594

    
595

    
596

    
597
/**
598
* @param {String} sStart
599
* 
600
* @returns Whether or not this string starts with the string passed in.
601
* @type Boolean
602
*/
603
String.prototype.startsWith = function(sStart){
604
    return (this.substr(0,sStart.length) == sStart);
605
};
606

    
607
/**
608
* @param {String} str
609
* 
610
* @returns Whether or not this string contains with the string passed in.
611
* @type Boolean
612
*/
613
String.prototype.contains = function(str){
614
    return (this.indexOf(str) != -1);
615
};
616

    
617
/**
618
* @returns A trimmed version of the string - all leading and 
619
*          trailing spaces removed
620
* @type String
621
*/
622
String.prototype.trim = function() {
623
    
624
    var b = 0;
625
    while(this.substr(b,1) == " ") {
626
        b++;
627
    }
628
    
629
    var e = this.length - 1;
630
    while(this.substr(e,1) == " ") {
631
        e--;
632
    }
633
    
634
    return this.substring(b, e+1);
635
};
636

    
637

    
638

    
639

    
640
/*********************
641
 *                   *
642
 *      ARRAY        * 
643
 *                   * 
644
 *********************/
645

    
646

    
647

    
648
/** Remove an object from an array. Iterates through the array
649
*    to find the item, then removes it.
650
*
651
* @param {Object} item
652
* 
653
* @returns A reference to the array
654
* @type Array
655
*/
656
Array.prototype.remove = function(item) {
657
    for(var i=0; i < this.length; i++) {
658
        if(this[i] == item) {
659
            this.splice(i,1);
660
            //break;more than once??
661
        }
662
    }
663
    return this;
664
}
665

    
666
/**
667
* @returns A fresh copy of the array
668
* @type Array
669
*/
670
Array.prototype.clone = function() {
671
  var clone = new Array();
672
  for (var i = 0; i < this.length; i++) {
673
      clone[i] = this[i];
674
  }
675
  return clone;
676
};
677

    
678
/**
679
*/
680
Array.prototype.clear = function() {
681
    this.length = 0;
682
};
683

    
684

    
685
/*********************
686
 *                   *
687
 *      NUMBER       * 
688
 *                   * 
689
 *********************/
690

    
691

    
692

    
693
/** NOTE: Works only with integer values does *not* work with floats!
694
 * 
695
 * @param {int} sig
696
 * 
697
 * @returns The number, rounded to the specified number of significant digits.
698
 *          If null, 0, or negaive value passed in, returns 0
699
 * @type int
700
 */
701
Number.prototype.limitSigDigs = function(sig) {
702
    var number = (sig > 0) ? this.toString() : 0;
703
    if (sig < number.length) {
704
        var exp = number.length - sig;
705
        number = Math.round( this / Math.pow(10, exp)) * Math.pow(10, exp);
706
    }
707
    return parseInt(number);
708
}
(2-2/13)