Add method to convert raster source to QgsRasterLayer

This commit is contained in:
Nyall Dawson 2022-06-20 11:34:42 +10:00
parent dd3058eb42
commit 31fc7405d1
4 changed files with 44 additions and 2 deletions

View File

@ -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
};

View File

@ -39,6 +39,8 @@
#include "qgsapplication.h"
#include "qgsfontmanager.h"
#include "qgis.h"
#include "qgsrasterlayer.h"
#include "qgsproviderregistry.h"
#include <QBuffer>
#include <QRegularExpression>
@ -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();
}

View File

@ -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;

View File

@ -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 = {