Context.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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/XML/VersionedOGC.js
  7. */
  8. /**
  9. * Class: OpenLayers.Format.Context
  10. * Base class for both Format.WMC and Format.OWSContext
  11. *
  12. * Inherits from:
  13. * - <OpenLayers.Format.XML.VersionedOGC>
  14. */
  15. OpenLayers.Format.Context = OpenLayers.Class(OpenLayers.Format.XML.VersionedOGC, {
  16. /**
  17. * Property: layerOptions
  18. * {Object} Default options for layers created by the parser. These
  19. * options are overridden by the options which are read from the
  20. * capabilities document.
  21. */
  22. layerOptions: null,
  23. /**
  24. * Property: layerParams
  25. * {Object} Default parameters for layers created by the parser. This
  26. * can be used e.g. to override DEFAULT_PARAMS for
  27. * OpenLayers.Layer.WMS.
  28. */
  29. layerParams: null,
  30. /**
  31. * Constructor: OpenLayers.Format.Context
  32. * Create a new parser for Context documents.
  33. *
  34. * Parameters:
  35. * options - {Object} An optional object whose properties will be set on
  36. * this instance.
  37. */
  38. /**
  39. * APIMethod: read
  40. * Read Context data from a string, and return an object with map
  41. * properties and a list of layers.
  42. *
  43. * Parameters:
  44. * data - {String} or {DOMElement} data to read/parse.
  45. * options - {Object} The options object must contain a map property. If
  46. * the map property is a string, it must be the id of a dom element
  47. * where the new map will be placed. If the map property is an
  48. * <OpenLayers.Map>, the layers from the context document will be added
  49. * to the map.
  50. *
  51. * Returns:
  52. * {<OpenLayers.Map>} A map based on the context.
  53. */
  54. read: function(data, options) {
  55. var context = OpenLayers.Format.XML.VersionedOGC.prototype.read.apply(this,
  56. arguments);
  57. var map;
  58. if(options && options.map) {
  59. this.context = context;
  60. if(options.map instanceof OpenLayers.Map) {
  61. map = this.mergeContextToMap(context, options.map);
  62. } else {
  63. var mapOptions = options.map;
  64. if(OpenLayers.Util.isElement(mapOptions) ||
  65. typeof mapOptions == "string") {
  66. // we assume mapOptions references a div
  67. // element
  68. mapOptions = {div: mapOptions};
  69. }
  70. map = this.contextToMap(context, mapOptions);
  71. }
  72. } else {
  73. // not documented as part of the API, provided as a non-API option
  74. map = context;
  75. }
  76. return map;
  77. },
  78. /**
  79. * Method: getLayerFromContext
  80. * Create a WMS layer from a layerContext object.
  81. *
  82. * Parameters:
  83. * layerContext - {Object} An object representing a WMS layer.
  84. *
  85. * Returns:
  86. * {<OpenLayers.Layer.WMS>} A WMS layer.
  87. */
  88. getLayerFromContext: function(layerContext) {
  89. var i, len;
  90. // fill initial options object from layerContext
  91. var options = {
  92. queryable: layerContext.queryable, //keep queryable for api compatibility
  93. visibility: layerContext.visibility,
  94. maxExtent: layerContext.maxExtent,
  95. metadata: OpenLayers.Util.applyDefaults(layerContext.metadata,
  96. {styles: layerContext.styles,
  97. formats: layerContext.formats,
  98. "abstract": layerContext["abstract"],
  99. dataURL: layerContext.dataURL
  100. }),
  101. numZoomLevels: layerContext.numZoomLevels,
  102. units: layerContext.units,
  103. isBaseLayer: layerContext.isBaseLayer,
  104. opacity: layerContext.opacity,
  105. displayInLayerSwitcher: layerContext.displayInLayerSwitcher,
  106. singleTile: layerContext.singleTile,
  107. tileSize: (layerContext.tileSize) ?
  108. new OpenLayers.Size(
  109. layerContext.tileSize.width,
  110. layerContext.tileSize.height
  111. ) : undefined,
  112. minScale: layerContext.minScale || layerContext.maxScaleDenominator,
  113. maxScale: layerContext.maxScale || layerContext.minScaleDenominator,
  114. srs: layerContext.srs,
  115. dimensions: layerContext.dimensions,
  116. metadataURL: layerContext.metadataURL
  117. };
  118. if (this.layerOptions) {
  119. OpenLayers.Util.applyDefaults(options, this.layerOptions);
  120. }
  121. var params = {
  122. layers: layerContext.name,
  123. transparent: layerContext.transparent,
  124. version: layerContext.version
  125. };
  126. if (layerContext.formats && layerContext.formats.length>0) {
  127. // set default value for params if current attribute is not positionned
  128. params.format = layerContext.formats[0].value;
  129. for (i=0, len=layerContext.formats.length; i<len; i++) {
  130. var format = layerContext.formats[i];
  131. if (format.current == true) {
  132. params.format = format.value;
  133. break;
  134. }
  135. }
  136. }
  137. if (layerContext.styles && layerContext.styles.length>0) {
  138. for (i=0, len=layerContext.styles.length; i<len; i++) {
  139. var style = layerContext.styles[i];
  140. if (style.current == true) {
  141. // three style types to consider
  142. // 1) linked SLD
  143. // 2) inline SLD
  144. // 3) named style
  145. if(style.href) {
  146. params.sld = style.href;
  147. } else if(style.body) {
  148. params.sld_body = style.body;
  149. } else {
  150. params.styles = style.name;
  151. }
  152. break;
  153. }
  154. }
  155. }
  156. if (this.layerParams) {
  157. OpenLayers.Util.applyDefaults(params, this.layerParams);
  158. }
  159. var layer = null;
  160. var service = layerContext.service;
  161. if (service == OpenLayers.Format.Context.serviceTypes.WFS) {
  162. options.strategies = [new OpenLayers.Strategy.BBOX()];
  163. options.protocol = new OpenLayers.Protocol.WFS({
  164. url: layerContext.url,
  165. // since we do not know featureNS, let the protocol
  166. // determine it automagically using featurePrefix
  167. featurePrefix: layerContext.name.split(":")[0],
  168. featureType: layerContext.name.split(":").pop()
  169. });
  170. layer = new OpenLayers.Layer.Vector(
  171. layerContext.title || layerContext.name,
  172. options
  173. );
  174. } else if (service == OpenLayers.Format.Context.serviceTypes.KML) {
  175. // use a vector layer with an HTTP Protcol and a Fixed strategy
  176. options.strategies = [new OpenLayers.Strategy.Fixed()];
  177. options.protocol = new OpenLayers.Protocol.HTTP({
  178. url: layerContext.url,
  179. format: new OpenLayers.Format.KML()
  180. });
  181. layer = new OpenLayers.Layer.Vector(
  182. layerContext.title || layerContext.name,
  183. options
  184. );
  185. } else if (service == OpenLayers.Format.Context.serviceTypes.GML) {
  186. // use a vector layer with a HTTP Protocol and a Fixed strategy
  187. options.strategies = [new OpenLayers.Strategy.Fixed()];
  188. options.protocol = new OpenLayers.Protocol.HTTP({
  189. url: layerContext.url,
  190. format: new OpenLayers.Format.GML()
  191. });
  192. layer = new OpenLayers.Layer.Vector(
  193. layerContext.title || layerContext.name,
  194. options
  195. );
  196. } else if (layerContext.features) {
  197. // inline GML or KML features
  198. layer = new OpenLayers.Layer.Vector(
  199. layerContext.title || layerContext.name,
  200. options
  201. );
  202. layer.addFeatures(layerContext.features);
  203. } else if (layerContext.categoryLayer !== true) {
  204. layer = new OpenLayers.Layer.WMS(
  205. layerContext.title || layerContext.name,
  206. layerContext.url,
  207. params,
  208. options
  209. );
  210. }
  211. return layer;
  212. },
  213. /**
  214. * Method: getLayersFromContext
  215. * Create an array of layers from an array of layerContext objects.
  216. *
  217. * Parameters:
  218. * layersContext - {Array(Object)} An array of objects representing layers.
  219. *
  220. * Returns:
  221. * {Array(<OpenLayers.Layer>)} An array of layers.
  222. */
  223. getLayersFromContext: function(layersContext) {
  224. var layers = [];
  225. for (var i=0, len=layersContext.length; i<len; i++) {
  226. var layer = this.getLayerFromContext(layersContext[i]);
  227. if (layer !== null) {
  228. layers.push(layer);
  229. }
  230. }
  231. return layers;
  232. },
  233. /**
  234. * Method: contextToMap
  235. * Create a map given a context object.
  236. *
  237. * Parameters:
  238. * context - {Object} The context object.
  239. * options - {Object} Default map options.
  240. *
  241. * Returns:
  242. * {<OpenLayers.Map>} A map based on the context object.
  243. */
  244. contextToMap: function(context, options) {
  245. options = OpenLayers.Util.applyDefaults({
  246. maxExtent: context.maxExtent,
  247. projection: context.projection,
  248. units: context.units
  249. }, options);
  250. if (options.maxExtent) {
  251. options.maxResolution =
  252. options.maxExtent.getWidth() / OpenLayers.Map.TILE_WIDTH;
  253. }
  254. var metadata = {
  255. contactInformation: context.contactInformation,
  256. "abstract": context["abstract"],
  257. keywords: context.keywords,
  258. logo: context.logo,
  259. descriptionURL: context.descriptionURL
  260. };
  261. options.metadata = metadata;
  262. var map = new OpenLayers.Map(options);
  263. map.addLayers(this.getLayersFromContext(context.layersContext));
  264. map.setCenter(
  265. context.bounds.getCenterLonLat(),
  266. map.getZoomForExtent(context.bounds, true)
  267. );
  268. return map;
  269. },
  270. /**
  271. * Method: mergeContextToMap
  272. * Add layers from a context object to a map.
  273. *
  274. * Parameters:
  275. * context - {Object} The context object.
  276. * map - {<OpenLayers.Map>} The map.
  277. *
  278. * Returns:
  279. * {<OpenLayers.Map>} The same map with layers added.
  280. */
  281. mergeContextToMap: function(context, map) {
  282. map.addLayers(this.getLayersFromContext(context.layersContext));
  283. return map;
  284. },
  285. /**
  286. * APIMethod: write
  287. * Write a context document given a map.
  288. *
  289. * Parameters:
  290. * obj - {<OpenLayers.Map> | Object} A map or context object.
  291. * options - {Object} Optional configuration object.
  292. *
  293. * Returns:
  294. * {String} A context document string.
  295. */
  296. write: function(obj, options) {
  297. obj = this.toContext(obj);
  298. return OpenLayers.Format.XML.VersionedOGC.prototype.write.apply(this,
  299. arguments);
  300. },
  301. CLASS_NAME: "OpenLayers.Format.Context"
  302. });
  303. /**
  304. * Constant: OpenLayers.Format.Context.serviceTypes
  305. * Enumeration for service types
  306. */
  307. OpenLayers.Format.Context.serviceTypes = {
  308. "WMS": "urn:ogc:serviceType:WMS",
  309. "WFS": "urn:ogc:serviceType:WFS",
  310. "WCS": "urn:ogc:serviceType:WCS",
  311. "GML": "urn:ogc:serviceType:GML",
  312. "SLD": "urn:ogc:serviceType:SLD",
  313. "FES": "urn:ogc:serviceType:FES",
  314. "KML": "urn:ogc:serviceType:KML"
  315. };