Openlayers-WMS层:如何从特定层获取时间维度

时间:2019-02-04 22:18:42

标签: openlayers wms openlayers-5

我正在尝试加载WMS层。使用当前的ISO-8601时间显示图层,但我还希望能够设置不同的日期/时间。

为了使用在一定范围内的日期/时间,我需要通过例如为此特定层的GetCapabilities并将检索到的值放在数组中。从那时起,我可以使用updateParams设置/更新日期/时间。

我该怎么做?

例如:

var WMS_DWD = new ol.layer.Image({
    name: 'Radar D',
    title: "Radar D",
    source: new ol.source.ImageWMS({
        ratio: 1,
        url: 'https://maps.dwd.de/geoserver/ows?service=wms&version=1.3.0&request=GetCapabilities',
        params: {
            'FORMAT': "image/png",
            'VERSION': '1.1.1',
            'LAYERS': 'dwd:RX-Produkt',
             time   : '2019-02-03T15:35:00.000Z',
            "exceptions": 'application/vnd.ogc.se_inimage'
        }
    })
});

当我在浏览器中查看URL时,生成的XML具有TAG“” .....在这种情况下为“ dwd:RX-Produkt”层。

在此TAG中,显示了许多可用的日期/时间。这些“日期/时间”我需要放入数组中。

希望你能帮助我!

编辑:在更新的代码下方(对oicgasser表示感谢)

WMSlyr = new ol.layer.Tile({
        name: 'myLYR',
        title: "myLYR",
        preload: Infinity,
        source: new ol.source.TileWMS({
            url: 'https://ogcie.iblsoft.com/observations',
            params: {
               'FORMAT': "image/png",
               'VERSION': '1.3.0'
            }
        }),
        urlCapabilities: 'https://ogcie.iblsoft.com/observations?REQUEST=GetCapabilities&SERVICE=WMS&VERSION=1.3.0'
    });

LYR = 'metar';

url = 'https://ogcie.iblsoft.com/observations?REQUEST=GetCapabilities&SERVICE=WMS&VERSION=1.3.0';

window['timeArray'] = [];

var parser = new ol.format.WMSCapabilities();

Time = '2019-02-15T17:00:00.000Z';

fetch(url).then(function (response) {
       return response.text();
    }).then(function (text) {
    var capabilities = parser.read(text);
    var currentProj = map.getView().getProjection().getCode();
    var crs;

    // the parser can only read the list of projections from the WMS 1.3.0 responses.
    // For previous version the current projection wil be used as the default one.
    // If the WMS (< 1.3.0) does not support the default projection the layer will not load.
    if (capabilities.version === '1.3.0'){
        crs = capabilities.Capability.Layer.CRS; // supported EPSG-numbers
    }
    else {
        crs = [currentProj];
    }

    console.log('Projection WMS: ' + crs);

    var layers = capabilities.Capability.Layer.Layer;

    var AllLayerNames = [];

    if (layers.length > 0 && crs.indexOf(currentProj) > -1){
        for (var i = 0; i < layers.length; i += 1){
            if ( _.isArray(layers[i]['Dimension']) && layers[i]['Dimension'].length > 0 ){
                console.log(layers[i].Name);

                AllLayerNames.push(layers[i].Name);

                console.log(layers[i]['Dimension'][0]['values']);
            }
        }
    }


    var LYRnr =  (_.invert(AllLayerNames))[LYR];

    window['timeArray'] = layers[LYRnr]['Dimension'][0]['values'].split(',');

    var formats = capabilities.Capability.Request.GetMap.Format;
    var allformats = [];
    for (var i = 0; i < formats.length; i += 1){
        allformats.push(formats[i]);
    }
    console.log(allformats); // array with all the supported output-formats

    if (window['timeArray'].indexOf(Time.substr(0,16))) { // use part of string because some WMS dimensions use milliseconds and some do not
        WMSlyr.getSource().updateParams({'LAYERS': LYR, 'TIME': Time});
    }
});

剩下的问题是:

1 个答案:

答案 0 :(得分:1)

Openlayers中有一个WMS GetCapabilities解析器。

以下是有关如何使用ol 4.6.5实现此目标的代码示例:

var parser = new ol.format.WMSCapabilities();
fetch("https://maps.dwd.de/geoserver/ows?service=wms&version=1.3.0&request=GetCapabilities")
  .then(function(response) {
    return response.text();
  })
  .then(function(text) {
    var result = parser.read(text);
    var layers = result.Capability.Layer.Layer;
    console.log(layers);
  })
});

然后,您需要查找您对数组感兴趣的图层并分析Dimension字段。确保您递归查看所有子图层,因为它们大部分是嵌套的。

以下是带有您的WMS GetCapabilities的Codepen: https://codepen.io/loicgasser/pen/BMdLYX