Skip to content
Permalink
Browse files

Refactor dimension related code into seperate module

fixes #233
  • Loading branch information...
raitisbe committed Jul 22, 2019
1 parent dde440e commit 6771bbc4172d36fcc5d948c5943e1911785a7f20
@@ -0,0 +1,76 @@
import moment from 'moment';
global.moment = moment;
import momentinterval from 'moment-interval/src/moment-interval';

//TODO needs to be documented
export default [function () {
var me = this;
angular.extend(me, {
prepareTimeSteps(step_string) {
var step_array = step_string.split(',');
var steps = [];
for (var i = 0; i < step_array.length; i++) {
if (step_array[i].indexOf('/') == -1) {
steps.push(new Date(step_array[i]).toISOString());
//console.log(new Date(step_array[i]).toISOString());
} else {
//"2016-03-16T12:00:00.000Z/2016-07-16T12:00:00.000Z/P30DT12H"
var interval_def = step_array[i].split('/');
var step;
if(interval_def.length==3)
step = momentinterval.interval(interval_def[2]);
else {
step = momentinterval.interval('P1D');
}
var interval = momentinterval.interval(interval_def[0] + '/' + interval_def[1]);
while (interval.start() < interval.end()) {
//console.log(interval.start().toDate().toISOString());
steps.push(interval.start().toDate().toISOString());
interval.start(momentinterval.utc(interval.start().toDate()).add(step.period()));
}
}
}
return steps;
},

getDimensionValues(dimension) {
debugger;
try {
if (moment(dimension.default).isValid())
return me.prepareTimeSteps(dimension.values)
else
return dimension.values.split(',');
} catch (ex) {
console.error(ex)
}
},

hasNestedLayers(layer) {
if (angular.isUndefined(layer)) return false;
return angular.isDefined(layer.Layer);
},

paramsFromDimensions(layer) {
var tmp = {};
angular.forEach(layer.Dimension, function (dimension) {
if (dimension.value)
tmp[dimension.name] = dimension.value;
});
return tmp;
},

fillDimensionValues(layer) {
angular.forEach(layer.Layer, function (layer) {
if (me.hasNestedLayers(layer)) {
me.fillDimensionValues(layer);
}
angular.forEach(layer.Dimension, function (dimension) {
dimension.values = me.getDimensionValues(dimension);
debugger;
})
})
}
})

return me;
}]
@@ -1,6 +1,7 @@
import wfsGetCapabilitiesService from './wfs/get-capabilities.service';
import wmsGetCapabilitiesService from './wms/get-capabilities.service';
import wmtsGetCapabilities from './wmts/get-capabilities.service';
import dimensionService from './dimension.service';

/**
* @namespace hs.common
@@ -24,6 +25,15 @@ angular.module('hs.getCapabilities', [])
*/
.service("hs.wms.getCapabilitiesService", wmsGetCapabilitiesService)


/**
* @class hs.dimensionService
* @ngdoc service
* @memberOf hs.getCapabilities
* @description Service for filling dimension values such as time
*/
.service("hs.dimensionService", dimensionService)

/**
* @name hs.wmts.getCapabilitiesService
* @ngdoc service
@@ -1,17 +1,15 @@
import { WMSCapabilities } from 'ol/format';
import 'components/utils/utils.module';
import moment from 'moment';
global.moment = moment;
import momentinterval from 'moment-interval/src/moment-interval';
import { Tile, Image as ImageLayer } from 'ol/layer';
import { TileWMS } from 'ol/source';
import { ImageWMS } from 'ol/source';
import { Attribution } from 'ol/control.js';
import { getPreferedFormat } from '../../common/format-utils';
import '../../common/get-capabilities.module';
import { addAnchors } from '../../common/attribution-utils';
import 'angular-cookies';

export default ['$rootScope', 'hs.map.service', 'hs.wms.getCapabilitiesService', 'Core', '$cookies', 'hs.permalink.urlService', function ($rootScope, OlMap, WmsCapsService, Core, $cookies, permalink) {
export default ['$rootScope', 'hs.map.service', 'hs.wms.getCapabilitiesService', 'Core', '$cookies', 'hs.permalink.urlService', 'hs.dimensionService', function ($rootScope, OlMap, WmsCapsService, Core, $cookies, permalink, dimensionService) {
var me = this;

this.data = {
@@ -24,7 +22,6 @@ export default ['$rootScope', 'hs.map.service', 'hs.wms.getCapabilitiesService',

this.capabilitiesReceived = function (response) {
try {
debugger;
var parser = new WMSCapabilities();
var caps = parser.read(response);
me.data.mapProjection = OlMap.map.getView().getProjection().getCode().toUpperCase();
@@ -53,8 +50,7 @@ export default ['$rootScope', 'hs.map.service', 'hs.wms.getCapabilitiesService',
me.srsChanged();
me.data.services = caps.Capability.Layer;

fillDimensionValues(caps.Capability.Layer);
debugger;
dimensionService.fillDimensionValues(caps.Capability.Layer);

me.data.getMapUrl = caps.Capability.Request.GetMap.DCPType[0].HTTP.Get.OnlineResource;
me.data.image_format = getPreferedFormat(me.data.image_formats, ["image/png; mode=8bit", "image/png", "image/gif", "image/jpeg"]);
@@ -66,17 +62,6 @@ export default ['$rootScope', 'hs.map.service', 'hs.wms.getCapabilitiesService',
}
}

function fillDimensionValues(layer) {
angular.forEach(layer.Layer, function (layer) {
if (me.hasNestedLayers(layer)) {
fillDimensionValues(layer);
}
angular.forEach(layer.Dimension, function (dimension) {
dimension.values = me.getDimensionValues(dimension)
})
})
}

$rootScope.$on('ows.capabilities_received', function (event, response) {
me.capabilitiesReceived(response.data);
});
@@ -132,57 +117,17 @@ export default ['$rootScope', 'hs.map.service', 'hs.wms.getCapabilitiesService',
recurse(layer)
});
Core.setMainPanel('layermanager');
}
}

//TODO all dimension related things need to be refactored into seperate module
function prepareTimeSteps(step_string) {
var step_array = step_string.split(',');
var steps = [];
for (var i = 0; i < step_array.length; i++) {
if (step_array[i].indexOf('/') == -1) {
steps.push(new Date(step_array[i]).toISOString());
//console.log(new Date(step_array[i]).toISOString());
} else {
//"2016-03-16T12:00:00.000Z/2016-07-16T12:00:00.000Z/P30DT12H"
var interval_def = step_array[i].split('/');
var step = momentinterval.interval(interval_def[2]);
var interval = momentinterval.interval(interval_def[0] + '/' + interval_def[1]);
while (interval.start() < interval.end()) {
//console.log(interval.start().toDate().toISOString());
steps.push(interval.start().toDate().toISOString());
interval.start(momentinterval.utc(interval.start().toDate()).add(step.period()));
}
}
}
return steps;
}

//TODO all dimension related things need to be refactored into seperate module
me.getDimensionValues = function (dimension) {
try {
if (moment(dimension.default).isValid())
return prepareTimeSteps(dimension.values)
else
return dimension.values.split(',');
} catch (ex) {
console.error(ex)
}

}
me.getDimensionValues = dimensionService.getDimensionValues

me.hasNestedLayers = function (layer) {
if (angular.isUndefined(layer)) return false;
return angular.isDefined(layer.Layer);
};

function paramsFromDimensions(layer) {
var tmp = {};
angular.forEach(layer.Dimension, function (dimension) {
if (dimension.value)
tmp[dimension.name] = dimension.value;
});
return tmp;
}


/**
* @function addLayer
@@ -245,7 +190,7 @@ export default ['$rootScope', 'hs.map.service', 'hs.wms.getCapabilitiesService',
FORMAT: me.data.image_format,
FROMCRS: me.data.srs,
VERSION: me.data.version
}, paramsFromDimensions(layer)),
}, dimensionService.paramsFromDimensions(layer)),
crossOrigin: 'anonymous'
}),
minResolution: layer.MinScaleDenominator,
@@ -8,7 +8,6 @@ export default {
$scope.types = config.connectTypes;
} else {
$scope.types = [
{ id: "none", text: "" },
{ id: "wms", text: "Web map service (WMS)" },
{ id: "vector", text: "Vector file (GeoJson, KML)" }
];
@@ -13,7 +13,7 @@ import addLayersUrlDirective from './add-layers-url.directive';
* @namespace hs.addLayers
* @memberOf hs
*/
export const AddLayersModule = angular.module('hs.addLayers', ['hs.map', 'hs.addLayersWms', 'hs.addLayersWfs', 'hs.addLayersWmts', 'hs.addLayersVector', 'ngCookies', 'hs.historyList'])
export const AddLayersModule = angular.module('hs.addLayers', ['hs.map', 'hs.addLayersWms', 'hs.addLayersWfs', 'hs.addLayersWmts', 'hs.addLayersVector', 'ngCookies', 'hs.historyList', 'hs.getCapabilities'])
/**
* @memberof hs.addLayers
* @ngdoc directive

0 comments on commit 6771bbc

Please sign in to comment.
You can’t perform that action at this time.