diff --git a/src/app/core/layers/baselayers/baselayer.js b/src/app/core/layers/baselayer.js similarity index 100% rename from src/app/core/layers/baselayers/baselayer.js rename to src/app/core/layers/baselayer.js diff --git a/src/app/core/layers/baselayers/arcgislayer.js b/src/app/core/layers/baselayers/arcgislayer.js deleted file mode 100644 index 1ee7a3c8e..000000000 --- a/src/app/core/layers/baselayers/arcgislayer.js +++ /dev/null @@ -1,28 +0,0 @@ -const { base, inherit } = require('core/utils/utils'); -const BaseLayer = require('core/layers/baselayers/baselayer'); -const BasesLayers = require('g3w-ol/layers/bases'); - -function ARCGISMAPSERVERLayer(config={}, options={}) { - this.config = config; - base(this, config, options); -} - -inherit(ARCGISMAPSERVERLayer, BaseLayer); - -const proto = ARCGISMAPSERVERLayer.prototype; - -proto._makeOlLayer = function() { - // here configuration to create TMS - const {url, attributions, crs} = this.config; - const projection = this.getProjectionFromCrs(crs); - const olLayer = BasesLayers.TMS.get({ - url, - source_type: 'arcgismapserver', - projection, - attributions - }); - return olLayer -}; - - -module.exports = ARCGISMAPSERVERLayer; diff --git a/src/app/core/layers/baselayers/baselayers.js b/src/app/core/layers/baselayers/baselayers.js deleted file mode 100644 index 2cc887fc1..000000000 --- a/src/app/core/layers/baselayers/baselayers.js +++ /dev/null @@ -1,10 +0,0 @@ -const BaseLayers = { - 'OSM': require('./osmlayer'), - 'Bing': require('./binglayer'), - 'TMS': require('./tmslayer'), - 'ARCGISMAPSERVER': require('./arcgislayer'), - 'WMTS': require('./wmtslayer'), - 'WMS': require('./wmslayer') -}; - -module.exports = BaseLayers; diff --git a/src/app/core/layers/baselayers/binglayer.js b/src/app/core/layers/baselayers/binglayer.js deleted file mode 100644 index a7f776b5d..000000000 --- a/src/app/core/layers/baselayers/binglayer.js +++ /dev/null @@ -1,47 +0,0 @@ -import ApplicationState from 'store/application-state' - -const { base, inherit } = require('core/utils/utils'); -const BaseLayer = require('core/layers/baselayers/baselayer'); -const BasesLayers = require('g3w-ol/layers/bases'); - -function BingLayer(config={}, options={}){ - base(this, config, options); -} - -inherit(BingLayer, BaseLayer); - -const proto = BingLayer.prototype; - -proto._makeOlLayer = function(){ - let olLayer; - const key = ApplicationState.keys.vendorkeys.bing; - const subtype = this.config.source ? this.config.source.subtype : null; - switch(subtype) { - case 'streets': - olLayer = BasesLayers.BING.get({ - imagerySet: 'Road', - key - }); - break; - case 'aerial': - olLayer = BasesLayers.BING.get({ - imagerySet: 'Aerial', - key - }); - break; - case 'aerialwithlabels': - olLayer = BasesLayers.BING.get({ - imagerySet: 'AerialWithLabels', - key - }); - break; - default: - olLayer = BasesLayers.BING.get({ - imagerySet: 'Aerial', - key - }); - } - return olLayer -}; - -module.exports = BingLayer; diff --git a/src/app/core/layers/baselayers/osmlayer.js b/src/app/core/layers/baselayers/osmlayer.js deleted file mode 100644 index eabd7397e..000000000 --- a/src/app/core/layers/baselayers/osmlayer.js +++ /dev/null @@ -1,23 +0,0 @@ -const { inherit, base } = require('core/utils/utils'); -const BaseLayer = require('core/layers/baselayers/baselayer'); -const BasesLayers = require('g3w-ol/layers/bases'); - -function OSMLayer(config={}, options={}){ - base(this, config, options); -} - -inherit(OSMLayer, BaseLayer); - -const proto = OSMLayer.prototype; - -proto._makeOlLayer = function() { - const olLayer = BasesLayers.OSM.get({ - id: this.config.name, - title: this.config.title, - url: this.config.url - }); - return olLayer -}; - - -module.exports = OSMLayer; diff --git a/src/app/core/layers/baselayers/tmslayer.js b/src/app/core/layers/baselayers/tmslayer.js deleted file mode 100644 index 274d90f35..000000000 --- a/src/app/core/layers/baselayers/tmslayer.js +++ /dev/null @@ -1,29 +0,0 @@ -const { base, inherit } = require('core/utils/utils'); -const BaseLayer = require('core/layers/baselayers/baselayer'); -const BasesLayers = require('g3w-ol/layers/bases'); - -function TMSLayer(config={}, options={}) { - this.config = config; - base(this, config, options); -} - -inherit(TMSLayer, BaseLayer); - -const proto = TMSLayer.prototype; - -proto._makeOlLayer = function() { - // here configuration to create TMS - const {url, attributions, minZoom, maxZoom, crs} = this.config; - const projection = this.getProjectionFromCrs(crs); - const olLayer = BasesLayers.TMS.get({ - url, - minZoom, - maxZoom, - attributions, - projection - }); - return olLayer -}; - - -module.exports = TMSLayer; diff --git a/src/app/core/layers/baselayers/wmslayer.js b/src/app/core/layers/baselayers/wmslayer.js deleted file mode 100644 index ba586354c..000000000 --- a/src/app/core/layers/baselayers/wmslayer.js +++ /dev/null @@ -1,29 +0,0 @@ -const { inherit, base } = require('core/utils/utils'); -const BaseLayer = require('core/layers/baselayers/baselayer'); -const BasesLayers = require('g3w-ol/layers/bases'); - -function WMSLayer(config={}, options={}) { - this.config = config; - base(this, config, options); -} - -inherit(WMSLayer, BaseLayer); - -const proto = WMSLayer.prototype; - -proto._makeOlLayer = function() { - //use this config to get params - const {url, layers, singleTile, attributions, crs, opacity} = this.config; - const projection = this.getProjectionFromCrs(crs); - const olLayer = BasesLayers.WMS.get({ - url, - layers, - singleTile, - attributions, - projection, - opacity - }); - return olLayer -}; - -module.exports = WMSLayer; diff --git a/src/app/core/layers/baselayers/wmtslayer.js b/src/app/core/layers/baselayers/wmtslayer.js deleted file mode 100644 index da0195e81..000000000 --- a/src/app/core/layers/baselayers/wmtslayer.js +++ /dev/null @@ -1,32 +0,0 @@ -const { base, inherit } = require('core/utils/utils'); -const BaseLayer = require('core/layers/baselayers/baselayer'); -const BasesLayers = require('g3w-ol/layers/bases'); - -function WMTSLayer(config={}, options={}) { - this.config = config; - base(this, config, options); -} - -inherit(WMTSLayer, BaseLayer); - -const proto = WMTSLayer.prototype; - -proto._makeOlLayer = function() { - //use this config to get params - const {url, layer, attributions, matrixSet, format, style, requestEncoding, crs} = this.config; - const projection = this.getProjectionFromCrs(crs); - const olLayer = BasesLayers.WMTS.get({ - url, - layer, - attributions, - format, - projection, - requestEncoding, - matrixSet, - style - }); - return olLayer -}; - - -module.exports = WMTSLayer; diff --git a/src/app/core/layers/layerfactory.js b/src/app/core/layers/layerfactory.js index 864cf363e..9ebbfb4e3 100644 --- a/src/app/core/layers/layerfactory.js +++ b/src/app/core/layers/layerfactory.js @@ -1,90 +1,200 @@ -const Layer = require('core/layers/layer'); -const TableLayer = require('core/layers/tablelayer'); -const VectorLayer = require('core/layers/vectorlayer'); -const ImageLayer = require('core/layers/imagelayer'); -const BaseLayers = require('core/layers/baselayers/baselayers'); +import ApplicationState from 'store/application-state' + +const Layer = require('core/layers/layer'); +const TableLayer = require('core/layers/tablelayer'); +const VectorLayer = require('core/layers/vectorlayer'); +const ImageLayer = require('core/layers/imagelayer'); +const BaseLayer = require('core/layers/baselayer'); +const BASE = require('g3w-ol/layers/bases'); const GeojsonLayer = require('core/layers/geojson'); +const WITH_GEOMETRY = [ + Layer.SourceTypes.VIRTUAL, + Layer.SourceTypes.POSTGIS, + Layer.SourceTypes.MSSQL, + Layer.SourceTypes.SPATIALITE, + Layer.SourceTypes.WFS, + Layer.SourceTypes.CSV, + Layer.SourceTypes.ORACLE, + Layer.SourceTypes.OGR, + Layer.SourceTypes.MDAL, +]; + +const NO_GEOMETRY = [ + Layer.SourceTypes.WMST, + Layer.SourceTypes.WCS, + Layer.SourceTypes.WMS, + Layer.SourceTypes.GDAL, + Layer.SourceTypes.VECTORTILE, + Layer.SourceTypes["VECTOR-TILE"], + Layer.SourceTypes.MDAL, + /** @since 3.8.7 */ + Layer.SourceTypes.ARCGISMAPSERVER, +]; + +const BASE_LAYERS = { + + /** + * ORIGINAL SOURCE: src/core/layers/baselayers/osmlayer.js@3.8.6 + */ + [Layer.ServerTypes.OSM]: class OSMLayer extends BaseLayer { + _makeOlLayer() { + return BASE.OSM.get({ + id: this.config.name, + title: this.config.title, + url: this.config.url, + }); + } + }, + + /** + * ORIGINAL SOURCE: core/layers/baselayers/binglayer.js@3.8.6 + */ + [Layer.ServerTypes.BING]: class BingLayer extends BaseLayer { + _makeOlLayer() { + const key = ApplicationState.keys.vendorkeys.bing; + switch(this.config.source ? this.config.source.subtype : null) { + case 'streets': return BASE.BING.get({ key, imagerySet: 'Road' }); + case 'aerial': return BASE.BING.get({ key, imagerySet: 'Aerial' }); + case 'aerialwithlabels': return BASE.BING.get({ key, imagerySet: 'AerialWithLabels' }); + default: return BASE.BING.get({ key, imagerySet: 'Aerial' }); + } + } + }, + + /** + * ORIGINAL SOURCE: src/core/layers/baselayers/tmslayer.js@3.8.6 + */ + [Layer.ServerTypes.TMS]: class TMSLayer extends BaseLayer { + _makeOlLayer() { + // configuration to create TMS + const { url, attributions, minZoom, maxZoom, crs } = this.config; + return BASE.TMS.get({ + url, + minZoom, + maxZoom, + attributions, + projection: this.getProjectionFromCrs(crs), + }); + } + }, + + /** + * ORIGINAL SOURCE: src/require('core/layers/baselayers/arcgislayer.js@3.8.6 + */ + [Layer.ServerTypes.ARCGISMAPSERVER]: class ARCGISMAPSERVERLayer extends BaseLayer { + _makeOlLayer() { + // configuration to create TMS + const { url, attributions, crs } = this.config; + return BASE.TMS.get({ + url, + source_type: 'arcgismapserver', + projection: this.getProjectionFromCrs(crs), + attributions, + }); + } + }, + + /** + * ORIGINAL SOURCE: src/require('core/layers/baselayers/wmtslayer.js@3.8.6 + */ + [Layer.ServerTypes.WMTS]: class WMTSLayer extends BaseLayer { + _makeOlLayer() { + // use this config to get params + const { url, layer, attributions, matrixSet, format, style, requestEncoding, crs } = this.config; + return BASE.WMTS.get({ + url, + layer, + attributions, + format, + projection: this.getProjectionFromCrs(crs), + requestEncoding, + matrixSet, + style, + }); + } + }, + + /** + * ORIGINAL SOURCE: src/require('core/layers/baselayers/wmslayer.js@3.8.6 + */ + [Layer.ServerTypes.WMS]: class WMSLayer extends BaseLayer { + _makeOlLayer() { + // use this config to get params + const { url, layers, singleTile, attributions, crs, opacity } = this.config; + return BASE.WMS.get({ + url, + layers, + singleTile, + attributions, + projection: this.getProjectionFromCrs(crs), + opacity, + }); + } + }, + +}; + // Class to build layer based on configuration -function LayerFactory() { - this.build = function(config, options) { - // return the layer instance +class LayerFactory { + + /** + * @returns layer instance + */ + build(config, options) { const layerClass = this.get(config); return layerClass ? new layerClass(config, options) : null - }; - - this.get = function(config={}) { - let LayerClass; - const serverType = config.servertype; - switch (serverType) { - case Layer.ServerTypes.QGIS: - if (config.source) { - if (config.geometrytype) { - if ([ - Layer.SourceTypes.VIRTUAL, - Layer.SourceTypes.POSTGIS, - Layer.SourceTypes.MSSQL, - Layer.SourceTypes.SPATIALITE, - Layer.SourceTypes.WFS, - Layer.SourceTypes.CSV, - Layer.SourceTypes.ORACLE, - Layer.SourceTypes.OGR, - Layer.SourceTypes.MDAL, - ].find(sourcetype => sourcetype === config.source.type)) { - if (config.geometrytype && config.geometrytype === 'NoGeometry') LayerClass = TableLayer; - else LayerClass = ImageLayer; - } - } else if ([ // here set new layer has to be threat as wms - Layer.SourceTypes.WMST, - Layer.SourceTypes.WCS, - Layer.SourceTypes.WMS, - Layer.SourceTypes.GDAL, - Layer.SourceTypes.VECTORTILE, - Layer.SourceTypes["VECTOR-TILE"], - Layer.SourceTypes.MDAL, - /** - * @since 3.8.7 - */ - Layer.SourceTypes.ARCGISMAPSERVER, - ].find(sourcetype => sourcetype === config.source.type)) LayerClass = ImageLayer; - } - break; - case Layer.ServerTypes.OGC: - if(config.source) { - const type = config.source.type; - switch (type) { - case Layer.SourceTypes.WMS: - LayerClass = ImageLayer; - break; - case Layer.SourceTypes.WFS: - LayerClass = VectorLayer; - } - } - break; - case Layer.ServerTypes.LOCAL: - LayerClass = VectorLayer; - break; - case Layer.ServerTypes.OSM: - case Layer.ServerTypes.BING: - case Layer.ServerTypes.TMS: - case Layer.ServerTypes.WMS: - case Layer.ServerTypes.WMTS: - case Layer.ServerTypes.ARCGISMAPSERVER: - LayerClass = BaseLayers[serverType]; - break; - case Layer.ServerTypes.G3WSUITE: - LayerClass = VectorLayer; - if (config.source) { - switch (config.source.type) { - case 'geojson': - LayerClass = GeojsonLayer; - break; - } - } - break; + } + + /** + * @returns layer class + */ + get(config = {}) { + + if (!config.servertype) { + console.warn('Undefined layer server type'); + return; } - return LayerClass; + + // Get Source + const source = config.source || {}; + + // Check Server Type + const is_qgis = Layer.ServerTypes.QGIS === config.servertype; + const is_ogc = Layer.ServerTypes.OGC === config.servertype; + const is_g3w = Layer.ServerTypes.G3WSUITE === config.servertype + const is_local = Layer.ServerTypes.LOCAL === config.servertype; + + const is_wms = is_ogc && Layer.SourceTypes.WMS === source.type; + const is_wfs = is_ogc && Layer.SourceTypes.WFS === source.type; + const is_geojson = is_g3w && Layer.SourceTypes.GEOJSON === source.type; + + // Check Geometry Type + const is_tabular = config.geometrytype === 'NoGeometry'; + const has_geom = config.geometrytype && WITH_GEOMETRY.includes(source.type); + const no_geom = !config.geometrytype && NO_GEOMETRY.includes(source.type); + + // Check Layer Type + const is_base_layer = config.servertype in BASE_LAYERS; + const is_table_layer = is_qgis && has_geom && is_tabular; + const is_image_layer = is_wms || is_qgis && (no_geom || (has_geom && !is_tabular)); + const is_vector_layer = is_local || is_wfs || (is_g3w && !is_geojson); + const is_geojson_layer = is_geojson; + + // Return Layer Class + if (is_table_layer) return TableLayer; + if (is_image_layer) return ImageLayer; + if (is_vector_layer) return VectorLayer; + if (is_base_layer) return BASE_LAYERS[config.servertype]; + if (is_geojson_layer) return GeojsonLayer; + + console.warn('Uknown layer server type', config); + + // return BaseLayers[config.source.type.toUpperCase()]; + // return ImageLayer; + } + } -module.exports = new LayerFactory(); +module.exports = new LayerFactory(); \ No newline at end of file