[Commits] r466 - in core/trunk/geoext: examples lib lib/GeoExt/data tests tests/lib/GeoExt/data

commits at geoext.org commits at geoext.org
Fri Apr 24 23:16:09 CEST 2009


Author: sbenthall
Date: 2009-04-24 23:16:09 +0200 (Fri, 24 Apr 2009)
New Revision: 466

Added:
   core/trunk/geoext/examples/zoom-chooser.html
   core/trunk/geoext/examples/zoom-chooser.js
   core/trunk/geoext/lib/GeoExt/data/ScaleStore.js
   core/trunk/geoext/tests/lib/GeoExt/data/ScaleStore.html
Modified:
   core/trunk/geoext/lib/GeoExt.js
   core/trunk/geoext/tests/list-tests.html
Log:
A ScaleStore, which takes a map and provides records with information about zoom level, scale denominator, and map resolution.  Thanks to dwins for the patch! (Closes #41)


Added: core/trunk/geoext/examples/zoom-chooser.html
===================================================================
--- core/trunk/geoext/examples/zoom-chooser.html	                        (rev 0)
+++ core/trunk/geoext/examples/zoom-chooser.html	2009-04-24 21:16:09 UTC (rev 466)
@@ -0,0 +1,17 @@
+<html>
+    <head>
+        <link rel="stylesheet" type="text/css" href="../../ext/resources/css/ext-all.css"></link>
+        <link rel="stylesheet" type="text/css" href="../../ext/examples/shared/examples.css"></link>
+        <script type="text/javascript" src="../../openlayers/lib/OpenLayers.js"></script>
+        <script type="text/javascript" src="../../ext/adapter/ext/ext-base.js"></script>
+        <script type="text/javascript" src="../../ext/ext-all.js"></script>
+        <script type="text/javascript" src="../lib/GeoExt.js"></script>
+        <script type="text/javascript" src="zoom-chooser.js"></script>
+    </head>
+    <body>
+        <h1>GeoExt.data.ScaleStore with an Existing OpenLayers.Map</h1>
+        <p>This example demonstrates the use of a GeoExt.data.ScaleStore to list the available zoom levels for a map.<p>
+        <p>The js is not minified so it is readable. See <a href="zoom-chooser.js">zoom-chooser.js</a>.</p>
+        <div id="mappanel"></div>
+    </body>
+</html>

Added: core/trunk/geoext/examples/zoom-chooser.js
===================================================================
--- core/trunk/geoext/examples/zoom-chooser.js	                        (rev 0)
+++ core/trunk/geoext/examples/zoom-chooser.js	2009-04-24 21:16:09 UTC (rev 466)
@@ -0,0 +1,55 @@
+
+var mapPanel;
+
+Ext.onReady(function() {
+    var map = new OpenLayers.Map();
+    var layer = new OpenLayers.Layer.WMS(
+        "bluemarble",
+        "http://sigma.openplans.org/geoserver/wms?",
+        {layers: 'bluemarble'}
+    );
+    map.addLayer(layer);
+
+    var scaleStore = new GeoExt.data.ScaleStore({map: map});
+    var zoomSelector = new Ext.form.ComboBox({
+        store: scaleStore,
+        emptyText: "Zoom Level",
+        tpl: '<tpl for="."><div class="x-combo-list-item">1 : {[parseInt(values.scale)]}</div></tpl>',
+        editable: false,
+        triggerAction: 'all', // needed so that the combo box doesn't filter by its current content
+        mode: 'local' // keep the combo box from forcing a lot of unneeded data refreshes
+    });
+
+    zoomSelector.on('select', 
+        function(combo, record, index) {
+            map.zoomTo(record.data.level);
+        },
+        this
+    );     
+
+    map.events.register('zoomend', this, function() {
+        var scale = scaleStore.queryBy(function(record){
+            return this.map.getZoom() == record.data.level;
+        });
+
+        if (scale.length > 0) {
+            scale = scale.items[0];
+            zoomSelector.setValue("1 : " + parseInt(scale.data.scale));
+        } else {
+            if (!zoomSelector.rendered) return;
+            zoomSelector.clearValue();
+        }
+    });
+
+    mapPanel = new GeoExt.MapPanel({
+        title: "GeoExt MapPanel",
+        renderTo: "mappanel",
+        height: 400,
+        width: 600,
+        map: map,
+        center: new OpenLayers.LonLat(5, 45),
+        zoom: 4,
+        bbar: [zoomSelector]
+    });
+});
+

Added: core/trunk/geoext/lib/GeoExt/data/ScaleStore.js
===================================================================
--- core/trunk/geoext/lib/GeoExt/data/ScaleStore.js	                        (rev 0)
+++ core/trunk/geoext/lib/GeoExt/data/ScaleStore.js	2009-04-24 21:16:09 UTC (rev 466)
@@ -0,0 +1,111 @@
+/* Copyright (C) 2008-2009 The Open Source Geospatial Foundation ¹
+ * Published under the BSD license.
+ * See http://geoext.org/svn/geoext/core/trunk/license.txt for the full text
+ * of the license.
+ * 
+ * ¹ pending approval */
+
+Ext.namespace("GeoExt.data");
+
+/**
+ *  Class: GeoExt.data.ScaleStore
+ *  This store maintains a list of available zoom levels, optionally keeping it synchronized with 
+ *  a Map or MapPanel instance.   The entries in the list have the following fields: 
+ *  zoom - the number of the zoom level
+ *  scale - the scale denominator for the zoom level
+ *  resolution - the map resolution when the zoom level is active.
+ */
+GeoExt.data.ScaleStore = Ext.extend(Ext.data.Store, {
+    /**
+     * Property: map
+     * The OpenLayers.Map instance to which the store is bound, if any.
+     */
+    map: null,
+
+    /**
+     * Constructor: GeoExt.data.ScaleStore
+     * Construct a ScaleStore from a configuration.  The ScaleStore accepts some custom parameters 
+     * addition to the fields accepted by Ext.Store.
+     * Additional options:
+     * map - the GeoExt.MapPanel or OpenLayers.Map instance the store should stay sync'ed with
+     */
+    constructor: function(config) {
+        var map = (config.map instanceof GeoExt.MapPanel ? config.map.map : config.map);
+        delete config.map;
+        config = Ext.applyIf(config, {reader: new Ext.data.JsonReader({}, [
+            "level",
+            "resolution",
+            "scale"
+        ])});
+
+        GeoExt.data.ScaleStore.superclass.constructor.call(this, config);
+
+        if (map) this.bind(map);
+    },
+
+    /**
+     * APIMethod: bind
+     * Bind this store to a map; that is, maintain the zoom list in sync with the map's current 
+     * configuration.  If the map does not currently have a set scale list, then the store will 
+     * remain empty until the map is configured with one.
+     *
+     * Parameters: 
+     * map - the GeoExt.MapPanel or OpenLayers.Map to which we should bind
+     * options - additional parameters for the bind operation (optional, currently unused)
+     */
+    bind: function(map, options) {
+        this.map = (map instanceof GeoExt.MapPanel ? map.map : map);
+        this.map.events.register('changebaselayer', this, this.populateFromMap);
+        if (this.map.baseLayer) {
+            this.populateFromMap();
+        } else {
+            this.map.register('layeradded', this, this.populateOnAdd);
+        }
+    },
+
+    /**
+     * APIMethod: unbind
+     * Un-bind this store from the map to which it is currently bound.  The currently stored zoom 
+     * levels will remain, but no further changes from the map will affect it.
+     */
+    unbind: function() {
+        if (this.map) {
+            this.map.events.unregister('changebaselayer', this, this.populateFromMap);
+            delete this.map;
+        }
+    },
+
+    /**
+     * Method: populateOnAdd
+     * This method handles the case where we have bind() called on a not-fully-configured map so 
+     * that the zoom levels can be detected when a baselayer is finally added.
+     *
+     * Parameters:
+     * evt - the OpenLayers event
+     */
+    populateOnAdd: function(evt) {
+        if (evt.layer.isBaseLayer) {
+            this.populateFromMap();
+            this.map.events.unregister('layeradded', this, this.populateOnAdd);
+        }
+    },
+
+    /**
+     * Method: populateFromMap
+     * This method actually loads the zoom level information from the OpenLayers.Map and converts 
+     * it to Ext Records.
+     */
+    populateFromMap: function() {
+        var zooms = [];
+
+        for (var i = this.map.numZoomLevels-1; i > 0; i--) { 
+            var res = this.map.getResolutionForZoom(i);
+            var units = this.map.baseLayer.units;
+            var scale = OpenLayers.Util.getScaleFromResolution(res, units);
+
+            zooms.push({level: i, resolution: res, scale: scale});
+        }
+
+        this.loadData(zooms);
+    }
+});

Modified: core/trunk/geoext/lib/GeoExt.js
===================================================================
--- core/trunk/geoext/lib/GeoExt.js	2009-04-24 19:33:15 UTC (rev 465)
+++ core/trunk/geoext/lib/GeoExt.js	2009-04-24 21:16:09 UTC (rev 466)
@@ -64,6 +64,7 @@
             "GeoExt/data/LayerRecord.js",
             "GeoExt/data/LayerReader.js",
             "GeoExt/data/LayerStore.js",
+            "GeoExt/data/ScaleStore.js",
             "GeoExt/data/WMSCapabilitiesRecord.js",
             "GeoExt/data/WMSCapabilitiesReader.js",
             "GeoExt/data/WMSCapabilitiesStore.js",

Added: core/trunk/geoext/tests/lib/GeoExt/data/ScaleStore.html
===================================================================
--- core/trunk/geoext/tests/lib/GeoExt/data/ScaleStore.html	                        (rev 0)
+++ core/trunk/geoext/tests/lib/GeoExt/data/ScaleStore.html	2009-04-24 21:16:09 UTC (rev 466)
@@ -0,0 +1,64 @@
+<!DOCTYPE html>
+<html debug="true">
+  <head>
+    <script type="text/javascript" src="../../../../../openlayers/lib/OpenLayers.js"></script>
+    <script type="text/javascript" src="../../../../../ext/adapter/ext/ext-base.js"></script>
+    <script type="text/javascript" src="../../../../../ext/ext-all-debug.js"></script>
+    <script type="text/javascript" src="../../../../lib/GeoExt.js"></script>
+
+    <script type="text/javascript">
+       
+        function createMap() {
+            var map = new OpenLayers.Map();
+            map.addLayer(new OpenLayers.Layer('dummy layer', {
+                maxExtent: new OpenLayers.Bounds(-180, -90, 180, 90),
+                isBaseLayer: true
+            }));
+            map.zoomToMaxExtent();
+            return map;
+        }
+
+        function loadMapPanel() {
+            var map = createMap();
+
+            var mapPanel = new GeoExt.MapPanel({
+                // panel options
+                id: "map-panel",
+                title: "GeoExt MapPanel",
+                renderTo: "mappanel",
+                height: 400,
+                width: 600,
+                // map panel-specific options
+                map: map,
+                center: new OpenLayers.LonLat(5, 45),
+                zoom: 4
+            });
+
+            return mapPanel;
+        }
+
+        function test_constructor(t) {
+            t.plan(1);
+
+            var store, map;
+
+            map = createMap();
+            store = new GeoExt.data.ScaleStore({map: map});
+            t.ok(store.map == map, "ctor sets the passed map in the instance");
+        }
+
+        function test_scalestore(t) {
+            t.plan(1);
+
+            var mapPanel = loadMapPanel();
+            var map = mapPanel.map;
+            var store = new GeoExt.data.ScaleStore({map: map});
+            t.eq(store.data.length, 15, 'Found expected number of zoomlevels');
+        }
+        
+    </script>
+  </head>  
+  <body>
+    <div id="mappanel" style="width:400px; height:300px"></div>
+  </body>
+</html>

Modified: core/trunk/geoext/tests/list-tests.html
===================================================================
--- core/trunk/geoext/tests/list-tests.html	2009-04-24 19:33:15 UTC (rev 465)
+++ core/trunk/geoext/tests/list-tests.html	2009-04-24 21:16:09 UTC (rev 466)
@@ -5,6 +5,7 @@
   <li>lib/GeoExt/data/LayerRecord.html</li>
   <li>lib/GeoExt/data/LayerReader.html</li>
   <li>lib/GeoExt/data/LayerStore.html</li>
+  <li>lib/GeoExt/data/ScaleStore.html</li>
   <li>lib/GeoExt/data/ProtocolProxy.html</li>
   <li>lib/GeoExt/data/WMSCapabilitiesReader.html</li>
   <li>lib/GeoExt/widgets/MapPanel.html</li>



More information about the Commits mailing list