ol之filter过滤获取feature

ol之filter过滤获取feature

使用场景:

通过geoserver的省界服务查询对应省的要素,定位到对应位置

代码:       sfCode 为省份编码 

queryLocation (sfCode) {
      let featureRequest = new ol.format.WFS().writeGetFeature({
        ssName: 'EPSG:3857',
        featureNS: 'gistone', // 命名空间 URI
        featurePrefix: 'njs', // 工作区名称
        featureTypes: ['sj'], // 查询图层,可以同一个工作区下多个图层,逗号隔开
        outputFormat: 'application/json',
        filter: ol.format.filter.like('code', sfCode)
      })
      fetch('http://localhost:8080/geoserver/njs/wfs', {
        method: 'POST',
        body: new XMLSerializer().serializeToString(featureRequest)
      }).then((res) => {
        return res.json()
      }).then(res => {
        if (res.totalFeatures !== 0) {
          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)
        }
      })
    },

注意事项:

1.在查询字段设置错误时,可能会报错:   json格式不正确 <

2.地图服务中字段名和查询字段中的数据不能为中文。

3.‘http://localhost:8080/geoserver/njs/wfs’中njs为工作空间名称

 

 

 

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

 

posted @ 2020-04-17 16:15  莫小龙  阅读(1458)  评论(0编辑  收藏  举报