Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Minor code refactor
  • Loading branch information
nyalldawson committed Sep 10, 2020
1 parent 5b19f4a commit 14b1fde
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 26 deletions.
54 changes: 28 additions & 26 deletions src/core/vectortile/qgsmapboxglstyleconverter.cpp
Expand Up @@ -329,23 +329,13 @@ bool QgsMapBoxGlStyleConverter::parseFillLayer( const QVariantMap &jsonLayer, Qg
{
case QVariant::String:
{
const QImage sprite = retrieveSprite( fillPatternJson.toString(), context );

if ( !sprite.isNull() )
QSize spriteSize;
const QString sprite = retrieveSpriteAsBase64( fillPatternJson.toString(), context, spriteSize );
if ( !sprite.isEmpty() )
{
// when fill-pattern exists, set and insert QgsRasterFillSymbolLayer
QgsRasterFillSymbolLayer *rasterFill = new QgsRasterFillSymbolLayer();

QByteArray blob;
QBuffer buffer( &blob );
buffer.open( QIODevice::WriteOnly );
sprite.save( &buffer, "PNG" );
buffer.close();
QByteArray encoded = blob.toBase64();

QString path( encoded );
path.prepend( QLatin1String( "base64:" ) );
rasterFill->setImageFilePath( path );
rasterFill->setImageFilePath( sprite );
rasterFill->setCoordinateMode( QgsRasterFillSymbolLayer::Viewport );

if ( rasterOpacity >= 0 )
Expand Down Expand Up @@ -1187,20 +1177,12 @@ bool QgsMapBoxGlStyleConverter::parseSymbolLayerAsRenderer( const QVariantMap &j
}

QgsRasterMarkerSymbolLayer *markerLayer = new QgsRasterMarkerSymbolLayer( );
const QImage sprite = retrieveSprite( jsonLayout.value( QStringLiteral( "icon-image" ) ).toString(), context );
QSize spriteSize;
const QString sprite = retrieveSpriteAsBase64( jsonLayout.value( QStringLiteral( "icon-image" ) ).toString(), context, spriteSize );
if ( !sprite.isNull() )
{
QByteArray blob;
QBuffer buffer( &blob );
buffer.open( QIODevice::WriteOnly );
sprite.save( &buffer, "PNG" );
buffer.close();
QByteArray encoded = blob.toBase64();

QString path( encoded );
path.prepend( QLatin1String( "base64:" ) );
markerLayer->setPath( path );
markerLayer->setSize( context.pixelSizeConversionFactor() * sprite.width() );
markerLayer->setPath( sprite );
markerLayer->setSize( context.pixelSizeConversionFactor() * spriteSize.width() );
markerLayer->setSizeUnit( context.targetUnit() );
}

Expand Down Expand Up @@ -1899,6 +1881,26 @@ QImage QgsMapBoxGlStyleConverter::retrieveSprite( const QString &name, QgsMapBox
return sprite;
}

QString QgsMapBoxGlStyleConverter::retrieveSpriteAsBase64( const QString &name, QgsMapBoxGlStyleConversionContext &context, QSize &size )
{
const QImage sprite = retrieveSprite( name, context );
if ( !sprite.isNull() )
{
size = sprite.size();
QByteArray blob;
QBuffer buffer( &blob );
buffer.open( QIODevice::WriteOnly );
sprite.save( &buffer, "PNG" );
buffer.close();
QByteArray encoded = blob.toBase64();

QString path( encoded );
path.prepend( QLatin1String( "base64:" ) );
return path;
}
return QString();
}

QString QgsMapBoxGlStyleConverter::parseValue( const QVariant &value, QgsMapBoxGlStyleConversionContext &context )
{
switch ( value.type() )
Expand Down
8 changes: 8 additions & 0 deletions src/core/vectortile/qgsmapboxglstyleconverter.h
Expand Up @@ -441,6 +441,14 @@ class CORE_EXPORT QgsMapBoxGlStyleConverter
*/
static QImage retrieveSprite( const QString &name, QgsMapBoxGlStyleConversionContext &context );

/**
* Retrieves the sprite image with the specified \a name, taken from the specified \a context as a base64 encoded value
*
* The \a context must have valid sprite definitions and images set via QgsMapBoxGlStyleConversionContext::setSprites()
* prior to conversion.
*/
static QString retrieveSpriteAsBase64( const QString &name, QgsMapBoxGlStyleConversionContext &context, QSize &size );

private:

#ifdef SIP_RUN
Expand Down

0 comments on commit 14b1fde

Please sign in to comment.