WFS.js 5.5 KB

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