WFS.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the 2-clause BSD license.
  3. * See license.txt in the OpenLayers distribution or repository for the
  4. * full text of the license. */
  5. /**
  6. * @requires OpenLayers/Protocol.js
  7. */
  8. /**
  9. * Class: OpenLayers.Protocol.WFS
  10. * Used to create a versioned WFS protocol. Default version is 1.0.0.
  11. *
  12. * Returns:
  13. * {<OpenLayers.Protocol>} A WFS protocol of the given version.
  14. *
  15. * Example:
  16. * (code)
  17. * var protocol = new OpenLayers.Protocol.WFS({
  18. * version: "1.1.0",
  19. * url: "http://demo.opengeo.org/geoserver/wfs",
  20. * featureType: "tasmania_roads",
  21. * featureNS: "http://www.openplans.org/topp",
  22. * geometryName: "the_geom"
  23. * });
  24. * (end)
  25. *
  26. * See the protocols for specific WFS versions for more detail.
  27. */
  28. OpenLayers.Protocol.WFS = function(options) {
  29. options = OpenLayers.Util.applyDefaults(
  30. options, OpenLayers.Protocol.WFS.DEFAULTS
  31. );
  32. var cls = OpenLayers.Protocol.WFS["v"+options.version.replace(/\./g, "_")];
  33. if(!cls) {
  34. throw "Unsupported WFS version: " + options.version;
  35. }
  36. return new cls(options);
  37. };
  38. /**
  39. * Function: fromWMSLayer
  40. * Convenience function to create a WFS protocol from a WMS layer. This makes
  41. * the assumption that a WFS requests can be issued at the same URL as
  42. * WMS requests and that a WFS featureType exists with the same name as the
  43. * WMS layer.
  44. *
  45. * This function is designed to auto-configure <url>, <featureType>,
  46. * <featurePrefix> and <srsName> for WFS <version> 1.1.0. Note that
  47. * srsName matching with the WMS layer will not work with WFS 1.0.0.
  48. *
  49. * Parameters:
  50. * layer - {<OpenLayers.Layer.WMS>} WMS layer that has a matching WFS
  51. * FeatureType at the same server url with the same typename.
  52. * options - {Object} Default properties to be set on the protocol.
  53. *
  54. * Returns:
  55. * {<OpenLayers.Protocol.WFS>}
  56. */
  57. OpenLayers.Protocol.WFS.fromWMSLayer = function(layer, options) {
  58. var typeName, featurePrefix;
  59. var param = layer.params["LAYERS"];
  60. var parts = (OpenLayers.Util.isArray(param) ? param[0] : param).split(":");
  61. if(parts.length > 1) {
  62. featurePrefix = parts[0];
  63. }
  64. typeName = parts.pop();
  65. var protocolOptions = {
  66. url: layer.url,
  67. featureType: typeName,
  68. featurePrefix: featurePrefix,
  69. srsName: layer.projection && layer.projection.getCode() ||
  70. layer.map && layer.map.getProjectionObject().getCode(),
  71. version: "1.1.0"
  72. };
  73. return new OpenLayers.Protocol.WFS(OpenLayers.Util.applyDefaults(
  74. options, protocolOptions
  75. ));
  76. };
  77. /**
  78. * Constant: OpenLayers.Protocol.WFS.DEFAULTS
  79. */
  80. OpenLayers.Protocol.WFS.DEFAULTS = {
  81. "version": "1.0.0"
  82. };