简体   繁体   English

使用GIF作为OpenLayers Map上ImageWMS的来源

[英]Use GIF as a source of ImageWMS on OpenLayers Map

OpenLayers version 2 seemed to display gifs without any problem. OpenLayers版本2似乎可以毫无问题地显示gif。

OpenLayers: 4.6.4 (the version that I am using now) only displays the first frame of the gif: https://prnt.sc/i5xrun OpenLayers:4.6.4(我现在使用的版本)仅显示gif的第一帧: https : //prnt.sc/i5xrun

Andreas Hocevar suggested a solution to this problem ( https://github.com/openlayers/openlayers/issues/4133 ): Andreas Hocevar提出了针对此问题的解决方案( https://github.com/openlayers/openlayers/issues/4133 ):

Use a "custom imageLoadFunction for the image source. Instead of returning the image itself, return a canvas with a frame of the gif, which you can get using third party libraries (eg http://themadcreator.github.io/gifler/docs.html#animator::createBufferCanvas() )." 对图像源使用“自定义imageLoadFunction。不返回图像本身,而是返回带有gif框架的画布,您可以使用第三方库(例如http://themadcreator.github.io/gifler/docs)获得该框架。 .html#animator :: createBufferCanvas( )。”。

I tried to use his approach but didn't manage to display the canvas on the map. 我尝试使用他的方法,但没有设法在地图上显示画布。 Could someone either advise how to show gif on the map or how to display canvas holding that gif? 有人可以建议如何在地图上显示gif或如何显示持有该gif的画布吗?

Here's the code that I used: 这是我使用的代码:

<script src="/js/gifler.min.js"></script>

...

var ImageSource = new ol.source.ImageWMS({
  url: 'http://weather-services.telventdtn.com/basic/wms_v1/wms.wsgi?',
  params: {
    'REQUEST'    : 'GetAnimationFile',
    'VERSION'    : '1.3.0',
    'SERVICE'    : 'WMS',
    'STYLES'     : '',
    'LAYERS'     : 'RADAR_US_CURRENT',
    'CRS'        : 'EPSG:900913',
    'BBOX'       : '-16730536.751059378,2397065.207023127,-3629841.5992064495,8198941.401981145',
    'FORMAT'     : 'image/gif',
    'WIDTH'      : '2678',
    'HEIGHT'     : '1186',
    'FRAMEDELAY' : '20',
    'TRANSPARENT': 'TRUE'
  },
  imageLoadFunction: function (image, src) {

    var client = new XMLHttpRequest();
    client.open('GET', src, true);
    client.setRequestHeader('Authorization', "Basic " + btoa("login:password"));
    client.responseType = "arraybuffer";

    client.onload = function () {

        var byteArray = new Uint8Array(this.response);
        var blob = new Blob([byteArray], {type: "image/png"});
        var urlCreator = window.URL || window.webkitURL;
        var imageUrl = urlCreator.createObjectURL(blob);

        gifler(imageUrl)
            .get()
            .then(function(animator) {
                var BufferCanvas = animator.constructor.createBufferCanvas(animator._frames[0], animator.width, animator.height);
                animator.animateInCanvas(BufferCanvas);
                image.setImage(BufferCanvas);
            });
    };

    client.send();
  }
});

var Image = new ol.layer.Image({
  name: 'precip_layer',
  opacity: 0.8,
  zIndex: 1,
  source: ImageSource
});

The solution is in the new release of OpenLayers v4.6.5. 该解决方案在新版本的OpenLayers v4.6.5中。 Below is the example from their official website of how the animated WMS layer can be achieved: 以下是其官方网站上如何实现动画WMS图层的示例:

https://openlayers.org/en/latest/examples/wms-time.html https://openlayers.org/en/latest/examples/wms-time.html

I had some success animating gif downloaded through imageWMS (used ncWMS for this). 我在通过imageWMS(为此使用ncWMS)下载的gif动画上取得了一些成功。

  1. create an ImageWMS source image layer to get the animated gif from ncWMS or other WMS service that provides animated gifs. 创建一个ImageWMS源图像层,以从ncWMS或其他提供动画gif的WMS服务中获取动画gif。
  2. Then I would use the gifuct-js.js library to unpack the gif in the ImageWMS "imageLoadFunction". 然后,我将使用gifuct-js.js库在ImageWMS“ imageLoadFunction”中解压缩gif。
  3. I then have another ImageLayer which uses an ImageCanvasSource, in which I have the canvasFunction create the canvas, which I give to the gif library functions to render on top of. 然后,我有另一个使用ImageCanvasSource的ImageLayer,在其中我用canvas函数创建画布,然后将其提供给gif库函数以在其上进行渲染。

The net result is one layer to downloade the imageWMS and another layer to display it as an ImageCanvasSource. 最终结果是一层下载imageWMS,另一层将其显示为ImageCanvasSource。 You may have to do some projection alignment, but now you can have controls to animate the gif in another layer. 您可能需要进行一些投影对齐,但是现在您可以具有将另一层gif动画的控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM