WFS服务的请求

发布时间 2023-08-02 11:35:27作者: 雨崩

 

参考geosever的官网

https://docs.geoserver.org/latest/en/user/services/wfs/reference.html

 

GetFeature操作,获取要素

 ①获取已知图层的坐标要素,

getFeatures()
the source.getFeatures() method will only retrieve the features that have been loaded into the vector source at that moment,
which are typically the features that are within the current visible extent (viewport) of the map.
// 创建一个空数组来存储所有坐标数据
var coordinatesArray = [];

// 获取源中的所有要素(features)
var features =图层名.getSource().getFeatures();

// 遍历每个要素,提取坐标数据并存储到数组变量中
features.forEach(function (feature) {
    var geometry = feature.getGeometry();
    if (geometry) {
        var coordinates = geometry.getCoordinates();
        coordinatesArray.push(coordinates);
    }
});

// 此时,coordinatesArray 中包含了所有要素的坐标数据
console.log(coordinatesArray);

 

②返回发布图层的所有要素

// 空数组存储
var coordinatesArray = [];

// 获取发布图层的url请求
var getFeatureUrl = 'http://xxx/geoserver/namespace/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=&outputFormat=application/json&srsname=EPSG:4326';

// 使用fetch API发起请求
fetch(getFeatureUrl)
  .then(function(response) {
    return response.json();
  })
  .then(function(jsonData) {
    // 解析geojson回应,提取坐标
    var features = new ol.format.GeoJSON().readFeatures(jsonData);
    features.forEach(function(feature) {
      var geometry = feature.getGeometry();
      if (geometry) {
        var coordinates = geometry.getCoordinates();
        coordinatesArray.push(coordinates);
      }
    });

    // 打印
    console.log(coordinatesArray);
  });