[76] | 1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
| 2 | * full list of contributors). Published under the Clear BSD license. |
---|
| 3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
| 4 | * full text of the license. */ |
---|
| 5 | |
---|
| 6 | /** |
---|
| 7 | * @requires OpenLayers/Format/WMC.js |
---|
| 8 | * @requires OpenLayers/Format/XML.js |
---|
| 9 | */ |
---|
| 10 | |
---|
| 11 | /** |
---|
| 12 | * Class: OpenLayers.Format.WMC.v1 |
---|
| 13 | * Superclass for WMC version 1 parsers. |
---|
| 14 | * |
---|
| 15 | * Inherits from: |
---|
| 16 | * - <OpenLayers.Format.XML> |
---|
| 17 | */ |
---|
| 18 | OpenLayers.Format.WMC.v1 = OpenLayers.Class(OpenLayers.Format.XML, { |
---|
| 19 | |
---|
| 20 | /** |
---|
| 21 | * Property: namespaces |
---|
| 22 | * {Object} Mapping of namespace aliases to namespace URIs. |
---|
| 23 | */ |
---|
| 24 | namespaces: { |
---|
| 25 | ol: "http://openlayers.org/context", |
---|
| 26 | wmc: "http://www.opengis.net/context", |
---|
| 27 | sld: "http://www.opengis.net/sld", |
---|
| 28 | xlink: "http://www.w3.org/1999/xlink", |
---|
| 29 | xsi: "http://www.w3.org/2001/XMLSchema-instance" |
---|
| 30 | }, |
---|
| 31 | |
---|
| 32 | /** |
---|
| 33 | * Property: schemaLocation |
---|
| 34 | * {String} Schema location for a particular minor version. |
---|
| 35 | */ |
---|
| 36 | schemaLocation: "", |
---|
| 37 | |
---|
| 38 | /** |
---|
| 39 | * Method: getNamespacePrefix |
---|
| 40 | * Get the namespace prefix for a given uri from the <namespaces> object. |
---|
| 41 | * |
---|
| 42 | * Returns: |
---|
| 43 | * {String} A namespace prefix or null if none found. |
---|
| 44 | */ |
---|
| 45 | getNamespacePrefix: function(uri) { |
---|
| 46 | var prefix = null; |
---|
| 47 | if(uri == null) { |
---|
| 48 | prefix = this.namespaces[this.defaultPrefix]; |
---|
| 49 | } else { |
---|
| 50 | for(prefix in this.namespaces) { |
---|
| 51 | if(this.namespaces[prefix] == uri) { |
---|
| 52 | break; |
---|
| 53 | } |
---|
| 54 | } |
---|
| 55 | } |
---|
| 56 | return prefix; |
---|
| 57 | }, |
---|
| 58 | |
---|
| 59 | /** |
---|
| 60 | * Property: defaultPrefix |
---|
| 61 | */ |
---|
| 62 | defaultPrefix: "wmc", |
---|
| 63 | |
---|
| 64 | /** |
---|
| 65 | * Property: rootPrefix |
---|
| 66 | * {String} Prefix on the root node that maps to the context namespace URI. |
---|
| 67 | */ |
---|
| 68 | rootPrefix: null, |
---|
| 69 | |
---|
| 70 | /** |
---|
| 71 | * Property: defaultStyleName |
---|
| 72 | * {String} Style name used if layer has no style param. Default is "". |
---|
| 73 | */ |
---|
| 74 | defaultStyleName: "", |
---|
| 75 | |
---|
| 76 | /** |
---|
| 77 | * Property: defaultStyleTitle |
---|
| 78 | * {String} Default style title. Default is "Default". |
---|
| 79 | */ |
---|
| 80 | defaultStyleTitle: "Default", |
---|
| 81 | |
---|
| 82 | /** |
---|
| 83 | * Constructor: OpenLayers.Format.WMC.v1 |
---|
| 84 | * Instances of this class are not created directly. Use the |
---|
| 85 | * <OpenLayers.Format.WMC> constructor instead. |
---|
| 86 | * |
---|
| 87 | * Parameters: |
---|
| 88 | * options - {Object} An optional object whose properties will be set on |
---|
| 89 | * this instance. |
---|
| 90 | */ |
---|
| 91 | initialize: function(options) { |
---|
| 92 | OpenLayers.Format.XML.prototype.initialize.apply(this, [options]); |
---|
| 93 | }, |
---|
| 94 | |
---|
| 95 | /** |
---|
| 96 | * Method: read |
---|
| 97 | * Read capabilities data from a string, and return a list of layers. |
---|
| 98 | * |
---|
| 99 | * Parameters: |
---|
| 100 | * data - {String} or {DOMElement} data to read/parse. |
---|
| 101 | * |
---|
| 102 | * Returns: |
---|
| 103 | * {Array} List of named layers. |
---|
| 104 | */ |
---|
| 105 | read: function(data) { |
---|
| 106 | if(typeof data == "string") { |
---|
| 107 | data = OpenLayers.Format.XML.prototype.read.apply(this, [data]); |
---|
| 108 | } |
---|
| 109 | var root = data.documentElement; |
---|
| 110 | this.rootPrefix = root.prefix; |
---|
| 111 | var context = { |
---|
| 112 | version: root.getAttribute("version") |
---|
| 113 | }; |
---|
| 114 | this.runChildNodes(context, root); |
---|
| 115 | return context; |
---|
| 116 | }, |
---|
| 117 | |
---|
| 118 | /** |
---|
| 119 | * Method: runChildNodes |
---|
| 120 | */ |
---|
| 121 | runChildNodes: function(obj, node) { |
---|
| 122 | var children = node.childNodes; |
---|
| 123 | var childNode, processor, prefix, local; |
---|
| 124 | for(var i=0, len=children.length; i<len; ++i) { |
---|
| 125 | childNode = children[i]; |
---|
| 126 | if(childNode.nodeType == 1) { |
---|
| 127 | prefix = this.getNamespacePrefix(childNode.namespaceURI); |
---|
| 128 | local = childNode.nodeName.split(":").pop(); |
---|
| 129 | processor = this["read_" + prefix + "_" + local]; |
---|
| 130 | if(processor) { |
---|
| 131 | processor.apply(this, [obj, childNode]); |
---|
| 132 | } |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | }, |
---|
| 136 | |
---|
| 137 | /** |
---|
| 138 | * Method: read_wmc_General |
---|
| 139 | */ |
---|
| 140 | read_wmc_General: function(context, node) { |
---|
| 141 | this.runChildNodes(context, node); |
---|
| 142 | }, |
---|
| 143 | |
---|
| 144 | /** |
---|
| 145 | * Method: read_wmc_BoundingBox |
---|
| 146 | */ |
---|
| 147 | read_wmc_BoundingBox: function(context, node) { |
---|
| 148 | context.projection = node.getAttribute("SRS"); |
---|
| 149 | context.bounds = new OpenLayers.Bounds( |
---|
| 150 | parseFloat(node.getAttribute("minx")), |
---|
| 151 | parseFloat(node.getAttribute("miny")), |
---|
| 152 | parseFloat(node.getAttribute("maxx")), |
---|
| 153 | parseFloat(node.getAttribute("maxy")) |
---|
| 154 | ); |
---|
| 155 | }, |
---|
| 156 | |
---|
| 157 | /** |
---|
| 158 | * Method: read_wmc_LayerList |
---|
| 159 | */ |
---|
| 160 | read_wmc_LayerList: function(context, node) { |
---|
| 161 | // layersContext is an array containing info for each layer |
---|
| 162 | context.layersContext = []; |
---|
| 163 | this.runChildNodes(context, node); |
---|
| 164 | }, |
---|
| 165 | |
---|
| 166 | /** |
---|
| 167 | * Method: read_wmc_Layer |
---|
| 168 | */ |
---|
| 169 | read_wmc_Layer: function(context, node) { |
---|
| 170 | var layerContext = { |
---|
| 171 | visibility: (node.getAttribute("hidden") != "1"), |
---|
| 172 | queryable: (node.getAttribute("queryable") == "1"), |
---|
| 173 | formats: [], |
---|
| 174 | styles: [] |
---|
| 175 | }; |
---|
| 176 | |
---|
| 177 | this.runChildNodes(layerContext, node); |
---|
| 178 | // set properties common to multiple objects on layer options/params |
---|
| 179 | context.layersContext.push(layerContext); |
---|
| 180 | }, |
---|
| 181 | |
---|
| 182 | /** |
---|
| 183 | * Method: read_wmc_Extension |
---|
| 184 | */ |
---|
| 185 | read_wmc_Extension: function(obj, node) { |
---|
| 186 | this.runChildNodes(obj, node); |
---|
| 187 | }, |
---|
| 188 | |
---|
| 189 | /** |
---|
| 190 | * Method: read_ol_units |
---|
| 191 | */ |
---|
| 192 | read_ol_units: function(layerContext, node) { |
---|
| 193 | layerContext.units = this.getChildValue(node); |
---|
| 194 | }, |
---|
| 195 | |
---|
| 196 | /** |
---|
| 197 | * Method: read_ol_maxExtent |
---|
| 198 | */ |
---|
| 199 | read_ol_maxExtent: function(obj, node) { |
---|
| 200 | var bounds = new OpenLayers.Bounds( |
---|
| 201 | node.getAttribute("minx"), node.getAttribute("miny"), |
---|
| 202 | node.getAttribute("maxx"), node.getAttribute("maxy") |
---|
| 203 | ); |
---|
| 204 | obj.maxExtent = bounds; |
---|
| 205 | }, |
---|
| 206 | |
---|
| 207 | /** |
---|
| 208 | * Method: read_ol_transparent |
---|
| 209 | */ |
---|
| 210 | read_ol_transparent: function(layerContext, node) { |
---|
| 211 | layerContext.transparent = this.getChildValue(node); |
---|
| 212 | }, |
---|
| 213 | |
---|
| 214 | /** |
---|
| 215 | * Method: read_ol_numZoomLevels |
---|
| 216 | */ |
---|
| 217 | read_ol_numZoomLevels: function(layerContext, node) { |
---|
| 218 | layerContext.numZoomLevels = parseInt(this.getChildValue(node)); |
---|
| 219 | }, |
---|
| 220 | |
---|
| 221 | /** |
---|
| 222 | * Method: read_ol_opacity |
---|
| 223 | */ |
---|
| 224 | read_ol_opacity: function(layerContext, node) { |
---|
| 225 | layerContext.opacity = parseFloat(this.getChildValue(node)); |
---|
| 226 | }, |
---|
| 227 | |
---|
| 228 | /** |
---|
| 229 | * Method: read_ol_singleTile |
---|
| 230 | */ |
---|
| 231 | read_ol_singleTile: function(layerContext, node) { |
---|
| 232 | layerContext.singleTile = (this.getChildValue(node) == "true"); |
---|
| 233 | }, |
---|
| 234 | |
---|
| 235 | /** |
---|
| 236 | * Method: read_ol_tileSize |
---|
| 237 | */ |
---|
| 238 | read_ol_tileSize: function(layerContext, node) { |
---|
| 239 | var obj = {"width": node.getAttribute("width"), "height": node.getAttribute("height")}; |
---|
| 240 | layerContext.tileSize = obj; |
---|
| 241 | }, |
---|
| 242 | |
---|
| 243 | /** |
---|
| 244 | * Method: read_ol_isBaseLayer |
---|
| 245 | */ |
---|
| 246 | read_ol_isBaseLayer: function(layerContext, node) { |
---|
| 247 | layerContext.isBaseLayer = (this.getChildValue(node) == "true"); |
---|
| 248 | }, |
---|
| 249 | |
---|
| 250 | /** |
---|
| 251 | * Method: read_ol_displayInLayerSwitcher |
---|
| 252 | */ |
---|
| 253 | read_ol_displayInLayerSwitcher: function(layerContext, node) { |
---|
| 254 | layerContext.displayInLayerSwitcher = (this.getChildValue(node) == "true"); |
---|
| 255 | }, |
---|
| 256 | |
---|
| 257 | /** |
---|
| 258 | * Method: read_wmc_Server |
---|
| 259 | */ |
---|
| 260 | read_wmc_Server: function(layerContext, node) { |
---|
| 261 | layerContext.version = node.getAttribute("version"); |
---|
| 262 | var server = {}; |
---|
| 263 | var links = node.getElementsByTagName("OnlineResource"); |
---|
| 264 | if(links.length > 0) { |
---|
| 265 | this.read_wmc_OnlineResource(server, links[0]); |
---|
| 266 | } |
---|
| 267 | layerContext.url = server.href; |
---|
| 268 | }, |
---|
| 269 | |
---|
| 270 | /** |
---|
| 271 | * Method: read_wmc_FormatList |
---|
| 272 | */ |
---|
| 273 | read_wmc_FormatList: function(layerContext, node) { |
---|
| 274 | this.runChildNodes(layerContext, node); |
---|
| 275 | }, |
---|
| 276 | |
---|
| 277 | /** |
---|
| 278 | * Method: read_wmc_Format |
---|
| 279 | */ |
---|
| 280 | read_wmc_Format: function(layerContext, node) { |
---|
| 281 | var format = { |
---|
| 282 | value: this.getChildValue(node) |
---|
| 283 | }; |
---|
| 284 | if(node.getAttribute("current") == "1") { |
---|
| 285 | format.current = true; |
---|
| 286 | } |
---|
| 287 | layerContext.formats.push(format); |
---|
| 288 | }, |
---|
| 289 | |
---|
| 290 | /** |
---|
| 291 | * Method: read_wmc_StyleList |
---|
| 292 | */ |
---|
| 293 | read_wmc_StyleList: function(layerContext, node) { |
---|
| 294 | this.runChildNodes(layerContext, node); |
---|
| 295 | }, |
---|
| 296 | |
---|
| 297 | /** |
---|
| 298 | * Method: read_wmc_Style |
---|
| 299 | */ |
---|
| 300 | read_wmc_Style: function(layerContext, node) { |
---|
| 301 | var style = {}; |
---|
| 302 | this.runChildNodes(style, node); |
---|
| 303 | if(node.getAttribute("current") == "1") { |
---|
| 304 | style.current = true; |
---|
| 305 | } |
---|
| 306 | layerContext.styles.push(style); |
---|
| 307 | }, |
---|
| 308 | |
---|
| 309 | /** |
---|
| 310 | * Method: read_wmc_SLD |
---|
| 311 | */ |
---|
| 312 | read_wmc_SLD: function(style, node) { |
---|
| 313 | this.runChildNodes(style, node); |
---|
| 314 | // style either comes back with an href or a body property |
---|
| 315 | }, |
---|
| 316 | |
---|
| 317 | /** |
---|
| 318 | * Method: read_sld_StyledLayerDescriptor |
---|
| 319 | */ |
---|
| 320 | read_sld_StyledLayerDescriptor: function(sld, node) { |
---|
| 321 | var xml = OpenLayers.Format.XML.prototype.write.apply(this, [node]); |
---|
| 322 | sld.body = xml; |
---|
| 323 | }, |
---|
| 324 | |
---|
| 325 | /** |
---|
| 326 | * Method: read_wmc_OnlineResource |
---|
| 327 | */ |
---|
| 328 | read_wmc_OnlineResource: function(obj, node) { |
---|
| 329 | obj.href = this.getAttributeNS( |
---|
| 330 | node, this.namespaces.xlink, "href" |
---|
| 331 | ); |
---|
| 332 | }, |
---|
| 333 | |
---|
| 334 | /** |
---|
| 335 | * Method: read_wmc_Name |
---|
| 336 | */ |
---|
| 337 | read_wmc_Name: function(obj, node) { |
---|
| 338 | var name = this.getChildValue(node); |
---|
| 339 | if(name) { |
---|
| 340 | obj.name = name; |
---|
| 341 | } |
---|
| 342 | }, |
---|
| 343 | |
---|
| 344 | /** |
---|
| 345 | * Method: read_wmc_Title |
---|
| 346 | */ |
---|
| 347 | read_wmc_Title: function(obj, node) { |
---|
| 348 | var title = this.getChildValue(node); |
---|
| 349 | if(title) { |
---|
| 350 | obj.title = title; |
---|
| 351 | } |
---|
| 352 | }, |
---|
| 353 | |
---|
| 354 | /** |
---|
| 355 | * Method: read_wmc_MetadataURL |
---|
| 356 | */ |
---|
| 357 | read_wmc_MetadataURL: function(layerContext, node) { |
---|
| 358 | var metadataURL = {}; |
---|
| 359 | var links = node.getElementsByTagName("OnlineResource"); |
---|
| 360 | if(links.length > 0) { |
---|
| 361 | this.read_wmc_OnlineResource(metadataURL, links[0]); |
---|
| 362 | } |
---|
| 363 | layerContext.metadataURL = metadataURL.href; |
---|
| 364 | |
---|
| 365 | }, |
---|
| 366 | |
---|
| 367 | /** |
---|
| 368 | * Method: read_wmc_Abstract |
---|
| 369 | */ |
---|
| 370 | read_wmc_Abstract: function(obj, node) { |
---|
| 371 | var abst = this.getChildValue(node); |
---|
| 372 | if(abst) { |
---|
| 373 | obj["abstract"] = abst; |
---|
| 374 | } |
---|
| 375 | }, |
---|
| 376 | |
---|
| 377 | /** |
---|
| 378 | * Method: read_wmc_LegendURL |
---|
| 379 | */ |
---|
| 380 | read_wmc_LegendURL: function(style, node) { |
---|
| 381 | var legend = { |
---|
| 382 | width: node.getAttribute('width'), |
---|
| 383 | height: node.getAttribute('height') |
---|
| 384 | }; |
---|
| 385 | var links = node.getElementsByTagName("OnlineResource"); |
---|
| 386 | if(links.length > 0) { |
---|
| 387 | this.read_wmc_OnlineResource(legend, links[0]); |
---|
| 388 | } |
---|
| 389 | style.legend = legend; |
---|
| 390 | }, |
---|
| 391 | |
---|
| 392 | /** |
---|
| 393 | * Method: write |
---|
| 394 | * |
---|
| 395 | * Parameters: |
---|
| 396 | * context - {Object} An object representing the map context. |
---|
| 397 | * options - {Object} Optional object. |
---|
| 398 | * |
---|
| 399 | * Returns: |
---|
| 400 | * {String} A WMC document string. |
---|
| 401 | */ |
---|
| 402 | write: function(context, options) { |
---|
| 403 | var root = this.createElementDefaultNS("ViewContext"); |
---|
| 404 | this.setAttributes(root, { |
---|
| 405 | version: this.VERSION, |
---|
| 406 | id: (options && typeof options.id == "string") ? |
---|
| 407 | options.id : |
---|
| 408 | OpenLayers.Util.createUniqueID("OpenLayers_Context_") |
---|
| 409 | }); |
---|
| 410 | |
---|
| 411 | // add schemaLocation attribute |
---|
| 412 | this.setAttributeNS( |
---|
| 413 | root, this.namespaces.xsi, |
---|
| 414 | "xsi:schemaLocation", this.schemaLocation |
---|
| 415 | ); |
---|
| 416 | |
---|
| 417 | // required General element |
---|
| 418 | root.appendChild(this.write_wmc_General(context)); |
---|
| 419 | |
---|
| 420 | // required LayerList element |
---|
| 421 | root.appendChild(this.write_wmc_LayerList(context)); |
---|
| 422 | |
---|
| 423 | return OpenLayers.Format.XML.prototype.write.apply(this, [root]); |
---|
| 424 | }, |
---|
| 425 | |
---|
| 426 | /** |
---|
| 427 | * Method: createElementDefaultNS |
---|
| 428 | * Shorthand for createElementNS with namespace from <defaultPrefix>. |
---|
| 429 | * Can optionally be used to set attributes and a text child value. |
---|
| 430 | * |
---|
| 431 | * Parameters: |
---|
| 432 | * name - {String} The qualified node name. |
---|
| 433 | * childValue - {String} Optional value for text child node. |
---|
| 434 | * attributes - {Object} Optional object representing attributes. |
---|
| 435 | * |
---|
| 436 | * Returns: |
---|
| 437 | * {Element} An element node. |
---|
| 438 | */ |
---|
| 439 | createElementDefaultNS: function(name, childValue, attributes) { |
---|
| 440 | var node = this.createElementNS( |
---|
| 441 | this.namespaces[this.defaultPrefix], |
---|
| 442 | name |
---|
| 443 | ); |
---|
| 444 | if(childValue) { |
---|
| 445 | node.appendChild(this.createTextNode(childValue)); |
---|
| 446 | } |
---|
| 447 | if(attributes) { |
---|
| 448 | this.setAttributes(node, attributes); |
---|
| 449 | } |
---|
| 450 | return node; |
---|
| 451 | }, |
---|
| 452 | |
---|
| 453 | /** |
---|
| 454 | * Method: setAttributes |
---|
| 455 | * Set multiple attributes given key value pairs from an object. |
---|
| 456 | * |
---|
| 457 | * Parameters: |
---|
| 458 | * node - {Element} An element node. |
---|
| 459 | * obj - {Object} An object whose properties represent attribute names and |
---|
| 460 | * values represent attribute values. |
---|
| 461 | */ |
---|
| 462 | setAttributes: function(node, obj) { |
---|
| 463 | var value; |
---|
| 464 | for(var name in obj) { |
---|
| 465 | value = obj[name].toString(); |
---|
| 466 | if(value.match(/[A-Z]/)) { |
---|
| 467 | // safari lowercases attributes with setAttribute |
---|
| 468 | this.setAttributeNS(node, null, name, value); |
---|
| 469 | } else { |
---|
| 470 | node.setAttribute(name, value); |
---|
| 471 | } |
---|
| 472 | } |
---|
| 473 | }, |
---|
| 474 | |
---|
| 475 | /** |
---|
| 476 | * Method: write_wmc_General |
---|
| 477 | * Create a General node given an context object. |
---|
| 478 | * |
---|
| 479 | * Parameters: |
---|
| 480 | * context - {Object} Context object. |
---|
| 481 | * |
---|
| 482 | * Returns: |
---|
| 483 | * {Element} A WMC General element node. |
---|
| 484 | */ |
---|
| 485 | write_wmc_General: function(context) { |
---|
| 486 | var node = this.createElementDefaultNS("General"); |
---|
| 487 | |
---|
| 488 | // optional Window element |
---|
| 489 | if(context.size) { |
---|
| 490 | node.appendChild(this.createElementDefaultNS( |
---|
| 491 | "Window", null, |
---|
| 492 | { |
---|
| 493 | width: context.size.w, |
---|
| 494 | height: context.size.h |
---|
| 495 | } |
---|
| 496 | )); |
---|
| 497 | } |
---|
| 498 | |
---|
| 499 | // required BoundingBox element |
---|
| 500 | var bounds = context.bounds; |
---|
| 501 | node.appendChild(this.createElementDefaultNS( |
---|
| 502 | "BoundingBox", null, |
---|
| 503 | { |
---|
| 504 | minx: bounds.left.toPrecision(18), |
---|
| 505 | miny: bounds.bottom.toPrecision(18), |
---|
| 506 | maxx: bounds.right.toPrecision(18), |
---|
| 507 | maxy: bounds.top.toPrecision(18), |
---|
| 508 | SRS: context.projection |
---|
| 509 | } |
---|
| 510 | )); |
---|
| 511 | |
---|
| 512 | // required Title element |
---|
| 513 | node.appendChild(this.createElementDefaultNS( |
---|
| 514 | "Title", context.title |
---|
| 515 | )); |
---|
| 516 | |
---|
| 517 | // OpenLayers specific map properties |
---|
| 518 | node.appendChild(this.write_ol_MapExtension(context)); |
---|
| 519 | |
---|
| 520 | return node; |
---|
| 521 | }, |
---|
| 522 | |
---|
| 523 | /** |
---|
| 524 | * Method: write_ol_MapExtension |
---|
| 525 | */ |
---|
| 526 | write_ol_MapExtension: function(context) { |
---|
| 527 | var node = this.createElementDefaultNS("Extension"); |
---|
| 528 | |
---|
| 529 | var bounds = context.maxExtent; |
---|
| 530 | if(bounds) { |
---|
| 531 | var maxExtent = this.createElementNS( |
---|
| 532 | this.namespaces.ol, "ol:maxExtent" |
---|
| 533 | ); |
---|
| 534 | this.setAttributes(maxExtent, { |
---|
| 535 | minx: bounds.left.toPrecision(18), |
---|
| 536 | miny: bounds.bottom.toPrecision(18), |
---|
| 537 | maxx: bounds.right.toPrecision(18), |
---|
| 538 | maxy: bounds.top.toPrecision(18) |
---|
| 539 | }); |
---|
| 540 | node.appendChild(maxExtent); |
---|
| 541 | } |
---|
| 542 | |
---|
| 543 | return node; |
---|
| 544 | }, |
---|
| 545 | |
---|
| 546 | /** |
---|
| 547 | * Method: write_wmc_LayerList |
---|
| 548 | * Create a LayerList node given an context object. |
---|
| 549 | * |
---|
| 550 | * Parameters: |
---|
| 551 | * context - {Object} Context object. |
---|
| 552 | * |
---|
| 553 | * Returns: |
---|
| 554 | * {Element} A WMC LayerList element node. |
---|
| 555 | */ |
---|
| 556 | write_wmc_LayerList: function(context) { |
---|
| 557 | var list = this.createElementDefaultNS("LayerList"); |
---|
| 558 | |
---|
| 559 | for(var i=0, len=context.layersContext.length; i<len; ++i) { |
---|
| 560 | list.appendChild(this.write_wmc_Layer(context.layersContext[i])); |
---|
| 561 | } |
---|
| 562 | |
---|
| 563 | return list; |
---|
| 564 | }, |
---|
| 565 | |
---|
| 566 | /** |
---|
| 567 | * Method: write_wmc_Layer |
---|
| 568 | * Create a Layer node given a layer context object. |
---|
| 569 | * |
---|
| 570 | * Parameters: |
---|
| 571 | * context - {Object} A layer context object.} |
---|
| 572 | * |
---|
| 573 | * Returns: |
---|
| 574 | * {Element} A WMC Layer element node. |
---|
| 575 | */ |
---|
| 576 | write_wmc_Layer: function(context) { |
---|
| 577 | var node = this.createElementDefaultNS( |
---|
| 578 | "Layer", null, { |
---|
| 579 | queryable: context.queryable ? "1" : "0", |
---|
| 580 | hidden: context.visibility ? "0" : "1" |
---|
| 581 | } |
---|
| 582 | ); |
---|
| 583 | |
---|
| 584 | // required Server element |
---|
| 585 | node.appendChild(this.write_wmc_Server(context)); |
---|
| 586 | |
---|
| 587 | // required Name element |
---|
| 588 | node.appendChild(this.createElementDefaultNS( |
---|
| 589 | "Name", context.name |
---|
| 590 | )); |
---|
| 591 | |
---|
| 592 | // required Title element |
---|
| 593 | node.appendChild(this.createElementDefaultNS( |
---|
| 594 | "Title", context.title |
---|
| 595 | )); |
---|
| 596 | |
---|
| 597 | // optional MetadataURL element |
---|
| 598 | if (context.metadataURL) { |
---|
| 599 | node.appendChild(this.write_wmc_MetadataURL(context.metadataURL)); |
---|
| 600 | } |
---|
| 601 | |
---|
| 602 | return node; |
---|
| 603 | }, |
---|
| 604 | |
---|
| 605 | /** |
---|
| 606 | * Method: write_wmc_LayerExtension |
---|
| 607 | * Add OpenLayers specific layer parameters to an Extension element. |
---|
| 608 | * |
---|
| 609 | * Parameters: |
---|
| 610 | * context - {Object} A layer context object. |
---|
| 611 | * |
---|
| 612 | * Returns: |
---|
| 613 | * {Element} A WMC Extension element (for a layer). |
---|
| 614 | */ |
---|
| 615 | write_wmc_LayerExtension: function(context) { |
---|
| 616 | var node = this.createElementDefaultNS("Extension"); |
---|
| 617 | |
---|
| 618 | var bounds = context.maxExtent; |
---|
| 619 | var maxExtent = this.createElementNS( |
---|
| 620 | this.namespaces.ol, "ol:maxExtent" |
---|
| 621 | ); |
---|
| 622 | this.setAttributes(maxExtent, { |
---|
| 623 | minx: bounds.left.toPrecision(18), |
---|
| 624 | miny: bounds.bottom.toPrecision(18), |
---|
| 625 | maxx: bounds.right.toPrecision(18), |
---|
| 626 | maxy: bounds.top.toPrecision(18) |
---|
| 627 | }); |
---|
| 628 | node.appendChild(maxExtent); |
---|
| 629 | |
---|
| 630 | if (context.tileSize && !context.singleTile) { |
---|
| 631 | var size = this.createElementNS( |
---|
| 632 | this.namespaces.ol, "ol:tileSize" |
---|
| 633 | ); |
---|
| 634 | this.setAttributes(size, context.tileSize); |
---|
| 635 | node.appendChild(size); |
---|
| 636 | } |
---|
| 637 | |
---|
| 638 | var properties = [ |
---|
| 639 | "transparent", "numZoomLevels", "units", "isBaseLayer", |
---|
| 640 | "opacity", "displayInLayerSwitcher", "singleTile" |
---|
| 641 | ]; |
---|
| 642 | var child; |
---|
| 643 | for(var i=0, len=properties.length; i<len; ++i) { |
---|
| 644 | child = this.createOLPropertyNode(context, properties[i]); |
---|
| 645 | if(child) { |
---|
| 646 | node.appendChild(child); |
---|
| 647 | } |
---|
| 648 | } |
---|
| 649 | |
---|
| 650 | return node; |
---|
| 651 | }, |
---|
| 652 | |
---|
| 653 | /** |
---|
| 654 | * Method: createOLPropertyNode |
---|
| 655 | * Create a node representing an OpenLayers property. If the property is |
---|
| 656 | * null or undefined, null will be returned. |
---|
| 657 | * |
---|
| 658 | * Parameters: |
---|
| 659 | * object - {Object} An object. |
---|
| 660 | * prop - {String} A property. |
---|
| 661 | * |
---|
| 662 | * Returns: |
---|
| 663 | * {Element} A property node. |
---|
| 664 | */ |
---|
| 665 | createOLPropertyNode: function(obj, prop) { |
---|
| 666 | var node = null; |
---|
| 667 | if(obj[prop] != null) { |
---|
| 668 | node = this.createElementNS(this.namespaces.ol, "ol:" + prop); |
---|
| 669 | node.appendChild(this.createTextNode(obj[prop].toString())); |
---|
| 670 | } |
---|
| 671 | return node; |
---|
| 672 | }, |
---|
| 673 | |
---|
| 674 | /** |
---|
| 675 | * Method: write_wmc_Server |
---|
| 676 | * Create a Server node given a layer context object. |
---|
| 677 | * |
---|
| 678 | * Parameters: |
---|
| 679 | * context - {Object} Layer context object. |
---|
| 680 | * |
---|
| 681 | * Returns: |
---|
| 682 | * {Element} A WMC Server element node. |
---|
| 683 | */ |
---|
| 684 | write_wmc_Server: function(context) { |
---|
| 685 | var node = this.createElementDefaultNS("Server"); |
---|
| 686 | this.setAttributes(node, { |
---|
| 687 | service: "OGC:WMS", |
---|
| 688 | version: context.version |
---|
| 689 | }); |
---|
| 690 | |
---|
| 691 | // required OnlineResource element |
---|
| 692 | node.appendChild(this.write_wmc_OnlineResource(context.url)); |
---|
| 693 | |
---|
| 694 | return node; |
---|
| 695 | }, |
---|
| 696 | |
---|
| 697 | /** |
---|
| 698 | * Method: write_wmc_MetadataURL |
---|
| 699 | * Create a MetadataURL node given a metadataURL string. |
---|
| 700 | * |
---|
| 701 | * Parameters: |
---|
| 702 | * metadataURL - {String} MetadataURL string value. |
---|
| 703 | * |
---|
| 704 | * Returns: |
---|
| 705 | * {Element} A WMC metadataURL element node. |
---|
| 706 | */ |
---|
| 707 | write_wmc_MetadataURL: function(metadataURL) { |
---|
| 708 | var node = this.createElementDefaultNS("MetadataURL"); |
---|
| 709 | |
---|
| 710 | // required OnlineResource element |
---|
| 711 | node.appendChild(this.write_wmc_OnlineResource(metadataURL)); |
---|
| 712 | |
---|
| 713 | return node; |
---|
| 714 | }, |
---|
| 715 | |
---|
| 716 | /** |
---|
| 717 | * Method: write_wmc_FormatList |
---|
| 718 | * Create a FormatList node given a layer context. |
---|
| 719 | * |
---|
| 720 | * Parameters: |
---|
| 721 | * context - {Object} Layer context object. |
---|
| 722 | * |
---|
| 723 | * Returns: |
---|
| 724 | * {Element} A WMC FormatList element node. |
---|
| 725 | */ |
---|
| 726 | write_wmc_FormatList: function(context) { |
---|
| 727 | var node = this.createElementDefaultNS("FormatList"); |
---|
| 728 | for (var i=0, len=context.formats.length; i<len; i++) { |
---|
| 729 | var format = context.formats[i]; |
---|
| 730 | node.appendChild(this.createElementDefaultNS( |
---|
| 731 | "Format", |
---|
| 732 | format.value, |
---|
| 733 | (format.current && format.current == true) ? |
---|
| 734 | {current: "1"} : null |
---|
| 735 | )); |
---|
| 736 | } |
---|
| 737 | |
---|
| 738 | return node; |
---|
| 739 | }, |
---|
| 740 | |
---|
| 741 | /** |
---|
| 742 | * Method: write_wmc_StyleList |
---|
| 743 | * Create a StyleList node given a layer context. |
---|
| 744 | * |
---|
| 745 | * Parameters: |
---|
| 746 | * context - {Object} Layer context object. |
---|
| 747 | * |
---|
| 748 | * Returns: |
---|
| 749 | * {Element} A WMC StyleList element node. |
---|
| 750 | */ |
---|
| 751 | write_wmc_StyleList: function(layer) { |
---|
| 752 | var node = this.createElementDefaultNS("StyleList"); |
---|
| 753 | |
---|
| 754 | var styles = layer.styles; |
---|
| 755 | if (styles && styles instanceof Array) { |
---|
| 756 | var sld; |
---|
| 757 | for (var i=0, len=styles.length; i<len; i++) { |
---|
| 758 | var s = styles[i]; |
---|
| 759 | // three style types to consider |
---|
| 760 | // [1] linked SLD |
---|
| 761 | // [2] inline SLD |
---|
| 762 | // [3] named style |
---|
| 763 | // running child nodes always gets name, optionally gets href or body |
---|
| 764 | var style = this.createElementDefaultNS( |
---|
| 765 | "Style", |
---|
| 766 | null, |
---|
| 767 | (s.current && s.current == true) ? |
---|
| 768 | {current: "1"} : null |
---|
| 769 | ); |
---|
| 770 | if(s.href) { // [1] |
---|
| 771 | sld = this.createElementDefaultNS("SLD"); |
---|
| 772 | var link = this.write_wmc_OnlineResource(s.href); |
---|
| 773 | sld.appendChild(link); |
---|
| 774 | // Name is required. |
---|
| 775 | sld.appendChild(this.createElementDefaultNS("Name", s.name)); |
---|
| 776 | // Title is optional. |
---|
| 777 | if (s.title) { |
---|
| 778 | sld.appendChild(this.createElementDefaultNS("Title", s.title)); |
---|
| 779 | } |
---|
| 780 | style.appendChild(sld); |
---|
| 781 | } else if(s.body) { // [2] |
---|
| 782 | sld = this.createElementDefaultNS("SLD"); |
---|
| 783 | // read in body as xml doc - assume proper namespace declarations |
---|
| 784 | var doc = OpenLayers.Format.XML.prototype.read.apply(this, [s.body]); |
---|
| 785 | // append to StyledLayerDescriptor node |
---|
| 786 | var imported = doc.documentElement; |
---|
| 787 | if(sld.ownerDocument && sld.ownerDocument.importNode) { |
---|
| 788 | imported = sld.ownerDocument.importNode(imported, true); |
---|
| 789 | } |
---|
| 790 | sld.appendChild(imported); |
---|
| 791 | // Name is required. |
---|
| 792 | sld.appendChild(this.createElementDefaultNS("Name", s.name)); |
---|
| 793 | // Title is optional. |
---|
| 794 | if (s.title) { |
---|
| 795 | sld.appendChild(this.createElementDefaultNS("Title", s.title)); |
---|
| 796 | } |
---|
| 797 | style.appendChild(sld); |
---|
| 798 | } else { // [3] |
---|
| 799 | // both Name and Title are required. |
---|
| 800 | style.appendChild(this.createElementDefaultNS("Name", s.name)); |
---|
| 801 | style.appendChild(this.createElementDefaultNS("Title", s.title)); |
---|
| 802 | // Abstract is optional |
---|
| 803 | if (s['abstract']) { // abstract is a js keyword |
---|
| 804 | style.appendChild(this.createElementDefaultNS( |
---|
| 805 | "Abstract", s['abstract'] |
---|
| 806 | )); |
---|
| 807 | } |
---|
| 808 | } |
---|
| 809 | node.appendChild(style); |
---|
| 810 | } |
---|
| 811 | } |
---|
| 812 | |
---|
| 813 | return node; |
---|
| 814 | }, |
---|
| 815 | |
---|
| 816 | /** |
---|
| 817 | * Method: write_wmc_OnlineResource |
---|
| 818 | * Create an OnlineResource node given a URL. |
---|
| 819 | * |
---|
| 820 | * Parameters: |
---|
| 821 | * href - {String} URL for the resource. |
---|
| 822 | * |
---|
| 823 | * Returns: |
---|
| 824 | * {Element} A WMC OnlineResource element node. |
---|
| 825 | */ |
---|
| 826 | write_wmc_OnlineResource: function(href) { |
---|
| 827 | var node = this.createElementDefaultNS("OnlineResource"); |
---|
| 828 | this.setAttributeNS(node, this.namespaces.xlink, "xlink:type", "simple"); |
---|
| 829 | this.setAttributeNS(node, this.namespaces.xlink, "xlink:href", href); |
---|
| 830 | return node; |
---|
| 831 | }, |
---|
| 832 | |
---|
| 833 | CLASS_NAME: "OpenLayers.Format.WMC.v1" |
---|
| 834 | |
---|
| 835 | }); |
---|