2018-06-05 21:39:01 +08:00
|
|
|
const dateWrapper = require('./date-wrapper');
|
|
|
|
|
2018-03-09 19:58:05 +08:00
|
|
|
module.exports = class LayergroupMetadata {
|
|
|
|
constructor (resourceLocator) {
|
|
|
|
this.resourceLocator = resourceLocator;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO this should take into account several URL patterns
|
|
|
|
addDataviewsAndWidgetsUrls (username, layergroup, mapConfig) {
|
|
|
|
this._addDataviewsUrls(username, layergroup, mapConfig);
|
|
|
|
this._addWidgetsUrl(username, layergroup, mapConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
_addDataviewsUrls (username, layergroup, mapConfig) {
|
|
|
|
layergroup.metadata.dataviews = layergroup.metadata.dataviews || {};
|
|
|
|
var dataviews = mapConfig.dataviews || {};
|
|
|
|
|
|
|
|
Object.keys(dataviews).forEach((dataviewName) => {
|
|
|
|
var resource = layergroup.layergroupid + '/dataview/' + dataviewName;
|
|
|
|
layergroup.metadata.dataviews[dataviewName] = {
|
|
|
|
url: this.resourceLocator.getUrls(username, resource)
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_addWidgetsUrl (username, layergroup, mapConfig) {
|
|
|
|
if (layergroup.metadata && Array.isArray(layergroup.metadata.layers) && Array.isArray(mapConfig.layers)) {
|
|
|
|
layergroup.metadata.layers = layergroup.metadata.layers.map((layer, layerIndex) => {
|
|
|
|
var mapConfigLayer = mapConfig.layers[layerIndex];
|
|
|
|
if (mapConfigLayer.options && mapConfigLayer.options.widgets) {
|
|
|
|
layer.widgets = layer.widgets || {};
|
|
|
|
Object.keys(mapConfigLayer.options.widgets).forEach((widgetName) => {
|
|
|
|
var resource = layergroup.layergroupid + '/' + layerIndex + '/widget/' + widgetName;
|
|
|
|
layer.widgets[widgetName] = {
|
|
|
|
type: mapConfigLayer.options.widgets[widgetName].type,
|
|
|
|
url: this.resourceLocator.getUrls(username, resource)
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return layer;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addAnalysesMetadata (username, layergroup, analysesResults, includeQuery) {
|
|
|
|
includeQuery = includeQuery || false;
|
|
|
|
analysesResults = analysesResults || [];
|
|
|
|
layergroup.metadata.analyses = [];
|
|
|
|
|
|
|
|
analysesResults.forEach((analysis) => {
|
|
|
|
var nodes = analysis.getNodes();
|
|
|
|
layergroup.metadata.analyses.push({
|
|
|
|
nodes: nodes.reduce((nodesIdMap, node) => {
|
|
|
|
if (node.params.id) {
|
|
|
|
var nodeResource = layergroup.layergroupid + '/analysis/node/' + node.id();
|
|
|
|
var nodeRepr = {
|
|
|
|
status: node.getStatus(),
|
|
|
|
url: this.resourceLocator.getUrls(username, nodeResource)
|
|
|
|
};
|
|
|
|
if (includeQuery) {
|
|
|
|
nodeRepr.query = node.getQuery();
|
|
|
|
}
|
|
|
|
if (node.getStatus() === 'failed') {
|
|
|
|
nodeRepr.error_message = node.getErrorMessage();
|
|
|
|
}
|
|
|
|
nodesIdMap[node.params.id] = nodeRepr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodesIdMap;
|
|
|
|
}, {})
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
addAggregationContextMetadata (layergroup, mapConfig, context) {
|
|
|
|
if (layergroup.metadata && Array.isArray(layergroup.metadata.layers) && Array.isArray(mapConfig.layers)) {
|
|
|
|
layergroup.metadata.layers = layergroup.metadata.layers.map(function(layer, layerIndex) {
|
|
|
|
if (context.aggregation && Array.isArray(context.aggregation.layers)) {
|
|
|
|
layer.meta.aggregation = context.aggregation.layers[layerIndex];
|
|
|
|
}
|
|
|
|
return layer;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 23:31:45 +08:00
|
|
|
addTileJsonMetadata (layergroup, user, mapconfig, userApiKey = null) {
|
2018-03-09 19:58:05 +08:00
|
|
|
const isVectorOnlyMapConfig = mapconfig.isVectorOnlyMapConfig();
|
|
|
|
let hasMapnikLayers = false;
|
2018-06-22 23:31:45 +08:00
|
|
|
const apiKey = userApiKey ? `?api_key=${userApiKey}` : '';
|
2018-03-09 19:58:05 +08:00
|
|
|
|
|
|
|
layergroup.metadata.layers.forEach((layerMetadata, index) => {
|
|
|
|
const layerId = mapconfig.getLayerId(index);
|
2018-06-22 23:31:45 +08:00
|
|
|
const rasterResource = `${layergroup.layergroupid}/${layerId}/{z}/{x}/{y}.png${apiKey}`;
|
|
|
|
|
2018-03-09 19:58:05 +08:00
|
|
|
if (mapconfig.layerType(index) === 'mapnik') {
|
|
|
|
hasMapnikLayers = true;
|
2018-06-22 23:31:45 +08:00
|
|
|
const vectorResource = `${layergroup.layergroupid}/${layerId}/{z}/{x}/{y}.mvt${apiKey}`;
|
2018-03-09 19:58:05 +08:00
|
|
|
const layerTilejson = {
|
|
|
|
vector: this._getTilejson(this.resourceLocator.getTileUrls(user, vectorResource))
|
|
|
|
};
|
|
|
|
if (!isVectorOnlyMapConfig) {
|
|
|
|
let grids = null;
|
|
|
|
const layer = mapconfig.getLayer(index);
|
|
|
|
if (layer.options.interactivity) {
|
2018-06-22 23:31:45 +08:00
|
|
|
const gridResource = `${layergroup.layergroupid}/${layerId}/{z}/{x}/{y}.grid.json${apiKey}`;
|
2018-03-09 19:58:05 +08:00
|
|
|
grids = this.resourceLocator.getTileUrls(user, gridResource);
|
|
|
|
}
|
|
|
|
layerTilejson.raster = this._getTilejson(
|
|
|
|
this.resourceLocator.getTileUrls(user, rasterResource),
|
|
|
|
grids
|
|
|
|
);
|
|
|
|
}
|
|
|
|
layerMetadata.tilejson = layerTilejson;
|
|
|
|
} else {
|
|
|
|
layerMetadata.tilejson = {
|
|
|
|
raster: this._getTilejson(this.resourceLocator.getTileUrls(user, rasterResource))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const tilejson = {};
|
|
|
|
const url = {};
|
|
|
|
|
|
|
|
if (hasMapnikLayers) {
|
2018-06-22 23:31:45 +08:00
|
|
|
const vectorResource = `${layergroup.layergroupid}/{z}/{x}/{y}.mvt${apiKey}`;
|
2018-03-09 19:58:05 +08:00
|
|
|
tilejson.vector = this._getTilejson(
|
|
|
|
this.resourceLocator.getTileUrls(user, vectorResource)
|
|
|
|
);
|
|
|
|
url.vector = this._getTemplateUrl(this.resourceLocator.getTemplateUrls(user, vectorResource));
|
|
|
|
|
|
|
|
if (!isVectorOnlyMapConfig) {
|
2018-06-22 23:31:45 +08:00
|
|
|
const rasterResource = `${layergroup.layergroupid}/{z}/{x}/{y}.png${apiKey}`;
|
2018-03-09 19:58:05 +08:00
|
|
|
tilejson.raster = this._getTilejson(
|
|
|
|
this.resourceLocator.getTileUrls(user, rasterResource)
|
|
|
|
);
|
|
|
|
url.raster = this._getTemplateUrl(this.resourceLocator.getTemplateUrls(user, rasterResource));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
layergroup.metadata.tilejson = tilejson;
|
|
|
|
layergroup.metadata.url = url;
|
|
|
|
}
|
|
|
|
|
|
|
|
_getTilejson(tiles, grids) {
|
|
|
|
const tilejson = {
|
|
|
|
tilejson: '2.2.0',
|
|
|
|
tiles: tiles.https || tiles.http
|
|
|
|
};
|
|
|
|
|
|
|
|
if (grids) {
|
|
|
|
tilejson.grids = grids.https || grids.http;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tilejson;
|
|
|
|
}
|
|
|
|
|
|
|
|
_getTemplateUrl(url) {
|
|
|
|
return url.https || url.http;
|
|
|
|
}
|
|
|
|
|
|
|
|
addTurboCartoContextMetadata(layergroup, mapConfig, context) {
|
|
|
|
if (layergroup.metadata && Array.isArray(layergroup.metadata.layers) && Array.isArray(mapConfig.layers)) {
|
|
|
|
layergroup.metadata.layers = layergroup.metadata.layers.map(function(layer, layerIndex) {
|
|
|
|
if (context.turboCarto && Array.isArray(context.turboCarto.layers)) {
|
|
|
|
layer.meta.cartocss_meta = context.turboCarto.layers[layerIndex];
|
|
|
|
}
|
|
|
|
return layer;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-06-05 21:39:01 +08:00
|
|
|
|
|
|
|
addDateWrappingMetadata (layergroup, mapConfig) {
|
|
|
|
if (layergroup.metadata && Array.isArray(layergroup.metadata.layers) && Array.isArray(mapConfig.layers)) {
|
|
|
|
layergroup.metadata.layers = layergroup.metadata.layers.map(function(layer, layerIndex) {
|
|
|
|
const mapConfigLayer = mapConfig.layers[layerIndex];
|
2018-06-12 18:05:31 +08:00
|
|
|
const layerOptions = mapConfigLayer.options;
|
|
|
|
if (layerOptions.dates_as_numbers && layerOptions.sql) {
|
2018-06-12 18:59:10 +08:00
|
|
|
const wrappedColumns = dateWrapper.getColumnsWithWrappedDates(layerOptions.sql);
|
2018-06-12 18:05:31 +08:00
|
|
|
if (wrappedColumns) {
|
|
|
|
layer.meta.dates_as_numbers = wrappedColumns;
|
|
|
|
}
|
2018-06-05 21:39:01 +08:00
|
|
|
}
|
|
|
|
return layer;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-09 19:58:05 +08:00
|
|
|
};
|