OpenLayers OpenLayers

Ticket #1176: wms_capabilities.patch

File wms_capabilities.patch, 8.0 kB (added by tschaub, 9 months ago)

wms capabilities parsing

  • lib/OpenLayers/Format/WMSCapabilities_1_1_1.js

    old new  
     1/** 
     2 * @requires OpenLayers/Format/WMSCapabilities.js 
     3 * 
     4 * Class: OpenLayers.Format.WMSCapabilities_1_1_1 
     5 * Read WMS Capabilities version 1.1.1. 
     6 *  
     7 * Inherits from: 
     8 *  - <OpenLayers.Format.WMSCapabilities> 
     9 */ 
     10OpenLayers.Format.WMSCapabilities_1_1_1 = OpenLayers.Class( 
     11    OpenLayers.Format.WMSCapabilities, { 
     12     
     13    /** 
     14     * Constructor: OpenLayers.Format.WMSCapabilities_1_1_1 
     15     * Create a new parser for WMS capabilities version 1_1_1. 
     16     * 
     17     * Parameters: 
     18     * options - {Object} An optional object whose properties will be set on 
     19     *     this instance. 
     20     */ 
     21    initialize: function(options) { 
     22        OpenLayers.Format.WMSCapabilities.prototype.initialize.apply( 
     23            this, [options] 
     24        ); 
     25    }, 
     26 
     27    /** 
     28     * APIMethod: read 
     29     * Read capabilities data from a string, and return a list of layers.  
     30     *  
     31     * Parameters:  
     32     * data - {String} or {DOMElement} data to read/parse. 
     33     * 
     34     * Returns: 
     35     * {Array} List of named layers. 
     36     */ 
     37    read: function(data) { 
     38        if(typeof data == "string") { 
     39            data = OpenLayers.Format.XML.prototype.read.apply(this, [data]); 
     40        } 
     41        var capabilities = {}; 
     42        var root = data.documentElement; 
     43        this.runChildNodes(capabilities, root); 
     44        return capabilities; 
     45    }, 
     46     
     47    /** 
     48     * Method: runChildNodes 
     49     */ 
     50    runChildNodes: function(obj, node) { 
     51        var children = node.childNodes; 
     52        var childNode, processor; 
     53        for(var i=0; i<children.length; ++i) { 
     54            childNode = children[i]; 
     55            if(childNode.nodeType == 1) { 
     56                processor = this["process" + childNode.nodeName]; 
     57                if(processor) { 
     58                    processor.apply(this, [obj, childNode]); 
     59                } 
     60            } 
     61        } 
     62    }, 
     63     
     64    /** 
     65     * Method: processCapability 
     66     */ 
     67    processCapability: function(capabilities, node) { 
     68        var capability = { 
     69            layers: [] 
     70        }; 
     71        this.runChildNodes(capability, node); 
     72        capabilities.capability = capability; 
     73    }, 
     74     
     75    /** 
     76     * Method: processService 
     77     */ 
     78    processService: function(capabilities, node) { 
     79        var service = {}; 
     80        this.runChildNodes(service, node); 
     81        capabilities.service = service; 
     82    }, 
     83 
     84    /** 
     85     * Method: processLayer 
     86     */ 
     87    processLayer: function(capability, node, parentLayer) { 
     88        var layer = { 
     89            styles: [] 
     90        }; 
     91        // deal with property inheritance 
     92        if(parentLayer) { 
     93            // add style 
     94            layer.styles = layer.styles.concat(parent.styles); 
     95        } 
     96        var children = node.childNodes; 
     97        var childNode, nodeName, processor; 
     98        for(var i=0; i<children.length; ++i) { 
     99            childNode = children[i]; 
     100            nodeName = childNode.nodeName; 
     101            processor = this["process" + childNode.nodeName]; 
     102            if(processor) { 
     103                if(nodeName == "Layer") { 
     104                    processor.apply(this, [capability, childNode, parentLayer]); 
     105                } else { 
     106                    processor.apply(this, [layer, childNode]); 
     107                } 
     108            } 
     109        } 
     110        if(layer.name) { 
     111            var index = layer.name.indexOf(":"); 
     112            if(index > 0) { 
     113                layer.prefix = layer.name.substring(0, index); 
     114            } 
     115            capability.layers.push(layer); 
     116        } 
     117    }, 
     118     
     119    /** 
     120     * Method: processName 
     121     */ 
     122    processName: function(obj, node) { 
     123        var name = this.getChildValue(node); 
     124        if(name) { 
     125            obj.name = name; 
     126        } 
     127    }, 
     128 
     129    /** 
     130     * Method: processTitle 
     131     */ 
     132    processTitle: function(obj, node) { 
     133        var title = this.getChildValue(node); 
     134        if(title) { 
     135            obj.title = title; 
     136        } 
     137    }, 
     138 
     139    /** 
     140     * Method: processAbstract 
     141     */ 
     142    processAbstract: function(obj, node) { 
     143        var abst = this.getChildValue(node); 
     144        if(abst) { 
     145            obj["abstract"] = abst; 
     146        } 
     147    }, 
     148     
     149    /** 
     150     * Method: processLatLonBoundingBox 
     151     */ 
     152    processLatLonBoundingBox: function(layer, node) { 
     153        layer.llbbox = [ 
     154            parseFloat(node.getAttribute("minx")), 
     155            parseFloat(node.getAttribute("miny")), 
     156            parseFloat(node.getAttribute("maxx")), 
     157            parseFloat(node.getAttribute("maxy")) 
     158        ]; 
     159    }, 
     160 
     161    /** 
     162     * Method: processStyle 
     163     */ 
     164    processStyle: function(layer, node) { 
     165        var style = {}; 
     166        this.runChildNodes(style, node); 
     167        layer.styles.push(style); 
     168    }, 
     169 
     170    /** 
     171     * Method: processLegendURL 
     172     */ 
     173    processLegendURL: function(style, node) { 
     174        var legend = { 
     175            width: node.getAttribute('width'), 
     176            height: node.getAttribute('height') 
     177        }; 
     178        var links = node.getElementsByTagName("OnlineResource"); 
     179        if(links.length > 0) { 
     180            this.processOnlineResource(legend, links[0]); 
     181        } 
     182        style.legend = legend; 
     183    }, 
     184     
     185    /** 
     186     * Method: processOnlineResource 
     187     */ 
     188    processOnlineResource: function(obj, node) { 
     189        obj.href = this.getAttributeNS( 
     190            node, "http://www.w3.org/1999/xlink", "href" 
     191        ); 
     192    }, 
     193 
     194    CLASS_NAME: "OpenLayers.Format.WMSCapabilities_1_1_1"  
     195 
     196}); 
  • lib/OpenLayers/Format/WMSCapabilities.js

    old new  
     1/** 
     2 * @requires OpenLayers/Format/XML.js 
     3 * 
     4 * Class: OpenLayers.Format.WMSCapabilities 
     5 * Read WMS Capabilities. 
     6 *  
     7 * Inherits from: 
     8 *  - <OpenLayers.Format.XML> 
     9 */ 
     10OpenLayers.Format.WMSCapabilities = OpenLayers.Class(OpenLayers.Format.XML, { 
     11     
     12    /** 
     13     * APIProperty: defaultVersion 
     14     * {String} Version number to assume if none found.  Default is "1.1.1". 
     15     */ 
     16    defaultVersion: "1.1.1", 
     17     
     18    /** 
     19     * APIProperty: version 
     20     * {String} Specify a version string if one is known. 
     21     */ 
     22    version: null, 
     23 
     24    /** 
     25     * Constructor: OpenLayers.Format.WMSCapabilities 
     26     * Create a new parser for WMS capabilities. 
     27     * 
     28     * Parameters: 
     29     * options - {Object} An optional object whose properties will be set on 
     30     *     this instance. 
     31     */ 
     32    initialize: function(options) { 
     33        OpenLayers.Format.XML.prototype.initialize.apply(this, [options]); 
     34        this.options = options; 
     35    }, 
     36 
     37    /** 
     38     * APIMethod: read 
     39     * Read capabilities data from a string, and return a list of layers.  
     40     *  
     41     * Parameters:  
     42     * data - {String} or {DOMElement} data to read/parse. 
     43     * 
     44     * Returns: 
     45     * {Array} List of named layers. 
     46     */ 
     47    read: function(data) { 
     48        if(typeof data == "string") { 
     49            data = OpenLayers.Format.XML.prototype.read.apply(this, [data]); 
     50        } 
     51        var root = data.documentElement; 
     52        var version = this.version; 
     53        if(!version) { 
     54            version = root.getAttribute("version"); 
     55            if(!version) { 
     56                version = this.defaultVersion; 
     57            } 
     58        } 
     59        var constructor = OpenLayers.Format[ 
     60            "WMSCapabilities_" + version.replace(/\./g, "_") 
     61        ]; 
     62        if(!constructor) { 
     63            throw "Can't find a WMS capabilities parser for version " + version; 
     64        } 
     65        var parser = new constructor(this.options); 
     66        var capabilities = parser.read(data); 
     67        capabilities.version = version; 
     68        return capabilities; 
     69    }, 
     70     
     71    CLASS_NAME: "OpenLayers.Format.WMSCapabilities"  
     72 
     73});