src/gm3/actions/mapSource.js
/** Add a map-source from XML
 *
 */
Add a map-source from XML
Add a map - source from XML
function addFromXml(xml, config) {
    const map_source = {
        name: xml.getAttribute('name'),
        urls: util.getTagContents(xml, 'url', true),
        type: xml.getAttribute('type'),
        label: xml.getAttribute('title'),
        opacity: xml.getAttribute('opacity'),
        zIndex: xml.getAttribute('z-index'),
        queryable: util.parseBoolean(xml.getAttribute('queryable'), true),
        printable: util.parseBoolean(xml.getAttribute('printable'), true),
        refresh: null,
        layers: [],
        transforms: {},
        params: {},
        properties: [],
    };
    if(map_source.zIndex) {
        map_source.zIndex = parseInt(map_source.zIndex, 10);
    }
    map_source.opacity = parseFloat(map_source.opacity);
    if(isNaN(map_source.opacity)) {
        map_source.opacity = 1.0;
    }
    const server_type = xml.getAttribute('server-type');
    if(server_type) {
        map_source.serverType = server_type;
    }
    if(map_source.type === 'mapserver') {
        Object.assign(map_source, mapServerToWMS(xml, config));
    } else if(map_source.type === 'mapserver-wfs') {
        Object.assign(map_source, mapServerToWFS(xml, config));
    }
    ['refresh', 'minresolution', 'maxresolution'].forEach((attr) => {
        const value = xml.getAttribute(attr);
        if(value) {
            map_source[attr] = parseFloat(value);
        }
    });
    const transforms = xml.getElementsByTagName('transform');
    for(let x = 0, xx = transforms.length; x < xx; x++) {
        const transform = transforms[x];
        map_source.transforms[transform.getAttribute('attribute')] =
        transform.getAttribute('function');
    }
    Object.assign(map_source.params, parseParams(xml));
    Object.assign(map_source.properties, parseProperties(xml));
    const map_layers = [];
    const xml_layers = xml.getElementsByTagName('layer');
    for(let i = 0, ii = xml_layers.length; i < ii; i++) {
        const layerXml = xml_layers[i];
        const layer_title = layerXml.getAttribute('title');
        const layer = {
            name: layerXml.getAttribute('name'),
            on: util.parseBoolean(layerXml.getAttribute('status')),
            favorite: util.parseBoolean(layerXml.getAttribute('favorite')),
            selectable: util.parseBoolean(layerXml.getAttribute('selectable')),
            label: layer_title ? layer_title : map_source.label,
            templates: {},
            legend: null,
            attribution: null,
            style: null,
            queryAs: layerXml.getAttribute('query-as'),
        };
        const legends = layerXml.getElementsByTagName('legend');
        if(legends.length > 0) {
            layer.legend = {
                type: legends[0].getAttribute('type'),
                contents: util.getXmlTextContents(legends[0])
            };
        }
        const attribution = layerXml.getElementsByTagName('attribution');
        if (attribution.length > 0) {
            layer.attribution = util.getXmlTextContents(attribution[0]);
        }
        const templates = layerXml.getElementsByTagName('template');
        for(let x = 0, xx = templates.length; x < xx; x++) {
            let template_def = {};
            const template_xml = templates[x];
            const template_name = template_xml.getAttribute('name');
            const template_src = template_xml.getAttribute('src');
            const template_alias = template_xml.getAttribute('alias');
            const auto_template = util.parseBoolean(template_xml.getAttribute('auto'));
            if(template_alias) {
                template_def = {
                    type: 'alias',
                    alias: template_alias
                };
            } else if(template_src) {
                template_def = {
                    type: 'remote',
                    src: template_src
                };
            } else if(auto_template) {
                template_def = {
                    type: 'auto'
                };
            } else {
                template_def = {
                    type: 'local',
                    contents: util.getXmlTextContents(template_xml)
                };
            }
            layer.templates[template_name] = template_def;
        }
        const style = util.getTagContents(layerXml, 'style', false);
        if(style && style.length > 0) {
            try {
                layer.style = JSON.parse(style);
            } catch(err) {
                console.error('There was an error parsing the style for: ', map_source.name);
                console.error('Error details', err);
            }
        }
        const filter = util.getTagContents(layerXml, 'filter', false);
        if(filter && filter.length > 0) {
            try {
                layer.filter = JSON.parse(filter);
            } catch(err) {
                console.error('There was an error parsing the filter for: ', map_source.name);
                console.error('Error details', err);
            }
        }
        if(layer.queryAs) {
            layer.queryAs = layer.queryAs.split(':');
        }
        map_layers.push(addLayer(map_source.name, layer));
    }
    return [add(map_source)].concat(map_layers);
}
function addFromXml ( xml , config ) { const map_source = { name : xml . getAttribute ( 'name' ) , urls : util . getTagContents ( xml , 'url' , true ) , type : xml . getAttribute ( 'type' ) , label : xml . getAttribute ( 'title' ) , opacity : xml . getAttribute ( 'opacity' ) , zIndex : xml . getAttribute ( 'z-index' ) , queryable : util . parseBoolean ( xml . getAttribute ( 'queryable' ) , true ) , printable : util . parseBoolean ( xml . getAttribute ( 'printable' ) , true ) , refresh : null , layers : [ ] , transforms : { } , params : { } , properties : [ ] , } ; if ( map_source . zIndex ) { map_source . zIndex = parseInt ( map_source . zIndex , 10 ) ; } map_source . opacity = parseFloat ( map_source . opacity ) ; if ( isNaN ( map_source . opacity ) ) { map_source . opacity = 1.0 ; } const server_type = xml . getAttribute ( 'server-type' ) ; if ( server_type ) { map_source . serverType = server_type ; } if ( map_source . type === 'mapserver' ) { Object . assign ( map_source , mapServerToWMS ( xml , config ) ) ; } else if ( map_source . type === 'mapserver-wfs' ) { Object . assign ( map_source , mapServerToWFS ( xml , config ) ) ; } [ 'refresh' , 'minresolution' , 'maxresolution' ] . forEach ( ( attr ) => { const value = xml . getAttribute ( attr ) ; if ( value ) { map_source [ attr ] = parseFloat ( value ) ; } } ) ; const transforms = xml . getElementsByTagName ( 'transform' ) ; for ( let x = 0 , xx = transforms . length ; x < xx ; x ++ ) { const transform = transforms [ x ] ; map_source . transforms [ transform . getAttribute ( 'attribute' ) ] = transform . getAttribute ( 'function' ) ; } Object . assign ( map_source . params , parseParams ( xml ) ) ; Object . assign ( map_source . properties , parseProperties ( xml ) ) ; const map_layers = [ ] ; const xml_layers = xml . getElementsByTagName ( 'layer' ) ; for ( let i = 0 , ii = xml_layers . length ; i < ii ; i ++ ) { const layerXml = xml_layers [ i ] ; const layer_title = layerXml . getAttribute ( 'title' ) ; const layer = { name : layerXml . getAttribute ( 'name' ) , on : util . parseBoolean ( layerXml . getAttribute ( 'status' ) ) , favorite : util . parseBoolean ( layerXml . getAttribute ( 'favorite' ) ) , selectable : util . parseBoolean ( layerXml . getAttribute ( 'selectable' ) ) , label : layer_title ? layer_title : map_source . label , templates : { } , legend : null , attribution : null , style : null , queryAs : layerXml . getAttribute ( 'query-as' ) , } ; const legends = layerXml . getElementsByTagName ( 'legend' ) ; if ( legends . length > 0 ) { layer . legend = { type : legends [ 0 ] . getAttribute ( 'type' ) , contents : util . getXmlTextContents ( legends [ 0 ] ) } ; } const attribution = layerXml . getElementsByTagName ( 'attribution' ) ; if ( attribution . length > 0 ) { layer . attribution = util . getXmlTextContents ( attribution [ 0 ] ) ; } const templates = layerXml . getElementsByTagName ( 'template' ) ; for ( let x = 0 , xx = templates . length ; x < xx ; x ++ ) { let template_def = { } ; const template_xml = templates [ x ] ; const template_name = template_xml . getAttribute ( 'name' ) ; const template_src = template_xml . getAttribute ( 'src' ) ; const template_alias = template_xml . getAttribute ( 'alias' ) ; const auto_template = util . parseBoolean ( template_xml . getAttribute ( 'auto' ) ) ; if ( template_alias ) { template_def = { type : 'alias' , alias : template_alias } ; } else if ( template_src ) { template_def = { type : 'remote' , src : template_src } ; } else if ( auto_template ) { template_def = { type : 'auto' } ; } else { template_def = { type : 'local' , contents : util . getXmlTextContents ( template_xml ) } ; } layer . templates [ template_name ] = template_def ; } const style = util . getTagContents ( layerXml , 'style' , false ) ; if ( style && style . length > 0 ) { try { layer . style = JSON . parse ( style ) ; } catch ( err ) { console . error ( 'There was an error parsing the style for: ' , map_source . name ) ; console . error ( 'Error details' , err ) ; } } const filter = util . getTagContents ( layerXml , 'filter' , false ) ; if ( filter && filter . length > 0 ) { try { layer . filter = JSON . parse ( filter ) ; } catch ( err ) { console . error ( 'There was an error parsing the filter for: ' , map_source . name ) ; console . error ( 'Error details' , err ) ; } } if ( layer . queryAs ) { layer . queryAs = layer . queryAs . split ( ':' ) ; } map_layers . push ( addLayer ( map_source . name , layer ) ) ; } return [ add ( map_source ) ] . concat ( map_layers ) ; }
Add a map-source from XML
Add a map - source from XML
// initialize the map source object. // assume layers are printable // handle setting up the zIndex // try to get an opacity,  if it won't parse then default to 1.0 // allow server-type hinting for hidpi displays. // set of 'correction' functions for mapsources that //  are parsed/tweaked to help the configurator. // parse the optional float attributes // catalog the transforms for the layer // mix in the params // and lets get some properties // add all of the layers. // user defined legend. // two types currently supported: "html" and "img" // pull in an HTML attribution as available. // check to see if there are any style definitions // convert to JSON // check to see if there are any filter definitions // convert to JSON // queryAs can be defined as multiple paths, //  and is normalized to an array using split.