派生自 wuyushui/SewerAndRainNetwork

徐旺旺
2021-05-19 c065531c87e7dc199c7fc4d35e4f6fbedf26167d
src/components/helpers/WfsHelper.js
@@ -1,37 +1,106 @@
/**
 * 加载WMS,拼接FILTER,LAYERS参数等
 */
import { WFS_URL } from '../../conf/Constants'
import { lrtrim } from '../../utils/utils'
function WfsHelper () {
  this.filters = []
  this.typeNames = []
  this.url = 'http://xearth.cn:6289/server/ogcserver/PipeLine/wfs'
  this.url = WFS_URL
  this.page = 1
  this.pageSize = 10
  this.params = {
    REQUEST: 'getfeature',
    OUTPUTFORMAT: 'JSON',
    maxFeatures: 20000,
    OUTPUTFORMAT: 'application/json',
    maxFeatures: 10,
    version: '1.0.0'
  }
  this.setTypeName = (typeName) => {
    if (typeof typeName === 'string' || typeName instanceof String) {
      const comma = typeName.indexOf(',')
      if (comma >= 0) {
        const typeNameArr = typeName.split(',')
        for (let i = 0; i < typeNameArr.length; i++) {
          this.addTypeName(lrtrim(typeNameArr[i]))
        }
      }
    } else if (Array.isArray(typeName)) {
      this.typeNames = typeName
    }
  }
  this.setFilter = (filter) => {
    console.log(filter)
    if (typeof filter === 'string' || filter instanceof String) {
      const eq = filter.indexOf('=')
      const lk = filter.indexOf('like')
      if (eq >= 0) {
        const filterArr = filter.split('=')
        this.addEquals(lrtrim(filterArr[0]), lrtrim(filterArr[1]))
      }
      if (lk >= 0) {
        const filterArr = filter.split('like')
        this.addLike(lrtrim(filterArr[0]), lrtrim(filterArr[1]))
      }
    } else if (Array.isArray(filter)) {
      this.filters = filter
    }
  }
  this.clearFilter = () => {
    this.filters = []
  }
  this.addTypeName = (typeName) => {
    this.typeNames.push(typeName)
  }
  this.appendEquals = (property, literal) => {
    var filter = '<PropertyIsEqualTo><PropertyName>' + property + '</PropertyName><Literal>' + literal + '</Literal></PropertyIsEqualTo>'
  this.addEquals = (property, equals) => {
    // var filter = '<PropertyIsEqualTo><PropertyName>' + property + '</PropertyName><Literal>' + literal + '</Literal></PropertyIsEqualTo>'
    var filter = property + '=' + equals
    this.filters.push(filter)
  }
  this.addLike = (property, literal) => {
    // if (property && literal) {
    // var filter = '<PropertyIsLike><PropertyName>' + property + '</PropertyName><Literal>*' + literal + '*</Literal></PropertyIsLike>'
    // this.filters.push(filter)
    // }
    var filter = property + ' like \'%' + literal + '%\''
    this.filters.push(filter)
  }
  /**
   * 得到filter参数值
   * @returns {string|null}
   */
  this.getFilterParams = () => {
    var head = '<Filter xmlns="http://www.opengis.net/ogc">'
    /* var head = '<Filter xmlns="http://www.opengis.net/ogc">'
    var end = '</Filter>'
    var filter = ''
    if (this.filters.length > 0) {
      for (var i = 0; i < this.filters.length; i++) {
        filter += this.filters[i]
      }
      // 当条件数 > 1时,需要用and标签包裹
      if (this.filters.length > 1) {
        return ('FILTER=' + head + '<And>' + filter + '</And>' + end)
      }
      return ('FILTER=' + head + filter + end)
    } */
    var filter = ''
    if (this.filters.length > 0) {
      filter = 'CQL_FILTER='
      for (var i = 0; i < this.filters.length; i++) {
        filter += this.filters[i]
        if (i !== this.filters.length - 1) {
          filter += ' AND '
        }
      }
      return filter
    }
    return null
    return filter
  }
  this.getUrlParams = () => {
@@ -46,7 +115,8 @@
    if (filterParam) {
      params += '&' + filterParam
    }
    return encodeURI(params)
    // return encodeURI(params)
    return params
  }
  this.getUrl = () => {
@@ -58,6 +128,20 @@
    }
    return url + this.getUrlParams()
  }
  this.setPage = (page) => {
    const startIndex = page * this.pageSize
    this.params.startIndex = startIndex
    this.page = page
  }
  this.setPageSize = (pageSize) => {
    this.pageSize = pageSize
  }
  this.setMaxFeatures = (maxFeatures) => {
    this.params.maxFeatures = maxFeatures
  }
}
export default WfsHelper