OpenLayers OpenLayers

Changeset 5625

Show
Ignore:
Timestamp:
01/02/08 14:16:58 (1 year ago)
Author:
tschaub
Message:

Giving vector features an onScreen method. By default, this uses geometry.intersects. If a rougher approximation will do, call with boundsOnly set to true. r=crschmidt (closes #1238)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/lib/OpenLayers/BaseTypes/Bounds.js

    r5614 r5625  
    147147 
    148148        return bbox; 
     149    }, 
     150     
     151    /** 
     152     * APIMethod: toGeometry 
     153     * Create a new polygon geometry based on this bounds. 
     154     * 
     155     * Returns: 
     156     * {<OpenLayers.Geometry.Polygon>} A new polygon with the coordinates 
     157     *     of this bounds. 
     158     */ 
     159    toGeometry: function() { 
     160        return new OpenLayers.Geometry.Polygon([ 
     161            new OpenLayers.Geometry.LinearRing([ 
     162                new OpenLayers.Geometry.Point(this.left, this.bottom), 
     163                new OpenLayers.Geometry.Point(this.right, this.bottom), 
     164                new OpenLayers.Geometry.Point(this.right, this.top), 
     165                new OpenLayers.Geometry.Point(this.left, this.top) 
     166            ]) 
     167        ]); 
    149168    }, 
    150169     
  • trunk/openlayers/lib/OpenLayers/Feature/Vector.js

    r5614 r5625  
    115115    /** 
    116116     * Method: onScreen 
    117      * HACK - we need to rewrite this for non-point geometry 
    118      *  
    119      * Returns: 
    120      * {Boolean} For now just returns null 
    121      */ 
    122     onScreen:function() { 
    123         return null; 
     117     * Determine whether the feature is within the map viewport.  This method 
     118     *     tests for an intersection between the geometry and the viewport 
     119     *     bounds.  If a more effecient but less precise geometry bounds 
     120     *     intersection is desired, call the method with the boundsOnly 
     121     *     parameter true. 
     122     * 
     123     * Parameters: 
     124     * boundsOnly - {Boolean} Only test whether a feature's bounds intersects 
     125     *     the viewport bounds.  Default is false.  If false, the feature's 
     126     *     geometry must intersect the viewport for onScreen to return true. 
     127     *  
     128     * Returns: 
     129     * {Boolean} The feature is currently visible on screen (optionally 
     130     *     based on its bounds if boundsOnly is true). 
     131     */ 
     132    onScreen:function(boundsOnly) { 
     133        var onScreen = false; 
     134        if(this.layer && this.layer.map) { 
     135            var screenBounds = this.layer.map.getExtent(); 
     136            if(boundsOnly) { 
     137                var featureBounds = this.geometry.getBounds(); 
     138                onScreen = screenBounds.intersectsBounds(featureBounds); 
     139            } else { 
     140                var screenPoly = screenBounds.toGeometry(); 
     141                onScreen = screenPoly.intersects(this.geometry); 
     142            } 
     143        }     
     144        return onScreen; 
    124145    }, 
    125146     
  • trunk/openlayers/tests/BaseTypes/test_Bounds.html

    r5597 r5625  
    7777    } 
    7878 
     79    function test_Bounds_toGeometry(t) { 
     80        t.plan(7); 
     81        var minx = Math.random(); 
     82        var miny = Math.random(); 
     83        var maxx = Math.random(); 
     84        var maxy = Math.random(); 
     85        var bounds = new OpenLayers.Bounds(minx, miny, maxx, maxy); 
     86        var poly = bounds.toGeometry(); 
     87        t.eq(poly.CLASS_NAME, "OpenLayers.Geometry.Polygon", 
     88             "polygon instance created"); 
     89        t.eq(poly.components.length, 1, 
     90             "polygon with one ring created"); 
     91        var ring = poly.components[0]; 
     92        t.eq(ring.components.length, 5, 
     93             "four sided polygon created"); 
     94        t.eq(ring.components[0].x, minx, 
     95             "bounds left preserved"); 
     96        t.eq(ring.components[0].y, miny, 
     97             "bounds bottom preserved"); 
     98        t.eq(ring.components[2].x, maxx, 
     99             "bounds left preserved"); 
     100        t.eq(ring.components[2].y, maxy, 
     101             "bounds bottom preserved"); 
     102    } 
     103 
    79104    function test_04_Bounds_contains(t) { 
    80105        t.plan( 6 ); 
  • trunk/openlayers/tests/Feature/test_Vector.html

    r4059 r5625  
    2222        t.eq(feature.geometry.id, geometry.id, 
    2323             "geometry.property set properly" ); 
     24    } 
     25     
     26    function test_Feature_onScreen(t) { 
     27        t.plan(6); 
     28        var line = new OpenLayers.Geometry.LineString([ 
     29            new OpenLayers.Geometry.Point(0, 0), 
     30            new OpenLayers.Geometry.Point(10, 20) 
     31        ]); 
     32        var feature = new OpenLayers.Feature.Vector(line); 
     33        feature.layer = { 
     34            map: { 
     35                getExtent: function() { 
     36                    return new OpenLayers.Bounds(5, 5, 10, 10); 
     37                } 
     38            } 
     39        }; 
     40        t.eq(feature.onScreen(), true, 
     41             "intersecting feature returns true for intersection"); 
     42        t.eq(feature.onScreen(true), true, 
     43             "intersecting feature returns true for bounds only"); 
     44         
     45        // move the line so only the bounds intersects 
     46        line.move(0, 5); 
     47        t.eq(feature.onScreen(), false, 
     48             "bounds-only feature returns false for intersection"); 
     49        t.eq(feature.onScreen(true), true, 
     50             "bounds-only feature returns true for bounds only"); 
     51 
     52        // move the line so bounds does not intersect 
     53        line.move(0, 10); 
     54        t.eq(feature.onScreen(), false, 
     55             "off-screen feature returns false for intersection"); 
     56        t.eq(feature.onScreen(true), false, 
     57             "off-screen feature returns false for bounds only"); 
     58         
    2459    } 
    2560