v1.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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/WMSCapabilities.js
  7. * @requires OpenLayers/Format/OGCExceptionReport.js
  8. * @requires OpenLayers/Format/XML.js
  9. */
  10. /**
  11. * Class: OpenLayers.Format.WMSCapabilities.v1
  12. * Abstract class not to be instantiated directly. Creates
  13. * the common parts for both WMS 1.1.X and WMS 1.3.X.
  14. *
  15. * Inherits from:
  16. * - <OpenLayers.Format.XML>
  17. */
  18. OpenLayers.Format.WMSCapabilities.v1 = OpenLayers.Class(
  19. OpenLayers.Format.XML, {
  20. /**
  21. * Property: namespaces
  22. * {Object} Mapping of namespace aliases to namespace URIs.
  23. */
  24. namespaces: {
  25. wms: "http://www.opengis.net/wms",
  26. xlink: "http://www.w3.org/1999/xlink",
  27. xsi: "http://www.w3.org/2001/XMLSchema-instance"
  28. },
  29. /**
  30. * Property: defaultPrefix
  31. */
  32. defaultPrefix: "wms",
  33. /**
  34. * Constructor: OpenLayers.Format.WMSCapabilities.v1
  35. * Create an instance of one of the subclasses.
  36. *
  37. * Parameters:
  38. * options - {Object} An optional object whose properties will be set on
  39. * this instance.
  40. */
  41. /**
  42. * APIMethod: read
  43. * Read capabilities data from a string, and return a list of layers.
  44. *
  45. * Parameters:
  46. * data - {String} or {DOMElement} data to read/parse.
  47. *
  48. * Returns:
  49. * {Array} List of named layers.
  50. */
  51. read: function(data) {
  52. if(typeof data == "string") {
  53. data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);
  54. }
  55. var raw = data;
  56. if(data && data.nodeType == 9) {
  57. data = data.documentElement;
  58. }
  59. var capabilities = {};
  60. this.readNode(data, capabilities);
  61. if (capabilities.service === undefined) {
  62. // an exception must have occurred, so parse it
  63. var parser = new OpenLayers.Format.OGCExceptionReport();
  64. capabilities.error = parser.read(raw);
  65. }
  66. return capabilities;
  67. },
  68. /**
  69. * Property: readers
  70. * Contains public functions, grouped by namespace prefix, that will
  71. * be applied when a namespaced node is found matching the function
  72. * name. The function will be applied in the scope of this parser
  73. * with two arguments: the node being read and a context object passed
  74. * from the parent.
  75. */
  76. readers: {
  77. "wms": {
  78. "Service": function(node, obj) {
  79. obj.service = {};
  80. this.readChildNodes(node, obj.service);
  81. },
  82. "Name": function(node, obj) {
  83. obj.name = this.getChildValue(node);
  84. },
  85. "Title": function(node, obj) {
  86. obj.title = this.getChildValue(node);
  87. },
  88. "Abstract": function(node, obj) {
  89. obj["abstract"] = this.getChildValue(node);
  90. },
  91. "BoundingBox": function(node, obj) {
  92. var bbox = {};
  93. bbox.bbox = [
  94. parseFloat(node.getAttribute("minx")),
  95. parseFloat(node.getAttribute("miny")),
  96. parseFloat(node.getAttribute("maxx")),
  97. parseFloat(node.getAttribute("maxy"))
  98. ];
  99. var res = {
  100. x: parseFloat(node.getAttribute("resx")),
  101. y: parseFloat(node.getAttribute("resy"))
  102. };
  103. if (! (isNaN(res.x) && isNaN(res.y))) {
  104. bbox.res = res;
  105. }
  106. // return the bbox so that descendant classes can set the
  107. // CRS and SRS and add it to the obj
  108. return bbox;
  109. },
  110. "OnlineResource": function(node, obj) {
  111. obj.href = this.getAttributeNS(node, this.namespaces.xlink,
  112. "href");
  113. },
  114. "ContactInformation": function(node, obj) {
  115. obj.contactInformation = {};
  116. this.readChildNodes(node, obj.contactInformation);
  117. },
  118. "ContactPersonPrimary": function(node, obj) {
  119. obj.personPrimary = {};
  120. this.readChildNodes(node, obj.personPrimary);
  121. },
  122. "ContactPerson": function(node, obj) {
  123. obj.person = this.getChildValue(node);
  124. },
  125. "ContactOrganization": function(node, obj) {
  126. obj.organization = this.getChildValue(node);
  127. },
  128. "ContactPosition": function(node, obj) {
  129. obj.position = this.getChildValue(node);
  130. },
  131. "ContactAddress": function(node, obj) {
  132. obj.contactAddress = {};
  133. this.readChildNodes(node, obj.contactAddress);
  134. },
  135. "AddressType": function(node, obj) {
  136. obj.type = this.getChildValue(node);
  137. },
  138. "Address": function(node, obj) {
  139. obj.address = this.getChildValue(node);
  140. },
  141. "City": function(node, obj) {
  142. obj.city = this.getChildValue(node);
  143. },
  144. "StateOrProvince": function(node, obj) {
  145. obj.stateOrProvince = this.getChildValue(node);
  146. },
  147. "PostCode": function(node, obj) {
  148. obj.postcode = this.getChildValue(node);
  149. },
  150. "Country": function(node, obj) {
  151. obj.country = this.getChildValue(node);
  152. },
  153. "ContactVoiceTelephone": function(node, obj) {
  154. obj.phone = this.getChildValue(node);
  155. },
  156. "ContactFacsimileTelephone": function(node, obj) {
  157. obj.fax = this.getChildValue(node);
  158. },
  159. "ContactElectronicMailAddress": function(node, obj) {
  160. obj.email = this.getChildValue(node);
  161. },
  162. "Fees": function(node, obj) {
  163. var fees = this.getChildValue(node);
  164. if (fees && fees.toLowerCase() != "none") {
  165. obj.fees = fees;
  166. }
  167. },
  168. "AccessConstraints": function(node, obj) {
  169. var constraints = this.getChildValue(node);
  170. if (constraints && constraints.toLowerCase() != "none") {
  171. obj.accessConstraints = constraints;
  172. }
  173. },
  174. "Capability": function(node, obj) {
  175. obj.capability = {
  176. nestedLayers: [],
  177. layers: []
  178. };
  179. this.readChildNodes(node, obj.capability);
  180. },
  181. "Request": function(node, obj) {
  182. obj.request = {};
  183. this.readChildNodes(node, obj.request);
  184. },
  185. "GetCapabilities": function(node, obj) {
  186. obj.getcapabilities = {formats: []};
  187. this.readChildNodes(node, obj.getcapabilities);
  188. },
  189. "Format": function(node, obj) {
  190. if (OpenLayers.Util.isArray(obj.formats)) {
  191. obj.formats.push(this.getChildValue(node));
  192. } else {
  193. obj.format = this.getChildValue(node);
  194. }
  195. },
  196. "DCPType": function(node, obj) {
  197. this.readChildNodes(node, obj);
  198. },
  199. "HTTP": function(node, obj) {
  200. this.readChildNodes(node, obj);
  201. },
  202. "Get": function(node, obj) {
  203. obj.get = {};
  204. this.readChildNodes(node, obj.get);
  205. // backwards compatibility
  206. if (!obj.href) {
  207. obj.href = obj.get.href;
  208. }
  209. },
  210. "Post": function(node, obj) {
  211. obj.post = {};
  212. this.readChildNodes(node, obj.post);
  213. // backwards compatibility
  214. if (!obj.href) {
  215. obj.href = obj.get.href;
  216. }
  217. },
  218. "GetMap": function(node, obj) {
  219. obj.getmap = {formats: []};
  220. this.readChildNodes(node, obj.getmap);
  221. },
  222. "GetFeatureInfo": function(node, obj) {
  223. obj.getfeatureinfo = {formats: []};
  224. this.readChildNodes(node, obj.getfeatureinfo);
  225. },
  226. "Exception": function(node, obj) {
  227. obj.exception = {formats: []};
  228. this.readChildNodes(node, obj.exception);
  229. },
  230. "Layer": function(node, obj) {
  231. var parentLayer, capability;
  232. if (obj.capability) {
  233. capability = obj.capability;
  234. parentLayer = obj;
  235. } else {
  236. capability = obj;
  237. }
  238. var attrNode = node.getAttributeNode("queryable");
  239. var queryable = (attrNode && attrNode.specified) ?
  240. node.getAttribute("queryable") : null;
  241. attrNode = node.getAttributeNode("cascaded");
  242. var cascaded = (attrNode && attrNode.specified) ?
  243. node.getAttribute("cascaded") : null;
  244. attrNode = node.getAttributeNode("opaque");
  245. var opaque = (attrNode && attrNode.specified) ?
  246. node.getAttribute('opaque') : null;
  247. var noSubsets = node.getAttribute('noSubsets');
  248. var fixedWidth = node.getAttribute('fixedWidth');
  249. var fixedHeight = node.getAttribute('fixedHeight');
  250. var parent = parentLayer || {},
  251. extend = OpenLayers.Util.extend;
  252. var layer = {
  253. nestedLayers: [],
  254. styles: parentLayer ? [].concat(parentLayer.styles) : [],
  255. srs: parentLayer ? extend({}, parent.srs) : {},
  256. metadataURLs: [],
  257. bbox: parentLayer ? extend({}, parent.bbox) : {},
  258. llbbox: parent.llbbox,
  259. dimensions: parentLayer ? extend({}, parent.dimensions) : {},
  260. authorityURLs: parentLayer ? extend({}, parent.authorityURLs) : {},
  261. identifiers: {},
  262. keywords: [],
  263. queryable: (queryable && queryable !== "") ?
  264. (queryable === "1" || queryable === "true" ) :
  265. (parent.queryable || false),
  266. cascaded: (cascaded !== null) ? parseInt(cascaded) :
  267. (parent.cascaded || 0),
  268. opaque: opaque ?
  269. (opaque === "1" || opaque === "true" ) :
  270. (parent.opaque || false),
  271. noSubsets: (noSubsets !== null) ?
  272. (noSubsets === "1" || noSubsets === "true" ) :
  273. (parent.noSubsets || false),
  274. fixedWidth: (fixedWidth != null) ?
  275. parseInt(fixedWidth) : (parent.fixedWidth || 0),
  276. fixedHeight: (fixedHeight != null) ?
  277. parseInt(fixedHeight) : (parent.fixedHeight || 0),
  278. minScale: parent.minScale,
  279. maxScale: parent.maxScale,
  280. attribution: parent.attribution
  281. };
  282. obj.nestedLayers.push(layer);
  283. layer.capability = capability;
  284. this.readChildNodes(node, layer);
  285. delete layer.capability;
  286. if(layer.name) {
  287. var parts = layer.name.split(":"),
  288. request = capability.request,
  289. gfi = request.getfeatureinfo;
  290. if(parts.length > 0) {
  291. layer.prefix = parts[0];
  292. }
  293. capability.layers.push(layer);
  294. if (layer.formats === undefined) {
  295. layer.formats = request.getmap.formats;
  296. }
  297. if (layer.infoFormats === undefined && gfi) {
  298. layer.infoFormats = gfi.formats;
  299. }
  300. }
  301. },
  302. "Attribution": function(node, obj) {
  303. obj.attribution = {};
  304. this.readChildNodes(node, obj.attribution);
  305. },
  306. "LogoURL": function(node, obj) {
  307. obj.logo = {
  308. width: node.getAttribute("width"),
  309. height: node.getAttribute("height")
  310. };
  311. this.readChildNodes(node, obj.logo);
  312. },
  313. "Style": function(node, obj) {
  314. var style = {};
  315. obj.styles.push(style);
  316. this.readChildNodes(node, style);
  317. },
  318. "LegendURL": function(node, obj) {
  319. var legend = {
  320. width: node.getAttribute("width"),
  321. height: node.getAttribute("height")
  322. };
  323. obj.legend = legend;
  324. this.readChildNodes(node, legend);
  325. },
  326. "MetadataURL": function(node, obj) {
  327. var metadataURL = {type: node.getAttribute("type")};
  328. obj.metadataURLs.push(metadataURL);
  329. this.readChildNodes(node, metadataURL);
  330. },
  331. "DataURL": function(node, obj) {
  332. obj.dataURL = {};
  333. this.readChildNodes(node, obj.dataURL);
  334. },
  335. "FeatureListURL": function(node, obj) {
  336. obj.featureListURL = {};
  337. this.readChildNodes(node, obj.featureListURL);
  338. },
  339. "AuthorityURL": function(node, obj) {
  340. var name = node.getAttribute("name");
  341. var authority = {};
  342. this.readChildNodes(node, authority);
  343. obj.authorityURLs[name] = authority.href;
  344. },
  345. "Identifier": function(node, obj) {
  346. var authority = node.getAttribute("authority");
  347. obj.identifiers[authority] = this.getChildValue(node);
  348. },
  349. "KeywordList": function(node, obj) {
  350. this.readChildNodes(node, obj);
  351. },
  352. "SRS": function(node, obj) {
  353. obj.srs[this.getChildValue(node)] = true;
  354. }
  355. }
  356. },
  357. CLASS_NAME: "OpenLayers.Format.WMSCapabilities.v1"
  358. });