1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2 * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3 * full text of the license. */
6 * @requires OpenLayers/Format/XML.js
10 * Class: OpenLayers.Format.WMC
11 * Read and write Web Map Context documents.
14 * - <OpenLayers.Format.XML>
16 OpenLayers.Format.WMC = OpenLayers.Class({
19 * APIProperty: defaultVersion
20 * {String} Version number to assume if none found. Default is "1.1.0".
22 defaultVersion: "1.1.0",
25 * APIProperty: version
26 * {String} Specify a version string if one is known.
31 * Property: layerOptions
32 * {Object} Default options for layers created by the parser. These
33 * options are overridden by the options which are read from the
34 * capabilities document.
39 * Property: layerParams
40 * {Object} Default parameters for layers created by the parser. This
41 * can be used to override DEFAULT_PARAMS for OpenLayers.Layer.WMS.
47 * {Object} Instance of the versioned parser. Cached for multiple read and
48 * write calls of the same version.
53 * Constructor: OpenLayers.Format.WMC
54 * Create a new parser for WMC docs.
57 * options - {Object} An optional object whose properties will be set on
60 initialize: function(options) {
61 OpenLayers.Util.extend(this, options);
62 this.options = options;
67 * Read WMC data from a string, and return an object with map properties
68 * and a list of layers.
71 * data - {String} or {DOMElement} data to read/parse.
72 * options - {Object} The options object must contain a map property. If
73 * the map property is a string, it must be the id of a dom element
74 * where the new map will be placed. If the map property is an
75 * <OpenLayers.Map>, the layers from the context document will be added
79 * {<OpenLayers.Map>} A map based on the context.
81 read: function(data, options) {
82 if(typeof data == "string") {
83 data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);
85 var root = data.documentElement;
86 var version = this.version;
88 version = root.getAttribute("version");
90 version = this.defaultVersion;
93 if(!this.parser || this.parser.VERSION != version) {
94 var format = OpenLayers.Format.WMC[
95 "v" + version.replace(/\./g, "_")
98 throw "Can't find a WMC parser for version " +
101 this.parser = new format(this.options);
103 var context = this.parser.read(data, options);
106 this.context = context;
107 if(options.map instanceof OpenLayers.Map) {
108 map = this.mergeContextToMap(context, options.map);
110 map = this.contextToMap(context, options.map);
113 // not documented as part of the API, provided as a non-API option
120 * Method: contextToMap
121 * Create a map given a context object.
124 * context - {Object} The context object.
125 * id - {String | Element} The dom element or element id that will contain
129 * {<OpenLayers.Map>} A map based on the context object.
131 contextToMap: function(context, id) {
132 var map = new OpenLayers.Map(id, {
133 maxExtent: context.maxExtent,
134 projection: context.projection
136 map.addLayers(context.layers);
138 context.bounds.getCenterLonLat(),
139 map.getZoomForExtent(context.bounds, true)
145 * Method: mergeContextToMap
146 * Add layers from a context object to a map.
149 * context - {Object} The context object.
150 * map - {<OpenLayers.Map>} The map.
153 * {<OpenLayers.Map>} The same map with layers added.
155 mergeContextToMap: function(context, map) {
156 map.addLayers(context.layers);
162 * Write a WMC document given a map.
165 * obj - {<OpenLayers.Map> | Object} A map or context object.
166 * options - {Object} Optional configuration object.
169 * {String} A WMC document string.
171 write: function(obj, options) {
172 if(obj.CLASS_NAME == "OpenLayers.Map") {
173 obj = this.mapToContext(obj);
175 var version = (options && options.version) ||
176 this.version || this.defaultVersion;
177 if(!this.parser || this.parser.VERSION != version) {
178 var format = OpenLayers.Format.WMC[
179 "v" + version.replace(/\./g, "_")
182 throw "Can't find a WMS capabilities parser for version " +
185 this.parser = new format(this.options);
187 var wmc = this.parser.write(obj, options);
192 * Method: mapToContext
193 * Create a context object given a map.
196 * map - {<OpenLayers.Map>} The map.
199 * {Object} A context object.
201 mapToContext: function(map) {
203 bounds: map.getExtent(),
204 maxExtent: map.maxExtent,
205 projection: map.projection,
212 CLASS_NAME: "OpenLayers.Format.WMC"