Skip to content

Commit ec7718b

Browse files
committedJan 29, 2014
Second part of fix for #9196 (WMTS)
If WMTS capabilities do not include a bounding box, try to detect it from tileset matrix information (instead of using conservative assumption that the layer spans the whole world: -180,-90 - 180,90 in WGS84)
1 parent ba0a637 commit ec7718b

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed
 

‎src/providers/wms/qgswmsprovider.cpp

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2964,12 +2964,6 @@ void QgsWmsProvider::parseWMTSContents( QDomElement const &e )
29642964
}
29652965
}
29662966

2967-
if ( l.boundingBox.crs.isEmpty() )
2968-
{
2969-
l.boundingBox.box = QgsRectangle( -180.0, -90.0, 180.0, 90.0 );
2970-
l.boundingBox.crs = DEFAULT_LATLON_CRS;
2971-
}
2972-
29732967
for ( QDomElement e1 = e0.firstChildElement( "Style" );
29742968
!e1.isNull();
29752969
e1 = e1.nextSiblingElement( "Style" ) )
@@ -3170,6 +3164,61 @@ void QgsWmsProvider::parseWMTSContents( QDomElement const &e )
31703164
mTileThemes << QgsWmtsTheme();
31713165
parseTheme( e0, mTileThemes.back() );
31723166
}
3167+
3168+
// make sure that all layers have a bounding box
3169+
for( QList<QgsWmtsTileLayer>::iterator it = mTileLayersSupported.begin(); it != mTileLayersSupported.end(); ++it )
3170+
{
3171+
QgsWmtsTileLayer& l = *it;
3172+
3173+
if ( l.boundingBox.crs.isEmpty() )
3174+
{
3175+
if ( !detectTileLayerBoundingBox( l ) )
3176+
{
3177+
QgsDebugMsg( "failed to detect bounding box for " + l.identifier + " - using extent of the whole world" );
3178+
l.boundingBox.box = QgsRectangle( -180.0, -90.0, 180.0, 90.0 );
3179+
l.boundingBox.crs = DEFAULT_LATLON_CRS;
3180+
}
3181+
}
3182+
}
3183+
}
3184+
3185+
3186+
bool QgsWmsProvider::detectTileLayerBoundingBox( QgsWmtsTileLayer& l )
3187+
{
3188+
if ( l.setLinks.isEmpty() )
3189+
return false;
3190+
3191+
// take first supported tile matrix set
3192+
const QgsWmtsTileMatrixSetLink& setLink = l.setLinks.constBegin().value();
3193+
3194+
QHash<QString, QgsWmtsTileMatrixSet>::const_iterator tmsIt = mTileMatrixSets.constFind( setLink.tileMatrixSet );
3195+
if ( tmsIt == mTileMatrixSets.constEnd() )
3196+
return false;
3197+
3198+
QgsCoordinateReferenceSystem crs;
3199+
if ( !crs.createFromOgcWmsCrs( tmsIt->crs ) )
3200+
return false;
3201+
3202+
// take most coarse tile matrix ...
3203+
QMap<double, QgsWmtsTileMatrix>::const_iterator tmIt = tmsIt->tileMatrices.constEnd() - 1;
3204+
if ( tmIt == tmsIt->tileMatrices.constEnd() )
3205+
return false;
3206+
3207+
const QgsWmtsTileMatrix& tm = *tmIt;
3208+
double metersPerUnit = QGis::fromUnitToUnitFactor( crs.mapUnits(), QGis::Meters );
3209+
double res = tm.scaleDenom * 0.00028 / metersPerUnit;
3210+
QgsPoint bottomRight( tm.topLeft.x() + res * tm.tileWidth * tm.matrixWidth,
3211+
tm.topLeft.y() - res * tm.tileHeight * tm.matrixHeight );
3212+
3213+
QgsDebugMsg( QString( "detecting WMTS layer bounding box: tileset %1 matrix %2 crs %3 res %4" )
3214+
.arg( tmsIt->identifier ).arg( tm.identifier ).arg( tmsIt->crs ).arg( res ) );
3215+
3216+
QgsRectangle extent( tm.topLeft, bottomRight );
3217+
extent.normalize();
3218+
3219+
l.boundingBox.box = extent;
3220+
l.boundingBox.crs = tmsIt->crs;
3221+
return true;
31733222
}
31743223

31753224

‎src/providers/wms/qgswmsprovider.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,12 @@ class QgsWmsProvider : public QgsRasterDataProvider
783783
*/
784784
bool extentForNonTiledLayer( const QString& layerName, const QString& crs, QgsRectangle& extent );
785785

786+
/**
787+
* In case no bounding box is present in WMTS capabilities, try to estimate it from tile matrix sets.
788+
* Returns true if the detection went fine.
789+
*/
790+
bool detectTileLayerBoundingBox( QgsWmtsTileLayer& l );
791+
786792
// case insensitive attribute value lookup
787793
static QString nodeAttribute( const QDomElement &e, QString name, QString defValue = QString::null );
788794

0 commit comments

Comments
 (0)
Please sign in to comment.