WFSController.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * WFSController.js
  3. *
  4. * @author realor
  5. */
  6. import { Controller } from './Controller.js'
  7. import { GMLLoader } from '../io/gis/GMLLoader.js'
  8. import { ObjectUtils } from '../utils/ObjectUtils.js'
  9. import { GeometryMerger } from '../builders/GeometryMerger.js'
  10. import { ObjectBuilder } from '../builders/ObjectBuilder.js'
  11. import { GeoJSONLoader } from '../io/gis/GeoJSONLoader.js'
  12. import * as THREE from '../lib/three.module.js'
  13. class WFSController extends Controller {
  14. constructor(object, name) {
  15. super(object, name)
  16. this.url = 'https://your_server.com/wfs'
  17. this.username = 'username'
  18. this.password = 'password'
  19. this.layer = 'layer'
  20. this.format = 'GeoJSON'
  21. this.bbox = ''
  22. this.cqlFilter = ''
  23. this.count = 0
  24. this.srsName = ''
  25. this.mergeGeometries = false
  26. this.origin = new THREE.Vector3(420878, 4582247, 0)
  27. this.autoStart = false
  28. this._onLoad = this.onLoad.bind(this)
  29. this._onProgress = this.onProgress.bind(this)
  30. this._onError = this.onError.bind(this)
  31. }
  32. onStart() {
  33. this.getFeature()
  34. }
  35. onStop() {}
  36. onLoad(featureGroup) {
  37. console.info('Feature ' + this.layer + ' loaded.')
  38. const oldFeatureGroup = this.object.getObjectByName('features')
  39. if (oldFeatureGroup) {
  40. ObjectUtils.dispose(oldFeatureGroup)
  41. this.object.remove(oldFeatureGroup)
  42. }
  43. featureGroup.updateMatrix()
  44. if (this.mergeGeometries) {
  45. const mergeGroup = new THREE.Group()
  46. mergeGroup.builder = new GeometryMerger()
  47. mergeGroup.add(featureGroup)
  48. ObjectBuilder.build(mergeGroup)
  49. featureGroup = mergeGroup
  50. featureGroup.updateMatrix()
  51. }
  52. this.object.add(featureGroup)
  53. featureGroup.name = 'features'
  54. if (featureGroup.userData.export === undefined) {
  55. featureGroup.userData.export = {}
  56. featureGroup.userData.export.export = false
  57. }
  58. featureGroup.userData.export.exportChildren = false
  59. this.application.notifyObjectsChanged(this.object, this, 'structureChanged')
  60. }
  61. onProgress() {}
  62. onError() {}
  63. getFeature() {
  64. let url = this.url
  65. if (url.trim().length === 0) return
  66. if (url[0] === '/') {
  67. url = document.location.protocol + '//' + document.location.host + url
  68. }
  69. let layer = this.layer
  70. let format = this.format || 'GeoJSON'
  71. let loader
  72. if (format === 'GML') {
  73. loader = new GMLLoader()
  74. } else {
  75. loader = new GeoJSONLoader()
  76. }
  77. if (url.indexOf('?') === -1) {
  78. url += '?'
  79. } else {
  80. url += '&'
  81. }
  82. url += 'service=wfs&version=2.0.0&request=GetFeature&outputFormat=' + loader.mimeType + '&typeName=' + layer
  83. const count = this.count
  84. if (count > 0) {
  85. url += '&count=' + count
  86. }
  87. const bbox = this.bbox
  88. if (bbox && bbox.length > 0) {
  89. url += '&bbox=' + bbox
  90. }
  91. const cqlFilter = this.cqlFilter
  92. if (cqlFilter && cqlFilter.length > 0) {
  93. url += '&CQL_FILTER=' + cqlFilter
  94. }
  95. const srsName = this.srsName
  96. if (srsName && srsName.length > 0) {
  97. url += '&srsName=' + srsName
  98. }
  99. loader.options = {
  100. name: layer || 'wfs',
  101. username: this.username,
  102. password: this.password,
  103. origin: this.origin,
  104. representation: this.object.getObjectByName('representation')
  105. }
  106. loader.load(url, this._onLoad, this._onProgress, this._onError)
  107. }
  108. static getDescription() {
  109. return 'gis|controller.' + this.name
  110. }
  111. }
  112. Controller.addClass(WFSController)
  113. export { WFSController }