OpenLayers OpenLayers

Ticket #1176: WMSCapabilities.patch

File WMSCapabilities.patch, 12.4 kB (added by openlayers, 2 years ago)

wms capabilities parsing

  • Format/WMSCapabilities/v1_1_1.js

    old new  
     1/** 
     2 * @requires OpenLayers/Format/WMSCapabilities.js 
     3 * 
     4 * Class: OpenLayers.Format.WMSCapabilities.v1_1_1 
     5 * Read WMS Capabilities version 1.1.1. 
     6 *  
     7 * Inherits from: 
     8 *  - <OpenLayers.Format.WMSCapabilities> 
     9 */ 
     10OpenLayers.Format.WMSCapabilities.v1_1_1 = OpenLayers.Class( 
     11    OpenLayers.Format.WMSCapabilities, { 
     12     
     13    /** 
     14     * Constructor: OpenLayers.Format.WMSCapabilities.v1_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    /** 
     66     * Method: processService 
     67     */ 
     68    processService: function(capabilities, node) { 
     69        var service = {}; 
     70        this.runChildNodes(service, node); 
     71        capabilities.service = service; 
     72    }, 
     73 
     74    /** 
     75     * Method: processName 
     76     */ 
     77    processName: function(obj, node) { 
     78        var id = this.getChildValue(node); 
     79        if(id) { 
     80            obj["name"] = id; 
     81        } 
     82    }, 
     83 
     84    /** 
     85     * Method: processAbstract 
     86     */ 
     87    processAbstract: function(obj, node) { 
     88        var abst = this.getChildValue(node); 
     89        if(abst) { 
     90            obj["abstract"] = abst; 
     91        } 
     92    }, 
     93 
     94    /** 
     95     * Method: processKeywordList 
     96     */ 
     97    processKeywordList: function(obj, node) { 
     98        obj.keywords = []; 
     99        this.runChildNodes(obj, node); 
     100    }, 
     101 
     102    /** 
     103     * Method: processKeyword 
     104     */ 
     105    processKeyword: function(obj, node) { 
     106        var keyword = this.getChildValue(node); 
     107        obj.keywords.push(keyword); 
     108    }, 
     109 
     110 
     111    /** 
     112     * Method: processCapability 
     113     */ 
     114    processCapability: function(capabilities, node) { 
     115        var capability = { 
     116            layers: [] 
     117        }; 
     118        this.runChildNodes(capability, node); 
     119        capabilities.capability = capability; 
     120    }, 
     121 
     122    /** 
     123     * Method: processRequest 
     124     */ 
     125    processRequest: function(capabilities, node) { 
     126        var requests = {}; 
     127        this.runChildNodes(requests, node); 
     128        capabilities.requests = requests; 
     129    },     
     130 
     131    /** 
     132     * Method: processGetCapabilities 
     133     */ 
     134    processGetCapabilities: function(requests, node) { 
     135        var GetCapabilities = { 
     136            format: [] 
     137        }; 
     138        this.runChildNodes(GetCapabilities, node); 
     139        requests.GetCapabilities = GetCapabilities; 
     140    }, 
     141 
     142    /** 
     143     * Method: processGetMap 
     144     */ 
     145    processGetMap: function(requests, node) { 
     146        var GetMap = { 
     147            format: [] 
     148        }; 
     149        this.runChildNodes(GetMap, node); 
     150        requests.GetMap = GetMap; 
     151    }, 
     152 
     153    /** 
     154     * Method: processGetFeatureInfo 
     155     */ 
     156    processGetFeatureInfo: function(requests, node) { 
     157        var GetFeatureInfo = { 
     158            format: [] 
     159        }; 
     160        this.runChildNodes(GetFeatureInfo, node); 
     161        requests.GetFeatureInfo = GetFeatureInfo; 
     162    }, 
     163 
     164    /** 
     165     * Method: processFormat 
     166     */ 
     167    processFormat: function(request, node) { 
     168        var format = this.getChildValue(node); 
     169        request.format.push(format); 
     170    }, 
     171 
     172    /** 
     173     * Method: processDCPType 
     174     */ 
     175    processDCPType: function(request, node) { 
     176        this.runChildNodes(request, node); 
     177    }, 
     178 
     179    /** 
     180     * Method: processHTTP 
     181     */ 
     182    processHTTP: function(request, node) { 
     183        this.runChildNodes(request, node); 
     184    }, 
     185 
     186    /** 
     187     * Method: processGet 
     188     */ 
     189    processGet: function(request, node) { 
     190        this.readMethod(request, "GET", node); 
     191    }, 
     192 
     193    /** 
     194     * Method: processPost 
     195     */ 
     196    processPost: function(request, node) { 
     197        this.readMethod(request, "POST", node); 
     198    }, 
     199 
     200    readMethod: function(request, method, node) { 
     201        var children = node.childNodes; 
     202        for(var i=0; i<children.length; ++i) { 
     203            childNode = children[i]; 
     204            if(childNode.nodeType == 1) { 
     205                var href = this.getAttributeNS( 
     206                    childNode, "http://www.w3.org/1999/xlink", "href" 
     207                ); 
     208                 
     209                if(href.indexOf("?") < 0) 
     210                    href += "?"; 
     211                else if(href.lastIndexOf("&", href.length-1) != href.length-1) 
     212                    href += "&"; 
     213                 
     214                request[method] = href; 
     215            } 
     216        } 
     217    }, 
     218     
     219    /** 
     220     * Method: processLayer 
     221     */ 
     222    processLayer: function(capability, node, parentLayer) { 
     223        var layer = { 
     224            styles: [], 
     225            bbox: {}, 
     226            srs: [] 
     227        }; 
     228         
     229        // deal with property inheritance 
     230        if(parentLayer) { 
     231            if("styles" in parentLayer) 
     232                layer.styles = layer.styles.concat(parentLayer.styles); 
     233            if("srs" in parentLayer) 
     234                layer.srs = layer.srs.concat(parentLayer.srs); 
     235            if("bbox" in parentLayer) 
     236                OpenLayers.Util.applyDefaults(layer.bbox, parentLayer.bbox); 
     237        } 
     238         
     239        // if the layer supports GetFeatureInfo request 
     240        if(node.getAttribute("queryable")) 
     241            layer.queryable = node.getAttribute("queryable") == "1"; 
     242        // this attribute can be inherited 
     243        else if(parentLayer && "queryable" in parentLayer) 
     244            layer.queryable = parentLayer.queryable; 
     245        else 
     246            layer.queryable = false; 
     247         
     248        var children = node.childNodes; 
     249        var childNode, nodeName, processor; 
     250        for(var i=0; i<children.length; ++i) { 
     251            childNode = children[i]; 
     252            nodeName = childNode.nodeName; 
     253            processor = this["process" + childNode.nodeName]; 
     254            if(processor) { 
     255                if(nodeName == "Layer") { 
     256                    processor.apply(this, [capability, childNode, layer]); 
     257                } else { 
     258                    processor.apply(this, [layer, childNode]); 
     259                } 
     260            } 
     261        } 
     262        if("name" in layer) { 
     263            var index = layer.name.indexOf(":"); 
     264            if(index > 0) { 
     265                layer.prefix = layer.name.substring(0, index); 
     266            } 
     267            capability.layers.push(layer); 
     268        } 
     269    }, 
     270     
     271    /** 
     272     * Method: processSRS 
     273     */ 
     274    processSRS: function(obj, node) { 
     275        var srs = this.getChildValue(node); 
     276        if(srs) { 
     277            srs = srs.split(' '); 
     278             
     279            // do not duplicate values 
     280            for(var i=0; i<srs.length; ++i) { 
     281                var test = false; 
     282                for(var j=0; j<obj.srs.length; ++j) 
     283                    if(srs[i] == obj.srs[j]) { 
     284                        test = true; 
     285                        break; 
     286                    } 
     287                if(!test) 
     288                    obj.srs.push(srs[i]); 
     289            } 
     290        } 
     291    }, 
     292 
     293    /** 
     294     * Method: processTitle 
     295     */ 
     296    processTitle: function(obj, node) { 
     297        var title = this.getChildValue(node); 
     298        if(title) { 
     299            obj.title = title; 
     300        } 
     301    }, 
     302 
     303    /** 
     304     * Method: processLatLonBoundingBox 
     305     */ 
     306    processLatLonBoundingBox: function(layer, node) { 
     307        layer.bbox["default"] = new OpenLayers.Bounds( 
     308            parseFloat(node.getAttribute("minx")), 
     309            parseFloat(node.getAttribute("miny")), 
     310            parseFloat(node.getAttribute("maxx")), 
     311            parseFloat(node.getAttribute("maxy")) 
     312        ); 
     313    }, 
     314     
     315     
     316    /** 
     317     * Method: processBoundingBox 
     318     */ 
     319    processBoundingBox: function(layer, node) { 
     320        var srs = node.getAttribute("SRS"); 
     321         
     322        layer.bbox[srs] = new OpenLayers.Bounds( 
     323            parseFloat(node.getAttribute("minx")), 
     324            parseFloat(node.getAttribute("miny")), 
     325            parseFloat(node.getAttribute("maxx")), 
     326            parseFloat(node.getAttribute("maxy")) 
     327        ); 
     328    }, 
     329     
     330    /** 
     331     * Method: processStyle 
     332     */ 
     333    processStyle: function(layer, node) { 
     334        var style = {}; 
     335        this.runChildNodes(style, node); 
     336        layer.styles.push(style); 
     337    }, 
     338 
     339    /** 
     340     * Method: processLegendURL 
     341     */ 
     342    processLegendURL: function(style, node) { 
     343        var legend = { 
     344            width: node.getAttribute('width'), 
     345            height: node.getAttribute('height') 
     346        }; 
     347        var links = node.getElementsByTagName("OnlineResource"); 
     348        if(links.length > 0) { 
     349            this.processOnlineResource(legend, links[0]); 
     350        } 
     351        style.legend = legend; 
     352    }, 
     353     
     354    /** 
     355     * Method: processOnlineResource 
     356     */ 
     357    processOnlineResource: function(obj, node) { 
     358        obj.href = this.getAttributeNS( 
     359            node, "http://www.w3.org/1999/xlink", "href" 
     360        ); 
     361    }, 
     362 
     363    CLASS_NAME: "OpenLayers.Format.WMSCapabilities.v1_1_1"  
     364 
     365}); 
  • 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.WMSCapabilities[ 
     60            "v" + 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});