ol之要素查询

ol之要素查询

要素查询可以直接从geoserver上的矢量数据服务中查询符合条件的要素。

代码:

let featureRequest = new ol.format.WFS().writeGetFeature({
        srsName: 'EPSG:3857',
        featureNS: 'gistone', // 命名空间 URI
        featurePrefix: 'gzq', // 工作区名称
        featureTypes: ['sj'], // 查询图层,可以同一个工作区下多个图层,逗号隔开
        outputFormat: 'application/json',
        filter: ol.format.filter.like('code', sfCode)
      })
      fetch('http://localhost:8080/geoserver/gzq/wfs', {
        method: 'POST',
        body: new XMLSerializer().serializeToString(featureRequest)
      }).then((res) => {
        return res.json()
      }).then(res => {
        if (res.totalFeatures !== 0) {
          console.log(res)
          let vectorSource = new ol.source.Vector()
          let features = new ol.format.GeoJSON().readFeatures(res)
          vectorSource.addFeatures(features)
          let extent = ol.proj.transformExtent(vectorSource.getExtent(), 'EPSG:4326', 'EPSG:3857')
          this.view.fit(extent)
        }
      })

其中查询条件主要通过 filter 编写。

例子:

1.筛选字段信息

filter: ol.format.filter.EqualTo('NAME', '北京')

2.模糊查询

filter: ol.format.filter.like('code', sfCode)

3.空间筛选(筛选包含指定点的要素)

filter: ol.format.filter.contains('the_geom', new ol.geom.Point(coordinate, 'XY'))

注意:

1.坐标系错误时将查询不到

2.当图层数据中存在中文乱码时,会导致查询不到。可在geoserver的数据存储中配置字符集为GBK解决。

钻研不易,转载请注明出处、、、、、、

原文地址:https://www.cnblogs.com/s313139232/p/12755397.html