Source: map/layer/DwsLayer/DwsLayer.js

import { MeasurementsLayer } from "../MeasurementsLayer/MeasurementsLayer.js";
import { DwsLoader, DWS_DEFAULT_URL } from "../../../data/DwsLoader.js";
import { DwsLayerSchema } from "./DwsLayer.schema.js";

export { DwsLayer }

/**
 * Layer for displaying Data from the DWS in the viewer
 * 
 * @author rhess <robin.hess@awi.de>
 * @memberof vef.map.layer
 */
class DwsLayer extends MeasurementsLayer {

    /**
     * Set all properties for the layer based on the options object.
     * 
     * @param {object} config 
     * @param {object} cache 
     * @param {string} id 
     */
    constructor(config, cache, id) {
        // calling parent constructor
        super(config, cache, id);

        // init default values and define getters and setters
        this.setSchema_(DwsLayerSchema.getSchema());
        this.defaults.defaultYAxis = this.columnData;
        this.defaults.title = this.columnData;
        this.dwsLoader_ = new DwsLoader(this.url);

        this.fetchMetadata_();
    }

    fetchMetadata_() {
        const url = this.url || DWS_DEFAULT_URL;
        fetch(url + "/search", {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                hits: 1,
                query: 'metadata.code:("' + this.columnData + '")',
                sorts: [],
                indices: [],
                offset: 0,
                queryFacets: {},
                types: []
            })
        })
            .then(respone => respone.json())
            .then(json => {
                if (json.records && json.records.length) {
                    const record = json.records[0];

                    this.abstract = record.description || "";
                    this.sensorMetadata = Object.assign(this.sensorMetadata, record)
                }
            });
    }

    /**
     * Set the colorscale for the layer
     * 
     * @param colorScale 
     */
    setColorScale(colorScale) {
        this.colorScale = colorScale;

        if (this.activeGeoJSON_) {
            this.activeGeoJSON_.features.forEach(feature => {
                for (let property in feature.properties) {
                    if ((property == this.columnData) || (property.split(" ")[0] == this.columnData)) {
                        const value = feature.properties[property];
                        const style = this.getColorOptions_(value);
                        feature.properties.color = style.color;
                    }
                }
            });
            this.activeLayer_.updateStyle();
        }
    }


    /**
     * Loads data of the dws filtered by time
     * @private
     */
    applyFilter_() {
        let beginDate, endDate;

        // Time column name checks are ignored and only the first time filter is used
        for (let i = 0; i < this.filter.length; ++i) {
            if ((this.filter[i].type == "time") && (this.filter[i].values.length > 0)) {
                const values = this.filter[i].values[0];
                if ((values.length == 3) && (values[0] == "bt")) {
                    beginDate = values[1].substr(0, 19);
                    endDate = values[2].substr(0, 19);
                    break;
                }
            }
        }

        // fallback: today - 30 days
        if (!beginDate || !endDate) {
            const tempDate = new Date();
            endDate = tempDate.toISOString().substr(0, 19);
            tempDate.setMonth(tempDate.getMonth() - 1);
            beginDate = tempDate.toISOString().substr(0, 19);
        }

        return new Promise(resolve => {
            this.dwsLoader_.data([this.columnData, this.columnLatitude, this.columnLongitude], beginDate, endDate, this.limit || 10000, this.aggregate).then(df => {
                this.dataFrameOriginal = df;
                this.dataFrame = df;
                resolve(df);
            });
        });
    }

}