]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Tile/WFS.js
initial commit
[syp.git] / openlayers / lib / OpenLayers / Tile / WFS.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 /**
7  * @requires OpenLayers/Tile.js
8  * @requires OpenLayers/Request/XMLHttpRequest.js
9  */
10
11 /**
12  * Class: OpenLayers.Tile.WFS
13  * Instances of OpenLayers.Tile.WFS are used to manage the image tiles
14  * used by various layers.  Create a new image tile with the
15  * <OpenLayers.Tile.WFS> constructor.
16  *
17  * Inherits from:
18  *  - <OpenLayers.Tile>
19  */
20 OpenLayers.Tile.WFS = OpenLayers.Class(OpenLayers.Tile, {
21
22     /** 
23      * Property: features 
24      * {Array(<OpenLayers.Feature>)} list of features in this tile 
25      */
26     features: null,
27
28     /** 
29      * Property: url 
30      * {String} 
31      */
32     url: null,
33     
34     /** 
35      * Property: request 
36      * {<OpenLayers.Request.XMLHttpRequest>} 
37      */ 
38     request: null,     
39     
40     /** TBD 3.0 - reorder the parameters to the init function to put URL 
41      *             as last, so we can continue to call tile.initialize() 
42      *             without changing the arguments. 
43      * 
44      * Constructor: OpenLayers.Tile.WFS
45      * Constructor for a new <OpenLayers.Tile.WFS> instance.
46      * 
47      * Parameters:
48      * layer - {<OpenLayers.Layer>} layer that the tile will go in.
49      * position - {<OpenLayers.Pixel>}
50      * bounds - {<OpenLayers.Bounds>}
51      * url - {<String>}
52      * size - {<OpenLayers.Size>}
53      */   
54     initialize: function(layer, position, bounds, url, size) {
55         OpenLayers.Tile.prototype.initialize.apply(this, arguments);
56         this.url = url;        
57         this.features = [];
58     },
59
60     /** 
61      * APIMethod: destroy
62      * nullify references to prevent circular references and memory leaks
63      */
64     destroy: function() {
65         OpenLayers.Tile.prototype.destroy.apply(this, arguments);
66         this.destroyAllFeatures();
67         this.features = null;
68         this.url = null;
69         if(this.request) {
70             this.request.abort();
71             //this.request.destroy();
72             this.request = null;
73         }
74     },
75
76     /** 
77      * Method: clear
78      *  Clear the tile of any bounds/position-related data so that it can 
79      *   be reused in a new location.
80      */
81     clear: function() {
82         this.destroyAllFeatures();
83     },
84     
85     /**
86      * Method: draw
87      * Check that a tile should be drawn, and load features for it.
88      */
89     draw:function() {
90         if (OpenLayers.Tile.prototype.draw.apply(this, arguments)) {
91             if (this.isLoading) {
92                 //if already loading, send 'reload' instead of 'loadstart'.
93                 this.events.triggerEvent("reload"); 
94             } else {
95                 this.isLoading = true;
96                 this.events.triggerEvent("loadstart");
97             }
98             this.loadFeaturesForRegion(this.requestSuccess);
99         }
100     },
101
102     /** 
103     * Method: loadFeaturesForRegion
104     * Abort any pending requests and issue another request for data. 
105     *
106     * Input are function pointers for what to do on success and failure.
107     *
108     * Parameters:
109     * success - {function}
110     * failure - {function}
111     */
112     loadFeaturesForRegion:function(success, failure) {
113         if(this.request) {
114             this.request.abort();
115         }
116         this.request = OpenLayers.Request.GET({
117             url: this.url,
118             success: success,
119             failure: failure,
120             scope: this
121         });
122     },
123     
124     /**
125     * Method: requestSuccess
126     * Called on return from request succcess. Adds results via 
127     * layer.addFeatures in vector mode, addResults otherwise. 
128     *
129     * Parameters:
130     * request - {<OpenLayers.Request.XMLHttpRequest>}
131     */
132     requestSuccess:function(request) {
133         if (this.features) {
134             var doc = request.responseXML;
135             if (!doc || !doc.documentElement) {
136                 doc = request.responseText; 
137             }
138             if (this.layer.vectorMode) {
139                 this.layer.addFeatures(this.layer.formatObject.read(doc));
140             } else {
141                 var xml = new OpenLayers.Format.XML();
142                 if (typeof doc == "string") {
143                     doc = xml.read(doc);
144                 }
145                 var resultFeatures = xml.getElementsByTagNameNS(
146                     doc, "http://www.opengis.net/gml", "featureMember"
147                 );
148                 this.addResults(resultFeatures);
149             }
150         }
151         if (this.events) {
152             this.events.triggerEvent("loadend"); 
153         }
154
155         //request produced with success, we can delete the request object.
156         //this.request.destroy();
157         this.request = null;
158     },
159
160     /**
161      * Method: addResults
162      * Construct new feature via layer featureClass constructor, and add to
163      * this.features.
164      * 
165      * Parameters:
166      * results - {Object}
167      */
168     addResults: function(results) {
169         for (var i=0; i < results.length; i++) {
170             var feature = new this.layer.featureClass(this.layer, 
171                                                       results[i]);
172             this.features.push(feature);
173         }
174     },
175
176
177     /** 
178      * Method: destroyAllFeatures
179      * Iterate through and call destroy() on each feature, removing it from
180      *   the local array
181      */
182     destroyAllFeatures: function() {
183         while(this.features.length > 0) {
184             var feature = this.features.shift();
185             feature.destroy();
186         }
187     },
188
189     CLASS_NAME: "OpenLayers.Tile.WFS"
190   }
191 );