all files / src/mixin/ RealEarthAnimateVector.ts

34.78% Statements 16/46
0% Branches 0/14
11.11% Functions 1/9
34.09% Lines 15/44
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118                                                                                                                                                                                                             
/**
 * Created by gavorhes on 12/4/2015.
 */
import RealEarthAnimate from './RealEarthAnimate';
import provide from '../util/provide';
import ol = require('custom-ol');
import {LayerVectorRealEarth} from "../layers/LayerRealEarthVector";
import $ = require('jquery');
const nm = provide('mixin');
 
 
/**
 * class mixin to animate RealEarth vector layers
 * @augments RealEarthAnimate
 */
class RealEarthAnimateVector extends RealEarthAnimate {
    _dataCache: Array<Array<Object>|Object>;
    _source: ol.source.Vector;
    _rawTimesLookup: {[s: string]: any};
    _currentIndex: number;
    _olLayer: ol.layer.Vector;
    _lyr: LayerVectorRealEarth;
 
    constructor(layer: LayerVectorRealEarth, loadCallback?: (lyr: LayerVectorRealEarth) => void){
        super(layer, loadCallback);
        this._source = layer.source;
        this._olLayer = layer.olLayer;
        this._lyr = layer;
    }
 
 
    /**
     * Call this after the mixin has been applied
     */
    timeInit() {
        super.timeInit();
        this._rawTimesLookup = {};
        this._dataCache = [];
    }
 
    /**
     * Given the raw time string, add to the arrays to keep track of dates and cache
     * @param {string} inString - input date string
     * @protected
     */
    _loadDates(inString: string): string {
        let rawDte = super._loadDates(inString);
        this._dataCache.push(null);
        this._rawTimesLookup[rawDte] = null;
        return '';
    }
 
    /**
     * @protected
     */
    _loadLatest(): boolean {
        if (super._loadLatest()) {
            this._loadAtTimeIndex.call(this, this._currentIndex);
        }
        return true;
    }
 
    //
    //http://realearth.ssec.wisc.edu/api/image?products=nexrhres_20160108_212500&x=1&y=5&z=4
    //
    //    20160108.205500
    //    http://realearth.ssec.wisc.edu/api/image?products=nexrhres_20160108_205500&x=34&y=46&z=7
 
    /**
     * Load the features at the date index specified
     * @param {number} i the index of the features to be loaded by date
     * @param {boolean} [setAsSource=true] set to false to trigger cache load only
     * @private
     */
    _loadAtTimeIndex(i: number, setAsSource = true) {
        setAsSource = typeof setAsSource == 'boolean' ? setAsSource : true;
        if (this._dataCache[i] != null) {
            this._source.clear();
            this._loadFeatures(this._dataCache[i]);
        } else {
            let __this = this;
            $.get('http://realearth.ssec.wisc.edu:80/api/shapes',
                {products: `${this._products}_${this._rawDateStrings[i]}`},
                function (d) {
                    __this._dataCache[i] = d;
                    __this._rawTimesLookup[__this._rawDateStrings[i]] = d;
                    if (setAsSource) {
                        __this._source.clear();
                        __this._loadFeatures.call(__this, __this._dataCache[i]);
                    }
                }, 'json'
            );
        }
    }
 
    /**
     * helper to load the features at the index specified
     * @param {object} geojObj - the geojson object
     * @private
     */
    _loadFeatures(geojObj) {
        this._source.addFeatures(this._lyr._geoJsonFormat.readFeatures(geojObj,
            {featureProjection: this._lyr._transform.featureProjection, dataProjection: this._lyr._transform.dataProjection}));
    }
 
    setLayerTime(theTime: number): boolean {
        if (super.setLayerTime(theTime)) {
            this._loadAtTimeIndex(this._currentIndex);
        } else {
            this._source.clear();
        }
        return true;
    }
}
 
nm.RealEarthAnimateVector = RealEarthAnimateVector;
export default RealEarthAnimateVector;