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

commits at geoext.org commits at geoext.org
Thu Oct 8 20:09:30 CEST 2009


Author: bbinet
Date: 2009-10-08 20:09:30 +0200 (Thu, 08 Oct 2009)
New Revision: 1393

Added:
   core/trunk/geoext/lib/GeoExt/data/WMCReader.js
   core/trunk/geoext/tests/lib/GeoExt/data/WMCReader.html
   core/trunk/geoext/tests/lib/GeoExt/data/WMCReader.js
Modified:
   core/trunk/geoext/lib/GeoExt.js
   core/trunk/geoext/tests/list-tests.html
Log:
Add a WMCReader for GeoExt. r=tschaub (closes #156)

Added: core/trunk/geoext/lib/GeoExt/data/WMCReader.js
===================================================================
--- core/trunk/geoext/lib/GeoExt/data/WMCReader.js	                        (rev 0)
+++ core/trunk/geoext/lib/GeoExt/data/WMCReader.js	2009-10-08 18:09:30 UTC (rev 1393)
@@ -0,0 +1,116 @@
+/**
+ * Copyright (c) 2008-2009 The Open Source Geospatial Foundation
+ * 
+ * Published under the BSD license.
+ * See http://svn.geoext.org/core/trunk/geoext/license.txt for the full text
+ * of the license.
+ */
+
+/**
+ * @include GeoExt/data/LayerRecord.js
+ */
+
+/** api: (define)
+ *  module = GeoExt.data
+ *  class = WMCReader
+ *  base_link = `Ext.data.DataReader <http://extjs.com/deploy/dev/docs/?class=Ext.data.DataReader>`_
+ */
+Ext.namespace("GeoExt.data");
+
+/** api: constructor
+ *  .. class:: WMCReader(meta, recordType)
+ *  
+ *      :param meta: ``Object`` Reader configuration.
+ *      :param recordType: ``Array | Ext.data.Record`` An array of field
+ *          configuration objects or a record object.  Default is
+ *          :class:`GeoExt.data.LayerRecord`.
+ *   
+ *      Data reader class to create an array of
+ *      :class:`GeoExt.data.LayerRecord` objects from a WMS GetCapabilities
+ *      response.
+ */
+GeoExt.data.WMCReader = function(meta, recordType) {
+    meta = meta || {};
+    if(!meta.format) {
+        meta.format = new OpenLayers.Format.WMC();
+    }
+    if(!(typeof recordType === "function")) {
+        recordType = GeoExt.data.LayerRecord.create(
+            recordType || meta.fields || [
+                // give only non-OpenLayers fields as default recordType
+                {name: "abstract", type: "string"},
+                {name: "metadataURL", type: "string"},
+                {name: "queryable", type: "boolean"},
+                {name: "formats"}, // array
+                {name: "styles"} // array
+            ]
+        );
+    }
+    GeoExt.data.WMCReader.superclass.constructor.call(
+        this, meta, recordType
+    );
+};
+
+Ext.extend(GeoExt.data.WMCReader, Ext.data.DataReader, {
+
+    /** private: method[read]
+     *  :param request: ``Object`` The XHR object which contains the parsed XML
+     *      document.
+     *  :return: ``Object`` A data block which is used by an ``Ext.data.Store``
+     *      as a cache of ``Ext.data.Record`` objects.
+     */
+    read: function(request) {
+        var data = request.responseXML;
+        if(!data || !data.documentElement) {
+            data = request.responseText;
+        }
+        return this.readRecords(data);
+    },
+
+    /** private: method[readRecords]
+     *  :param data: ``DOMElement | String | Object`` A document element or XHR
+     *      response string.  As an alternative to fetching capabilities data
+     *      from a remote source, an object representing the capabilities can
+     *      be provided given that the structure mirrors that returned from the
+     *      capabilities parser.
+     *  :return: ``Object`` A data block which is used by an ``Ext.data.Store``
+     *      as a cache of ``Ext.data.Record`` objects.
+     *  
+     *  Create a data block containing Ext.data.Records from an XML document.
+     */
+    readRecords: function(data) {
+        var format = this.meta.format;
+        if(typeof data === "string" || data.nodeType) {
+            data = format.read(data);
+        }
+        var layersContext = data ? data.layersContext : undefined;
+        var records = [];        
+
+        if(layersContext) {
+            var recordType = this.recordType, fields = recordType.prototype.fields;
+            var i, lenI, j, lenJ, layerContext, values, field, v;
+            for (i = 0, lenI = layersContext.length; i < lenI; i++) {
+                layerContext = layersContext[i];
+                values = {};
+                for(j = 0, lenJ = fields.length; j < lenJ; j++){
+                    field = fields.items[j];
+                    v = layerContext[field.mapping || field.name] ||
+                        field.defaultValue;
+                    v = field.convert(v);
+                    values[field.name] = v;
+                }
+                values.layer = format.getLayerFromContext(layerContext);
+                records.push(new this.recordType(values, values.layer.id));
+            }
+        }
+        
+        return {
+            totalRecords: records.length,
+            success: true,
+            records: records
+        };
+
+    }
+
+});
+

Modified: core/trunk/geoext/lib/GeoExt.js
===================================================================
--- core/trunk/geoext/lib/GeoExt.js	2009-10-08 17:55:08 UTC (rev 1392)
+++ core/trunk/geoext/lib/GeoExt.js	2009-10-08 18:09:30 UTC (rev 1393)
@@ -79,6 +79,7 @@
             "GeoExt/data/WFSCapabilitiesStore.js",
             "GeoExt/data/WMSDescribeLayerReader.js",
             "GeoExt/data/WMSDescribeLayerStore.js",
+            "GeoExt/data/WMCReader.js",
             "GeoExt/widgets/Action.js",
             "GeoExt/data/ProtocolProxy.js",
             "GeoExt/widgets/MapPanel.js",

Added: core/trunk/geoext/tests/lib/GeoExt/data/WMCReader.html
===================================================================
--- core/trunk/geoext/tests/lib/GeoExt/data/WMCReader.html	                        (rev 0)
+++ core/trunk/geoext/tests/lib/GeoExt/data/WMCReader.html	2009-10-08 18:09:30 UTC (rev 1393)
@@ -0,0 +1,86 @@
+<!DOCTYPE html>
+<html debug="true">
+  <head>
+    <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="../../../../../openlayers/lib/OpenLayers.js"></script>
+    <script type="text/javascript" src="../../../../lib/GeoExt.js"></script>
+    <script type="text/javascript" src="WMCReader.js"></script>
+
+    <script type="text/javascript">
+      
+        function test_constructor(t) {
+            t.plan(2);
+            var reader = new GeoExt.data.WMCReader();
+
+            var fields = reader.recordType.prototype.fields;
+
+            // 1 test
+            t.eq(fields.items.length, 7, 'number of default items is correct');
+
+            reader = new GeoExt.data.WMCReader({},[
+                {name: "foo"},
+                {name: "bar"}
+            ]);
+
+            fields = reader.recordType.prototype.fields;
+
+            //1 test
+            t.ok(fields.items[2].name == 'foo' &&
+                 fields.items[3].name == 'bar',
+                 'field values set from configuration are correct');
+        }
+        function test_read(t) {
+            t.plan(19);
+
+            // test a reader with the only two default LayerRecord fields
+
+            var reader = new GeoExt.data.WMCReader({},{});
+            var records = reader.read({responseXML : doc});
+
+            //1 test
+            t.eq(records.totalRecords, 3, 'readRecords returns correct number of records');
+
+            var record = records.records[1];
+            //2 tests -- testing the fields of a record
+            t.eq(record.get("title"), "geob:communes_geofla", "correct title record field");
+            t.eq(record.get("abstract"), undefined, "correct undefined abstract which is not part of fields");
+
+
+            // test a reader with all default fields
+
+            var reader = new GeoExt.data.WMCReader();
+            var records = reader.read({responseXML : doc});
+
+            //1 test
+            t.eq(records.totalRecords, 3, 'readRecords returns correct number of records');
+
+            var record = records.records[1];
+            //10 tests -- testing the fields of a record
+            t.eq(record.get("title"), "geob:communes_geofla", "correct title record field");
+            t.eq(record.get("abstract"), "Communes abstract", "correct abstract record field");
+            t.eq(record.get("metadataURL"), "", "correct metadataURL record field");
+            t.eq(record.get("queryable"), true, "correct queryable record field");
+            t.eq(record.get("formats").length, 28, "correct length for formats record field");
+            t.eq(record.get("formats")[0].value, "image/png", "correct value for formats record field");
+            t.eq(record.get("styles").length, 2, "correct length for styles record field");
+            t.eq(record.get("styles")[0].abstract, "Default line style, 1 pixel wide blue", "correct abstract for styles record field");
+            t.eq(record.get("styles")[0].name, "line", "correct name for styles record field");
+            t.eq(record.get("styles")[0].title, "1 px blue line", "correct title for styles record field");
+
+            //4 tests -- Testing the layer field
+            var layer = record.get("layer");
+            t.eq(layer.CLASS_NAME, "OpenLayers.Layer.WMS", "layer record field is of type OpenLayers.Layer.WMS");
+            t.eq(layer.url, "../geoserver/wms?SERVICE=WMS&", "layer record field has correct URL");
+            t.eq(layer.params.LAYERS, "geob:communes_geofla","layer record field has correct LAYERS parameter");
+            t.eq(layer.name, "Communes","layer record field has correct name");
+
+            //1 test
+            t.eq(record.id, layer.id, 'record id is the same as layer id');
+        }
+    </script>
+  <body>
+    <div id="map"></div>
+  </body>
+</html>

Added: core/trunk/geoext/tests/lib/GeoExt/data/WMCReader.js
===================================================================
--- core/trunk/geoext/tests/lib/GeoExt/data/WMCReader.js	                        (rev 0)
+++ core/trunk/geoext/tests/lib/GeoExt/data/WMCReader.js	2009-10-08 18:09:30 UTC (rev 1393)
@@ -0,0 +1,133 @@
+var doc = (new OpenLayers.Format.XML).read(
+    '<ViewContext xmlns="http://www.opengis.net/context" version="1.1.0" id="default.wmc" xsi:schemaLocation="http://www.opengis.net/context http://schemas.opengis.net/context/1.1.0/context.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' +
+      '<General>' +
+        '<Window width="1031" height="389"/>' +
+        '<BoundingBox minx="-37179.8441128839986" miny="6691080.05881679989" maxx="540179.844112879946" maxy="6908919.94118320011" SRS="EPSG:2154"/>' +
+        '<Title/>' +
+        '<Extension>' +
+          '<ol:maxExtent xmlns:ol="http://openlayers.org/context" minx="83000.0000000000000" miny="6700000.00000000000" maxx="420000.000000000000" maxy="6900000.00000000000"/>' +
+        '</Extension>' +
+      '</General>' +
+      '<LayerList>' +
+        '<Layer queryable="0" hidden="0">' +
+          '<Server service="OGC:WMS" version="1.1.1">' +
+            '<OnlineResource xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="../geoserver/gwc/service/wms"/>' +
+          '</Server>' +
+          '<Name>geob:SC1000_0050_7130_L93</Name>' +
+          '<Title>Scan 1000</Title>' +
+          '<Abstract>Scan 1000 abstract</Abstract>' +
+          '<sld:MinScaleDenominator xmlns:sld="http://www.opengis.net/sld">250000.0000000000</sld:MinScaleDenominator>' +
+          '<sld:MaxScaleDenominator xmlns:sld="http://www.opengis.net/sld">2000000.000000000</sld:MaxScaleDenominator>' +
+          '<FormatList>' +
+            '<Format current="1">image/png</Format>' +
+          '</FormatList>' +
+          '<StyleList>' +
+            '<Style>' +
+              '<Name/>' +
+              '<Title/>' +
+            '</Style>' +
+          '</StyleList>' +
+          '<Extension>' +
+            '<ol:maxExtent xmlns:ol="http://openlayers.org/context" minx="83000.0000000000000" miny="6700000.00000000000" maxx="420000.000000000000" maxy="6900000.00000000000"/>' +
+            '<ol:numZoomLevels xmlns:ol="http://openlayers.org/context">4</ol:numZoomLevels>' +
+            '<ol:units xmlns:ol="http://openlayers.org/context">m</ol:units>' +
+            '<ol:isBaseLayer xmlns:ol="http://openlayers.org/context">false</ol:isBaseLayer>' +
+            '<ol:opacity xmlns:ol="http://openlayers.org/context">1</ol:opacity>' +
+            '<ol:displayInLayerSwitcher xmlns:ol="http://openlayers.org/context">true</ol:displayInLayerSwitcher>' +
+            '<ol:singleTile xmlns:ol="http://openlayers.org/context">false</ol:singleTile>' +
+          '</Extension>' +
+        '</Layer>' +
+        '<Layer queryable="1" hidden="0">' +
+          '<Server service="OGC:WMS" version="1.1.1">' +
+            '<OnlineResource xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="../geoserver/wms?SERVICE=WMS&amp;"/>' +
+          '</Server>' +
+          '<Name>geob:communes_geofla</Name>' +
+          '<Title>Communes</Title>' +
+          '<Abstract>Communes abstract</Abstract>' +
+          '<FormatList>' +
+            '<Format current="1">image/png</Format>' +
+            '<Format>application/atom xml</Format>' +
+            '<Format>application/atom+xml</Format>' +
+            '<Format>application/openlayers</Format>' +
+            '<Format>application/pdf</Format>' +
+            '<Format>application/rss xml</Format>' +
+            '<Format>application/rss+xml</Format>' +
+            '<Format>application/vnd.google-earth.kml</Format>' +
+            '<Format>application/vnd.google-earth.kml xml</Format>' +
+            '<Format>application/vnd.google-earth.kml+xml</Format>' +
+            '<Format>application/vnd.google-earth.kmz</Format>' +
+            '<Format>application/vnd.google-earth.kmz xml</Format>' +
+            '<Format>application/vnd.google-earth.kmz+xml</Format>' +
+            '<Format>atom</Format>' +
+            '<Format>image/geotiff</Format>' +
+            '<Format>image/geotiff8</Format>' +
+            '<Format>image/gif</Format>' +
+            '<Format>image/jpeg</Format>' +
+            '<Format>image/png8</Format>' +
+            '<Format>image/svg</Format>' +
+            '<Format>image/svg xml</Format>' +
+            '<Format>image/svg+xml</Format>' +
+            '<Format>image/tiff</Format>' +
+            '<Format>image/tiff8</Format>' +
+            '<Format>kml</Format>' +
+            '<Format>kmz</Format>' +
+            '<Format>openlayers</Format>' +
+            '<Format>rss</Format>' +
+          '</FormatList>' +
+          '<StyleList>' +
+            '<Style>' +
+              '<Name>line</Name>' +
+              '<Title>1 px blue line</Title>' +
+              '<Abstract>Default line style, 1 pixel wide blue</Abstract>' +
+            '</Style>' +
+            '<Style>' +
+              '<Name>dpt_classif</Name>' +
+              '<Title>Classification par départements</Title>' +
+            '</Style>' +
+          '</StyleList>' +
+          '<Extension>' +
+            '<ol:maxExtent xmlns:ol="http://openlayers.org/context" minx="83000.0000000000000" miny="6700000.00000000000" maxx="420000.000000000000" maxy="6900000.00000000000"/>' +
+            '<ol:transparent xmlns:ol="http://openlayers.org/context">true</ol:transparent>' +
+            '<ol:numZoomLevels xmlns:ol="http://openlayers.org/context">13</ol:numZoomLevels>' +
+            '<ol:units xmlns:ol="http://openlayers.org/context">m</ol:units>' +
+            '<ol:isBaseLayer xmlns:ol="http://openlayers.org/context">false</ol:isBaseLayer>' +
+            '<ol:opacity xmlns:ol="http://openlayers.org/context">0.5</ol:opacity>' +
+            '<ol:displayInLayerSwitcher xmlns:ol="http://openlayers.org/context">true</ol:displayInLayerSwitcher>' +
+            '<ol:singleTile xmlns:ol="http://openlayers.org/context">true</ol:singleTile>' +
+          '</Extension>' +
+        '</Layer>' +
+        '<Layer queryable="1" hidden="0">' +
+          '<Server service="OGC:WMS" version="1.1.1">' +
+            '<OnlineResource xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://geolittoral.application.equipement.gouv.fr/map/mapserv?map=/opt/data/carto/applis/geolittoral/map/metropole.www.map&amp;"/>' +
+          '</Server>' +
+          '<Name>Sentiers_littoraux</Name>' +
+          '<Title>Sentiers littoraux</Title>' +
+          '<FormatList>' +
+            '<Format current="1">image/png</Format>' +
+            '<Format>image/gif</Format>' +
+            '<Format>image/png; mode=24bit</Format>' +
+            '<Format>image/jpeg</Format>' +
+            '<Format>image/vnd.wap.wbmp</Format>' +
+            '<Format>image/tiff</Format>' +
+            '<Format>image/svg+xml</Format>' +
+          '</FormatList>' +
+          '<StyleList>' +
+            '<Style>' +
+              '<Name>default</Name>' +
+              '<Title>default</Title>' +
+            '</Style>' +
+          '</StyleList>' +
+          '<Extension>' +
+            '<ol:maxExtent xmlns:ol="http://openlayers.org/context" minx="83000.0000000000000" miny="6700000.00000000000" maxx="420000.000000000000" maxy="6900000.00000000000"/>' +
+            '<ol:transparent xmlns:ol="http://openlayers.org/context">true</ol:transparent>' +
+            '<ol:numZoomLevels xmlns:ol="http://openlayers.org/context">13</ol:numZoomLevels>' +
+            '<ol:units xmlns:ol="http://openlayers.org/context">m</ol:units>' +
+            '<ol:isBaseLayer xmlns:ol="http://openlayers.org/context">false</ol:isBaseLayer>' +
+            '<ol:opacity xmlns:ol="http://openlayers.org/context">1</ol:opacity>' +
+            '<ol:displayInLayerSwitcher xmlns:ol="http://openlayers.org/context">true</ol:displayInLayerSwitcher>' +
+            '<ol:singleTile xmlns:ol="http://openlayers.org/context">true</ol:singleTile>' +
+          '</Extension>' +
+        '</Layer>' +
+      '</LayerList>' +
+    '</ViewContext>'
+);

Modified: core/trunk/geoext/tests/list-tests.html
===================================================================
--- core/trunk/geoext/tests/list-tests.html	2009-10-08 17:55:08 UTC (rev 1392)
+++ core/trunk/geoext/tests/list-tests.html	2009-10-08 18:09:30 UTC (rev 1393)
@@ -12,6 +12,7 @@
   <li>lib/GeoExt/data/WFSCapabilitiesReader.html</li>
   <li>lib/GeoExt/data/WMSCapabilitiesReader.html</li>
   <li>lib/GeoExt/data/WMSDescribeLayerReader.html</li>
+  <li>lib/GeoExt/data/WMCReader.html</li>
   <li>lib/GeoExt/widgets/Action.html</li>
   <li>lib/GeoExt/widgets/LayerOpacitySlider.html</li>
   <li>lib/GeoExt/widgets/MapPanel.html</li>



More information about the Commits mailing list