Snippets

Piotr Szrajber Geospatial Portal - Animate topmost time-aware layer

Created by Piotr Szrajber
/**
 * Animate topmost map layer if it is time aware layer, for example from http://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r-t.cgi
 * 2017-03-02 Piotr Szrajber <piotr.szrajber@hexagongeospatial.com>
 *
 */
var T = Intergraph.WebSolutions.Core.WebClient.Platform.Common.TemporalUtils,
    mapState = $mapStateManager.findMapState("map"),
    mapControl = mapState.get_mapControl(),
    mapLayer = mapControl.get_mapLayers().slice(-1)[0],
    config = mapLayer.get_config(),
    legend = mapState.get_legend().get_flatLegend(),
    legendItem = legend.find(function(legendItem) {
        return legendItem.get_priority() === config.get_priority();
    }),
    definition = legendItem.get_definition(),
    dimension = definition.get_dimensions().time,
    extent = dimension.extent[0],
    timeFormat = config.get_timeFmt(),
    dateMin = T.parseDateTimeStr(extent.min),
    dateMax = T.parseDateTimeStr(extent.max),
    time = new Date(dateMin),
    step = T.unitPerResolution[extent.res] || 300000, // make sure that the step is defined and the resolution is defined for this resolution
    preloadSize = 100,
    interval = null;



function preloadNext() {
    var preload = Array.apply(null, {
        length: preloadSize
    }).map(Number.call, Number).map(function(x) {
        return new Date(x * step + time.getTime())
    }).map(getUrl);
    preload.forEach(function(x) {
        var i = new Image();
        i.src = x;
    })
}

function next() {
    time.setTime(dateMin.getTime() + step);
    mapLayer.updateTimeAndRender(time, timeFormat);
}

function getFormattedTime(t) {
    return mapLayer.get_config().getFormattedTime.call({
        _time: t,
        _timeFmt: timeFormat,
        get_mapService: function() {
            return config.get_mapService()
        }
    });
}

function getUrl(t) {
    return mapLayer.getURL(mapState.get_mapRange()).replace(/(&TIME=)[^$&]*/, '$1' + getFormattedTime(t));
}

function startAnimation() {
    interval = setInterval(next, 1500);
}

function stopAnimation() {
    interval && clearInterval(interval);
    interval = null;
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.