all files / src/layers/ LayerItsInventory.ts

14.29% Statements 17/119
0% Branches 0/64
20% Functions 2/10
13.79% Lines 16/116
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
/**
 * Created by gavorhes on 12/8/2015.
 */
 
import LayerBaseVectorGeoJson from './LayerBaseVectorGeoJson';
import mapPopup from '../olHelpers/mapPopup';
import provide from '../util/provide';
import ol = require('custom-ol');
import $ = require('jquery');
import {proj4326, proj3857} from '../olHelpers/projections'
 
let nm = provide('layers');
 
 
function checkStyleNumber(itsIcon, itsLineStyle, itsIconConfig, itsLineConfig) {
    "use strict";
 
    //make sure one and only one configuration is defined;
    let configCount = 0;
    if (typeof itsIcon == 'string') {
        configCount++;
    }
 
    if (typeof itsLineStyle == 'object') {
        itsLineStyle.width = typeof itsLineStyle.width == 'number' ? itsLineStyle.width : 5;
        itsLineStyle.color = typeof itsLineStyle.color == 'string' ? itsLineStyle.color : 'red';
        configCount++;
    }
 
    if (typeof itsIconConfig == 'object') {
        itsIconConfig.defaultName = itsIconConfig.defaultName || 'Other';
 
        if (typeof itsIconConfig.iconArray == 'undefined') {
            itsIconConfig.iconArray = [];
        }
 
        configCount++;
    }
 
    if (typeof itsLineConfig == 'object') {
        itsLineConfig.defaultName = itsLineConfig.defaultName || 'Other';
        itsLineConfig.defaultWidth = itsLineConfig.defaultWidth || 5;
        itsLineConfig.defaultColor = itsLineConfig.defaultColor || 'red';
 
 
        if (typeof itsLineConfig.lineArray == 'undefined') {
            itsLineConfig.lineArray = [];
        }
 
        // set the width if not defined
        for (let i = 0; i < itsLineConfig.lineArray.length; i++) {
            if (itsLineConfig.lineArray[i].length == 3) {
                itsLineConfig.lineArray[i].push(5);
            }
        }
 
        configCount++;
    }
 
    if (configCount > 1) {
        throw 'Only one style config can be defined';
    }
}
 
/**
 *
 * @param {string} [itsIcon=undefined] the ITS device type icon image see http://transportal.cee.wisc.edu/its/inventory/icons/
 *
 * @param {object} [itsLineStyle=undefined] A single line style
 * @param {string} itsLineStyle.color the line color as rgb or hex
 * @param {number} [itsLineStyle.width=5] the line width
 *
 * @param {object} [itsIconConfig=undefined] The icon subtype configuration
 * @param {string} itsIconConfig.prop The property used to define icon attribute symbolization
 * @param {string} itsIconConfig.defaultName The default name to be used if no other match is found
 * @param {string} itsIconConfig.defaultIcon The default icon to be used for no other matches
 * @param {object} [itsIconConfig.iconArray=[]] an array, items with format [property, name, img]
 *
 * @param {object} [itsLineConfig=undefined] The property used to define icon attribute symbolization
 * @param {string} itsLineConfig.prop The property used to define icon attribute symbolization
 * @param {string} [itsLineConfig.defaultName=Other] The default name to be used if no other match is found
 * @param {string} [itsLineConfig.defaultColor=red] The default line color to be used for no other matches
 * @param {number} [itsLineConfig.defaultWidth=5] The default line width to be used for no other matches
 * @param {object} [itsLineConfig.lineArray=[]] an array, items with format [property, name, color, optional width]
 * @returns {*} undefined, style, or style function
 */
function defineStyle(itsIcon, itsLineStyle, itsIconConfig, itsLineConfig) : ol.style.Style|Array<ol.style.Style>|ol.StyleFunction{
    "use strict";
    checkStyleNumber(itsIcon, itsLineStyle, itsIconConfig, itsLineConfig);
 
    let _iconUrlRoot = 'http://transportal.cee.wisc.edu/its/inventory/icons/';
 
    if (itsIcon) {
        return new ol.style.Style({
            image: new ol.style.Icon(
                {
                    src: _iconUrlRoot + itsIcon,
                    crossOrigin: 'anonymous'
                }
            )
        });
    } else if (itsLineStyle) {
        return new ol.style.Style({
            stroke: new ol.style.Stroke({
                color: itsLineStyle.color,
                width: itsLineStyle.width
            })
        });
    } else if (itsIconConfig) {
        return function (feature: ol.Feature) {
            let symbolProp = feature.getProperties()[itsIconConfig.prop];
            let iconUrl = _iconUrlRoot + itsIconConfig.defaultIcon;
 
            for (let i = 0; i < itsIconConfig.iconArray.length; i++) {
                let thisProp = itsIconConfig.iconArray[i];
 
                if (symbolProp.trim().toLocaleLowerCase() == thisProp[0].trim().toLocaleLowerCase()) {
                    iconUrl = _iconUrlRoot + thisProp[2];
                    break;
                }
            }
 
            return [new ol.style.Style({
                image: new ol.style.Icon(
                    {
                        src: iconUrl,
                        crossOrigin: 'anonymous'
                    }
                )
            })];
        };
    } else if (itsLineConfig) {
        return function (feature: ol.Feature) {
            let symbolProp = feature.getProperties()[itsLineConfig.prop];
            let colr = itsLineConfig.defaultColor || 'red';
            let width = itsLineConfig.defaultWidth || 5;
 
            for (let i = 0; i < itsLineConfig.lineArray.length; i++) {
                let thisProp = itsLineConfig.lineArray[i];
 
                if (symbolProp.trim().toLocaleLowerCase() == thisProp[0].trim().toLocaleLowerCase()) {
                    colr = thisProp[2];
                    width = thisProp[3];
                    break;
                }
            }
 
            return [new ol.style.Style({
                stroke: new ol.style.Stroke({
                    color: colr,
                    width: width
                })
            })];
        };
    } else {
        return undefined;
    }
}
 
/**
 *
 * @param {string} [itsIcon=undefined] the ITS device type icon image see http://transportal.cee.wisc.edu/its/inventory/icons/
 *
 * @param {object} [itsLineStyle=undefined] A single line style
 * @param {string} itsLineStyle.color the line color as rgb or hex
 * @param {number} [itsLineStyle.width=5] the line width
 *
 * @param {object} [itsIconConfig=undefined] The icon subtype configuration
 * @param {string} itsIconConfig.prop The property used to define icon attribute symbolization
 * @param {string} itsIconConfig.defaultName The default name to be used if no other match is found
 * @param {string} itsIconConfig.defaultIcon The default icon to be used for no other matches
 * @param {object} [itsIconConfig.iconArray=[]] an array, items with format [property, name, img]
 *
 * @param {object} [itsLineConfig=undefined] The property used to define icon attribute symbolization
 * @param {string} itsLineConfig.prop The property used to define icon attribute symbolization
 * @param {string} [itsLineConfig.defaultName=Other] The default name to be used if no other match is found
 * @param {string} [itsLineConfig.defaultColor=red] The default line color to be used for no other matches
 * @param {number} [itsLineConfig.defaultWidth=5] The default line width to be used for no other matches
 * @param {object} [itsLineConfig.lineArray=[]] an array, items with format [property, name, color, optional width]
 * @returns {string} html to be added to the legend
 */
function defineLegend(itsIcon, itsLineStyle, itsIconConfig, itsLineConfig) {
    "use strict";
 
    let iconHeight = 17;
 
    checkStyleNumber(itsIcon, itsLineStyle, itsIconConfig, itsLineConfig);
 
    let _iconUrlRoot = 'http://transportal.cee.wisc.edu/its/inventory/icons/';
 
    if (itsIcon) {
        return `<img src="${_iconUrlRoot + itsIcon}" class="legend-layer-icon" height="${iconHeight}">`;
    } else if (itsLineStyle) {
        return `<hr style="height: ${itsLineStyle.width}px; background-color: ${itsLineStyle.color}">`;
    } else if (itsIconConfig) {
        let outHtml = '';
        outHtml += '<ul>';
 
        for (let a of itsIconConfig.iconArray) {
            outHtml += `<li><span class="legend-layer-subitem">${a[1]}</span><img src="${_iconUrlRoot + a[2]}" class="legend-layer-icon" height="${iconHeight}">`;
        }
        outHtml += `<li><span class="legend-layer-subitem">${itsIconConfig.defaultName}</span>` +
            `<img src="${_iconUrlRoot + itsIconConfig.defaultIcon}" class="legend-layer-icon" height="${iconHeight}"></li>`;
        outHtml += '</ul>';
 
        return outHtml;
    } else if (itsLineConfig) {
        let outHtml = '';
        outHtml += '<ul>';
        for (let ls of itsLineConfig.lineArray) {
            outHtml += `<li><span class="legend-layer-subitem">${ls[1]}</span>` +
                `<hr style="height: ${ls[3]}px; background-color: ${ls[2]}">`;
        }
        outHtml += `<li><span class="legend-layer-subitem">${itsLineConfig.defaultName}</span>` +
            `<hr style="height: ${itsLineConfig.defaultWidth}px; background-color: ${itsLineConfig.defaultColor}"></li>`;
        outHtml += '</ul>';
 
        return outHtml;
    } else {
        return '';
    }
}
 
/**
 * Its Layer class
 * @augments LayerBaseVectorGeoJson
 */
class LayerItsInventory extends LayerBaseVectorGeoJson {
 
    /**
     * ITS device layer, types available at http://transportal.cee.wisc.edu/its/inventory/
     * @param {object} options - config
     * @param {string} [options.id] - layer id
     * @param {string} [options.name=Unnamed Layer] - layer name
     * @param {number} [options.opacity=1] - opacity
     * @param {boolean} [options.visible=true] - default visible
     * @param {number} [options.minZoom=undefined] - min zoom level, 0 - 28
     * @param {number} [options.maxZoom=undefined] - max zoom level, 0 - 28
     * @param {object} [options.params={}] the get parameters to include to retrieve the layer
     * @param {number} [options.zIndex=0] the z index for the layer
     * @param {function} [options.loadCallback] function to call on load, context this is the layer object
     * @param {boolean} [options.legendCollapse=false] if the legend item should be initially collapsed
     * @param {boolean} [options.legendCheckbox=true] if the legend item should have a checkbox for visibility
     * @param {boolean} [options.legendContent] additional content to add to the legend
     *
     * @param {boolean} [options.autoLoad=false] if the layer should auto load if not visible
     * @param {object|*} [options.style=undefined] the layer style, use openlayers default style if not defined
     * @param {boolean} [options.onDemand=false] if the layer should be loaded by extent on map move
     * @param {number} [options.onDemandDelay=300] delay before the map move callback should be called
     * @param {MapMoveCls} [options.mapMoveObj=mapMove] alternate map move object for use with multi map pages
     *
     * @param {string} options.itsType the ITS device type, use the url flag at http://transportal.cee.wisc.edu/its/inventory/
     * @param {boolean} [options.addPopup=true] if the popup should be added automatically
     *
     * @param {string} [options.itsIcon=undefined] the ITS device type icon image see http://transportal.cee.wisc.edu/its/inventory/icons/
     *
     * @param {object} [options.itsLineStyle=undefined] A single line style
     * @param {string} options.itsLineStyle.color the line color as rgb or hex
     * @param {number} [options.itsLineStyle.width=5] the line width
     *
     * @param {object} [options.itsIconConfig=undefined] The icon subtype configuration
     * @param {string} options.itsIconConfig.prop The property used to define icon attribute symbolization
     * @param {string} options.itsIconConfig.defaultName The default name to be used if no other match is found
     * @param {string} options.itsIconConfig.defaultIcon The default icon to be used for no other matches
     * @param {object} [options.itsIconConfig.iconArray=[]] an array, items with format [property, name, img]
     *
     * @param {object} [options.itsLineConfig=undefined] The property used to define icon attribute symbolization
     * @param {string} options.itsLineConfig.prop The property used to define icon attribute symbolization
     * @param {string} [options.itsLineConfig.defaultName=Other] The default name to be used if no other match is found
     * @param {string} [options.itsLineConfig.defaultColor=red] The default line color to be used for no other matches
     * @param {number} [options.itsLineConfig.defaultWidth] The default line width to be used for no other matches
     * @param {object} [options.itsLineConfig.lineArray=[]] an array, items with format [property, name, color, optional width = 5]
     */
    constructor(options) {
        if (typeof options.itsType !== 'string') {
            throw 'its type must be defined';
        }
 
        options.transform = {dataProjection: proj4326, featureProjection: proj3857};
 
        let addToLegend = '';
 
        // define a style with the helper function if it is not explicitly defined
        if (typeof options.style == 'undefined') {
            options.style = defineStyle(
                options.itsIcon, options.itsLineStyle, options.itsIconConfig, options.itsLineConfig
            );
            addToLegend = defineLegend(
                options.itsIcon, options.itsLineStyle, options.itsIconConfig, options.itsLineConfig
            );
        }
 
        options.params = typeof options.params == 'object' ? options.params : {};
        $.extend(options.params, {format: 'JSON', resource: options.itsType});
 
        super('http://transportal.cee.wisc.edu/its/inventory/', options);
 
        //add any additional content to the legend
        this.addLegendContent(addToLegend);
 
        options.addPopup = typeof options.addPopup == 'boolean' ? options.addPopup : true;
 
        if (options.addPopup) {
            mapPopup.addVectorPopup(this, function (props) {
                return `<iframe src="http://transportal.cee.wisc.edu/its/inventory/?feature=${props['featureGuid']}" ` +
                    `height="250" width="350"></iframe>`;
            });
        }
    }
 
    /**
     * callback to generate the parameters passed in the get request
     * @callback makeGetParams
     * @param {object} extent - extent object
     * @param {number} extent.minX - minX
     * @param {number} extent.minY - minY
     * @param {number} extent.maxX - maxX
     * @param {number} extent.maxY - maxY
     * @param {number} zoomLevel - zoom level
     */
    mapMoveMakeGetParams(extent, zoomLevel) {
        super.mapMoveMakeGetParams(extent, zoomLevel);
        let lowerLeft = new ol.geom.Point([extent.minX, extent.minY]);
        lowerLeft.transform(this.mapProj, this._projection4326);
        let lowerLeftCoordinates = lowerLeft.getCoordinates();
        let upperRight = new ol.geom.Point([extent.maxX, extent.maxY]);
        upperRight.transform(this.mapProj, this._projection4326);
        let upperRightCoordinates = upperRight.getCoordinates();
 
        $.extend(this.mapMoveParams,
            {
                L: lowerLeftCoordinates[0],
                R: upperRightCoordinates[0],
                B: lowerLeftCoordinates[1],
                T: upperRightCoordinates[1]
            });
    }
}
 
nm.LayerItsInventory = LayerItsInventory;
export default LayerItsInventory;