dataAdd.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import {WFS} from 'ol/format';
  2. export default class useWfs{
  3. constructor(feature){
  4. this.feature = feature//传入要增删改的图层数据
  5. }
  6. //添加图层到数据库和服务器
  7. wfsAdd() {
  8. let WFSTSerializer = new WFS();
  9. let featObject = WFSTSerializer.writeTransaction([this.feature],
  10. null, null, {
  11. featureType: 'shop',
  12. featurePrefix: 'build',
  13. featureNS: 'Http://localhost:8084/geoserver/test',
  14. srsName: 'EPSG:3857'
  15. });
  16. //创建请求
  17. let serializer = new XMLSerializer();
  18. let featString = serializer.serializeToString(featObject);
  19. let request = new XMLHttpRequest();
  20. //发送请求
  21. this.wfsSend(request,featString)
  22. }
  23. //删除数据
  24. wfsDelete() {
  25. let WFSTSerializer = new WFS();
  26. let featObject = WFSTSerializer.writeTransaction(null,
  27. null, [this.feature], {
  28. featureType: 'shop',//图层名
  29. featurePrefix: 'build',
  30. featureNS: 'Http://localhost:8084/geoserver/test',//命名空间uri
  31. srsName: 'EPSG:3857'
  32. });
  33. //创建请求
  34. let serializer = new XMLSerializer();
  35. let featString = serializer.serializeToString(featObject);
  36. let request = new XMLHttpRequest();
  37. this.wfsSend(request,featString)
  38. }
  39. //修改数据
  40. wfsModify() {
  41. let WFSTSerializer = new WFS();
  42. let featObject = WFSTSerializer.writeTransaction(null,
  43. [this.feature], null, {
  44. featureType: 'shop',
  45. featurePrefix: 'build',
  46. featureNS: 'Http://localhost:8084/geoserver/test',
  47. srsName: 'EPSG:3857'
  48. });
  49. //创建请求
  50. let serializer = new XMLSerializer();
  51. let featString = serializer.serializeToString(featObject);
  52. let request = new XMLHttpRequest();
  53. this.wfsSend(request, featString)
  54. }
  55. //发送数据请求
  56. wfsSend(request, featString) {
  57. console.time('aaa')
  58. request.open('POST', 'http://localhost:8084/geoserver/test/ows?');
  59. request.setRequestHeader('Content-Type', 'text/xml');
  60. request.send(featString);
  61. request.onreadystatechange = function () {
  62. if (request.readyState === 4 && request.status === 200) {
  63. console.log(request.responseText);
  64. }
  65. }
  66. console.timeEnd('aaa')
  67. }
  68. }