]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Format/WMSCapabilities/v1_1.js
initial commit
[syp.git] / openlayers / lib / OpenLayers / Format / WMSCapabilities / v1_1.js
1 /**
2  * @requires OpenLayers/Format/WMSCapabilities.js
3  * @requires OpenLayers/Format/XML.js
4  */
5
6 /**
7  * Class: OpenLayers.Format.WMSCapabilities.v1_1
8  * Abstract class not to be instantiated directly.
9  * 
10  * Inherits from:
11  *  - <OpenLayers.Format.XML>
12  */
13 OpenLayers.Format.WMSCapabilities.v1_1 = OpenLayers.Class(
14     OpenLayers.Format.XML, {
15     
16     /**
17      * Constructor: OpenLayers.Format.WMSCapabilities.v1_1
18      * Create an instance of one of the subclasses.
19      *
20      * Parameters:
21      * options - {Object} An optional object whose properties will be set on
22      *     this instance.
23      */
24     initialize: function(options) {
25         OpenLayers.Format.XML.prototype.initialize.apply(this, [options]);
26         this.options = options;
27     },
28
29     /**
30      * APIMethod: read
31      * Read capabilities data from a string, and return a list of layers. 
32      * 
33      * Parameters: 
34      * data - {String} or {DOMElement} data to read/parse.
35      *
36      * Returns:
37      * {Array} List of named layers.
38      */
39     read: function(data) {
40         if(typeof data == "string") {
41             data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);
42         }
43         var capabilities = {};
44         var root = data.documentElement;
45         this.runChildNodes(capabilities, root);
46         return capabilities;
47     },
48     
49     /**
50      * Method: runChildNodes
51      */
52     runChildNodes: function(obj, node) {
53         var children = node.childNodes;
54         var childNode, processor;
55         for(var i=0; i<children.length; ++i) {
56             childNode = children[i];
57             if(childNode.nodeType == 1) {
58                 processor = this["read_cap_" + childNode.nodeName];
59                 if(processor) {
60                     processor.apply(this, [obj, childNode]);
61                 }
62             }
63         }
64     },
65     
66     /**
67      * Method: read_cap_Capability
68      */
69     read_cap_Capability: function(capabilities, node) {
70         var capability = {
71             layers: []
72         };
73         this.runChildNodes(capability, node);
74         capabilities.capability = capability;
75     },
76     
77     /**
78      * Method: read_cap_Request
79      */
80     read_cap_Request: function(obj, node) {
81         var request = {};
82         this.runChildNodes(request, node);
83         obj.request = request;
84     },
85     
86     /**
87      * Method: read_cap_GetMap
88      */
89     read_cap_GetMap: function(request, node) {
90         var getmap = {
91             formats: []
92         };
93         this.runChildNodes(getmap, node);
94         request.getmap = getmap;
95     },
96     
97     /**
98      * Method: read_cap_Format
99      */
100     read_cap_Format: function(obj, node) {
101         if(obj.formats) {
102             obj.formats.push(this.getChildValue(node));
103         }
104     },
105     
106     /**
107      * Method: read_cap_DCPType
108      * Super simplified HTTP href extractor.  Assumes the first online resource
109      *     will work.
110      */
111     read_cap_DCPType: function(obj, node) {
112         var children = node.getElementsByTagName("OnlineResource");
113         if(children.length > 0) {
114             this.read_cap_OnlineResource(obj, children[0]);
115         }
116     },
117
118     /**
119      * Method: read_cap_Service
120      */
121     read_cap_Service: function(capabilities, node) {
122         var service = {};
123         this.runChildNodes(service, node);
124         capabilities.service = service;
125     },
126
127     /**
128      * Method: read_cap_Layer
129      */
130     read_cap_Layer: function(capability, node, parentLayer) {
131         var layer = {
132             formats: capability.request.getmap.formats || [],
133             styles: [],
134             queryable: (node.getAttribute("queryable") === "1" 
135                         || node.getAttribute("queryable") === "true")
136         };
137         // deal with property inheritance
138         if(parentLayer) {
139             // add style
140             layer.styles = layer.styles.concat(parentLayer.styles);
141             // use llbbox
142             layer.llbbox = parentLayer.llbbox;
143             // use min/maxScale
144             layer.minScale = parentLayer.minScale;
145             layer.maxScale = parentLayer.maxScale;
146         }
147         var children = node.childNodes;
148         var childNode, nodeName, processor;
149         for(var i=0; i<children.length; ++i) {
150             childNode = children[i];
151             nodeName = childNode.nodeName;
152             processor = this["read_cap_" + childNode.nodeName];
153             if(processor) {
154                 if(nodeName == "Layer") {
155                     processor.apply(this, [capability, childNode, layer]);
156                 } else {
157                     processor.apply(this, [layer, childNode]);
158                 }
159             }
160         }
161         if(layer.name) {
162             var index = layer.name.indexOf(":");
163             if(index > 0) {
164                 layer.prefix = layer.name.substring(0, index);
165             }
166             capability.layers.push(layer);
167         }
168     },
169     
170     /**
171      * Method: read_cap_ScaleHint
172      * The Layer ScaleHint element has min and max attributes that relate to
173      *     the minimum and maximum resolution that the server supports.  The
174      *     values are pixel diagonals measured in meters (on the ground).
175      */
176     read_cap_ScaleHint: function(layer, node) {
177         var min = node.getAttribute("min");
178         var max = node.getAttribute("max");
179         var rad2 = Math.pow(2, 0.5);
180         var ipm = OpenLayers.INCHES_PER_UNIT["m"];
181         layer.maxScale = parseFloat(
182             ((rad2 * min) * ipm * OpenLayers.DOTS_PER_INCH).toPrecision(13)
183         );
184         layer.minScale = parseFloat(
185             ((rad2 * max) * ipm * OpenLayers.DOTS_PER_INCH).toPrecision(13)
186         );
187     },
188     
189     /**
190      * Method: read_cap_Name
191      */
192     read_cap_Name: function(obj, node) {
193         var name = this.getChildValue(node);
194         if(name) {
195             obj.name = name;
196         }
197     },
198
199     /**
200      * Method: read_cap_Title
201      */
202     read_cap_Title: function(obj, node) {
203         var title = this.getChildValue(node);
204         if(title) {
205             obj.title = title;
206         }
207     },
208
209     /**
210      * Method: read_cap_Abstract
211      */
212     read_cap_Abstract: function(obj, node) {
213         var abst = this.getChildValue(node);
214         if(abst) {
215             obj["abstract"] = abst;
216         }
217     },
218     
219     /**
220      * Method: read_cap_LatLonBoundingBox
221      */
222     read_cap_LatLonBoundingBox: function(layer, node) {
223         layer.llbbox = [
224             parseFloat(node.getAttribute("minx")),
225             parseFloat(node.getAttribute("miny")),
226             parseFloat(node.getAttribute("maxx")),
227             parseFloat(node.getAttribute("maxy"))
228         ];
229     },
230
231     /**
232      * Method: read_cap_Style
233      */
234     read_cap_Style: function(layer, node) {
235         var style = {};
236         this.runChildNodes(style, node);
237         layer.styles.push(style);
238     },
239
240     /**
241      * Method: read_cap_LegendURL
242      */
243     read_cap_LegendURL: function(style, node) {
244         var legend = {
245             width: node.getAttribute('width'),
246             height: node.getAttribute('height')
247         };
248         var links = node.getElementsByTagName("OnlineResource");
249         if(links.length > 0) {
250             this.read_cap_OnlineResource(legend, links[0]);
251         }
252         style.legend = legend;
253     },
254     
255     /**
256      * Method: read_cap_OnlineResource
257      */
258     read_cap_OnlineResource: function(obj, node) {
259         obj.href = this.getAttributeNS(
260             node, "http://www.w3.org/1999/xlink", "href"
261         );
262     },
263
264     CLASS_NAME: "OpenLayers.Format.WMSCapabilities.v1_1" 
265
266 });