Project

General

Profile

Download (11.7 KB) Statistics
| Branch: | Tag: | Revision:
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
/**
7
 * @requires OpenLayers/Format/XML/VersionedOGC.js
8
 */
9

    
10
/**
11
 * Class: OpenLayers.Format.Context
12
 * Base class for both Format.WMC and Format.OWSContext
13
 *
14
 * Inherits from:
15
 *  - <OpenLayers.Format.XML.VersionedOGC>
16
 */
17
OpenLayers.Format.Context = OpenLayers.Class(OpenLayers.Format.XML.VersionedOGC, {
18

    
19
    /**
20
     * Property: layerOptions
21
     * {Object} Default options for layers created by the parser. These
22
     *     options are overridden by the options which are read from the
23
     *     capabilities document.
24
     */
25
    layerOptions: null,
26

    
27
    /**
28
     * Property: layerParams
29
     * {Object} Default parameters for layers created by the parser. This
30
     *     can be used e.g. to override DEFAULT_PARAMS for 
31
     *     OpenLayers.Layer.WMS.
32
     */
33
    layerParams: null,
34

    
35
    /**
36
     * Constructor: OpenLayers.Format.Context
37
     * Create a new parser for Context documents.
38
     *
39
     * Parameters:
40
     * options - {Object} An optional object whose properties will be set on
41
     *     this instance.
42
     */
43

    
44
    /**
45
     * APIMethod: read
46
     * Read Context data from a string, and return an object with map
47
     *     properties and a list of layers.
48
     *
49
     * Parameters:
50
     * data - {String} or {DOMElement} data to read/parse.
51
     * options - {Object} The options object must contain a map property.  If
52
     *     the map property is a string, it must be the id of a dom element
53
     *     where the new map will be placed.  If the map property is an
54
     *     <OpenLayers.Map>, the layers from the context document will be added
55
     *     to the map.
56
     *
57
     * Returns:
58
     * {<OpenLayers.Map>} A map based on the context.
59
     */
60
    read: function(data, options) {
61
        var context = OpenLayers.Format.XML.VersionedOGC.prototype.read.apply(this, 
62
            arguments);
63
        var map;
64
        if(options && options.map) {
65
            this.context = context;
66
            if(options.map instanceof OpenLayers.Map) {
67
                map = this.mergeContextToMap(context, options.map);
68
            } else {
69
                var mapOptions = options.map;
70
                if(OpenLayers.Util.isElement(mapOptions) ||
71
                   typeof mapOptions == "string") {
72
                    // we assume mapOptions references a div
73
                    // element
74
                    mapOptions = {div: mapOptions};
75
                }
76
                map = this.contextToMap(context, mapOptions);
77
            }
78
        } else {
79
            // not documented as part of the API, provided as a non-API option
80
            map = context;
81
        }
82
        return map;
83
    },
84

    
85
    /**
86
     * Method: getLayerFromContext
87
     * Create a WMS layer from a layerContext object.
88
     *
89
     * Parameters:
90
     * layerContext - {Object} An object representing a WMS layer.
91
     *
92
     * Returns:
93
     * {<OpenLayers.Layer.WMS>} A WMS layer.
94
     */
95
    getLayerFromContext: function(layerContext) {
96
        var i, len;
97
        // fill initial options object from layerContext
98
        var options = {
99
            queryable: layerContext.queryable, //keep queryable for api compatibility
100
            visibility: layerContext.visibility,
101
            maxExtent: layerContext.maxExtent,
102
            metadata: OpenLayers.Util.applyDefaults(layerContext.metadata, 
103
            {styles: layerContext.styles,
104
             formats: layerContext.formats,
105
             "abstract": layerContext["abstract"],
106
             dataURL: layerContext.dataURL
107
            }),
108
            numZoomLevels: layerContext.numZoomLevels,
109
            units: layerContext.units,
110
            isBaseLayer: layerContext.isBaseLayer,
111
            opacity: layerContext.opacity,
112
            displayInLayerSwitcher: layerContext.displayInLayerSwitcher,
113
            singleTile: layerContext.singleTile,
114
            tileSize: (layerContext.tileSize) ? 
115
                new OpenLayers.Size(
116
                    layerContext.tileSize.width, 
117
                    layerContext.tileSize.height
118
                ) : undefined,
119
            minScale: layerContext.minScale || layerContext.maxScaleDenominator,
120
            maxScale: layerContext.maxScale || layerContext.minScaleDenominator,
121
            srs: layerContext.srs,
122
            dimensions: layerContext.dimensions,
123
            metadataURL: layerContext.metadataURL
124
        };
125
        if (this.layerOptions) {
126
            OpenLayers.Util.applyDefaults(options, this.layerOptions);
127
        }
128

    
129
        var params = {
130
            layers: layerContext.name,
131
            transparent: layerContext.transparent,
132
            version: layerContext.version
133
        };
134
        if (layerContext.formats && layerContext.formats.length>0) {
135
            // set default value for params if current attribute is not positionned
136
            params.format = layerContext.formats[0].value;
137
            for (i=0, len=layerContext.formats.length; i<len; i++) {
138
                var format = layerContext.formats[i];
139
                if (format.current == true) {
140
                    params.format = format.value;
141
                    break;
142
                }
143
            }
144
        }
145
        if (layerContext.styles && layerContext.styles.length>0) {
146
            for (i=0, len=layerContext.styles.length; i<len; i++) {
147
                var style = layerContext.styles[i];
148
                if (style.current == true) {
149
                    // three style types to consider
150
                    // 1) linked SLD
151
                    // 2) inline SLD
152
                    // 3) named style
153
                    if(style.href) {
154
                        params.sld = style.href;
155
                    } else if(style.body) {
156
                        params.sld_body = style.body;
157
                    } else {
158
                        params.styles = style.name;
159
                    }
160
                    break;
161
                }
162
            }
163
        }
164
        if (this.layerParams) {
165
            OpenLayers.Util.applyDefaults(params, this.layerParams);
166
        }
167

    
168
        var layer = null;
169
        var service = layerContext.service;
170
        if (service == OpenLayers.Format.Context.serviceTypes.WFS) {
171
            options.strategies = [new OpenLayers.Strategy.BBOX()];
172
            options.protocol = new OpenLayers.Protocol.WFS({
173
                url: layerContext.url,
174
                // since we do not know featureNS, let the protocol
175
                // determine it automagically using featurePrefix
176
                featurePrefix: layerContext.name.split(":")[0],
177
                featureType: layerContext.name.split(":").pop()
178
            });
179
            layer = new OpenLayers.Layer.Vector(
180
                layerContext.title || layerContext.name,
181
                options
182
            );
183
        } else if (service == OpenLayers.Format.Context.serviceTypes.KML) {
184
            // use a vector layer with an HTTP Protcol and a Fixed strategy
185
            options.strategies = [new OpenLayers.Strategy.Fixed()];
186
            options.protocol = new OpenLayers.Protocol.HTTP({
187
                url: layerContext.url, 
188
                format: new OpenLayers.Format.KML()
189
            });
190
            layer = new OpenLayers.Layer.Vector(
191
                layerContext.title || layerContext.name,
192
                options
193
            );
194
        } else if (service == OpenLayers.Format.Context.serviceTypes.GML) {
195
            // use a vector layer with a HTTP Protocol and a Fixed strategy
196
            options.strategies = [new OpenLayers.Strategy.Fixed()];
197
            options.protocol = new OpenLayers.Protocol.HTTP({
198
                url: layerContext.url, 
199
                format: new OpenLayers.Format.GML()
200
            });
201
            layer = new OpenLayers.Layer.Vector(
202
                layerContext.title || layerContext.name,
203
                options
204
            );
205
        } else if (layerContext.features) {
206
            // inline GML or KML features
207
            layer = new OpenLayers.Layer.Vector(
208
                layerContext.title || layerContext.name,
209
                options
210
            );
211
            layer.addFeatures(layerContext.features);
212
        } else if (layerContext.categoryLayer !== true) {
213
            layer = new OpenLayers.Layer.WMS(
214
                layerContext.title || layerContext.name,
215
                layerContext.url,
216
                params,
217
                options
218
            );
219
        }
220
        return layer;
221
    },
222

    
223
    /**
224
     * Method: getLayersFromContext
225
     * Create an array of layers from an array of layerContext objects.
226
     *
227
     * Parameters:
228
     * layersContext - {Array(Object)} An array of objects representing layers.
229
     *
230
     * Returns:
231
     * {Array(<OpenLayers.Layer>)} An array of layers.
232
     */
233
    getLayersFromContext: function(layersContext) {
234
        var layers = [];
235
        for (var i=0, len=layersContext.length; i<len; i++) {
236
            var layer = this.getLayerFromContext(layersContext[i]);
237
            if (layer !== null) {
238
                layers.push(layer);
239
            }
240
        }
241
        return layers;
242
    },
243

    
244
    /**
245
     * Method: contextToMap
246
     * Create a map given a context object.
247
     *
248
     * Parameters:
249
     * context - {Object} The context object.
250
     * options - {Object} Default map options.
251
     *
252
     * Returns:
253
     * {<OpenLayers.Map>} A map based on the context object.
254
     */
255
    contextToMap: function(context, options) {
256
        options = OpenLayers.Util.applyDefaults({
257
            maxExtent:  context.maxExtent,
258
            projection: context.projection,
259
            units:      context.units
260
        }, options);
261

    
262
        if (options.maxExtent) {
263
            options.maxResolution = 
264
                options.maxExtent.getWidth() / OpenLayers.Map.TILE_WIDTH;
265
        }
266

    
267
        var metadata = {
268
            contactInformation: context.contactInformation,
269
            "abstract":         context["abstract"],
270
            keywords:           context.keywords,
271
            logo:               context.logo,
272
            descriptionURL:     context.descriptionURL
273
        };
274

    
275
        options.metadata = metadata;
276

    
277
        var map = new OpenLayers.Map(options);
278
        map.addLayers(this.getLayersFromContext(context.layersContext));
279
        map.setCenter(
280
            context.bounds.getCenterLonLat(),
281
            map.getZoomForExtent(context.bounds, true)
282
        );
283
        return map;
284
    },
285

    
286
    /**
287
     * Method: mergeContextToMap
288
     * Add layers from a context object to a map.
289
     *
290
     * Parameters:
291
     * context - {Object} The context object.
292
     * map - {<OpenLayers.Map>} The map.
293
     *
294
     * Returns:
295
     * {<OpenLayers.Map>} The same map with layers added.
296
     */
297
    mergeContextToMap: function(context, map) {
298
        map.addLayers(this.getLayersFromContext(context.layersContext));
299
        return map;
300
    },
301

    
302
    /**
303
     * APIMethod: write
304
     * Write a context document given a map.
305
     *
306
     * Parameters:
307
     * obj - {<OpenLayers.Map> | Object} A map or context object.
308
     * options - {Object} Optional configuration object.
309
     *
310
     * Returns:
311
     * {String} A context document string.
312
     */
313
    write: function(obj, options) {
314
        obj = this.toContext(obj);
315
        return OpenLayers.Format.XML.VersionedOGC.prototype.write.apply(this,
316
            arguments);
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
};
(6-6/41)