[OpenLayers-Commits] r11266 - in sandbox/ahocevar/layercontainer: lib/OpenLayers lib/OpenLayers/Layer tests tests/Layer/Google

commits-20090109 at openlayers.org commits-20090109 at openlayers.org
Tue Feb 22 18:48:01 EST 2011


Author: ahocevar
Date: 2011-02-22 15:48:01 -0800 (Tue, 22 Feb 2011)
New Revision: 11266

Modified:
   sandbox/ahocevar/layercontainer/lib/OpenLayers/Layer.js
   sandbox/ahocevar/layercontainer/lib/OpenLayers/Layer/Grid.js
   sandbox/ahocevar/layercontainer/lib/OpenLayers/Map.js
   sandbox/ahocevar/layercontainer/tests/Layer/Google/v3.html
   sandbox/ahocevar/layercontainer/tests/Map.html
Log:
applied openlayers-3062.2.patch from #3062

Modified: sandbox/ahocevar/layercontainer/lib/OpenLayers/Layer/Grid.js
===================================================================
--- sandbox/ahocevar/layercontainer/lib/OpenLayers/Layer/Grid.js	2011-02-22 23:45:35 UTC (rev 11265)
+++ sandbox/ahocevar/layercontainer/lib/OpenLayers/Layer/Grid.js	2011-02-22 23:48:01 UTC (rev 11266)
@@ -665,7 +665,9 @@
         var shifted = true;
         var buffer = this.buffer || 1;
         var tlLayer = this.grid[0][0].position;
-        var tlViewPort = this.map.getViewPortPxFromLayerPx(tlLayer);
+        var offsetX = parseInt(this.map.layerContainerDiv.style.left);
+        var offsetY = parseInt(this.map.layerContainerDiv.style.top);
+        tlViewPort = tlLayer.add(offsetX, offsetY);
         if (tlViewPort.x > -this.tileSize.w * (buffer - 1)) {
             this.shiftColumn(true);
         } else if (tlViewPort.x < -this.tileSize.w * buffer) {

Modified: sandbox/ahocevar/layercontainer/lib/OpenLayers/Layer.js
===================================================================
--- sandbox/ahocevar/layercontainer/lib/OpenLayers/Layer.js	2011-02-22 23:45:35 UTC (rev 11265)
+++ sandbox/ahocevar/layercontainer/lib/OpenLayers/Layer.js	2011-02-22 23:48:01 UTC (rev 11266)
@@ -1234,12 +1234,15 @@
     getViewPortPxFromLonLat: function (lonlat) {
         var px = null; 
         if (lonlat != null) {
-            var resolution = this.map.getResolution();
-            var extent = this.map.getExtent();
-            px = new OpenLayers.Pixel(
-                (1/resolution * (lonlat.lon - extent.left)),
-                (1/resolution * (extent.top - lonlat.lat))
-            );    
+            var res = this.map.resolution;
+            var center = this.map.extent.getCenterLonLat();
+            var lonDelta = lonlat.lon - center.lon;
+            var latDelta = center.lat - lonlat.lat;
+            var halfViewPortWidth = this.map.size.w / 2;
+            var halfViewPortHeight = this.map.size.h / 2;
+            var left = halfViewPortWidth + lonDelta / res;
+            var top = halfViewPortHeight + latDelta / res;
+            px = new OpenLayers.Pixel(left, top);
         }
         return px;
     },

Modified: sandbox/ahocevar/layercontainer/lib/OpenLayers/Map.js
===================================================================
--- sandbox/ahocevar/layercontainer/lib/OpenLayers/Map.js	2011-02-22 23:45:35 UTC (rev 11265)
+++ sandbox/ahocevar/layercontainer/lib/OpenLayers/Map.js	2011-02-22 23:48:01 UTC (rev 11266)
@@ -166,19 +166,18 @@
     size: null,
     
     /**
+     * Property: extent
+     * {<OpenLayers.Bounds>} Cached extent.
+     */
+    extent: null,
+    
+    /**
      * Property: viewPortDiv
      * {HTMLDivElement} The element that represents the map viewport
      */
     viewPortDiv: null,
 
     /**
-     * Property: layerContainerOrigin
-     * {<OpenLayers.LonLat>} The lonlat at which the later container was
-     *                       re-initialized (on-zoom)
-     */
-    layerContainerOrigin: null,
-
-    /**
      * Property: layerContainerDiv
      * {HTMLDivElement} The element that contains the layers.
      */
@@ -233,7 +232,7 @@
      * {Integer} The current zoom level of the map
      */
     zoom: 0,    
-
+    
     /**
      * Property: panRatio
      * {Float} The ratio of the current extent within
@@ -1115,7 +1114,7 @@
             if (OpenLayers.Util.indexOf(this.layers, newBaseLayer) != -1) {
 
                 // preserve center and scale when changing base layers
-                var center = this.getCenter();
+                var center = this.center;
                 var newResolution = OpenLayers.Util.getResolutionFromScale(
                     this.getScale(), newBaseLayer.units
                 );
@@ -1363,7 +1362,7 @@
         var newSize = this.getCurrentSize();
         if (newSize && !isNaN(newSize.h) && !isNaN(newSize.w)) {
             this.events.clearMouseCache();
-            var oldSize = this.getSize();
+            var oldSize = this.size;
             if (oldSize == null) {
                 this.size = oldSize = newSize;
             }
@@ -1377,10 +1376,10 @@
                     this.layers[i].onMapResize();                
                 }
     
-                var center = this.getCenter();
+                var center = this.center;
     
                 if (this.baseLayer != null && center != null) {
-                    var zoom = this.getZoom();
+                    var zoom = this.zoom;
                     this.zoom = null;
                     this.setCenter(center, zoom);
                 }
@@ -1428,15 +1427,15 @@
         var extent = null;
         
         if (center == null) {
-            center = this.getCenter();
+            center = this.center;
         }                
         if (resolution == null) {
-            resolution = this.getResolution();
+            resolution = this.resolution;
         }
     
         if ((center != null) && (resolution != null)) {
 
-            var size = this.getSize();
+            var size = this.size;
             var w_deg = size.w * resolution;
             var h_deg = size.h * resolution;
         
@@ -1502,6 +1501,7 @@
             animate: true,
             dragging: false
         });
+        
         // getCenter
         var centerPx = this.getViewPortPxFromLonLat(this.getCenter());
 
@@ -1529,11 +1529,11 @@
      * lonlat - {<OpenLayers.Lonlat>}
      */
     panTo: function(lonlat) {
-        if (this.panMethod && this.getExtent().scale(this.panRatio).containsLonLat(lonlat)) {
+        if (this.panMethod && this.extent.scale(this.panRatio).containsLonLat(lonlat)) {
             if (!this.panTween) {
                 this.panTween = new OpenLayers.Tween(this.panMethod);
             }
-            var center = this.getCenter();
+            var center = this.center;
 
             // center will not change, don't do nothing
             if (lonlat.lon == center.lon &&
@@ -1609,105 +1609,96 @@
         if (!options) { 
             options = {};
         }
-        if (zoom != null) {
-            zoom = parseFloat(zoom);
-            if (!this.fractionalZoom) {
-                zoom = Math.round(zoom);
-            }
-        }
         // dragging is false by default
         var dragging = options.dragging;
         // forceZoomChange is false by default
         var forceZoomChange = options.forceZoomChange;
         // noEvent is false by default
         var noEvent = options.noEvent;
-
+        
         if (this.panTween && options.caller == "setCenter") {
             this.panTween.stop();
         }    
-             
-        if (!this.center && !this.isValidLonLat(lonlat)) {
-            lonlat = this.maxExtent.getCenterLonLat();
-        }
 
-        if(this.restrictedExtent != null) {
-            // In 3.0, decide if we want to change interpretation of maxExtent.
-            if(lonlat == null) { 
-                lonlat = this.getCenter(); 
+        var zoomChanged = forceZoomChange;
+        if (zoom != null) {
+            if (typeof zoom != "number") {
+                zoom = parseFloat(zoom);
             }
-            if(zoom == null) { 
-                zoom = this.getZoom(); 
+            if (!this.fractionalZoom) {
+                zoom = Math.round(zoom);
             }
-            var resolution = this.getResolutionForZoom(zoom);
-            var extent = this.calculateBounds(lonlat, resolution); 
-            if(!this.restrictedExtent.containsBounds(extent)) {
-                var maxCenter = this.restrictedExtent.getCenterLonLat(); 
-                if(extent.getWidth() > this.restrictedExtent.getWidth()) { 
-                    lonlat = new OpenLayers.LonLat(maxCenter.lon, lonlat.lat); 
-                } else if(extent.left < this.restrictedExtent.left) {
-                    lonlat = lonlat.add(this.restrictedExtent.left -
-                                        extent.left, 0); 
-                } else if(extent.right > this.restrictedExtent.right) { 
-                    lonlat = lonlat.add(this.restrictedExtent.right -
-                                        extent.right, 0); 
-                } 
-                if(extent.getHeight() > this.restrictedExtent.getHeight()) { 
-                    lonlat = new OpenLayers.LonLat(lonlat.lon, maxCenter.lat); 
-                } else if(extent.bottom < this.restrictedExtent.bottom) { 
-                    lonlat = lonlat.add(0, this.restrictedExtent.bottom -
-                                        extent.bottom); 
-                } 
-                else if(extent.top > this.restrictedExtent.top) { 
-                    lonlat = lonlat.add(0, this.restrictedExtent.top -
-                                        extent.top); 
-                } 
-            }
         }
+        zoomChanged = zoomChanged ||
+            zoom != this.zoom && this.isValidZoomLevel(zoom);
+        var res;
+        if (zoomChanged) {
+            res = this.resolution = this.getResolutionForZoom(zoom);
+        } else {
+            res = this.resolution = this.getResolution();
+        }
         
-        var zoomChanged = forceZoomChange || (
-                            (this.isValidZoomLevel(zoom)) && 
-                            (zoom != this.getZoom()) );
+        if (lonlat) {
+            if (this.restrictedExtent) {
+                var extent = this.calculateBounds(lonlat, res);
+                if (!this.restrictedExtent.containsBounds(extent)) {
+                    var maxCenter = this.restrictedExtent.getCenterLonLat(); 
+                    if (extent.getWidth() > this.restrictedExtent.getWidth()) { 
+                        lonlat = new OpenLayers.LonLat(maxCenter.lon, lonlat.lat); 
+                    } else if (extent.left < this.restrictedExtent.left) {
+                        lonlat = lonlat.add(this.restrictedExtent.left -
+                                            extent.left, 0); 
+                    } else if (extent.right > this.restrictedExtent.right) { 
+                        lonlat = lonlat.add(this.restrictedExtent.right -
+                                            extent.right, 0); 
+                    } 
+                    if (extent.getHeight() > this.restrictedExtent.getHeight()) { 
+                        lonlat = new OpenLayers.LonLat(lonlat.lon, maxCenter.lat); 
+                    } else if (extent.bottom < this.restrictedExtent.bottom) { 
+                        lonlat = lonlat.add(0, this.restrictedExtent.bottom -
+                                            extent.bottom); 
+                    } 
+                    else if (extent.top > this.restrictedExtent.top) { 
+                        lonlat = lonlat.add(0, this.restrictedExtent.top -
+                                            extent.top); 
+                    }
+                }
+            }
+        }
 
-        var centerChanged = (this.isValidLonLat(lonlat)) && 
-                            (!lonlat.equals(this.center));
-
-
+        if (zoomChanged) {
+            var maxExtent = this.getMaxExtent();
+            this.layerContainerDiv.style.width =
+                Math.round(maxExtent.getWidth() / res) + "px";
+            this.layerContainerDiv.style.height  =
+                Math.round(maxExtent.getHeight() / res) + "px";
+            this.zoom = zoom;
+            // zoom level has changed, increment viewRequestID.
+            this.viewRequestID++;
+        }
+        
+        var centerChanged =
+            this.isValidLonLat(lonlat) && !lonlat.equals(this.center);
+        if (centerChanged || !this.center) {
+            if (!this.center && !this.isValidLonLat(lonlat)) {
+                lonlat = this.maxExtent.getCenterLonLat();
+            }
+            this.centerLayerContainer(lonlat);
+            this.center = lonlat;
+        }
+        
         // if neither center nor zoom will change, no need to do anything
         if (zoomChanged || centerChanged || !dragging) {
 
             if (!this.dragging && !noEvent) {
                 this.events.triggerEvent("movestart");
             }
-
-            if (centerChanged) {
-                if ((!zoomChanged) && (this.center)) { 
-                    // if zoom hasnt changed, just slide layerContainer
-                    //  (must be done before setting this.center to new value)
-                    this.centerLayerContainer(lonlat);
-                }
-                this.center = lonlat.clone();
-            }
-
-            // (re)set the layerContainerDiv's location
-            if ((zoomChanged) || (this.layerContainerOrigin == null)) {
-                this.layerContainerOrigin = this.center.clone();
-                this.layerContainerDiv.style.left = "0px";
-                this.layerContainerDiv.style.top  = "0px";
-            }
-
-            if (zoomChanged) {
-                this.zoom = zoom;
-                this.resolution = this.getResolutionForZoom(zoom);
-                // zoom level has changed, increment viewRequestID.
-                this.viewRequestID++;
-            }    
             
-            var bounds = this.getExtent();
+            this.extent = this.baseLayer.getExtent();
             
             //send the move call to the baselayer and all the overlays    
-
             if(this.baseLayer.visibility) {
-                this.baseLayer.moveTo(bounds, zoomChanged, dragging);
+                this.baseLayer.moveTo(this.extent, zoomChanged, dragging);
                 if(dragging) {
                     this.baseLayer.events.triggerEvent("move");
                 } else {
@@ -1716,9 +1707,7 @@
                     );
                 }
             }
-            
-            bounds = this.baseLayer.getExtent();
-            
+                                    
             for (var i=0, len=this.layers.length; i<len; i++) {
                 var layer = this.layers[i];
                 if (layer !== this.baseLayer && !layer.isBaseLayer) {
@@ -1737,7 +1726,7 @@
                         });
                     }
                     if (inRange && layer.visibility) {
-                        layer.moveTo(bounds, zoomChanged, dragging);
+                        layer.moveTo(this.extent, zoomChanged, dragging);
                         if(dragging) {
                             layer.events.triggerEvent("move");
                         } else {
@@ -1779,14 +1768,18 @@
      * lonlat - {<OpenLayers.LonLat>}
      */
     centerLayerContainer: function (lonlat) {
-
-        var originPx = this.getViewPortPxFromLonLat(this.layerContainerOrigin);
-        var newPx = this.getViewPortPxFromLonLat(lonlat);
-
-        if ((originPx != null) && (newPx != null)) {
-            this.layerContainerDiv.style.left = Math.round(originPx.x - newPx.x) + "px";
-            this.layerContainerDiv.style.top  = Math.round(originPx.y - newPx.y) + "px";
-        }
+        var res = this.resolution;
+        var center = this.getMaxExtent().getCenterLonLat();
+        var lonDelta = lonlat.lon - center.lon;
+        var latDelta = center.lat - lonlat.lat;
+        var halfExtentWidth = parseInt(this.layerContainerDiv.style.width) / 2;
+        var halfExtentHeight = parseInt(this.layerContainerDiv.style.height) / 2;
+        var halfViewPortWidth = this.size.w / 2;
+        var halfViewPortHeight = this.size.h / 2;
+        var left = halfViewPortWidth - halfExtentWidth - lonDelta / res;
+        var top = halfViewPortHeight - halfExtentHeight - latDelta / res;
+        this.layerContainerDiv.style.left = Math.round(left) + "px";
+        this.layerContainerDiv.style.top  = Math.round(top) + "px";
     },
 
     /**
@@ -1955,8 +1948,8 @@
      */
     getExtent: function () {
         var extent = null;
-        if (this.baseLayer != null) {
-            extent = this.baseLayer.getExtent();
+        if (this.extent != null) {
+            extent = this.extent.clone();
         }
         return extent;
     },
@@ -2109,7 +2102,7 @@
      * zoom - {int}
      */
     zoomIn: function() {
-        this.zoomTo(this.getZoom() + 1);
+        this.zoomTo(this.zoom + 1);
     },
     
     /**
@@ -2119,7 +2112,7 @@
      * zoom - {int}
      */
     zoomOut: function() {
-        this.zoomTo(this.getZoom() - 1);
+        this.zoomTo(this.zoom - 1);
     },
 
     /**
@@ -2197,10 +2190,10 @@
     zoomToScale: function(scale, closest) {
         var res = OpenLayers.Util.getResolutionFromScale(scale, 
                                                          this.baseLayer.units);
-        var size = this.getSize();
+        var size = this.size;
         var w_deg = size.w * res;
         var h_deg = size.h * res;
-        var center = this.getCenter();
+        var center = this.center;
 
         var extent = new OpenLayers.Bounds(center.lon - w_deg / 2,
                                            center.lat - h_deg / 2,
@@ -2312,7 +2305,7 @@
      * {<OpenLayers.Size>} The geodesic size of the pixel in kilometers.
      */
     getGeodesicPixelSize: function(px) {
-        var lonlat = px ? this.getLonLatFromPixel(px) : (this.getCenter() ||
+        var lonlat = px ? this.getLonLatFromPixel(px) : (this.center ||
             new OpenLayers.LonLat(0, 0));
         var res = this.getResolution();
         var left = lonlat.add(-res / 2, 0);
@@ -2353,9 +2346,11 @@
     getViewPortPxFromLayerPx:function(layerPx) {
         var viewPortPx = null;
         if (layerPx != null) {
-            var dX = parseInt(this.layerContainerDiv.style.left);
-            var dY = parseInt(this.layerContainerDiv.style.top);
-            viewPortPx = layerPx.add(dX, dY);            
+            var res = this.resolution;
+            var center = this.getMaxExtent().getCenterLonLat();
+            var deltaX = Math.round(center.lon / res - this.center.lon / res);
+            var deltaY = Math.round(this.center.lat / res - center.lat / res);
+            viewPortPx = layerPx.add(deltaX, deltaY);
         }
         return viewPortPx;
     },
@@ -2373,12 +2368,11 @@
     getLayerPxFromViewPortPx:function(viewPortPx) {
         var layerPx = null;
         if (viewPortPx != null) {
-            var dX = -parseInt(this.layerContainerDiv.style.left);
-            var dY = -parseInt(this.layerContainerDiv.style.top);
-            layerPx = viewPortPx.add(dX, dY);
-            if (isNaN(layerPx.x) || isNaN(layerPx.y)) {
-                layerPx = null;
-            }
+            var res = this.resolution;
+            var center = this.getMaxExtent().getCenterLonLat();
+            var deltaX = Math.round(this.center.lon / res - center.lon / res);
+            var deltaY = Math.round(center.lat / res - this.center.lat / res);
+            layerPx = viewPortPx.add(deltaX, deltaY);
         }
         return layerPx;
     },

Modified: sandbox/ahocevar/layercontainer/tests/Layer/Google/v3.html
===================================================================
--- sandbox/ahocevar/layercontainer/tests/Layer/Google/v3.html	2011-02-22 23:45:35 UTC (rev 11265)
+++ sandbox/ahocevar/layercontainer/tests/Layer/Google/v3.html	2011-02-22 23:48:01 UTC (rev 11266)
@@ -266,12 +266,6 @@
 
          t.plan(8);
 
-         var origPrecision = OpenLayers.Util.DEFAULT_PRECISION;
-         // GMaps v3 seems to use a default precision of 13, which is lower
-         // than what we use in OpenLayers.
-         // See http://trac.osgeo.org/openlayers/ticket/3059
-         OpenLayers.Util.DEFAULT_PRECISION = 13;
-
          var map = new OpenLayers.Map('map', {allOverlays: true});
 
          var gmap = new OpenLayers.Layer.Google("Google Streets");
@@ -316,7 +310,6 @@
          t.eq(center.lat, origin.lat, "y panning with Google invisible " + (-dx) + ", " + (-dy));
 
          map.destroy();
-         OpenLayers.Util.DEFAULT_PRECISION = origPrecision;
      }
      
      function test_wrapDateLine(t) {

Modified: sandbox/ahocevar/layercontainer/tests/Map.html
===================================================================
--- sandbox/ahocevar/layercontainer/tests/Map.html	2011-02-22 23:45:35 UTC (rev 11265)
+++ sandbox/ahocevar/layercontainer/tests/Map.html	2011-02-22 23:48:01 UTC (rev 11266)
@@ -797,23 +797,20 @@
         map.destroy();
     }
 
-    function test_Map_setBaseLayer_after_pan (t) {
-        t.plan(1);
+    function test_Map_setBaseLayer_containerdiv (t) {
+        t.plan(4);
 
         map = new OpenLayers.Map('map');
-        var wmsLayer = new OpenLayers.Layer.WMS( "OpenLayers WMS", 
-            "http://labs.metacarta.com/wms/vmap0", {layers: 'basic'} );
-        var tmsLayer = new OpenLayers.Layer.TMS("TMS",
-                                    "http://labs.metacarta.com/wms-c/Basic.py/",
-                                    {'layername':'basic', 'type':'png'});
-        map.addLayers([wmsLayer,tmsLayer]);
-        map.setBaseLayer(wmsLayer);
+        map.addLayer(new OpenLayers.Layer(null, {isBaseLayer: true}));
         map.zoomToMaxExtent();
-        map.setBaseLayer(tmsLayer);
+        var res = map.getResolution();
+        t.eq(map.layerContainerDiv.style.top, (150-90/res)+"px", "layerContainer.style.top is correct");
+        t.eq(map.layerContainerDiv.style.left, (300-180/res)+"px", "layerContainer.style.left is correct");
         map.zoomIn();
-        map.pan(0, -200, {animate:false});
-        map.setBaseLayer(wmsLayer);
-        t.eq(map.layerContainerDiv.style.top, "0px", "layerContainer is recentered after setBaseLayer");
+        map.pan(50, -200, {animate:false});
+        var res = map.getResolution();
+        t.eq(map.layerContainerDiv.style.top, (350-90/res)+"px", "layerContainer.style.top is correct");
+        t.eq(map.layerContainerDiv.style.left, (250-180/res)+"px", "layerContainer.style.left is correct");
         
         map.destroy();
     }
@@ -1318,8 +1315,8 @@
         
         var m = {
             'baseLayer': { 'units': {} },
-            'getSize': function() { return {'w': 10, 'h': 15}; },
-            'getCenter': function() { return {'lon': -5, 'lat': -25}; },
+            'size': {'w': 10, 'h': 15},
+            'center': {'lon': -5, 'lat': -25},
             'zoomToExtent': function(extent, closest) {
                 t.ok(extent.equals(g_ExpectedExtent), "extent correctly calculated for zoomToExtent()");
                 t.ok(closest == g_Closest, "closest correctly passed on to zoomToExtent()");



More information about the Commits mailing list