Skip to content

Commit

Permalink
Merge pull request #51467 from elpaso/sld-export-options-png
Browse files Browse the repository at this point in the history
SLD: export options png
  • Loading branch information
elpaso committed Jan 22, 2023
2 parents 606cc0a + b137a9a commit 694ebda
Show file tree
Hide file tree
Showing 25 changed files with 912 additions and 157 deletions.
3 changes: 2 additions & 1 deletion python/core/auto_additions/qgis.py
Expand Up @@ -182,7 +182,8 @@
# monkey patching scoped based enum
Qgis.SldExportOption.NoOptions.__doc__ = "Default SLD export"
Qgis.SldExportOption.Svg.__doc__ = "Export complex styles to separate SVG files for better compatibility with OGC servers"
Qgis.SldExportOption.__doc__ = 'SLD export options\n\n.. versionadded:: 3.30\n\n' + '* ``NoOptions``: ' + Qgis.SldExportOption.NoOptions.__doc__ + '\n' + '* ``Svg``: ' + Qgis.SldExportOption.Svg.__doc__
Qgis.SldExportOption.Png.__doc__ = "Export complex styles to separate PNG files for better compatibility with OGC servers"
Qgis.SldExportOption.__doc__ = 'SLD export options\n\n.. versionadded:: 3.30\n\n' + '* ``NoOptions``: ' + Qgis.SldExportOption.NoOptions.__doc__ + '\n' + '* ``Svg``: ' + Qgis.SldExportOption.Svg.__doc__ + '\n' + '* ``Png``: ' + Qgis.SldExportOption.Png.__doc__
# --
Qgis.SldExportOption.baseClass = Qgis
Qgis.SldExportOptions.baseClass = Qgis
Expand Down
1 change: 1 addition & 0 deletions python/core/auto_generated/qgis.sip.in
Expand Up @@ -165,6 +165,7 @@ The development version
{
NoOptions,
Svg,
Png,
};
typedef QFlags<Qgis::SldExportOption> SldExportOptions;

Expand Down
18 changes: 18 additions & 0 deletions python/core/auto_generated/qgsfileutils.sip.in
Expand Up @@ -193,6 +193,24 @@ E.g. if ``path`` is "/home/user/Pictures/test.png", the returned list will conta

.. versionadded:: 3.28
%End

static QString uniquePath( const QString &path );
%Docstring
Creates a unique file path name from a desired path by appending "_<n>" (where "<n>" is an integer number) before the file suffix.

E.g. if "/path/my_image.png" already exists "/path/my_image_2.png" (and "_3", "_4" etc.) will be checked until a file path that does not already exist is found.

:param path: the desired path.

:return: the unmodified path if path is already unique or the new path with "_<n>" (where "<n>" is an integer number) appended to the file name before the suffix.

.. note::

This function does not make any check on path validity and write permissions.

.. versionadded:: 3.30
%End

};

/************************************************************************
Expand Down
10 changes: 5 additions & 5 deletions python/core/auto_generated/qgsmaplayer.sip.in
Expand Up @@ -966,6 +966,8 @@ Export the properties of this layer as SLD style in a QDomDocument
:param doc: the target QDomDocument
:param errorMsg: this QString will be initialized on error
during the execution of writeSymbology

.. seealso:: :py:func:`exportSldStyleV2`
%End

virtual void exportSldStyleV2( QDomDocument &doc, QString &errorMsg, const QgsSldExportContext &exportContext ) const;
Expand Down Expand Up @@ -1047,16 +1049,14 @@ Saves the properties of this layer to an SLD format file.
.. seealso:: :py:func:`saveSldStyleV2`
%End

virtual QString saveSldStyleV2( const QString &uri, bool &resultFlag, const QgsSldExportContext &exportContext ) const;
virtual QString saveSldStyleV2( bool &resultFlag /Out/, const QgsSldExportContext &exportContext ) const;
%Docstring
Saves the properties of this layer to an SLD format file.

:param uri: uri of destination for exported SLD file.
:param resultFlag: a reference to a flag that will be set to ``False`` if
the SLD file could not be generated
:param exportContext: SLD export context

:return: a string with any status or error messages
:return: - a string with any status or error messages
- resultFlag: a reference to a flag that will be set to ``False`` if the SLD file could not be generated

.. seealso:: :py:func:`loadSldStyle`

Expand Down
Expand Up @@ -186,6 +186,8 @@ Returns the map unit scale for the fill's offset.

virtual Qt::BrushStyle dxfBrushStyle() const;

virtual QImage toTiledPatternImage( ) const;


protected:

Expand Down Expand Up @@ -1547,6 +1549,8 @@ ownership of the returned object.

virtual void toSld( QDomDocument &doc, QDomElement &element, const QVariantMap &props ) const;

virtual QImage toTiledPatternImage( ) const;

virtual double estimateMaxBleed( const QgsRenderContext &context ) const;


Expand Down Expand Up @@ -1842,6 +1846,8 @@ Caller takes ownership of the returned symbol layer.

virtual void toSld( QDomDocument &doc, QDomElement &element, const QVariantMap &props ) const;

virtual QImage toTiledPatternImage( ) const;

virtual double estimateMaxBleed( const QgsRenderContext &context ) const;

virtual bool setSubSymbol( QgsSymbol *symbol /Transfer/ );
Expand Down
13 changes: 13 additions & 0 deletions python/core/auto_generated/symbology/qgssymbollayer.sip.in
Expand Up @@ -1313,6 +1313,19 @@ The ``rings`` argument optionally specifies a list of polygon rings to render as
void setAngle( double angle );
double angle() const;

virtual QImage toTiledPatternImage( ) const;
%Docstring
Renders the symbol layer as an image that can be used as a seamless pattern fill
for polygons, this method is used by SLD export to generate image tiles for
ExternalGraphic polygon fills.

The default implementation returns a null image.

:return: the tile image (not necessarily a square) or a null image if not implemented.

.. versionadded:: 3.30
%End

protected:
QgsFillSymbolLayer( bool locked = false );
void _renderPolygon( QPainter *p, const QPolygonF &points, const QVector<QPolygonF> *rings, QgsSymbolRenderContext &context );
Expand Down
14 changes: 14 additions & 0 deletions python/core/auto_generated/symbology/qgssymbollayerutils.sip.in
Expand Up @@ -997,6 +997,20 @@ Evaluates a map of properties using the given ``context`` and returns a variant
.. versionadded:: 3.18
%End

static QSize tileSize( int width, int height, double &angleRad /In,Out/ );
%Docstring
Calculate the minimum size in pixels of a symbol tile given the symbol ``width`` and ``height`` and the symbol layer rotation ``angleRad`` in radians (counter clockwise).
The method makes approximations and can modify ``angle`` in order to generate the smallest possible tile.

:param width: marker width, including margins
:param height: marker height, including margins
:param angleRad: symbol layer rotation angle in radians (counter clockwise), it may be approximated by the method to minimize the tile size.

:return: the size of the tile

.. versionadded:: 3.30
%End


};

Expand Down
1 change: 1 addition & 0 deletions src/core/qgis.h
Expand Up @@ -250,6 +250,7 @@ class CORE_EXPORT Qgis
{
NoOptions = 0, //!< Default SLD export
Svg = 1 << 0, //!< Export complex styles to separate SVG files for better compatibility with OGC servers
Png = 1 << 1, //!< Export complex styles to separate PNG files for better compatibility with OGC servers
};
Q_ENUM( SldExportOption )
Q_DECLARE_FLAGS( SldExportOptions, SldExportOption )
Expand Down
20 changes: 20 additions & 0 deletions src/core/qgsfileutils.cpp
Expand Up @@ -529,3 +529,23 @@ QStringList QgsFileUtils::splitPathToComponents( const QString &input )
std::reverse( result.begin(), result.end() );
return result;
}

QString QgsFileUtils::uniquePath( const QString &path )
{
if ( ! QFileInfo::exists( path ) )
{
return path;
}

QFileInfo info { path };
const QString suffix { info.completeSuffix() };
const QString pathPattern { QString( suffix.isEmpty() ? path : path.chopped( suffix.length() + 1 ) ).append( suffix.isEmpty() ? QStringLiteral( "_%1" ) : QStringLiteral( "_%1." ) ).append( suffix ) };
int i { 2 };
QString uniquePath { pathPattern.arg( i ) };
while ( QFileInfo::exists( uniquePath ) )
{
++i;
uniquePath = pathPattern.arg( i );
}
return uniquePath;
}
13 changes: 13 additions & 0 deletions src/core/qgsfileutils.h
Expand Up @@ -224,6 +224,19 @@ class CORE_EXPORT QgsFileUtils
* \since QGIS 3.28
*/
static QStringList splitPathToComponents( const QString &path );

/**
* Creates a unique file path name from a desired path by appending "_<n>" (where "<n>" is an integer number) before the file suffix.
*
* E.g. if "/path/my_image.png" already exists "/path/my_image_2.png" (and "_3", "_4" etc.) will be checked until a file path that does not already exist is found.
*
* \param path the desired path.
* \return the unmodified path if path is already unique or the new path with "_<n>" (where "<n>" is an integer number) appended to the file name before the suffix.
* \note This function does not make any check on path validity and write permissions.
* \since QGIS 3.30
*/
static QString uniquePath( const QString &path );

};

#endif // QGSFILEUTILS_H
8 changes: 6 additions & 2 deletions src/core/qgsmaplayer.cpp
Expand Up @@ -1758,6 +1758,7 @@ QString QgsMapLayer::saveNamedStyle( const QString &uri, bool &resultFlag, Style

void QgsMapLayer::exportSldStyle( QDomDocument &doc, QString &errorMsg ) const
{

return exportSldStyleV2( doc, errorMsg, QgsSldExportContext() );
}

Expand Down Expand Up @@ -1848,15 +1849,18 @@ void QgsMapLayer::exportSldStyleV2( QDomDocument &doc, QString &errorMsg, const

QString QgsMapLayer::saveSldStyle( const QString &uri, bool &resultFlag ) const
{
return saveSldStyleV2( uri, resultFlag, QgsSldExportContext() );
QgsSldExportContext context;
context.setExportFilePath( uri );
return saveSldStyleV2( resultFlag, context );
}

QString QgsMapLayer::saveSldStyleV2( const QString &uri, bool &resultFlag, const QgsSldExportContext &exportContext ) const
QString QgsMapLayer::saveSldStyleV2( bool &resultFlag, const QgsSldExportContext &exportContext ) const
{
QGIS_PROTECT_QOBJECT_THREAD_ACCESS

const QgsMapLayer *mlayer = qobject_cast<const QgsMapLayer *>( this );

const QString uri { exportContext.exportFilePath() };

// check if the uri is a file or ends with .sld,
// which indicates that it should become one
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsmaplayer.h
Expand Up @@ -1088,6 +1088,7 @@ class CORE_EXPORT QgsMapLayer : public QObject
* \param doc the target QDomDocument
* \param errorMsg this QString will be initialized on error
* during the execution of writeSymbology
* \see exportSldStyleV2()
*/
virtual void exportSldStyle( QDomDocument &doc, QString &errorMsg ) const;

Expand Down Expand Up @@ -1157,7 +1158,6 @@ class CORE_EXPORT QgsMapLayer : public QObject

/**
* Saves the properties of this layer to an SLD format file.
* \param uri uri of destination for exported SLD file.
* \param resultFlag a reference to a flag that will be set to FALSE if
* the SLD file could not be generated
* \param exportContext SLD export context
Expand All @@ -1166,7 +1166,7 @@ class CORE_EXPORT QgsMapLayer : public QObject
* \see loadSldStyle()
* \since QGIS 3.30
*/
virtual QString saveSldStyleV2( const QString &uri, bool &resultFlag, const QgsSldExportContext &exportContext ) const;
virtual QString saveSldStyleV2( bool &resultFlag SIP_OUT, const QgsSldExportContext &exportContext ) const;

/**
* Attempts to style the layer using the formatting from an SLD type file.
Expand Down

0 comments on commit 694ebda

Please sign in to comment.