Bienvenue sur PostGIS.fr

Bienvenue sur PostGIS.fr , le site de la communauté des utilisateurs francophones de PostGIS.

PostGIS ajoute le support d'objets géographique à la base de données PostgreSQL. En effet, PostGIS "spatialise" le serverur PostgreSQL, ce qui permet de l'utiliser comme une base de données SIG.

Maintenu à jour, en fonction de nos disponibilités et des diverses sorties des outils que nous testons, nous vous proposons l'ensemble de nos travaux publiés en langue française.

source: trunk/workshop-routing-foss4g/web/OpenLayers/lib/OpenLayers/BaseTypes/Bounds.js @ 76

Revision 76, 21.4 KB checked in by djay, 12 years ago (diff)

Ajout du répertoire web

  • Property svn:executable set to *
Line 
1/* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for
2 * full list of contributors). Published under the Clear BSD license. 
3 * See http://svn.openlayers.org/trunk/openlayers/license.txt for the
4 * full text of the license. */
5
6/**
7 * @requires OpenLayers/Console.js
8 */
9
10/**
11 * Class: OpenLayers.Bounds
12 * Instances of this class represent bounding boxes.  Data stored as left,
13 * bottom, right, top floats. All values are initialized to null, however,
14 * you should make sure you set them before using the bounds for anything.
15 *
16 * Possible use case:
17 * > bounds = new OpenLayers.Bounds();
18 * > bounds.extend(new OpenLayers.LonLat(4,5));
19 * > bounds.extend(new OpenLayers.LonLat(5,6));
20 * > bounds.toBBOX(); // returns 4,5,5,6
21 */
22OpenLayers.Bounds = OpenLayers.Class({
23
24    /**
25     * Property: left
26     * {Number} Minimum horizontal coordinate.
27     */
28    left: null,
29
30    /**
31     * Property: bottom
32     * {Number} Minimum vertical coordinate.
33     */
34    bottom: null,
35
36    /**
37     * Property: right
38     * {Number} Maximum horizontal coordinate.
39     */
40    right: null,
41
42    /**
43     * Property: top
44     * {Number} Maximum vertical coordinate.
45     */
46    top: null,
47   
48    /**
49     * Property: centerLonLat
50     * {<OpenLayers.LonLat>} A cached center location.  This should not be
51     *     accessed directly.  Use <getCenterLonLat> instead.
52     */
53    centerLonLat: null,
54
55    /**
56     * Constructor: OpenLayers.Bounds
57     * Construct a new bounds object.
58     *
59     * Parameters:
60     * left - {Number} The left bounds of the box.  Note that for width
61     *        calculations, this is assumed to be less than the right value.
62     * bottom - {Number} The bottom bounds of the box.  Note that for height
63     *          calculations, this is assumed to be more than the top value.
64     * right - {Number} The right bounds.
65     * top - {Number} The top bounds.
66     */
67    initialize: function(left, bottom, right, top) {
68        if (left != null) {
69            this.left = OpenLayers.Util.toFloat(left);
70        }
71        if (bottom != null) {
72            this.bottom = OpenLayers.Util.toFloat(bottom);
73        }
74        if (right != null) {
75            this.right = OpenLayers.Util.toFloat(right);
76        }
77        if (top != null) {
78            this.top = OpenLayers.Util.toFloat(top);
79        }
80    },
81
82    /**
83     * Method: clone
84     * Create a cloned instance of this bounds.
85     *
86     * Returns:
87     * {<OpenLayers.Bounds>} A fresh copy of the bounds
88     */
89    clone:function() {
90        return new OpenLayers.Bounds(this.left, this.bottom, 
91                                     this.right, this.top);
92    },
93
94    /**
95     * Method: equals
96     * Test a two bounds for equivalence.
97     *
98     * Parameters:
99     * bounds - {<OpenLayers.Bounds>}
100     *
101     * Returns:
102     * {Boolean} The passed-in bounds object has the same left,
103     *           right, top, bottom components as this.  Note that if bounds
104     *           passed in is null, returns false.
105     */
106    equals:function(bounds) {
107        var equals = false;
108        if (bounds != null) {
109            equals = ((this.left == bounds.left) && 
110                      (this.right == bounds.right) &&
111                      (this.top == bounds.top) && 
112                      (this.bottom == bounds.bottom));
113        }
114        return equals;
115    },
116
117    /**
118     * APIMethod: toString
119     *
120     * Returns:
121     * {String} String representation of bounds object.
122     *          (ex.<i>"left-bottom=(5,42) right-top=(10,45)"</i>)
123     */
124    toString:function() {
125        return ( "left-bottom=(" + this.left + "," + this.bottom + ")"
126                 + " right-top=(" + this.right + "," + this.top + ")" );
127    },
128
129    /**
130     * APIMethod: toArray
131     *
132     * Parameters:
133     * reverseAxisOrder - {Boolean} Should we reverse the axis order?
134     *
135     * Returns:
136     * {Array} array of left, bottom, right, top
137     */
138    toArray: function(reverseAxisOrder) {
139        if (reverseAxisOrder === true) {
140            return [this.bottom, this.left, this.top, this.right];
141        } else {
142            return [this.left, this.bottom, this.right, this.top];
143        }
144    },   
145
146    /**
147     * APIMethod: toBBOX
148     *
149     * Parameters:
150     * decimal - {Integer} How many significant digits in the bbox coords?
151     *                     Default is 6
152     * reverseAxisOrder - {Boolean} Should we reverse the axis order?
153     *
154     * Returns:
155     * {String} Simple String representation of bounds object.
156     *          (ex. <i>"5,42,10,45"</i>)
157     */
158    toBBOX:function(decimal, reverseAxisOrder) {
159        if (decimal== null) {
160            decimal = 6; 
161        }
162        var mult = Math.pow(10, decimal);
163        var xmin = Math.round(this.left * mult) / mult;
164        var ymin = Math.round(this.bottom * mult) / mult;
165        var xmax = Math.round(this.right * mult) / mult;
166        var ymax = Math.round(this.top * mult) / mult;
167        if (reverseAxisOrder === true) {
168            return ymin + "," + xmin + "," + ymax + "," + xmax;
169        } else {
170            return xmin + "," + ymin + "," + xmax + "," + ymax;
171        }
172    },
173 
174    /**
175     * APIMethod: toGeometry
176     * Create a new polygon geometry based on this bounds.
177     *
178     * Returns:
179     * {<OpenLayers.Geometry.Polygon>} A new polygon with the coordinates
180     *     of this bounds.
181     */
182    toGeometry: function() {
183        return new OpenLayers.Geometry.Polygon([
184            new OpenLayers.Geometry.LinearRing([
185                new OpenLayers.Geometry.Point(this.left, this.bottom),
186                new OpenLayers.Geometry.Point(this.right, this.bottom),
187                new OpenLayers.Geometry.Point(this.right, this.top),
188                new OpenLayers.Geometry.Point(this.left, this.top)
189            ])
190        ]);
191    },
192   
193    /**
194     * APIMethod: getWidth
195     *
196     * Returns:
197     * {Float} The width of the bounds
198     */
199    getWidth:function() {
200        return (this.right - this.left);
201    },
202
203    /**
204     * APIMethod: getHeight
205     *
206     * Returns:
207     * {Float} The height of the bounds (top minus bottom).
208     */
209    getHeight:function() {
210        return (this.top - this.bottom);
211    },
212
213    /**
214     * APIMethod: getSize
215     *
216     * Returns:
217     * {<OpenLayers.Size>} The size of the box.
218     */
219    getSize:function() {
220        return new OpenLayers.Size(this.getWidth(), this.getHeight());
221    },
222
223    /**
224     * APIMethod: getCenterPixel
225     *
226     * Returns:
227     * {<OpenLayers.Pixel>} The center of the bounds in pixel space.
228     */
229    getCenterPixel:function() {
230        return new OpenLayers.Pixel( (this.left + this.right) / 2,
231                                     (this.bottom + this.top) / 2);
232    },
233
234    /**
235     * APIMethod: getCenterLonLat
236     *
237     * Returns:
238     * {<OpenLayers.LonLat>} The center of the bounds in map space.
239     */
240    getCenterLonLat:function() {
241        if(!this.centerLonLat) {
242            this.centerLonLat = new OpenLayers.LonLat(
243                (this.left + this.right) / 2, (this.bottom + this.top) / 2
244            );
245        }
246        return this.centerLonLat;
247    },
248
249    /**
250     * Method: scale
251     * Scales the bounds around a pixel or lonlat. Note that the new
252     *     bounds may return non-integer properties, even if a pixel
253     *     is passed.
254     *
255     * Parameters:
256     * ratio - {Float}
257     * origin - {<OpenLayers.Pixel> or <OpenLayers.LonLat>}
258     *          Default is center.
259     *
260     * Returns:
261     * {<OpenLayers.Bound>} A new bounds that is scaled by ratio
262     *                      from origin.
263     */
264
265    scale: function(ratio, origin){
266        if(origin == null){
267            origin = this.getCenterLonLat();
268        }
269       
270        var origx,origy;
271
272        // get origin coordinates
273        if(origin.CLASS_NAME == "OpenLayers.LonLat"){
274            origx = origin.lon;
275            origy = origin.lat;
276        } else {
277            origx = origin.x;
278            origy = origin.y;
279        }
280
281        var left = (this.left - origx) * ratio + origx;
282        var bottom = (this.bottom - origy) * ratio + origy;
283        var right = (this.right - origx) * ratio + origx;
284        var top = (this.top - origy) * ratio + origy;
285       
286        return new OpenLayers.Bounds(left, bottom, right, top);
287    },
288
289    /**
290     * APIMethod: add
291     *
292     * Parameters:
293     * x - {Float}
294     * y - {Float}
295     *
296     * Returns:
297     * {<OpenLayers.Bounds>} A new bounds whose coordinates are the same as
298     *     this, but shifted by the passed-in x and y values.
299     */
300    add:function(x, y) {
301        if ( (x == null) || (y == null) ) {
302            var msg = OpenLayers.i18n("boundsAddError");
303            OpenLayers.Console.error(msg);
304            return null;
305        }
306        return new OpenLayers.Bounds(this.left + x, this.bottom + y,
307                                     this.right + x, this.top + y);
308    },
309   
310    /**
311     * APIMethod: extend
312     * Extend the bounds to include the point, lonlat, or bounds specified.
313     *     Note, this function assumes that left < right and bottom < top.
314     *
315     * Parameters:
316     * object - {Object} Can be LonLat, Point, or Bounds
317     */
318    extend:function(object) {
319        var bounds = null;
320        if (object) {
321            // clear cached center location
322            switch(object.CLASS_NAME) {
323                case "OpenLayers.LonLat":   
324                    bounds = new OpenLayers.Bounds(object.lon, object.lat,
325                                                    object.lon, object.lat);
326                    break;
327                case "OpenLayers.Geometry.Point":
328                    bounds = new OpenLayers.Bounds(object.x, object.y,
329                                                    object.x, object.y);
330                    break;
331                   
332                case "OpenLayers.Bounds":   
333                    bounds = object;
334                    break;
335            }
336   
337            if (bounds) {
338                this.centerLonLat = null;
339                if ( (this.left == null) || (bounds.left < this.left)) {
340                    this.left = bounds.left;
341                }
342                if ( (this.bottom == null) || (bounds.bottom < this.bottom) ) {
343                    this.bottom = bounds.bottom;
344                } 
345                if ( (this.right == null) || (bounds.right > this.right) ) {
346                    this.right = bounds.right;
347                }
348                if ( (this.top == null) || (bounds.top > this.top) ) { 
349                    this.top = bounds.top;
350                }
351            }
352        }
353    },
354
355    /**
356     * APIMethod: containsLonLat
357     *
358     * Parameters:
359     * ll - {<OpenLayers.LonLat>}
360     * inclusive - {Boolean} Whether or not to include the border.
361     *     Default is true.
362     *
363     * Returns:
364     * {Boolean} The passed-in lonlat is within this bounds.
365     */
366    containsLonLat:function(ll, inclusive) {
367        return this.contains(ll.lon, ll.lat, inclusive);
368    },
369
370    /**
371     * APIMethod: containsPixel
372     *
373     * Parameters:
374     * px - {<OpenLayers.Pixel>}
375     * inclusive - {Boolean} Whether or not to include the border. Default is
376     *     true.
377     *
378     * Returns:
379     * {Boolean} The passed-in pixel is within this bounds.
380     */
381    containsPixel:function(px, inclusive) {
382        return this.contains(px.x, px.y, inclusive);
383    },
384   
385    /**
386     * APIMethod: contains
387     *
388     * Parameters:
389     * x - {Float}
390     * y - {Float}
391     * inclusive - {Boolean} Whether or not to include the border. Default is
392     *     true.
393     *
394     * Returns:
395     * {Boolean} Whether or not the passed-in coordinates are within this
396     *     bounds.
397     */
398    contains:function(x, y, inclusive) {
399        //set default
400        if (inclusive == null) {
401            inclusive = true;
402        }
403
404        if (x == null || y == null) {
405            return false;
406        }
407
408        x = OpenLayers.Util.toFloat(x);
409        y = OpenLayers.Util.toFloat(y);
410
411        var contains = false;
412        if (inclusive) {
413            contains = ((x >= this.left) && (x <= this.right) && 
414                        (y >= this.bottom) && (y <= this.top));
415        } else {
416            contains = ((x > this.left) && (x < this.right) && 
417                        (y > this.bottom) && (y < this.top));
418        }             
419        return contains;
420    },
421
422    /**
423     * APIMethod: intersectsBounds
424     * Determine whether the target bounds intersects this bounds.  Bounds are
425     *     considered intersecting if any of their edges intersect or if one
426     *     bounds contains the other.
427     *
428     * Parameters:
429     * bounds - {<OpenLayers.Bounds>} The target bounds.
430     * inclusive - {Boolean} Treat coincident borders as intersecting.  Default
431     *     is true.  If false, bounds that do not overlap but only touch at the
432     *     border will not be considered as intersecting.
433     *
434     * Returns:
435     * {Boolean} The passed-in bounds object intersects this bounds.
436     */
437    intersectsBounds:function(bounds, inclusive) {
438        if (inclusive == null) {
439            inclusive = true;
440        }
441        var intersects = false;
442        var mightTouch = (
443            this.left == bounds.right ||
444            this.right == bounds.left ||
445            this.top == bounds.bottom ||
446            this.bottom == bounds.top
447        );
448       
449        // if the two bounds only touch at an edge, and inclusive is false,
450        // then the bounds don't *really* intersect.
451        if (inclusive || !mightTouch) {
452            // otherwise, if one of the boundaries even partially contains another,
453            // inclusive of the edges, then they do intersect.
454            var inBottom = (
455                ((bounds.bottom >= this.bottom) && (bounds.bottom <= this.top)) ||
456                ((this.bottom >= bounds.bottom) && (this.bottom <= bounds.top))
457            );
458            var inTop = (
459                ((bounds.top >= this.bottom) && (bounds.top <= this.top)) ||
460                ((this.top > bounds.bottom) && (this.top < bounds.top))
461            );
462            var inLeft = (
463                ((bounds.left >= this.left) && (bounds.left <= this.right)) ||
464                ((this.left >= bounds.left) && (this.left <= bounds.right))
465            );
466            var inRight = (
467                ((bounds.right >= this.left) && (bounds.right <= this.right)) ||
468                ((this.right >= bounds.left) && (this.right <= bounds.right))
469            );
470            intersects = ((inBottom || inTop) && (inLeft || inRight));
471        }
472        return intersects;
473    },
474   
475    /**
476     * APIMethod: containsBounds
477     * Determine whether the target bounds is contained within this bounds.
478     *
479     * bounds - {<OpenLayers.Bounds>} The target bounds.
480     * partial - {Boolean} If any of the target corners is within this bounds
481     *     consider the bounds contained.  Default is false.  If false, the
482     *     entire target bounds must be contained within this bounds.
483     * inclusive - {Boolean} Treat shared edges as contained.  Default is
484     *     true.
485     *
486     * Returns:
487     * {Boolean} The passed-in bounds object is contained within this bounds.
488     */
489    containsBounds:function(bounds, partial, inclusive) {
490        if (partial == null) {
491            partial = false;
492        }
493        if (inclusive == null) {
494            inclusive = true;
495        }
496        var bottomLeft  = this.contains(bounds.left, bounds.bottom, inclusive);
497        var bottomRight = this.contains(bounds.right, bounds.bottom, inclusive);
498        var topLeft  = this.contains(bounds.left, bounds.top, inclusive);
499        var topRight = this.contains(bounds.right, bounds.top, inclusive);
500       
501        return (partial) ? (bottomLeft || bottomRight || topLeft || topRight)
502                         : (bottomLeft && bottomRight && topLeft && topRight);
503    },
504
505    /**
506     * APIMethod: determineQuadrant
507     *
508     * Parameters:
509     * lonlat - {<OpenLayers.LonLat>}
510     *
511     * Returns:
512     * {String} The quadrant ("br" "tr" "tl" "bl") of the bounds in which the
513     *     coordinate lies.
514     */
515    determineQuadrant: function(lonlat) {
516   
517        var quadrant = "";
518        var center = this.getCenterLonLat();
519       
520        quadrant += (lonlat.lat < center.lat) ? "b" : "t";
521        quadrant += (lonlat.lon < center.lon) ? "l" : "r";
522   
523        return quadrant; 
524    },
525   
526    /**
527     * APIMethod: transform
528     * Transform the Bounds object from source to dest.
529     *
530     * Parameters:
531     * source - {<OpenLayers.Projection>} Source projection.
532     * dest   - {<OpenLayers.Projection>} Destination projection.
533     *
534     * Returns:
535     * {<OpenLayers.Bounds>} Itself, for use in chaining operations.
536     */
537    transform: function(source, dest) {
538        // clear cached center location
539        this.centerLonLat = null;
540        var ll = OpenLayers.Projection.transform(
541            {'x': this.left, 'y': this.bottom}, source, dest);
542        var lr = OpenLayers.Projection.transform(
543            {'x': this.right, 'y': this.bottom}, source, dest);
544        var ul = OpenLayers.Projection.transform(
545            {'x': this.left, 'y': this.top}, source, dest);
546        var ur = OpenLayers.Projection.transform(
547            {'x': this.right, 'y': this.top}, source, dest);
548        this.left   = Math.min(ll.x, ul.x);
549        this.bottom = Math.min(ll.y, lr.y);
550        this.right  = Math.max(lr.x, ur.x);
551        this.top    = Math.max(ul.y, ur.y);
552        return this;
553    },
554
555    /**
556     * APIMethod: wrapDateLine
557     * 
558     * Parameters:
559     * maxExtent - {<OpenLayers.Bounds>}
560     * options - {Object} Some possible options are:
561     *                    leftTolerance - {float} Allow for a margin of error
562     *                                            with the 'left' value of this
563     *                                            bound.
564     *                                            Default is 0.
565     *                    rightTolerance - {float} Allow for a margin of error
566     *                                             with the 'right' value of
567     *                                             this bound.
568     *                                             Default is 0.
569     *
570     * Returns:
571     * {<OpenLayers.Bounds>} A copy of this bounds, but wrapped around the
572     *                       "dateline" (as specified by the borders of
573     *                       maxExtent). Note that this function only returns
574     *                       a different bounds value if this bounds is
575     *                       *entirely* outside of the maxExtent. If this
576     *                       bounds straddles the dateline (is part in/part
577     *                       out of maxExtent), the returned bounds will be
578     *                       merely a copy of this one.
579     */
580    wrapDateLine: function(maxExtent, options) {   
581        options = options || {};
582       
583        var leftTolerance = options.leftTolerance || 0;
584        var rightTolerance = options.rightTolerance || 0;
585
586        var newBounds = this.clone();
587   
588        if (maxExtent) {
589
590           //shift right?
591           while ( newBounds.left < maxExtent.left && 
592                   (newBounds.right - rightTolerance) <= maxExtent.left ) { 
593                newBounds = newBounds.add(maxExtent.getWidth(), 0);
594           }
595
596           //shift left?
597           while ( (newBounds.left + leftTolerance) >= maxExtent.right && 
598                   newBounds.right > maxExtent.right ) { 
599                newBounds = newBounds.add(-maxExtent.getWidth(), 0);
600           }
601        }
602               
603        return newBounds;
604    },
605
606    CLASS_NAME: "OpenLayers.Bounds"
607});
608
609/**
610 * APIFunction: fromString
611 * Alternative constructor that builds a new OpenLayers.Bounds from a
612 *     parameter string
613 *
614 * Parameters:
615 * str - {String}Comma-separated bounds string. (ex. <i>"5,42,10,45"</i>)
616 *
617 * Returns:
618 * {<OpenLayers.Bounds>} New bounds object built from the
619 *                       passed-in String.
620 */
621OpenLayers.Bounds.fromString = function(str) {
622    var bounds = str.split(",");
623    return OpenLayers.Bounds.fromArray(bounds);
624};
625
626/**
627 * APIFunction: fromArray
628 * Alternative constructor that builds a new OpenLayers.Bounds
629 *     from an array
630 *
631 * Parameters:
632 * bbox - {Array(Float)} Array of bounds values (ex. <i>[5,42,10,45]</i>)
633 *
634 * Returns:
635 * {<OpenLayers.Bounds>} New bounds object built from the passed-in Array.
636 */
637OpenLayers.Bounds.fromArray = function(bbox) {
638    return new OpenLayers.Bounds(parseFloat(bbox[0]),
639                                 parseFloat(bbox[1]),
640                                 parseFloat(bbox[2]),
641                                 parseFloat(bbox[3]));
642};
643
644/**
645 * APIFunction: fromSize
646 * Alternative constructor that builds a new OpenLayers.Bounds
647 *     from a size
648 *
649 * Parameters:
650 * size - {<OpenLayers.Size>}
651 *
652 * Returns:
653 * {<OpenLayers.Bounds>} New bounds object built from the passed-in size.
654 */
655OpenLayers.Bounds.fromSize = function(size) {
656    return new OpenLayers.Bounds(0,
657                                 size.h,
658                                 size.w,
659                                 0);
660};
661
662/**
663 * Function: oppositeQuadrant
664 * Get the opposite quadrant for a given quadrant string.
665 *
666 * Parameters:
667 * quadrant - {String} two character quadrant shortstring
668 *
669 * Returns:
670 * {String} The opposing quadrant ("br" "tr" "tl" "bl"). For Example, if
671 *          you pass in "bl" it returns "tr", if you pass in "br" it
672 *          returns "tl", etc.
673 */
674OpenLayers.Bounds.oppositeQuadrant = function(quadrant) {
675    var opp = "";
676   
677    opp += (quadrant.charAt(0) == 't') ? 'b' : 't';
678    opp += (quadrant.charAt(1) == 'l') ? 'r' : 'l';
679   
680    return opp;
681};
Note: See TracBrowser for help on using the repository browser.