diff --git a/python/core/auto_generated/vectortile/qgsmapboxglstyleconverter.sip.in b/python/core/auto_generated/vectortile/qgsmapboxglstyleconverter.sip.in index e6d17b9cfc2..b9c8c0474e1 100644 --- a/python/core/auto_generated/vectortile/qgsmapboxglstyleconverter.sip.in +++ b/python/core/auto_generated/vectortile/qgsmapboxglstyleconverter.sip.in @@ -250,6 +250,13 @@ Returns the associated tile size. QStringList tiles() const; %Docstring Returns the list of tile sources. +%End + + QgsRasterLayer *toRasterLayer() const /Factory/; +%Docstring +Returns a new raster layer representing the raster source, or ``None`` if the source cannot be represented as a raster layer. + +The caller takes ownership of the returned layer. %End }; diff --git a/src/core/vectortile/qgsmapboxglstyleconverter.cpp b/src/core/vectortile/qgsmapboxglstyleconverter.cpp index 999bbdb1be9..29f7f7e55aa 100644 --- a/src/core/vectortile/qgsmapboxglstyleconverter.cpp +++ b/src/core/vectortile/qgsmapboxglstyleconverter.cpp @@ -39,6 +39,8 @@ #include "qgsapplication.h" #include "qgsfontmanager.h" #include "qgis.h" +#include "qgsrasterlayer.h" +#include "qgsproviderregistry.h" #include #include @@ -3581,3 +3583,21 @@ bool QgsMapBoxGlStyleRasterSource::setFromJson( const QVariantMap &json, QgsMapB return true; } + +QgsRasterLayer *QgsMapBoxGlStyleRasterSource::toRasterLayer() const +{ + QVariantMap parts; + parts.insert( QStringLiteral( "type" ), QStringLiteral( "xyz" ) ); + parts.insert( QStringLiteral( "url" ), mTiles.value( 0 ) ); + + if ( mTileSize == 256 ) + parts.insert( QStringLiteral( "tilePixelRation" ), QStringLiteral( "1" ) ); + else if ( mTileSize == 512 ) + parts.insert( QStringLiteral( "tilePixelRation" ), QStringLiteral( "2" ) ); + + parts.insert( QStringLiteral( "zmax" ), QString::number( mMaxZoom ) ); + parts.insert( QStringLiteral( "zmin" ), QString::number( mMinZoom ) ); + + std::unique_ptr< QgsRasterLayer > rl = std::make_unique< QgsRasterLayer >( QgsProviderRegistry::instance()->encodeUri( QStringLiteral( "wms" ), parts ), name(), QStringLiteral( "wms" ) ); + return rl.release(); +} diff --git a/src/core/vectortile/qgsmapboxglstyleconverter.h b/src/core/vectortile/qgsmapboxglstyleconverter.h index 7adcdead210..a50fa7c10a8 100644 --- a/src/core/vectortile/qgsmapboxglstyleconverter.h +++ b/src/core/vectortile/qgsmapboxglstyleconverter.h @@ -27,6 +27,7 @@ class QgsVectorTileRenderer; class QgsVectorTileLabeling; class QgsVectorTileBasicRendererStyle; class QgsVectorTileBasicLabelingStyle; +class QgsRasterLayer; /** * Context for a MapBox GL style conversion operation. @@ -249,7 +250,7 @@ class CORE_EXPORT QgsMapBoxGlStyleRasterSource : public QgsMapBoxGlStyleAbstract * * \see minimumZoom() */ - int maximumZoom() const { return mMinZoom; } + int maximumZoom() const { return mMaxZoom; } /** * Returns the associated tile size. @@ -261,6 +262,13 @@ class CORE_EXPORT QgsMapBoxGlStyleRasterSource : public QgsMapBoxGlStyleAbstract */ QStringList tiles() const { return mTiles; } + /** + * Returns a new raster layer representing the raster source, or NULLPTR if the source cannot be represented as a raster layer. + * + * The caller takes ownership of the returned layer. + */ + QgsRasterLayer *toRasterLayer() const SIP_FACTORY; + private: QStringList mTiles; diff --git a/tests/src/python/test_qgsmapboxglconverter.py b/tests/src/python/test_qgsmapboxglconverter.py index 5e2fd7fbee2..060d381ac1a 100644 --- a/tests/src/python/test_qgsmapboxglconverter.py +++ b/tests/src/python/test_qgsmapboxglconverter.py @@ -21,7 +21,8 @@ from qgis.core import (QgsMapBoxGlStyleConverter, QgsApplication, QgsFontManager, QgsSettings, - Qgis + Qgis, + QgsRasterLayer ) from qgis.testing import start_app, unittest @@ -978,6 +979,12 @@ class TestQgsMapBoxGlStyleConverter(unittest.TestCase): self.assertEqual(raster_source.tileSize(), 256) self.assertEqual(raster_source.tiles(), ['https://yyyyyy/v1/tiles/texturereliefshade/EPSG:3857/{z}/{x}/{y}.webp']) + # convert to raster layer + rl = raster_source.toRasterLayer() + self.assertIsInstance(rl, QgsRasterLayer) + self.assertEqual(rl.source(), 'tilePixelRation=1&type=xyz&url=https://yyyyyy/v1/tiles/texturereliefshade/EPSG:3857/%7Bz%7D/%7Bx%7D/%7By%7D.webp&zmax=20&zmin=3') + self.assertEqual(rl.providerType(), 'wms') + def testLabelWithStops(self): context = QgsMapBoxGlStyleConversionContext() style = {