From 5a252e628bc9e2d2cc1ed95c4a05d22bf151665c Mon Sep 17 00:00:00 2001 From: oterral Date: Thu, 9 Nov 2017 15:09:33 +0100 Subject: [PATCH 1/2] Fix #7460: use the matrixSet projection by default --- src/ol/source/wmts.js | 20 ++++++++++++++------ src/ol/tilegrid/wmts.js | 5 +++-- test/spec/ol/source/wmts.test.js | 24 ++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/ol/source/wmts.js b/src/ol/source/wmts.js index 7ec74c36a4..e6102f5260 100644 --- a/src/ol/source/wmts.js +++ b/src/ol/source/wmts.js @@ -314,8 +314,9 @@ ol.source.WMTS.optionsFromCapabilities = function(wmtsCap, config) { var tileMatrixSet = ol.array.find(tileMatrixSets, function(el) { return el['Identifier'] == elt['TileMatrixSet']; }); - var supportedCRS = tileMatrixSet['SupportedCRS'].replace(/urn:ogc:def:crs:(\w+):(.*:)?(\w+)$/, '$1:$3'); - var proj1 = ol.proj.get(supportedCRS); + var supportedCRS = tileMatrixSet['SupportedCRS']; + var proj1 = ol.proj.get(supportedCRS.replace(/urn:ogc:def:crs:(\w+):(.*:)?(\w+)$/, '$1:$3')) || + ol.proj.get(supportedCRS); var proj2 = ol.proj.get(config['projection']); if (proj1 && proj2) { return ol.proj.equivalent(proj1, proj2); @@ -374,11 +375,18 @@ ol.source.WMTS.optionsFromCapabilities = function(wmtsCap, config) { }); var projection; + var code = matrixSetObj['SupportedCRS']; + if (code) { + projection = ol.proj.get(code.replace(/urn:ogc:def:crs:(\w+):(.*:)?(\w+)$/, '$1:$3')) || + ol.proj.get(code); + } if ('projection' in config) { - projection = ol.proj.get(config['projection']); - } else { - projection = ol.proj.get(matrixSetObj['SupportedCRS'].replace( - /urn:ogc:def:crs:(\w+):(.*:)?(\w+)$/, '$1:$3')); + var projConfig = ol.proj.get(config['projection']); + if (projConfig) { + if (!projection || ol.proj.equivalent(projConfig, projection)) { + projection = projConfig; + } + } } var wgs84BoundingBox = l['WGS84BoundingBox']; diff --git a/src/ol/tilegrid/wmts.js b/src/ol/tilegrid/wmts.js index 46c0d239e7..58faa910e6 100644 --- a/src/ol/tilegrid/wmts.js +++ b/src/ol/tilegrid/wmts.js @@ -93,8 +93,9 @@ ol.tilegrid.WMTS.createFromCapabilitiesMatrixSet = function(matrixSet, opt_exten var tileHeightPropName = 'TileHeight'; var projection; - projection = ol.proj.get(matrixSet[supportedCRSPropName].replace( - /urn:ogc:def:crs:(\w+):(.*:)?(\w+)$/, '$1:$3')); + var code = matrixSet[supportedCRSPropName]; + projection = ol.proj.get(code.replace(/urn:ogc:def:crs:(\w+):(.*:)?(\w+)$/, '$1:$3')) || + ol.proj.get(code); var metersPerUnit = projection.getMetersPerUnit(); // swap origin x and y coordinates if axis orientation is lat/long var switchOriginXY = projection.getAxisOrientation().substr(0, 2) == 'ne'; diff --git a/test/spec/ol/source/wmts.test.js b/test/spec/ol/source/wmts.test.js index e5aeba8ef4..caaf6dd0ae 100644 --- a/test/spec/ol/source/wmts.test.js +++ b/test/spec/ol/source/wmts.test.js @@ -113,6 +113,7 @@ describe('ol.source.WMTS', function() { }); expect(options.matrixSet).to.be.eql('google3857'); + expect(options.projection.getCode()).to.be.eql('EPSG:3857'); }); it('can find a MatrixSet by equivalent SRS identifier', function() { @@ -123,8 +124,31 @@ describe('ol.source.WMTS', function() { }); expect(options.matrixSet).to.be.eql('google3857'); + expect(options.projection.getCode()).to.be.eql('EPSG:900913'); }); + it('can find the default MatrixSet', function() { + var options = ol.source.WMTS.optionsFromCapabilities(capabilities, { + layer: 'BlueMarbleNextGeneration', + requestEncoding: 'REST' + }); + + expect(options.matrixSet).to.be.eql('BigWorldPixel'); + expect(options.projection.getCode()).to.be.eql('urn:ogc:def:crs:OGC:1.3:CRS84'); + }); + + it('uses the projection of the default MatrixSet if the config\'s projection is not supported', function() { + var options = ol.source.WMTS.optionsFromCapabilities(capabilities, { + layer: 'BlueMarbleNextGeneration', + projection: new ol.proj.Projection({ + code: 'EPSG:2056', + units: 'm' + }) + }); + + expect(options.matrixSet).to.be.eql('BigWorldPixel'); + expect(options.projection.getCode()).to.be.eql('urn:ogc:def:crs:OGC:1.3:CRS84'); + }); }); describe('when creating tileUrlFunction', function() { From 9cc7cca447fc4690d26a76e4488b6196909890b2 Mon Sep 17 00:00:00 2001 From: oterral Date: Fri, 17 Nov 2017 08:59:23 +0100 Subject: [PATCH 2/2] Fix #6835: doesn't break if Constraint does not exist --- src/ol/source/wmts.js | 31 ++++++++++++++++++------------- src/ol/tilegrid/wmts.js | 3 +-- test/spec/ol/source/wmts.test.js | 29 ++++++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 16 deletions(-) diff --git a/src/ol/source/wmts.js b/src/ol/source/wmts.js index e6102f5260..0611f5505c 100644 --- a/src/ol/source/wmts.js +++ b/src/ol/source/wmts.js @@ -419,21 +419,26 @@ ol.source.WMTS.optionsFromCapabilities = function(wmtsCap, config) { var gets = wmtsCap['OperationsMetadata']['GetTile']['DCP']['HTTP']['Get']; for (var i = 0, ii = gets.length; i < ii; ++i) { - var constraint = ol.array.find(gets[i]['Constraint'], function(element) { - return element['name'] == 'GetEncoding'; - }); - var encodings = constraint['AllowedValues']['Value']; + if (gets[i]['Constraint']) { + var constraint = ol.array.find(gets[i]['Constraint'], function(element) { + return element['name'] == 'GetEncoding'; + }); + var encodings = constraint['AllowedValues']['Value']; - if (requestEncoding === '') { - // requestEncoding not provided, use the first encoding from the list - requestEncoding = encodings[0]; - } - if (requestEncoding === ol.source.WMTSRequestEncoding.KVP) { - if (ol.array.includes(encodings, ol.source.WMTSRequestEncoding.KVP)) { - urls.push(/** @type {string} */ (gets[i]['href'])); + if (requestEncoding === '') { + // requestEncoding not provided, use the first encoding from the list + requestEncoding = encodings[0]; } - } else { - break; + if (requestEncoding === ol.source.WMTSRequestEncoding.KVP) { + if (ol.array.includes(encodings, ol.source.WMTSRequestEncoding.KVP)) { + urls.push(/** @type {string} */ (gets[i]['href'])); + } + } else { + break; + } + } else if (gets[i]['href']) { + requestEncoding = ol.source.WMTSRequestEncoding.KVP; + urls.push(/** @type {string} */ (gets[i]['href'])); } } } diff --git a/src/ol/tilegrid/wmts.js b/src/ol/tilegrid/wmts.js index 58faa910e6..c67ff01f18 100644 --- a/src/ol/tilegrid/wmts.js +++ b/src/ol/tilegrid/wmts.js @@ -92,9 +92,8 @@ ol.tilegrid.WMTS.createFromCapabilitiesMatrixSet = function(matrixSet, opt_exten var tileWidthPropName = 'TileWidth'; var tileHeightPropName = 'TileHeight'; - var projection; var code = matrixSet[supportedCRSPropName]; - projection = ol.proj.get(code.replace(/urn:ogc:def:crs:(\w+):(.*:)?(\w+)$/, '$1:$3')) || + var projection = ol.proj.get(code.replace(/urn:ogc:def:crs:(\w+):(.*:)?(\w+)$/, '$1:$3')) || ol.proj.get(code); var metersPerUnit = projection.getMetersPerUnit(); // swap origin x and y coordinates if axis orientation is lat/long diff --git a/test/spec/ol/source/wmts.test.js b/test/spec/ol/source/wmts.test.js index caaf6dd0ae..e2674a29d4 100644 --- a/test/spec/ol/source/wmts.test.js +++ b/test/spec/ol/source/wmts.test.js @@ -11,10 +11,11 @@ describe('ol.source.WMTS', function() { describe('when creating options from capabilities', function() { var parser = new ol.format.WMTSCapabilities(); - var capabilities; + var capabilities, content; before(function(done) { afterLoadText('spec/ol/format/wmts/ogcsample.xml', function(xml) { try { + content = xml; capabilities = parser.read(xml); } catch (e) { done(e); @@ -149,6 +150,32 @@ describe('ol.source.WMTS', function() { expect(options.matrixSet).to.be.eql('BigWorldPixel'); expect(options.projection.getCode()).to.be.eql('urn:ogc:def:crs:OGC:1.3:CRS84'); }); + + it('doesn\'t fail if the GetCap doesn\'t contains Constraint tags', function() { + var tmpXml = content.replace(//g, ''); + var tmpCapabilities = parser.read(tmpXml); + expect(tmpCapabilities['OperationsMetadata']['GetTile']['DCP']['HTTP']['Get'][0]['Constraint']).to.be(undefined); + var options = ol.source.WMTS.optionsFromCapabilities(tmpCapabilities, + {layer: 'BlueMarbleNextGeneration', matrixSet: 'google3857'}); + expect(options.layer).to.be.eql('BlueMarbleNextGeneration'); + expect(options.matrixSet).to.be.eql('google3857'); + }); + + it('set KVP as default request encoding if the GetCap doesn\'t contains Constraint and ResourceUrl tags', function() { + var tmpXml = content.replace(//g, ''); + tmpXml = tmpXml.replace(//g, ''); + + var tmpCapabilities = parser.read(tmpXml); + expect(tmpCapabilities['OperationsMetadata']['GetTile']['DCP']['HTTP']['Get'][0]['Constraint']).to.be(undefined); + expect(tmpCapabilities['Contents']['Layer'][0]['ResourceURL']).to.be(undefined); + var options = ol.source.WMTS.optionsFromCapabilities(tmpCapabilities, + {layer: 'BlueMarbleNextGeneration', matrixSet: 'google3857'}); + expect(options.layer).to.be.eql('BlueMarbleNextGeneration'); + expect(options.matrixSet).to.be.eql('google3857'); + expect(options.urls).to.be.an('array'); + expect(options.urls).to.have.length(1); + expect(options.urls[0]).to.be.eql('http://www.maps.bob/cgi-bin/MiraMon5_0.cgi?'); + }); }); describe('when creating tileUrlFunction', function() {