v1.js 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. /* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the 2-clause BSD license.
  3. * See license.txt in the OpenLayers distribution or repository for the
  4. * full text of the license. */
  5. /**
  6. * @requires OpenLayers/Format/WMC.js
  7. * @requires OpenLayers/Format/XML.js
  8. */
  9. /**
  10. * Class: OpenLayers.Format.WMC.v1
  11. * Superclass for WMC version 1 parsers.
  12. *
  13. * Inherits from:
  14. * - <OpenLayers.Format.XML>
  15. */
  16. OpenLayers.Format.WMC.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
  17. /**
  18. * Property: namespaces
  19. * {Object} Mapping of namespace aliases to namespace URIs.
  20. */
  21. namespaces: {
  22. ol: "http://openlayers.org/context",
  23. wmc: "http://www.opengis.net/context",
  24. sld: "http://www.opengis.net/sld",
  25. xlink: "http://www.w3.org/1999/xlink",
  26. xsi: "http://www.w3.org/2001/XMLSchema-instance"
  27. },
  28. /**
  29. * Property: schemaLocation
  30. * {String} Schema location for a particular minor version.
  31. */
  32. schemaLocation: "",
  33. /**
  34. * Method: getNamespacePrefix
  35. * Get the namespace prefix for a given uri from the <namespaces> object.
  36. *
  37. * Returns:
  38. * {String} A namespace prefix or null if none found.
  39. */
  40. getNamespacePrefix: function(uri) {
  41. var prefix = null;
  42. if(uri == null) {
  43. prefix = this.namespaces[this.defaultPrefix];
  44. } else {
  45. for(prefix in this.namespaces) {
  46. if(this.namespaces[prefix] == uri) {
  47. break;
  48. }
  49. }
  50. }
  51. return prefix;
  52. },
  53. /**
  54. * Property: defaultPrefix
  55. */
  56. defaultPrefix: "wmc",
  57. /**
  58. * Property: rootPrefix
  59. * {String} Prefix on the root node that maps to the context namespace URI.
  60. */
  61. rootPrefix: null,
  62. /**
  63. * Property: defaultStyleName
  64. * {String} Style name used if layer has no style param. Default is "".
  65. */
  66. defaultStyleName: "",
  67. /**
  68. * Property: defaultStyleTitle
  69. * {String} Default style title. Default is "Default".
  70. */
  71. defaultStyleTitle: "Default",
  72. /**
  73. * Constructor: OpenLayers.Format.WMC.v1
  74. * Instances of this class are not created directly. Use the
  75. * <OpenLayers.Format.WMC> constructor instead.
  76. *
  77. * Parameters:
  78. * options - {Object} An optional object whose properties will be set on
  79. * this instance.
  80. */
  81. initialize: function(options) {
  82. OpenLayers.Format.XML.prototype.initialize.apply(this, [options]);
  83. },
  84. /**
  85. * Method: read
  86. * Read capabilities data from a string, and return a list of layers.
  87. *
  88. * Parameters:
  89. * data - {String} or {DOMElement} data to read/parse.
  90. *
  91. * Returns:
  92. * {Array} List of named layers.
  93. */
  94. read: function(data) {
  95. if(typeof data == "string") {
  96. data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);
  97. }
  98. var root = data.documentElement;
  99. this.rootPrefix = root.prefix;
  100. var context = {
  101. version: root.getAttribute("version")
  102. };
  103. this.runChildNodes(context, root);
  104. return context;
  105. },
  106. /**
  107. * Method: runChildNodes
  108. */
  109. runChildNodes: function(obj, node) {
  110. var children = node.childNodes;
  111. var childNode, processor, prefix, local;
  112. for(var i=0, len=children.length; i<len; ++i) {
  113. childNode = children[i];
  114. if(childNode.nodeType == 1) {
  115. prefix = this.getNamespacePrefix(childNode.namespaceURI);
  116. local = childNode.nodeName.split(":").pop();
  117. processor = this["read_" + prefix + "_" + local];
  118. if(processor) {
  119. processor.apply(this, [obj, childNode]);
  120. }
  121. }
  122. }
  123. },
  124. /**
  125. * Method: read_wmc_General
  126. */
  127. read_wmc_General: function(context, node) {
  128. this.runChildNodes(context, node);
  129. },
  130. /**
  131. * Method: read_wmc_BoundingBox
  132. */
  133. read_wmc_BoundingBox: function(context, node) {
  134. context.projection = node.getAttribute("SRS");
  135. context.bounds = new OpenLayers.Bounds(
  136. node.getAttribute("minx"), node.getAttribute("miny"),
  137. node.getAttribute("maxx"), node.getAttribute("maxy")
  138. );
  139. },
  140. /**
  141. * Method: read_wmc_LayerList
  142. */
  143. read_wmc_LayerList: function(context, node) {
  144. // layersContext is an array containing info for each layer
  145. context.layersContext = [];
  146. this.runChildNodes(context, node);
  147. },
  148. /**
  149. * Method: read_wmc_Layer
  150. */
  151. read_wmc_Layer: function(context, node) {
  152. var layerContext = {
  153. visibility: (node.getAttribute("hidden") != "1"),
  154. queryable: (node.getAttribute("queryable") == "1"),
  155. formats: [],
  156. styles: [],
  157. metadata: {}
  158. };
  159. this.runChildNodes(layerContext, node);
  160. // set properties common to multiple objects on layer options/params
  161. context.layersContext.push(layerContext);
  162. },
  163. /**
  164. * Method: read_wmc_Extension
  165. */
  166. read_wmc_Extension: function(obj, node) {
  167. this.runChildNodes(obj, node);
  168. },
  169. /**
  170. * Method: read_ol_units
  171. */
  172. read_ol_units: function(layerContext, node) {
  173. layerContext.units = this.getChildValue(node);
  174. },
  175. /**
  176. * Method: read_ol_maxExtent
  177. */
  178. read_ol_maxExtent: function(obj, node) {
  179. var bounds = new OpenLayers.Bounds(
  180. node.getAttribute("minx"), node.getAttribute("miny"),
  181. node.getAttribute("maxx"), node.getAttribute("maxy")
  182. );
  183. obj.maxExtent = bounds;
  184. },
  185. /**
  186. * Method: read_ol_transparent
  187. */
  188. read_ol_transparent: function(layerContext, node) {
  189. layerContext.transparent = this.getChildValue(node);
  190. },
  191. /**
  192. * Method: read_ol_numZoomLevels
  193. */
  194. read_ol_numZoomLevels: function(layerContext, node) {
  195. layerContext.numZoomLevels = parseInt(this.getChildValue(node));
  196. },
  197. /**
  198. * Method: read_ol_opacity
  199. */
  200. read_ol_opacity: function(layerContext, node) {
  201. layerContext.opacity = parseFloat(this.getChildValue(node));
  202. },
  203. /**
  204. * Method: read_ol_singleTile
  205. */
  206. read_ol_singleTile: function(layerContext, node) {
  207. layerContext.singleTile = (this.getChildValue(node) == "true");
  208. },
  209. /**
  210. * Method: read_ol_tileSize
  211. */
  212. read_ol_tileSize: function(layerContext, node) {
  213. var obj = {"width": node.getAttribute("width"), "height": node.getAttribute("height")};
  214. layerContext.tileSize = obj;
  215. },
  216. /**
  217. * Method: read_ol_isBaseLayer
  218. */
  219. read_ol_isBaseLayer: function(layerContext, node) {
  220. layerContext.isBaseLayer = (this.getChildValue(node) == "true");
  221. },
  222. /**
  223. * Method: read_ol_displayInLayerSwitcher
  224. */
  225. read_ol_displayInLayerSwitcher: function(layerContext, node) {
  226. layerContext.displayInLayerSwitcher = (this.getChildValue(node) == "true");
  227. },
  228. /**
  229. * Method: read_wmc_Server
  230. */
  231. read_wmc_Server: function(layerContext, node) {
  232. layerContext.version = node.getAttribute("version");
  233. layerContext.url = this.getOnlineResource_href(node);
  234. layerContext.metadata.servertitle = node.getAttribute("title");
  235. },
  236. /**
  237. * Method: read_wmc_FormatList
  238. */
  239. read_wmc_FormatList: function(layerContext, node) {
  240. this.runChildNodes(layerContext, node);
  241. },
  242. /**
  243. * Method: read_wmc_Format
  244. */
  245. read_wmc_Format: function(layerContext, node) {
  246. var format = {
  247. value: this.getChildValue(node)
  248. };
  249. if(node.getAttribute("current") == "1") {
  250. format.current = true;
  251. }
  252. layerContext.formats.push(format);
  253. },
  254. /**
  255. * Method: read_wmc_StyleList
  256. */
  257. read_wmc_StyleList: function(layerContext, node) {
  258. this.runChildNodes(layerContext, node);
  259. },
  260. /**
  261. * Method: read_wmc_Style
  262. */
  263. read_wmc_Style: function(layerContext, node) {
  264. var style = {};
  265. this.runChildNodes(style, node);
  266. if(node.getAttribute("current") == "1") {
  267. style.current = true;
  268. }
  269. layerContext.styles.push(style);
  270. },
  271. /**
  272. * Method: read_wmc_SLD
  273. */
  274. read_wmc_SLD: function(style, node) {
  275. this.runChildNodes(style, node);
  276. // style either comes back with an href or a body property
  277. },
  278. /**
  279. * Method: read_sld_StyledLayerDescriptor
  280. */
  281. read_sld_StyledLayerDescriptor: function(sld, node) {
  282. var xml = OpenLayers.Format.XML.prototype.write.apply(this, [node]);
  283. sld.body = xml;
  284. },
  285. /**
  286. * Method: read_sld_FeatureTypeStyle
  287. */
  288. read_sld_FeatureTypeStyle: function(sld, node) {
  289. var xml = OpenLayers.Format.XML.prototype.write.apply(this, [node]);
  290. sld.body = xml;
  291. },
  292. /**
  293. * Method: read_wmc_OnlineResource
  294. */
  295. read_wmc_OnlineResource: function(obj, node) {
  296. obj.href = this.getAttributeNS(
  297. node, this.namespaces.xlink, "href"
  298. );
  299. },
  300. /**
  301. * Method: read_wmc_Name
  302. */
  303. read_wmc_Name: function(obj, node) {
  304. var name = this.getChildValue(node);
  305. if(name) {
  306. obj.name = name;
  307. }
  308. },
  309. /**
  310. * Method: read_wmc_Title
  311. */
  312. read_wmc_Title: function(obj, node) {
  313. var title = this.getChildValue(node);
  314. if(title) {
  315. obj.title = title;
  316. }
  317. },
  318. /**
  319. * Method: read_wmc_MetadataURL
  320. */
  321. read_wmc_MetadataURL: function(layerContext, node) {
  322. layerContext.metadataURL = this.getOnlineResource_href(node);
  323. },
  324. /**
  325. * Method: read_wmc_KeywordList
  326. */
  327. read_wmc_KeywordList: function(context, node) {
  328. context.keywords = [];
  329. this.runChildNodes(context.keywords, node);
  330. },
  331. /**
  332. * Method: read_wmc_Keyword
  333. */
  334. read_wmc_Keyword: function(keywords, node) {
  335. keywords.push(this.getChildValue(node));
  336. },
  337. /**
  338. * Method: read_wmc_Abstract
  339. */
  340. read_wmc_Abstract: function(obj, node) {
  341. var abst = this.getChildValue(node);
  342. if(abst) {
  343. obj["abstract"] = abst;
  344. }
  345. },
  346. /**
  347. * Method: read_wmc_LogoURL
  348. */
  349. read_wmc_LogoURL: function(context, node) {
  350. context.logo = {
  351. width: node.getAttribute("width"),
  352. height: node.getAttribute("height"),
  353. format: node.getAttribute("format"),
  354. href: this.getOnlineResource_href(node)
  355. };
  356. },
  357. /**
  358. * Method: read_wmc_DescriptionURL
  359. */
  360. read_wmc_DescriptionURL: function(context, node) {
  361. context.descriptionURL = this.getOnlineResource_href(node);
  362. },
  363. /**
  364. * Method: read_wmc_ContactInformation
  365. */
  366. read_wmc_ContactInformation: function(obj, node) {
  367. var contact = {};
  368. this.runChildNodes(contact, node);
  369. obj.contactInformation = contact;
  370. },
  371. /**
  372. * Method: read_wmc_ContactPersonPrimary
  373. */
  374. read_wmc_ContactPersonPrimary: function(contact, node) {
  375. var personPrimary = {};
  376. this.runChildNodes(personPrimary, node);
  377. contact.personPrimary = personPrimary;
  378. },
  379. /**
  380. * Method: read_wmc_ContactPerson
  381. */
  382. read_wmc_ContactPerson: function(primaryPerson, node) {
  383. var person = this.getChildValue(node);
  384. if (person) {
  385. primaryPerson.person = person;
  386. }
  387. },
  388. /**
  389. * Method: read_wmc_ContactOrganization
  390. */
  391. read_wmc_ContactOrganization: function(primaryPerson, node) {
  392. var organization = this.getChildValue(node);
  393. if (organization) {
  394. primaryPerson.organization = organization;
  395. }
  396. },
  397. /**
  398. * Method: read_wmc_ContactPosition
  399. */
  400. read_wmc_ContactPosition: function(contact, node) {
  401. var position = this.getChildValue(node);
  402. if (position) {
  403. contact.position = position;
  404. }
  405. },
  406. /**
  407. * Method: read_wmc_ContactAddress
  408. */
  409. read_wmc_ContactAddress: function(contact, node) {
  410. var contactAddress = {};
  411. this.runChildNodes(contactAddress, node);
  412. contact.contactAddress = contactAddress;
  413. },
  414. /**
  415. * Method: read_wmc_AddressType
  416. */
  417. read_wmc_AddressType: function(contactAddress, node) {
  418. var type = this.getChildValue(node);
  419. if (type) {
  420. contactAddress.type = type;
  421. }
  422. },
  423. /**
  424. * Method: read_wmc_Address
  425. */
  426. read_wmc_Address: function(contactAddress, node) {
  427. var address = this.getChildValue(node);
  428. if (address) {
  429. contactAddress.address = address;
  430. }
  431. },
  432. /**
  433. * Method: read_wmc_City
  434. */
  435. read_wmc_City: function(contactAddress, node) {
  436. var city = this.getChildValue(node);
  437. if (city) {
  438. contactAddress.city = city;
  439. }
  440. },
  441. /**
  442. * Method: read_wmc_StateOrProvince
  443. */
  444. read_wmc_StateOrProvince: function(contactAddress, node) {
  445. var stateOrProvince = this.getChildValue(node);
  446. if (stateOrProvince) {
  447. contactAddress.stateOrProvince = stateOrProvince;
  448. }
  449. },
  450. /**
  451. * Method: read_wmc_PostCode
  452. */
  453. read_wmc_PostCode: function(contactAddress, node) {
  454. var postcode = this.getChildValue(node);
  455. if (postcode) {
  456. contactAddress.postcode = postcode;
  457. }
  458. },
  459. /**
  460. * Method: read_wmc_Country
  461. */
  462. read_wmc_Country: function(contactAddress, node) {
  463. var country = this.getChildValue(node);
  464. if (country) {
  465. contactAddress.country = country;
  466. }
  467. },
  468. /**
  469. * Method: read_wmc_ContactVoiceTelephone
  470. */
  471. read_wmc_ContactVoiceTelephone: function(contact, node) {
  472. var phone = this.getChildValue(node);
  473. if (phone) {
  474. contact.phone = phone;
  475. }
  476. },
  477. /**
  478. * Method: read_wmc_ContactFacsimileTelephone
  479. */
  480. read_wmc_ContactFacsimileTelephone: function(contact, node) {
  481. var fax = this.getChildValue(node);
  482. if (fax) {
  483. contact.fax = fax;
  484. }
  485. },
  486. /**
  487. * Method: read_wmc_ContactElectronicMailAddress
  488. */
  489. read_wmc_ContactElectronicMailAddress: function(contact, node) {
  490. var email = this.getChildValue(node);
  491. if (email) {
  492. contact.email = email;
  493. }
  494. },
  495. /**
  496. * Method: read_wmc_DataURL
  497. */
  498. read_wmc_DataURL: function(layerContext, node) {
  499. layerContext.dataURL = this.getOnlineResource_href(node);
  500. },
  501. /**
  502. * Method: read_wmc_LegendURL
  503. */
  504. read_wmc_LegendURL: function(style, node) {
  505. var legend = {
  506. width: node.getAttribute('width'),
  507. height: node.getAttribute('height'),
  508. format: node.getAttribute('format'),
  509. href: this.getOnlineResource_href(node)
  510. };
  511. style.legend = legend;
  512. },
  513. /**
  514. * Method: read_wmc_DimensionList
  515. */
  516. read_wmc_DimensionList: function(layerContext, node) {
  517. layerContext.dimensions = {};
  518. this.runChildNodes(layerContext.dimensions, node);
  519. },
  520. /**
  521. * Method: read_wmc_Dimension
  522. */
  523. read_wmc_Dimension: function(dimensions, node) {
  524. var name = node.getAttribute("name").toLowerCase();
  525. var dim = {
  526. name: name,
  527. units: node.getAttribute("units") || "",
  528. unitSymbol: node.getAttribute("unitSymbol") || "",
  529. userValue: node.getAttribute("userValue") || "",
  530. nearestValue: node.getAttribute("nearestValue") === "1",
  531. multipleValues: node.getAttribute("multipleValues") === "1",
  532. current: node.getAttribute("current") === "1",
  533. "default": node.getAttribute("default") || ""
  534. };
  535. var values = this.getChildValue(node);
  536. dim.values = values.split(",");
  537. dimensions[dim.name] = dim;
  538. },
  539. /**
  540. * Method: write
  541. *
  542. * Parameters:
  543. * context - {Object} An object representing the map context.
  544. * options - {Object} Optional object.
  545. *
  546. * Returns:
  547. * {String} A WMC document string.
  548. */
  549. write: function(context, options) {
  550. var root = this.createElementDefaultNS("ViewContext");
  551. this.setAttributes(root, {
  552. version: this.VERSION,
  553. id: (options && typeof options.id == "string") ?
  554. options.id :
  555. OpenLayers.Util.createUniqueID("OpenLayers_Context_")
  556. });
  557. // add schemaLocation attribute
  558. this.setAttributeNS(
  559. root, this.namespaces.xsi,
  560. "xsi:schemaLocation", this.schemaLocation
  561. );
  562. // required General element
  563. root.appendChild(this.write_wmc_General(context));
  564. // required LayerList element
  565. root.appendChild(this.write_wmc_LayerList(context));
  566. return OpenLayers.Format.XML.prototype.write.apply(this, [root]);
  567. },
  568. /**
  569. * Method: createElementDefaultNS
  570. * Shorthand for createElementNS with namespace from <defaultPrefix>.
  571. * Can optionally be used to set attributes and a text child value.
  572. *
  573. * Parameters:
  574. * name - {String} The qualified node name.
  575. * childValue - {String} Optional value for text child node.
  576. * attributes - {Object} Optional object representing attributes.
  577. *
  578. * Returns:
  579. * {Element} An element node.
  580. */
  581. createElementDefaultNS: function(name, childValue, attributes) {
  582. var node = this.createElementNS(
  583. this.namespaces[this.defaultPrefix],
  584. name
  585. );
  586. if(childValue) {
  587. node.appendChild(this.createTextNode(childValue));
  588. }
  589. if(attributes) {
  590. this.setAttributes(node, attributes);
  591. }
  592. return node;
  593. },
  594. /**
  595. * Method: setAttributes
  596. * Set multiple attributes given key value pairs from an object.
  597. *
  598. * Parameters:
  599. * node - {Element} An element node.
  600. * obj - {Object} An object whose properties represent attribute names and
  601. * values represent attribute values.
  602. */
  603. setAttributes: function(node, obj) {
  604. var value;
  605. for(var name in obj) {
  606. value = obj[name].toString();
  607. if(value.match(/[A-Z]/)) {
  608. // safari lowercases attributes with setAttribute
  609. this.setAttributeNS(node, null, name, value);
  610. } else {
  611. node.setAttribute(name, value);
  612. }
  613. }
  614. },
  615. /**
  616. * Method: write_wmc_General
  617. * Create a General node given an context object.
  618. *
  619. * Parameters:
  620. * context - {Object} Context object.
  621. *
  622. * Returns:
  623. * {Element} A WMC General element node.
  624. */
  625. write_wmc_General: function(context) {
  626. var node = this.createElementDefaultNS("General");
  627. // optional Window element
  628. if(context.size) {
  629. node.appendChild(this.createElementDefaultNS(
  630. "Window", null,
  631. {
  632. width: context.size.w,
  633. height: context.size.h
  634. }
  635. ));
  636. }
  637. // required BoundingBox element
  638. var bounds = context.bounds;
  639. node.appendChild(this.createElementDefaultNS(
  640. "BoundingBox", null,
  641. {
  642. minx: bounds.left.toPrecision(18),
  643. miny: bounds.bottom.toPrecision(18),
  644. maxx: bounds.right.toPrecision(18),
  645. maxy: bounds.top.toPrecision(18),
  646. SRS: context.projection
  647. }
  648. ));
  649. // required Title element
  650. node.appendChild(this.createElementDefaultNS(
  651. "Title", context.title
  652. ));
  653. // optional KeywordList element
  654. if (context.keywords) {
  655. node.appendChild(this.write_wmc_KeywordList(context.keywords));
  656. }
  657. // optional Abstract element
  658. if (context["abstract"]) {
  659. node.appendChild(this.createElementDefaultNS(
  660. "Abstract", context["abstract"]
  661. ));
  662. }
  663. // Optional LogoURL element
  664. if (context.logo) {
  665. node.appendChild(this.write_wmc_URLType("LogoURL", context.logo.href, context.logo));
  666. }
  667. // Optional DescriptionURL element
  668. if (context.descriptionURL) {
  669. node.appendChild(this.write_wmc_URLType("DescriptionURL", context.descriptionURL));
  670. }
  671. // Optional ContactInformation element
  672. if (context.contactInformation) {
  673. node.appendChild(this.write_wmc_ContactInformation(context.contactInformation));
  674. }
  675. // OpenLayers specific map properties
  676. node.appendChild(this.write_ol_MapExtension(context));
  677. return node;
  678. },
  679. /**
  680. * Method: write_wmc_KeywordList
  681. */
  682. write_wmc_KeywordList: function(keywords) {
  683. var node = this.createElementDefaultNS("KeywordList");
  684. for (var i=0, len=keywords.length; i<len; i++) {
  685. node.appendChild(this.createElementDefaultNS(
  686. "Keyword", keywords[i]
  687. ));
  688. }
  689. return node;
  690. },
  691. /**
  692. * Method: write_wmc_ContactInformation
  693. */
  694. write_wmc_ContactInformation: function(contact) {
  695. var node = this.createElementDefaultNS("ContactInformation");
  696. if (contact.personPrimary) {
  697. node.appendChild(this.write_wmc_ContactPersonPrimary(contact.personPrimary));
  698. }
  699. if (contact.position) {
  700. node.appendChild(this.createElementDefaultNS(
  701. "ContactPosition", contact.position
  702. ));
  703. }
  704. if (contact.contactAddress) {
  705. node.appendChild(this.write_wmc_ContactAddress(contact.contactAddress));
  706. }
  707. if (contact.phone) {
  708. node.appendChild(this.createElementDefaultNS(
  709. "ContactVoiceTelephone", contact.phone
  710. ));
  711. }
  712. if (contact.fax) {
  713. node.appendChild(this.createElementDefaultNS(
  714. "ContactFacsimileTelephone", contact.fax
  715. ));
  716. }
  717. if (contact.email) {
  718. node.appendChild(this.createElementDefaultNS(
  719. "ContactElectronicMailAddress", contact.email
  720. ));
  721. }
  722. return node;
  723. },
  724. /**
  725. * Method: write_wmc_ContactPersonPrimary
  726. */
  727. write_wmc_ContactPersonPrimary: function(personPrimary) {
  728. var node = this.createElementDefaultNS("ContactPersonPrimary");
  729. if (personPrimary.person) {
  730. node.appendChild(this.createElementDefaultNS(
  731. "ContactPerson", personPrimary.person
  732. ));
  733. }
  734. if (personPrimary.organization) {
  735. node.appendChild(this.createElementDefaultNS(
  736. "ContactOrganization", personPrimary.organization
  737. ));
  738. }
  739. return node;
  740. },
  741. /**
  742. * Method: write_wmc_ContactAddress
  743. */
  744. write_wmc_ContactAddress: function(contactAddress) {
  745. var node = this.createElementDefaultNS("ContactAddress");
  746. if (contactAddress.type) {
  747. node.appendChild(this.createElementDefaultNS(
  748. "AddressType", contactAddress.type
  749. ));
  750. }
  751. if (contactAddress.address) {
  752. node.appendChild(this.createElementDefaultNS(
  753. "Address", contactAddress.address
  754. ));
  755. }
  756. if (contactAddress.city) {
  757. node.appendChild(this.createElementDefaultNS(
  758. "City", contactAddress.city
  759. ));
  760. }
  761. if (contactAddress.stateOrProvince) {
  762. node.appendChild(this.createElementDefaultNS(
  763. "StateOrProvince", contactAddress.stateOrProvince
  764. ));
  765. }
  766. if (contactAddress.postcode) {
  767. node.appendChild(this.createElementDefaultNS(
  768. "PostCode", contactAddress.postcode
  769. ));
  770. }
  771. if (contactAddress.country) {
  772. node.appendChild(this.createElementDefaultNS(
  773. "Country", contactAddress.country
  774. ));
  775. }
  776. return node;
  777. },
  778. /**
  779. * Method: write_ol_MapExtension
  780. */
  781. write_ol_MapExtension: function(context) {
  782. var node = this.createElementDefaultNS("Extension");
  783. var bounds = context.maxExtent;
  784. if(bounds) {
  785. var maxExtent = this.createElementNS(
  786. this.namespaces.ol, "ol:maxExtent"
  787. );
  788. this.setAttributes(maxExtent, {
  789. minx: bounds.left.toPrecision(18),
  790. miny: bounds.bottom.toPrecision(18),
  791. maxx: bounds.right.toPrecision(18),
  792. maxy: bounds.top.toPrecision(18)
  793. });
  794. node.appendChild(maxExtent);
  795. }
  796. return node;
  797. },
  798. /**
  799. * Method: write_wmc_LayerList
  800. * Create a LayerList node given an context object.
  801. *
  802. * Parameters:
  803. * context - {Object} Context object.
  804. *
  805. * Returns:
  806. * {Element} A WMC LayerList element node.
  807. */
  808. write_wmc_LayerList: function(context) {
  809. var list = this.createElementDefaultNS("LayerList");
  810. for(var i=0, len=context.layersContext.length; i<len; ++i) {
  811. list.appendChild(this.write_wmc_Layer(context.layersContext[i]));
  812. }
  813. return list;
  814. },
  815. /**
  816. * Method: write_wmc_Layer
  817. * Create a Layer node given a layer context object.
  818. *
  819. * Parameters:
  820. * context - {Object} A layer context object.}
  821. *
  822. * Returns:
  823. * {Element} A WMC Layer element node.
  824. */
  825. write_wmc_Layer: function(context) {
  826. var node = this.createElementDefaultNS(
  827. "Layer", null, {
  828. queryable: context.queryable ? "1" : "0",
  829. hidden: context.visibility ? "0" : "1"
  830. }
  831. );
  832. // required Server element
  833. node.appendChild(this.write_wmc_Server(context));
  834. // required Name element
  835. node.appendChild(this.createElementDefaultNS(
  836. "Name", context.name
  837. ));
  838. // required Title element
  839. node.appendChild(this.createElementDefaultNS(
  840. "Title", context.title
  841. ));
  842. // optional Abstract element
  843. if (context["abstract"]) {
  844. node.appendChild(this.createElementDefaultNS(
  845. "Abstract", context["abstract"]
  846. ));
  847. }
  848. // optional DataURL element
  849. if (context.dataURL) {
  850. node.appendChild(this.write_wmc_URLType("DataURL", context.dataURL));
  851. }
  852. // optional MetadataURL element
  853. if (context.metadataURL) {
  854. node.appendChild(this.write_wmc_URLType("MetadataURL", context.metadataURL));
  855. }
  856. return node;
  857. },
  858. /**
  859. * Method: write_wmc_LayerExtension
  860. * Add OpenLayers specific layer parameters to an Extension element.
  861. *
  862. * Parameters:
  863. * context - {Object} A layer context object.
  864. *
  865. * Returns:
  866. * {Element} A WMC Extension element (for a layer).
  867. */
  868. write_wmc_LayerExtension: function(context) {
  869. var node = this.createElementDefaultNS("Extension");
  870. var bounds = context.maxExtent;
  871. var maxExtent = this.createElementNS(
  872. this.namespaces.ol, "ol:maxExtent"
  873. );
  874. this.setAttributes(maxExtent, {
  875. minx: bounds.left.toPrecision(18),
  876. miny: bounds.bottom.toPrecision(18),
  877. maxx: bounds.right.toPrecision(18),
  878. maxy: bounds.top.toPrecision(18)
  879. });
  880. node.appendChild(maxExtent);
  881. if (context.tileSize && !context.singleTile) {
  882. var size = this.createElementNS(
  883. this.namespaces.ol, "ol:tileSize"
  884. );
  885. this.setAttributes(size, context.tileSize);
  886. node.appendChild(size);
  887. }
  888. var properties = [
  889. "transparent", "numZoomLevels", "units", "isBaseLayer",
  890. "opacity", "displayInLayerSwitcher", "singleTile"
  891. ];
  892. var child;
  893. for(var i=0, len=properties.length; i<len; ++i) {
  894. child = this.createOLPropertyNode(context, properties[i]);
  895. if(child) {
  896. node.appendChild(child);
  897. }
  898. }
  899. return node;
  900. },
  901. /**
  902. * Method: createOLPropertyNode
  903. * Create a node representing an OpenLayers property. If the property is
  904. * null or undefined, null will be returned.
  905. *
  906. * Parameters:
  907. * obj - {Object} An object.
  908. * prop - {String} A property.
  909. *
  910. * Returns:
  911. * {Element} A property node.
  912. */
  913. createOLPropertyNode: function(obj, prop) {
  914. var node = null;
  915. if(obj[prop] != null) {
  916. node = this.createElementNS(this.namespaces.ol, "ol:" + prop);
  917. node.appendChild(this.createTextNode(obj[prop].toString()));
  918. }
  919. return node;
  920. },
  921. /**
  922. * Method: write_wmc_Server
  923. * Create a Server node given a layer context object.
  924. *
  925. * Parameters:
  926. * context - {Object} Layer context object.
  927. *
  928. * Returns:
  929. * {Element} A WMC Server element node.
  930. */
  931. write_wmc_Server: function(context) {
  932. var server = context.server;
  933. var node = this.createElementDefaultNS("Server");
  934. var attributes = {
  935. service: "OGC:WMS",
  936. version: server.version
  937. };
  938. if (server.title) {
  939. attributes.title = server.title;
  940. }
  941. this.setAttributes(node, attributes);
  942. // required OnlineResource element
  943. node.appendChild(this.write_wmc_OnlineResource(server.url));
  944. return node;
  945. },
  946. /**
  947. * Method: write_wmc_URLType
  948. * Create a LogoURL/DescriptionURL/MetadataURL/DataURL/LegendURL node given a object and elementName.
  949. *
  950. * Parameters:
  951. * elName - {String} Name of element (LogoURL/DescriptionURL/MetadataURL/LegendURL)
  952. * url - {String} URL string value
  953. * attr - {Object} Optional attributes (width, height, format)
  954. *
  955. * Returns:
  956. * {Element} A WMC element node.
  957. */
  958. write_wmc_URLType: function(elName, url, attr) {
  959. var node = this.createElementDefaultNS(elName);
  960. node.appendChild(this.write_wmc_OnlineResource(url));
  961. if (attr) {
  962. var optionalAttributes = ["width", "height", "format"];
  963. for (var i=0; i<optionalAttributes.length; i++) {
  964. if (optionalAttributes[i] in attr) {
  965. node.setAttribute(optionalAttributes[i], attr[optionalAttributes[i]]);
  966. }
  967. }
  968. }
  969. return node;
  970. },
  971. /**
  972. * Method: write_wmc_DimensionList
  973. */
  974. write_wmc_DimensionList: function(context) {
  975. var node = this.createElementDefaultNS("DimensionList");
  976. var required_attributes = {
  977. name: true,
  978. units: true,
  979. unitSymbol: true,
  980. userValue: true
  981. };
  982. for (var dim in context.dimensions) {
  983. var attributes = {};
  984. var dimension = context.dimensions[dim];
  985. for (var name in dimension) {
  986. if (typeof dimension[name] == "boolean") {
  987. attributes[name] = Number(dimension[name]);
  988. } else {
  989. attributes[name] = dimension[name];
  990. }
  991. }
  992. var values = "";
  993. if (attributes.values) {
  994. values = attributes.values.join(",");
  995. delete attributes.values;
  996. }
  997. node.appendChild(this.createElementDefaultNS(
  998. "Dimension", values, attributes
  999. ));
  1000. }
  1001. return node;
  1002. },
  1003. /**
  1004. * Method: write_wmc_FormatList
  1005. * Create a FormatList node given a layer context.
  1006. *
  1007. * Parameters:
  1008. * context - {Object} Layer context object.
  1009. *
  1010. * Returns:
  1011. * {Element} A WMC FormatList element node.
  1012. */
  1013. write_wmc_FormatList: function(context) {
  1014. var node = this.createElementDefaultNS("FormatList");
  1015. for (var i=0, len=context.formats.length; i<len; i++) {
  1016. var format = context.formats[i];
  1017. node.appendChild(this.createElementDefaultNS(
  1018. "Format",
  1019. format.value,
  1020. (format.current && format.current == true) ?
  1021. {current: "1"} : null
  1022. ));
  1023. }
  1024. return node;
  1025. },
  1026. /**
  1027. * Method: write_wmc_StyleList
  1028. * Create a StyleList node given a layer context.
  1029. *
  1030. * Parameters:
  1031. * layer - {Object} Layer context object.
  1032. *
  1033. * Returns:
  1034. * {Element} A WMC StyleList element node.
  1035. */
  1036. write_wmc_StyleList: function(layer) {
  1037. var node = this.createElementDefaultNS("StyleList");
  1038. var styles = layer.styles;
  1039. if (styles && OpenLayers.Util.isArray(styles)) {
  1040. var sld;
  1041. for (var i=0, len=styles.length; i<len; i++) {
  1042. var s = styles[i];
  1043. // three style types to consider
  1044. // [1] linked SLD
  1045. // [2] inline SLD
  1046. // [3] named style
  1047. // running child nodes always gets name, optionally gets href or body
  1048. var style = this.createElementDefaultNS(
  1049. "Style",
  1050. null,
  1051. (s.current && s.current == true) ?
  1052. {current: "1"} : null
  1053. );
  1054. if(s.href) { // [1]
  1055. sld = this.createElementDefaultNS("SLD");
  1056. // Name is optional.
  1057. if (s.name) {
  1058. sld.appendChild(this.createElementDefaultNS("Name", s.name));
  1059. }
  1060. // Title is optional.
  1061. if (s.title) {
  1062. sld.appendChild(this.createElementDefaultNS("Title", s.title));
  1063. }
  1064. // LegendURL is optional
  1065. if (s.legend) {
  1066. sld.appendChild(this.write_wmc_URLType("LegendURL", s.legend.href, s.legend));
  1067. }
  1068. var link = this.write_wmc_OnlineResource(s.href);
  1069. sld.appendChild(link);
  1070. style.appendChild(sld);
  1071. } else if(s.body) { // [2]
  1072. sld = this.createElementDefaultNS("SLD");
  1073. // Name is optional.
  1074. if (s.name) {
  1075. sld.appendChild(this.createElementDefaultNS("Name", s.name));
  1076. }
  1077. // Title is optional.
  1078. if (s.title) {
  1079. sld.appendChild(this.createElementDefaultNS("Title", s.title));
  1080. }
  1081. // LegendURL is optional
  1082. if (s.legend) {
  1083. sld.appendChild(this.write_wmc_URLType("LegendURL", s.legend.href, s.legend));
  1084. }
  1085. // read in body as xml doc - assume proper namespace declarations
  1086. var doc = OpenLayers.Format.XML.prototype.read.apply(this, [s.body]);
  1087. // append to StyledLayerDescriptor node
  1088. var imported = doc.documentElement;
  1089. if(sld.ownerDocument && sld.ownerDocument.importNode) {
  1090. imported = sld.ownerDocument.importNode(imported, true);
  1091. }
  1092. sld.appendChild(imported);
  1093. style.appendChild(sld);
  1094. } else { // [3]
  1095. // both Name and Title are required.
  1096. style.appendChild(this.createElementDefaultNS("Name", s.name));
  1097. style.appendChild(this.createElementDefaultNS("Title", s.title));
  1098. // Abstract is optional
  1099. if (s['abstract']) { // abstract is a js keyword
  1100. style.appendChild(this.createElementDefaultNS(
  1101. "Abstract", s['abstract']
  1102. ));
  1103. }
  1104. // LegendURL is optional
  1105. if (s.legend) {
  1106. style.appendChild(this.write_wmc_URLType("LegendURL", s.legend.href, s.legend));
  1107. }
  1108. }
  1109. node.appendChild(style);
  1110. }
  1111. }
  1112. return node;
  1113. },
  1114. /**
  1115. * Method: write_wmc_OnlineResource
  1116. * Create an OnlineResource node given a URL.
  1117. *
  1118. * Parameters:
  1119. * href - {String} URL for the resource.
  1120. *
  1121. * Returns:
  1122. * {Element} A WMC OnlineResource element node.
  1123. */
  1124. write_wmc_OnlineResource: function(href) {
  1125. var node = this.createElementDefaultNS("OnlineResource");
  1126. this.setAttributeNS(node, this.namespaces.xlink, "xlink:type", "simple");
  1127. this.setAttributeNS(node, this.namespaces.xlink, "xlink:href", href);
  1128. return node;
  1129. },
  1130. /**
  1131. * Method: getOnlineResource_href
  1132. */
  1133. getOnlineResource_href: function(node) {
  1134. var object = {};
  1135. var links = node.getElementsByTagName("OnlineResource");
  1136. if(links.length > 0) {
  1137. this.read_wmc_OnlineResource(object, links[0]);
  1138. }
  1139. return object.href;
  1140. },
  1141. CLASS_NAME: "OpenLayers.Format.WMC.v1"
  1142. });