@@ -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
0 commit comments