GML.js 5.4 KB

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