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