]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Layer/GML.js
initial commit
[syp.git] / openlayers / lib / OpenLayers / Layer / GML.js
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. */
4
5 /**
6  * @requires OpenLayers/Layer/Vector.js
7  * @requires OpenLayers/Request/XMLHttpRequest.js
8  * @requires OpenLayers/Console.js
9  */
10
11 /**
12  * Class: OpenLayers.Layer.GML
13  * Create a vector layer by parsing a GML file. The GML file is
14  *     passed in as a parameter.
15  *
16  * Inherits from:
17  *  - <OpenLayers.Layer.Vector>
18  */
19 OpenLayers.Layer.GML = OpenLayers.Class(OpenLayers.Layer.Vector, {
20     
21     /**
22       * Property: loaded
23       * {Boolean} Flag for whether the GML data has been loaded yet.
24       */
25     loaded: false,
26
27     /**
28       * APIProperty: format
29       * {<OpenLayers.Format>} The format you want the data to be parsed with.
30       */
31     format: null,
32
33     /**
34      * APIProperty: formatOptions
35      * {Object} Hash of options which should be passed to the format when it is
36      * created. Must be passed in the constructor.
37      */
38     formatOptions: null, 
39     
40     /**
41      * Constructor: OpenLayers.Layer.GML
42      * Load and parse a single file on the web, according to the format
43      * provided via the 'format' option, defaulting to GML. 
44      *
45      * Parameters:
46      * name - {String} 
47      * url - {String} URL of a GML file.
48      * options - {Object} Hashtable of extra options to tag onto the layer.
49      */
50      initialize: function(name, url, options) {
51         var newArguments = [];
52         newArguments.push(name, options);
53         OpenLayers.Layer.Vector.prototype.initialize.apply(this, newArguments);
54         this.url = url;
55     },
56
57     /**
58      * APIMethod: setVisibility
59      * Set the visibility flag for the layer and hide/show&redraw accordingly. 
60      * Fire event unless otherwise specified
61      * GML will be loaded if the layer is being made visible for the first
62      * time.
63      *  
64      * Parameters:
65      * visible - {Boolean} Whether or not to display the layer 
66      *                          (if in range)
67      * noEvent - {Boolean} 
68      */
69     setVisibility: function(visibility, noEvent) {
70         OpenLayers.Layer.Vector.prototype.setVisibility.apply(this, arguments);
71         if(this.visibility && !this.loaded){
72             // Load the GML
73             this.loadGML();
74         }
75     },
76
77     /**
78      * Method: moveTo
79      * If layer is visible and GML has not been loaded, load GML, then load GML
80      * and call OpenLayers.Layer.Vector.moveTo() to redraw at the new location.
81      * 
82      * Parameters:
83      * bounds - {Object} 
84      * zoomChanged - {Object} 
85      * minor - {Object} 
86      */
87     moveTo:function(bounds, zoomChanged, minor) {
88         OpenLayers.Layer.Vector.prototype.moveTo.apply(this, arguments);
89         // Wait until initialisation is complete before loading GML
90         // otherwise we can get a race condition where the root HTML DOM is
91         // loaded after the GML is paited.
92         // See http://trac.openlayers.org/ticket/404
93         if(this.visibility && !this.loaded){
94             this.loadGML();
95         }
96     },
97
98     /**
99      * Method: loadGML
100      */
101     loadGML: function() {
102         if (!this.loaded) {
103             this.events.triggerEvent("loadstart");
104             OpenLayers.Request.GET({
105                 url: this.url,
106                 success: this.requestSuccess,
107                 failure: this.requestFailure,
108                 scope: this
109             });
110             this.loaded = true;
111         }    
112     },    
113     
114     /**
115      * Method: setUrl
116      * Change the URL and reload the GML
117      *
118      * Parameters:
119      * url - {String} URL of a GML file.
120      */
121     setUrl:function(url) {
122         this.url = url;
123         this.destroyFeatures();
124         this.loaded = false;
125         this.loadGML();
126     },
127     
128     /**
129      * Method: requestSuccess
130      * Process GML after it has been loaded.
131      * Called by initialise() and loadUrl() after the GML has been loaded.
132      *
133      * Parameters:
134      * request - {String} 
135      */
136     requestSuccess:function(request) {
137         var doc = request.responseXML;
138         
139         if (!doc || !doc.documentElement) {
140             doc = request.responseText;
141         }
142         
143         var options = {};
144         
145         OpenLayers.Util.extend(options, this.formatOptions);
146         if (this.map && !this.projection.equals(this.map.getProjectionObject())) {
147             options.externalProjection = this.projection;
148             options.internalProjection = this.map.getProjectionObject();
149         }    
150         
151         var gml = this.format ? new this.format(options) : new OpenLayers.Format.GML(options);
152         this.addFeatures(gml.read(doc));
153         this.events.triggerEvent("loadend");
154     },
155     
156     /**
157      * Method: requestFailure
158      * Process a failed loading of GML.
159      * Called by initialise() and loadUrl() if there was a problem loading GML.
160      *
161      * Parameters:
162      * request - {String} 
163      */
164     requestFailure: function(request) {
165         OpenLayers.Console.userError(OpenLayers.i18n("errorLoadingGML", {'url':this.url}));
166         this.events.triggerEvent("loadend");
167     },
168
169     CLASS_NAME: "OpenLayers.Layer.GML"
170 });