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