From 906a132e89ebf0c950d3cdc099828cb7c76d781f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20Dub=C3=A9?= Date: Thu, 21 Jan 2016 10:54:26 -0500 Subject: [PATCH] Add layerName option for WMSGetFeatureInfo format This option allows the format to read only features of a given layer. This is useful if you wish to make a single query to a WMS server with multiple layers in it. --- .../osm-restaurant-hotel.xml | 283 ++++++++++++++++++ examples/getfeatureinfo-layers.html | 28 ++ examples/getfeatureinfo-layers.js | 24 ++ externs/olx.js | 18 ++ src/ol/format/wmsgetfeatureinfoformat.js | 21 +- .../ol/format/wmsgetfeatureinfoformat.test.js | 12 + 6 files changed, 384 insertions(+), 2 deletions(-) create mode 100644 examples/data/wmsgetfeatureinfo/osm-restaurant-hotel.xml create mode 100644 examples/getfeatureinfo-layers.html create mode 100644 examples/getfeatureinfo-layers.js diff --git a/examples/data/wmsgetfeatureinfo/osm-restaurant-hotel.xml b/examples/data/wmsgetfeatureinfo/osm-restaurant-hotel.xml new file mode 100644 index 0000000000..4e04180659 --- /dev/null +++ b/examples/data/wmsgetfeatureinfo/osm-restaurant-hotel.xml @@ -0,0 +1,283 @@ + + + + + Restaurants + + + + 537750.006175,150923.784252 537750.006175,150923.784252 + + + + + 537750.006175,150923.784252 + + + Château d'Ouchy + Château d'Ouchy + 1230808910 + + + restaurant + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 537770.247124,150975.613968 537770.247124,150975.613968 + + + + + 537770.247124,150975.613968 + + + Cafe du Vieil Ouchy + Cafe du Vieil Ouchy + 1433812389 + + + restaurant + + + + + + + + + + + + + + + Carine Duca + + + + + + + + + + + + + + + + + 537789.197617,150976.218227 537789.197617,150976.218227 + + + + + 537789.197617,150976.218227 + + + Creperie + Creperie + 1433812391 + + + restaurant + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 537810.679909,150983.377694 537810.679909,150983.377694 + + + + + 537810.679909,150983.377694 + + + 1433812390 + + 1433812390 + + + restaurant + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Hôtels + + + + 537762.425297,150971.904013 537762.425297,150971.904013 + + + + + 537762.425297,150971.904013 + + + Hotel du port + Hotel du port + 2886793101 + + + + + + + + + + + + + + + + + + + + + + + + + + + + hotel + + + + + + + 537798.352160,150985.584164 537798.352160,150985.584164 + + + + + 537798.352160,150985.584164 + + + Angleterre + Angleterre + 1433812387 + + + + + + + + + + + + + + + + + + + + + + + + + + + + hotel + + + + + diff --git a/examples/getfeatureinfo-layers.html b/examples/getfeatureinfo-layers.html new file mode 100644 index 0000000000..c8445a413a --- /dev/null +++ b/examples/getfeatureinfo-layers.html @@ -0,0 +1,28 @@ +--- +layout: example.html +title: WMS GetFeatureInfo (Layers) +shortdesc: > + Shows how to fetch features per layer name in a single WMS GetFeatureInfo + request +docs: > + Demonstrates the use of the `layers` option in the + `ol.format.WMSGetFeatureInfo` format object, which allows features returned + by a single WMS GetFeatureInfo request that asks for more than one layer + to be read by layer name. +resources: + - https://code.jquery.com/jquery-1.11.2.min.js +--- + + + + + + + + + + + + + +
All features:
Hotel features:
Restaurant features:
diff --git a/examples/getfeatureinfo-layers.js b/examples/getfeatureinfo-layers.js new file mode 100644 index 0000000000..d6e06664b6 --- /dev/null +++ b/examples/getfeatureinfo-layers.js @@ -0,0 +1,24 @@ +goog.require('ol.format.WMSGetFeatureInfo'); + +$.ajax({ + url: './data/wmsgetfeatureinfo/osm-restaurant-hotel.xml', + success: function(response) { + + // this is the standard way to read the features + var allFeatures = new ol.format.WMSGetFeatureInfo().readFeatures(response); + $('#all').html(allFeatures.length.toString()); + + // when specifying the 'layers' options, only the features of those + // layers are returned by the format + var hotelFeatures = new ol.format.WMSGetFeatureInfo({ + layers: ['hotel'] + }).readFeatures(response); + $('#hotel').html(hotelFeatures.length.toString()); + + var restaurantFeatures = new ol.format.WMSGetFeatureInfo({ + layers: ['restaurant'] + }).readFeatures(response); + $('#restaurant').html(restaurantFeatures.length.toString()); + + } +}); diff --git a/externs/olx.js b/externs/olx.js index c90548e044..3eb5418644 100644 --- a/externs/olx.js +++ b/externs/olx.js @@ -2217,6 +2217,24 @@ olx.format.WKTOptions; olx.format.WKTOptions.prototype.splitCollection; +/** + * @typedef {{ + * layers: (Array.|undefined) + * }} + * @api + */ +olx.format.WMSGetFeatureInfoOptions; + + +/** + * If set, only features of the given layers will be returned by the format + * when read. + * @type {Array.|undefined} + * @api + */ +olx.format.WMSGetFeatureInfoOptions.prototype.layers; + + /** * Namespace. * @type {Object} diff --git a/src/ol/format/wmsgetfeatureinfoformat.js b/src/ol/format/wmsgetfeatureinfoformat.js index ab5c577d01..0157b197fe 100644 --- a/src/ol/format/wmsgetfeatureinfoformat.js +++ b/src/ol/format/wmsgetfeatureinfoformat.js @@ -4,6 +4,7 @@ goog.require('goog.array'); goog.require('goog.asserts'); goog.require('goog.dom.NodeType'); goog.require('goog.object'); +goog.require('ol.array'); goog.require('ol.format.GML2'); goog.require('ol.format.XMLFeature'); goog.require('ol.xml'); @@ -16,9 +17,12 @@ goog.require('ol.xml'); * * @constructor * @extends {ol.format.XMLFeature} + * @param {olx.format.WMSGetFeatureInfoOptions=} opt_options Options. * @api */ -ol.format.WMSGetFeatureInfo = function() { +ol.format.WMSGetFeatureInfo = function(opt_options) { + + var options = opt_options ? opt_options : {}; /** * @private @@ -33,6 +37,13 @@ ol.format.WMSGetFeatureInfo = function() { */ this.gmlFormat_ = new ol.format.GML2(); + + /** + * @private + * @type {Array.} + */ + this.layers_ = options.layers ? options.layers : null; + goog.base(this); }; goog.inherits(ol.format.WMSGetFeatureInfo, ol.format.XMLFeature); @@ -86,7 +97,13 @@ ol.format.WMSGetFeatureInfo.prototype.readFeatures_ = function(node, objectStack 'localName of layer node should match layerIdentifier'); var toRemove = ol.format.WMSGetFeatureInfo.layerIdentifier_; - var featureType = layer.localName.replace(toRemove, '') + + var layerName = layer.localName.replace(toRemove, ''); + + if (this.layers_ && !ol.array.includes(this.layers_, layerName)) { + continue; + } + + var featureType = layerName + ol.format.WMSGetFeatureInfo.featureIdentifier_; context['featureType'] = featureType; diff --git a/test/spec/ol/format/wmsgetfeatureinfoformat.test.js b/test/spec/ol/format/wmsgetfeatureinfoformat.test.js index f49f8e8129..395b052b5f 100644 --- a/test/spec/ol/format/wmsgetfeatureinfoformat.test.js +++ b/test/spec/ol/format/wmsgetfeatureinfoformat.test.js @@ -140,6 +140,18 @@ describe('ol.format.WMSGetFeatureInfo', function() { expect(features.length).to.be(2); expect(features[0].get('OBJECTID')).to.be('287'); expect(features[1].get('OBJECTID')).to.be('1251'); + var aaa64Features = new ol.format.WMSGetFeatureInfo({ + layers: ['AAA64'] + }).readFeatures(text); + expect(aaa64Features.length).to.be(1); + var allFeatures = new ol.format.WMSGetFeatureInfo({ + layers: ['AAA64', 'AAA62'] + }).readFeatures(text); + expect(allFeatures.length).to.be(2); + var dummyFeatures = new ol.format.WMSGetFeatureInfo({ + layers: ['foo', 'bar'] + }).readFeatures(text); + expect(dummyFeatures.length).to.be(0); }); it('read geoserver’s response', function() {