v1.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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/WFS.js
  7. */
  8. /**
  9. * Class: OpenLayers.Protocol.WFS.v1
  10. * Abstract class for for v1.0.0 and v1.1.0 protocol.
  11. *
  12. * Inherits from:
  13. * - <OpenLayers.Protocol>
  14. */
  15. OpenLayers.Protocol.WFS.v1 = OpenLayers.Class(OpenLayers.Protocol, {
  16. /**
  17. * Property: version
  18. * {String} WFS version number.
  19. */
  20. version: null,
  21. /**
  22. * Property: srsName
  23. * {String} Name of spatial reference system. Default is "EPSG:4326".
  24. */
  25. srsName: "EPSG:4326",
  26. /**
  27. * Property: featureType
  28. * {String} Local feature typeName.
  29. */
  30. featureType: null,
  31. /**
  32. * Property: featureNS
  33. * {String} Feature namespace.
  34. */
  35. featureNS: null,
  36. /**
  37. * Property: geometryName
  38. * {String} Name of the geometry attribute for features. Default is
  39. * "the_geom" for WFS <version> 1.0, and null for higher versions.
  40. */
  41. geometryName: "the_geom",
  42. /**
  43. * Property: maxFeatures
  44. * {Integer} Optional maximum number of features to retrieve.
  45. */
  46. /**
  47. * Property: schema
  48. * {String} Optional schema location that will be included in the
  49. * schemaLocation attribute value. Note that the feature type schema
  50. * is required for a strict XML validator (on transactions with an
  51. * insert for example), but is *not* required by the WFS specification
  52. * (since the server is supposed to know about feature type schemas).
  53. */
  54. schema: null,
  55. /**
  56. * Property: featurePrefix
  57. * {String} Namespace alias for feature type. Default is "feature".
  58. */
  59. featurePrefix: "feature",
  60. /**
  61. * Property: formatOptions
  62. * {Object} Optional options for the format. If a format is not provided,
  63. * this property can be used to extend the default format options.
  64. */
  65. formatOptions: null,
  66. /**
  67. * Property: readFormat
  68. * {<OpenLayers.Format>} For WFS requests it is possible to get a
  69. * different output format than GML. In that case, we cannot parse
  70. * the response with the default format (WFST) and we need a different
  71. * format for reading.
  72. */
  73. readFormat: null,
  74. /**
  75. * Property: readOptions
  76. * {Object} Optional object to pass to format's read.
  77. */
  78. readOptions: null,
  79. /**
  80. * Constructor: OpenLayers.Protocol.WFS
  81. * A class for giving layers WFS protocol.
  82. *
  83. * Parameters:
  84. * options - {Object} Optional object whose properties will be set on the
  85. * instance.
  86. *
  87. * Valid options properties:
  88. * url - {String} URL to send requests to (required).
  89. * featureType - {String} Local (without prefix) feature typeName (required).
  90. * featureNS - {String} Feature namespace (required, but can be autodetected
  91. * during the first query if GML is used as readFormat and
  92. * featurePrefix is provided and matches the prefix used by the server
  93. * for this featureType).
  94. * featurePrefix - {String} Feature namespace alias (optional - only used
  95. * for writing if featureNS is provided). Default is 'feature'.
  96. * geometryName - {String} Name of geometry attribute. The default is
  97. * 'the_geom' for WFS <version> 1.0, and null for higher versions. If
  98. * null, it will be set to the name of the first geometry found in the
  99. * first read operation.
  100. * multi - {Boolean} If set to true, geometries will be casted to Multi
  101. * geometries before they are written in a transaction. No casting will
  102. * be done when reading features.
  103. */
  104. initialize: function(options) {
  105. OpenLayers.Protocol.prototype.initialize.apply(this, [options]);
  106. if(!options.format) {
  107. this.format = OpenLayers.Format.WFST(OpenLayers.Util.extend({
  108. version: this.version,
  109. featureType: this.featureType,
  110. featureNS: this.featureNS,
  111. featurePrefix: this.featurePrefix,
  112. geometryName: this.geometryName,
  113. srsName: this.srsName,
  114. schema: this.schema
  115. }, this.formatOptions));
  116. }
  117. if (!options.geometryName && parseFloat(this.format.version) > 1.0) {
  118. this.setGeometryName(null);
  119. }
  120. },
  121. /**
  122. * APIMethod: destroy
  123. * Clean up the protocol.
  124. */
  125. destroy: function() {
  126. if(this.options && !this.options.format) {
  127. this.format.destroy();
  128. }
  129. this.format = null;
  130. OpenLayers.Protocol.prototype.destroy.apply(this);
  131. },
  132. /**
  133. * APIMethod: read
  134. * Construct a request for reading new features. Since WFS splits the
  135. * basic CRUD operations into GetFeature requests (for read) and
  136. * Transactions (for all others), this method does not make use of the
  137. * format's read method (that is only about reading transaction
  138. * responses).
  139. *
  140. * Parameters:
  141. * options - {Object} Options for the read operation, in addition to the
  142. * options set on the instance (options set here will take precedence).
  143. *
  144. * To use a configured protocol to get e.g. a WFS hit count, applications
  145. * could do the following:
  146. *
  147. * (code)
  148. * protocol.read({
  149. * readOptions: {output: "object"},
  150. * resultType: "hits",
  151. * maxFeatures: null,
  152. * callback: function(resp) {
  153. * // process resp.numberOfFeatures here
  154. * }
  155. * });
  156. * (end)
  157. *
  158. * To use a configured protocol to use WFS paging (if supported by the
  159. * server), applications could do the following:
  160. *
  161. * (code)
  162. * protocol.read({
  163. * startIndex: 0,
  164. * count: 50
  165. * });
  166. * (end)
  167. *
  168. * To limit the attributes returned by the GetFeature request, applications
  169. * can use the propertyNames option to specify the properties to include in
  170. * the response:
  171. *
  172. * (code)
  173. * protocol.read({
  174. * propertyNames: ["DURATION", "INTENSITY"]
  175. * });
  176. * (end)
  177. */
  178. read: function(options) {
  179. OpenLayers.Protocol.prototype.read.apply(this, arguments);
  180. options = OpenLayers.Util.extend({}, options);
  181. OpenLayers.Util.applyDefaults(options, this.options || {});
  182. var response = new OpenLayers.Protocol.Response({requestType: "read"});
  183. var data = OpenLayers.Format.XML.prototype.write.apply(
  184. this.format, [this.format.writeNode("wfs:GetFeature", options)]
  185. );
  186. response.priv = OpenLayers.Request.POST({
  187. url: options.url,
  188. callback: this.createCallback(this.handleRead, response, options),
  189. params: options.params,
  190. headers: options.headers,
  191. data: data
  192. });
  193. return response;
  194. },
  195. /**
  196. * APIMethod: setFeatureType
  197. * Change the feature type on the fly.
  198. *
  199. * Parameters:
  200. * featureType - {String} Local (without prefix) feature typeName.
  201. */
  202. setFeatureType: function(featureType) {
  203. this.featureType = featureType;
  204. this.format.featureType = featureType;
  205. },
  206. /**
  207. * APIMethod: setGeometryName
  208. * Sets the geometryName option after instantiation.
  209. *
  210. * Parameters:
  211. * geometryName - {String} Name of geometry attribute.
  212. */
  213. setGeometryName: function(geometryName) {
  214. this.geometryName = geometryName;
  215. this.format.geometryName = geometryName;
  216. },
  217. /**
  218. * Method: handleRead
  219. * Deal with response from the read request.
  220. *
  221. * Parameters:
  222. * response - {<OpenLayers.Protocol.Response>} The response object to pass
  223. * to the user callback.
  224. * options - {Object} The user options passed to the read call.
  225. */
  226. handleRead: function(response, options) {
  227. options = OpenLayers.Util.extend({}, options);
  228. OpenLayers.Util.applyDefaults(options, this.options);
  229. if(options.callback) {
  230. var request = response.priv;
  231. if(request.status >= 200 && request.status < 300) {
  232. // success
  233. var result = this.parseResponse(request, options.readOptions);
  234. if (result && result.success !== false) {
  235. if (options.readOptions && options.readOptions.output == "object") {
  236. OpenLayers.Util.extend(response, result);
  237. } else {
  238. response.features = result;
  239. }
  240. response.code = OpenLayers.Protocol.Response.SUCCESS;
  241. } else {
  242. // failure (service exception)
  243. response.code = OpenLayers.Protocol.Response.FAILURE;
  244. response.error = result;
  245. }
  246. } else {
  247. // failure
  248. response.code = OpenLayers.Protocol.Response.FAILURE;
  249. }
  250. options.callback.call(options.scope, response);
  251. }
  252. },
  253. /**
  254. * Method: parseResponse
  255. * Read HTTP response body and return features
  256. *
  257. * Parameters:
  258. * request - {XMLHttpRequest} The request object
  259. * options - {Object} Optional object to pass to format's read
  260. *
  261. * Returns:
  262. * {Object} or {Array({<OpenLayers.Feature.Vector>})} or
  263. * {<OpenLayers.Feature.Vector>}
  264. * An object with a features property, an array of features or a single
  265. * feature.
  266. */
  267. parseResponse: function(request, options) {
  268. var doc = request.responseXML;
  269. if(!doc || !doc.documentElement) {
  270. doc = request.responseText;
  271. }
  272. if(!doc || doc.length <= 0) {
  273. return null;
  274. }
  275. var result = (this.readFormat !== null) ? this.readFormat.read(doc) :
  276. this.format.read(doc, options);
  277. if (!this.featureNS) {
  278. var format = this.readFormat || this.format;
  279. this.featureNS = format.featureNS;
  280. // no need to auto-configure again on subsequent reads
  281. format.autoConfig = false;
  282. if (!this.geometryName) {
  283. this.setGeometryName(format.geometryName);
  284. }
  285. }
  286. return result;
  287. },
  288. /**
  289. * Method: commit
  290. * Given a list of feature, assemble a batch request for update, create,
  291. * and delete transactions. A commit call on the prototype amounts
  292. * to writing a WFS transaction - so the write method on the format
  293. * is used.
  294. *
  295. * Parameters:
  296. * features - {Array(<OpenLayers.Feature.Vector>)}
  297. * options - {Object}
  298. *
  299. * Valid options properties:
  300. * nativeElements - {Array({Object})} Array of objects with information for writing
  301. * out <Native> elements, these objects have vendorId, safeToIgnore and
  302. * value properties. The <Native> element is intended to allow access to
  303. * vendor specific capabilities of any particular web feature server or
  304. * datastore.
  305. *
  306. * Returns:
  307. * {<OpenLayers.Protocol.Response>} A response object with a features
  308. * property containing any insertIds and a priv property referencing
  309. * the XMLHttpRequest object.
  310. */
  311. commit: function(features, options) {
  312. options = OpenLayers.Util.extend({}, options);
  313. OpenLayers.Util.applyDefaults(options, this.options);
  314. var response = new OpenLayers.Protocol.Response({
  315. requestType: "commit",
  316. reqFeatures: features
  317. });
  318. response.priv = OpenLayers.Request.POST({
  319. url: options.url,
  320. headers: options.headers,
  321. data: this.format.write(features, options),
  322. callback: this.createCallback(this.handleCommit, response, options)
  323. });
  324. return response;
  325. },
  326. /**
  327. * Method: handleCommit
  328. * Called when the commit request returns.
  329. *
  330. * Parameters:
  331. * response - {<OpenLayers.Protocol.Response>} The response object to pass
  332. * to the user callback.
  333. * options - {Object} The user options passed to the commit call.
  334. */
  335. handleCommit: function(response, options) {
  336. if(options.callback) {
  337. var request = response.priv;
  338. // ensure that we have an xml doc
  339. var data = request.responseXML;
  340. if(!data || !data.documentElement) {
  341. data = request.responseText;
  342. }
  343. var obj = this.format.read(data) || {};
  344. response.insertIds = obj.insertIds || [];
  345. if (obj.success) {
  346. response.code = OpenLayers.Protocol.Response.SUCCESS;
  347. } else {
  348. response.code = OpenLayers.Protocol.Response.FAILURE;
  349. response.error = obj;
  350. }
  351. options.callback.call(options.scope, response);
  352. }
  353. },
  354. /**
  355. * Method: filterDelete
  356. * Send a request that deletes all features by their filter.
  357. *
  358. * Parameters:
  359. * filter - {<OpenLayers.Filter>} filter
  360. */
  361. filterDelete: function(filter, options) {
  362. options = OpenLayers.Util.extend({}, options);
  363. OpenLayers.Util.applyDefaults(options, this.options);
  364. var response = new OpenLayers.Protocol.Response({
  365. requestType: "commit"
  366. });
  367. var root = this.format.createElementNSPlus("wfs:Transaction", {
  368. attributes: {
  369. service: "WFS",
  370. version: this.version
  371. }
  372. });
  373. var deleteNode = this.format.createElementNSPlus("wfs:Delete", {
  374. attributes: {
  375. typeName: (options.featureNS ? this.featurePrefix + ":" : "") +
  376. options.featureType
  377. }
  378. });
  379. if(options.featureNS) {
  380. deleteNode.setAttribute("xmlns:" + this.featurePrefix, options.featureNS);
  381. }
  382. var filterNode = this.format.writeNode("ogc:Filter", filter);
  383. deleteNode.appendChild(filterNode);
  384. root.appendChild(deleteNode);
  385. var data = OpenLayers.Format.XML.prototype.write.apply(
  386. this.format, [root]
  387. );
  388. return OpenLayers.Request.POST({
  389. url: this.url,
  390. callback : options.callback || function(){},
  391. data: data
  392. });
  393. },
  394. /**
  395. * Method: abort
  396. * Abort an ongoing request, the response object passed to
  397. * this method must come from this protocol (as a result
  398. * of a read, or commit operation).
  399. *
  400. * Parameters:
  401. * response - {<OpenLayers.Protocol.Response>}
  402. */
  403. abort: function(response) {
  404. if (response) {
  405. response.priv.abort();
  406. }
  407. },
  408. CLASS_NAME: "OpenLayers.Protocol.WFS.v1"
  409. });