【WebGIS实例】(5)通过POST请求进行WFS条件查询

适用于所有符合OGC标准的地图服务器。

示例

<wfs:GetFeature xmlns:wfs="http://www.opengis.net/wfs"
  service="WFS"
  version="1.0.0"
  outputFormat="application/json"
  maxFeatures="10"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.opengis.net/wfs
  http://schemas.opengis.net/wfs/1.0.0/WFS-transaction.xsd">
  <wfs:Query typeName="webgis:guangdong" userecent="true">
    <ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
      <ogc:PropertyIsLike wildCard="*" singleChar="." escape="!">
        <ogc:PropertyName>city</ogc:PropertyName>
        <ogc:Literal>guangzhou</ogc:Literal>
      </ogc:PropertyIsLike>
    </ogc:Filter>
  </wfs:Query>
</wfs:GetFeature>

解释

  • 查询的图层:webgis:guangdong
  • 查询的属性:city
  • 查询的值:guangzhou
  • 输出的格式 outputFormat:"application/json"
  • 输出的要素数量 maxFeatures:"10"

用例:通过axios

// 按属性查询
const dataType = 'application/json'; // 输出的格式,不同服务器可能不同
const layerName = 'webgis:guangdong'; // 查询的图层
const propertyName = 'city'; // 查询的属性
const attr = 'guangzhou'; // 查询的值

function postWFSAttribute() {
    
    
  const data = ''
  + `<wfs:GetFeature xmlns:wfs="http://www.opengis.net/wfs" service="WFS" version="1.0.0" outputFormat="${
      
      dataType}" maxFeatures="100" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-transaction.xsd">`
  + ` <wfs:Query typeName="${
      
      layerName}" userecent="true">`
  + '  <ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">'
  + '  <ogc:PropertyIsLike wildCard="*" singleChar="." escape="!">'
  + `    <ogc:PropertyName>${
      
      propertyName}</ogc:PropertyName>`
  + `    <ogc:Literal>${
      
      attr}</ogc:Literal>`
  + '  </ogc:PropertyIsLike>'
  + '</ogc:Filter>'
  + '</wfs:Query>'
  + '</wfs:GetFeature>';

  return axios({
    
    
    headers: {
    
    
      'Content-Type': 'application/xml',
    },
    method: 'POST',
    url: 'http://localhost/geoserver/webgis/ows',
    data,
    timeout: 10000, // 设置10秒超时
  });
}

参考与拓展

  1. OGC Filter: https://www.ogc.org/standards/filter
  2. GeoServer中使用WFS进行条件查询

猜你喜欢

转载自blog.csdn.net/ReBeX/article/details/128368588