Skip to content

Commit

Permalink
Merge pull request #2425 from camptocamp/query
Browse files Browse the repository at this point in the history
Query the layer only on when he is visible
  • Loading branch information
sbrunner committed Mar 10, 2017
2 parents 331c4b0 + adff5d6 commit 7c644b4
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 117 deletions.
81 changes: 42 additions & 39 deletions contribs/gmf/src/services/querymanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,41 @@ goog.require('ngeo.Query');
*
* @constructor
* @struct
* @param {ngeo.Query} ngeoQuery The ngeo Query service.
* @param {gmf.Themes} gmfThemes The gmf Themes service.
* @param {angular.$q} $q Angular q service
* @param {!ngeo.Query} ngeoQuery The ngeo Query service.
* @param {!gmf.Themes} gmfThemes The gmf Themes service.
* @param {!angular.$q} $q Angular q service
* @ngInject
* @ngdoc service
* @ngname gmfThemes
*/
gmf.QueryManager = function(ngeoQuery, gmfThemes, $q) {

/**
* @type {ngeo.Query}
* @type {!ngeo.Query}
* @private
*/
this.ngeoQuery_ = ngeoQuery;

/**
* @type {gmf.Themes}
* @type {!gmf.Themes}
* @private
*/
this.gmfThemes_ = gmfThemes;

/**
* @type {angular.$q}
* @type {!angular.$q}
* @private
*/
this.$q_ = $q;

/**
* @type {Array.<ngeox.QuerySource>}
* @type {!Array.<!ngeox.QuerySource>}
* @private
*/
this.sources_ = [];

/**
* @type {Object.<number|string, ngeox.QuerySource>}
* @type {!Object.<number|string, !ngeox.QuerySource>}
* @private
*/
this.cache_ = {};
Expand Down Expand Up @@ -144,8 +144,9 @@ gmf.QueryManager.prototype.createSources_ = function(firstLevelGroup, node, ogcS
// Don't create sources for WMTS layers without wmsUrl and ogcServer,
// they are not queryable.
if (gmfLayer.type === 'WMTS') {
layers = meta.queryLayers || meta.wmsLayers;
if (layers && meta.ogcServer && ogcServers[meta.ogcServer]) {
var layers_ = meta.queryLayers || meta.wmsLayers;
if (layers_ && meta.ogcServer && ogcServers[meta.ogcServer]) {
layers = layers_.split(',');
ogcServer = ogcServers[meta.ogcServer];
} else {
return;
Expand All @@ -156,7 +157,7 @@ gmf.QueryManager.prototype.createSources_ = function(firstLevelGroup, node, ogcS
var gmfLayerWMS;
if (gmfLayer.type === 'WMS') {
gmfLayerWMS = /** @type gmfThemes.GmfLayerWMS */ (gmfLayer);
layers = gmfLayerWMS.layers;
layers = gmfLayerWMS.layers.split(',');
if (!firstLevelGroup || firstLevelGroup.mixed) {
goog.asserts.assert(gmfLayerWMS.ogcServer);
ogcServer = ogcServers[/** @type string */ (gmfLayerWMS.ogcServer)];
Expand All @@ -165,43 +166,45 @@ gmf.QueryManager.prototype.createSources_ = function(firstLevelGroup, node, ogcS
ogcServer = ogcServers[/** @type string */ (firstLevelGroup.ogcServer)];
}
}
var childLayers = layers;
if (!this.cache_[id]) {
if (validateLayerParams) {
// Some nodes have child layers, i.e. a list of layer names that are
// part of a group. The name of the group itself can't be used 'as-is'
// as an identifier of the layers for this source. For example, a
// group named 'osm' might result in returning 'restaurant' features.
// This override makes sure that those layer names are used instead of
// the original one.
if (gmfLayerWMS.childLayers && gmfLayerWMS.childLayers.length) {
// skip layers with no queryable childLayer
var isQueryable = function(item) {
return item.queryable;
};
if (!gmfLayerWMS.childLayers.some(isQueryable)) {
return;
}

var childLayerNames = [];
gmfLayerWMS.childLayers.forEach(function(childLayer) {
if (childLayer.queryable) {
childLayerNames.push(childLayer.name);
}
}, this);
childLayers = childLayerNames.join(',');
}
}

goog.asserts.assert(ogcServer.urlWfs);
goog.asserts.assert(childLayers);
goog.asserts.assert(layers);

var source = {
'id': id,
'identifierAttributeField': identifierAttributeField,
'label': name,
'params': {'LAYERS': childLayers},
'getLayers': function(resolution) {
var childLayers = layers;
goog.asserts.assert(childLayers);
if (validateLayerParams) {
// Some nodes have child layers, i.e. a list of layer names that are
// part of a group. The name of the group itself can't be used 'as-is'
// as an identifier of the layers for this source. For example, a
// group named 'osm' might result in returning 'restaurant' features.
// This override makes sure that those layer names are used instead of
// the original one.
if (gmfLayerWMS.childLayers && gmfLayerWMS.childLayers.length) {
// skip layers with no queryable childLayer
var isQueryable = function(item) {
return item.queryable && resolution >= item.minResolutionHint && resolution <= item.maxResolutionHint;
};
if (!gmfLayerWMS.childLayers.some(isQueryable)) {
return [];
}

var childLayerNames = [];
gmfLayerWMS.childLayers.forEach(function(childLayer) {
if (childLayer.queryable) {
childLayerNames.push(childLayer.name);
}
}, this);
childLayers = childLayerNames;
}
}
return childLayers;
},
'layers': layers,
'dimensions': node.dimensions || firstLevelGroup.dimensions,
'url': ogcServer.urlWfs,
Expand Down
12 changes: 6 additions & 6 deletions contribs/gmf/test/spec/services/querymanager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('gmf.QueryManager', function() {
// background layer
var bgLayerSource = getSourceById(queryManager.sources_, 115);
expect(bgLayerSource).not.toBeNull();
expect(bgLayerSource.params.LAYERS).toBe('ch.swisstopo.dreiecksvermaschung');
expect(bgLayerSource.getLayers(0).join(',')).toBe('ch.swisstopo.dreiecksvermaschung');
expect(bgLayerSource.url).toBe('https://wms.geo.admin.ch?lang=fr');
});
});
Expand All @@ -62,22 +62,22 @@ describe('gmf.QueryManager', function() {
// Child 0 (osm_time) is queryable and has wfs support.
children = firstLevelGroup.children[0]; // osm_time
osmSource = getSourceById(queryManager.sources_, children.id);
expect(osmSource.params.LAYERS).toBe('osm_time');
expect(osmSource.getLayers(0).join(',')).toBe('osm_time');
expect(osmSource.wfsQuery).toBe(true);

// Child 8 (srtm) is not queryable
children = firstLevelGroup.children[8];
osmSource = getSourceById(queryManager.sources_, children.id);
expect(children.childLayers[0].queryable).toBe(false);
expect(osmSource).toBeNull();
expect(osmSource.getLayers(0).join(',')).toBe('');
});

it('Creates sources on queryable layer without WFS support', function() {
var osmTheme = gmf.Themes.findThemeByName(themes.themes, 'Cadastre');
var firstLevelGroup = osmTheme.children[0]; // 'Cadastre'
queryManager.createSources_(firstLevelGroup, firstLevelGroup, themes.ogcServers);
var osmSource = getSourceById(queryManager.sources_, 115);
expect(osmSource.params.LAYERS).toBe('ch.swisstopo.dreiecksvermaschung');
expect(osmSource.getLayers(0).join(',')).toBe('ch.swisstopo.dreiecksvermaschung');
expect(osmSource.wfsQuery).toBe(false);
});

Expand All @@ -95,12 +95,12 @@ describe('gmf.QueryManager', function() {
// `queryLayers`. (`queryLayers` takes precedence over `wmsLayers`)
var sourceAlpConvention = getSourceById(queryManager.sources_, 115);
expect(sourceAlpConvention).not.toBeNull();
expect(sourceAlpConvention.params.LAYERS).toBe('ch.swisstopo.dreiecksvermaschung');
expect(sourceAlpConvention.getLayers(0).join(',')).toBe('ch.swisstopo.dreiecksvermaschung');

// layer 'ch.astra.ausnahmetransportrouten' with `wmsUrl` and `queryLayers`
var sourceRoutes = getSourceById(queryManager.sources_, 116);
expect(sourceRoutes).not.toBeNull();
expect(sourceRoutes.params.LAYERS).toBe('ch.swisstopo.geologie-gravimetrischer_atlas');
expect(sourceRoutes.getLayers(0).join(',')).toBe('ch.swisstopo.geologie-gravimetrischer_atlas');
});

// FIXME no data to run this test
Expand Down
2 changes: 2 additions & 0 deletions examples/bboxquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ app.MainController = function($scope, ngeoQuery) {
ngeoQuery.addSource({
'id': busStopSourceId,
'layer': busStopLayer,
'layers': ['bus_stop'],
'wfsQuery': true
});

Expand All @@ -104,6 +105,7 @@ app.MainController = function($scope, ngeoQuery) {
ngeoQuery.addSource({
'id': informationSourceId,
'layer': informationLayer,
'layers': ['information'],
'wfsQuery': true
});

Expand Down
6 changes: 4 additions & 2 deletions examples/mapquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ app.MainController = function($scope, ngeoQuery, ngeoToolActivateMgr) {
});
ngeoQuery.addSource({
'id': busStopSourceId,
'layer': busStopLayer
'layer': busStopLayer,
layers: ['bus_stop']
});

var informationSourceId = 'information';
Expand All @@ -112,7 +113,8 @@ app.MainController = function($scope, ngeoQuery, ngeoToolActivateMgr) {
});
ngeoQuery.addSource({
'id': informationSourceId,
'layer': informationLayer
'layer': informationLayer,
layers: ['information']
});

/**
Expand Down
25 changes: 12 additions & 13 deletions options/ngeox.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,18 +428,18 @@ ngeox.QueryOptions.prototype.geometryName;
/**
* The configuration of a source for the Query service.
* @typedef {{
* format: (ol.format.Feature|undefined),
* format: (!ol.format.Feature|undefined),
* id: (number|string),
* identifierAttributeField: (string|undefined),
* infoFormat: (string|undefined),
* label: (string|undefined),
* layer: (ol.layer.Base|undefined),
* layers: (string|undefined),
* params: (Object.<string, *>|undefined),
* layer: (!ol.layer.Base|undefined),
* layers: (!Array.<string>|undefined),
* getLayers: (function(number): !Array.<string>|undefined),
* serverType: (string|undefined),
* url: (string|undefined),
* validateLayerParams: (boolean|undefined),
* wmsSource: (ol.source.ImageWMS|ol.source.TileWMS|undefined),
* wmsSource: (!ol.source.ImageWMS|!ol.source.TileWMS|undefined),
* wfsQuery: (boolean|undefined)
* }}
*/
Expand All @@ -448,7 +448,7 @@ ngeox.QuerySource;

/**
* The used to read the returned features from query requests for this source.
* @type {ol.format.Feature|undefined}
* @type {!ol.format.Feature|undefined}
*/
ngeox.QuerySource.prototype.format;

Expand Down Expand Up @@ -487,24 +487,23 @@ ngeox.QuerySource.prototype.label;
* A reference to the ol3 layer object. If not defined, will be automatically
* fetched using the source `name` and the according layer property that has
* the same value.
* @type {ol.layer.Base|undefined}
* @type {!ol.layer.Base|undefined}
*/
ngeox.QuerySource.prototype.layer;


/**
* A reference to the ol3 layers names. Multiple layers names can be separated
* by a comma.
* @type {string|undefined}
* A reference to the ol3 layers names. Multiple layers names.
* @type {!Array.<string>|undefined}
*/
ngeox.QuerySource.prototype.layers;


/**
* Additionnal params to use when querying this source.
* @type {Object.<string, *>|undefined}
* @type {function(number): !Array.<string>|undefined}
*/
ngeox.QuerySource.prototype.params;
ngeox.QuerySource.prototype.getLayers;


/**
Expand Down Expand Up @@ -547,7 +546,7 @@ ngeox.QuerySource.prototype.validateLayerParams;
* the layer source object will be used (if it's WMS), otherwise one will
* be created by the query service using the `url` and `params` properties of
* this source.
* @type {ol.source.ImageWMS|ol.source.TileWMS|undefined}
* @type {!ol.source.ImageWMS|!ol.source.TileWMS|undefined}
*/
ngeox.QuerySource.prototype.wmsSource;

Expand Down
Loading

0 comments on commit 7c644b4

Please sign in to comment.