Skip to content

Commit

Permalink
Merge pull request #51277 from elpaso/sld-export-options-svg
Browse files Browse the repository at this point in the history
Add SLD export context
  • Loading branch information
elpaso committed Dec 28, 2022
2 parents 85da91e + f84a0a8 commit 0aeb6dc
Show file tree
Hide file tree
Showing 12 changed files with 385 additions and 9 deletions.
15 changes: 15 additions & 0 deletions python/core/auto_additions/qgis.py
Expand Up @@ -180,6 +180,21 @@
# --
Qgis.SettingsType.baseClass = Qgis
# 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.baseClass = Qgis
Qgis.SldExportOptions.baseClass = Qgis
SldExportOptions = Qgis # dirty hack since SIP seems to introduce the flags in module
# monkey patching scoped based enum
Qgis.SldExportVendorExtension.NoVendorExtension.__doc__ = "No vendor extensions"
Qgis.SldExportVendorExtension.GeoServerVendorExtension.__doc__ = "Use GeoServer vendor extensions when required"
Qgis.SldExportVendorExtension.DeegreeVendorExtension.__doc__ = "Use Deegree vendor extensions when required"
Qgis.SldExportVendorExtension.__doc__ = 'SLD export vendor extensions, allow the use of vendor extensions when exporting to SLD.\n\n.. versionadded:: 3.30\n\n' + '* ``NoVendorExtension``: ' + Qgis.SldExportVendorExtension.NoVendorExtension.__doc__ + '\n' + '* ``GeoServerVendorExtension``: ' + Qgis.SldExportVendorExtension.GeoServerVendorExtension.__doc__ + '\n' + '* ``DeegreeVendorExtension``: ' + Qgis.SldExportVendorExtension.DeegreeVendorExtension.__doc__
# --
Qgis.SldExportVendorExtension.baseClass = Qgis
# monkey patching scoped based enum
Qgis.SettingsOption.SaveFormerValue.__doc__ = ""
Qgis.SettingsOption.SaveEnumFlagAsInt.__doc__ = ""
Qgis.SettingsOption.__doc__ = 'Settings options\n\n.. versionadded:: 3.26\n\n' + '* ``SaveFormerValue``: ' + Qgis.SettingsOption.SaveFormerValue.__doc__ + '\n' + '* ``SaveEnumFlagAsInt``: ' + Qgis.SettingsOption.SaveEnumFlagAsInt.__doc__
Expand Down
16 changes: 16 additions & 0 deletions python/core/auto_generated/qgis.sip.in
Expand Up @@ -161,6 +161,22 @@ The development version
Color
};

enum class SldExportOption
{
NoOptions,
Svg,
};
typedef QFlags<Qgis::SldExportOption> SldExportOptions;


enum class SldExportVendorExtension
{
NoVendorExtension,
GeoServerVendorExtension,
DeegreeVendorExtension,
};


enum class SettingsOption
{
SaveFormerValue,
Expand Down
30 changes: 30 additions & 0 deletions python/core/auto_generated/qgsmaplayer.sip.in
Expand Up @@ -982,6 +982,18 @@ Export the properties of this layer as SLD style in a QDomDocument
during the execution of writeSymbology
%End

virtual void exportSldStyleV2( QDomDocument &doc, QString &errorMsg, const QgsSldExportContext &exportContext ) const;
%Docstring
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
:param exportContext: SLD export context

.. versionadded:: 3.30
%End

virtual QString saveDefaultStyle( bool &resultFlag /Out/, StyleCategories categories );
%Docstring
Save the properties of this layer as the default style
Expand Down Expand Up @@ -1041,6 +1053,24 @@ Saves the properties of this layer to an SLD format file.
:return: a string with any status or error messages

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

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

virtual QString saveSldStyleV2( const QString &uri, bool &resultFlag, 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

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

.. versionadded:: 3.30
%End

virtual QString loadSldStyle( const QString &uri, bool &resultFlag );
Expand Down
83 changes: 83 additions & 0 deletions python/core/auto_generated/qgssldexportcontext.sip.in
@@ -0,0 +1,83 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgssldexportcontext.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/


class QgsSldExportContext
{
%Docstring(signature="appended")
The :py:class:`QgsSldExportContext` class holds SLD export options and other information related to SLD export of a QGIS layer style.

.. versionadded:: 3.30
%End

%TypeHeaderCode
#include "qgssldexportcontext.h"
%End
public:

QgsSldExportContext();
%Docstring
Constructs a default SLD export context
%End

~QgsSldExportContext();

QgsSldExportContext( const QgsSldExportContext &other );
%Docstring
Constructs a copy of SLD export context ``other``
%End


QgsSldExportContext( const Qgis::SldExportOptions &options, const Qgis::SldExportVendorExtension &vendorExtension, const QString &filePath );
%Docstring
/brief Create a new QgsSldExportContext
/param options SLD export options
/param vendorExtension SLD export vendor extension
/param filePath SLD export file path
%End

Qgis::SldExportOptions exportOptions() const;
%Docstring
Returns the export options
%End

void setExportOptions( const Qgis::SldExportOptions &exportOptions );
%Docstring
Set export options to ``exportOptions``
%End

Qgis::SldExportVendorExtension vendorExtensions() const;
%Docstring
Returns the vendor extension enabled for the SLD export
%End

void setVendorExtension( const Qgis::SldExportVendorExtension &vendorExtension );
%Docstring
Sets the vendor extensions to ``vendorExtension``
%End

QString exportFilePath() const;
%Docstring
Returns the export file path for the SLD
%End

void setExportFilePath( const QString &exportFilePath );
%Docstring
Sets the export file path for the SLD to ``exportFilePath``
%End

};


/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgssldexportcontext.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
1 change: 1 addition & 0 deletions python/core/core_auto.sip
Expand Up @@ -166,6 +166,7 @@
%Include auto_generated/qgsselectioncontext.sip
%Include auto_generated/qgssimplifymethod.sip
%Include auto_generated/qgssingleitemmodel.sip
%Include auto_generated/qgssldexportcontext.sip
%Include auto_generated/qgssnappingconfig.sip
%Include auto_generated/qgssnappingutils.sip
%Include auto_generated/qgsspatialindex.sip
Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -498,6 +498,7 @@ set(QGIS_CORE_SRCS
qgsshapegenerator.cpp
qgssimplifymethod.cpp
qgssingleitemmodel.cpp
qgssldexportcontext.cpp
qgssnappingutils.cpp
qgsspatialindex.cpp
qgsspatialindexkdbush.cpp
Expand Down Expand Up @@ -1177,6 +1178,7 @@ set(QGIS_CORE_HDRS
qgsshapegenerator.h
qgssimplifymethod.h
qgssingleitemmodel.h
qgssldexportcontext.h
qgssnappingconfig.h
qgssnappingutils.h
qgsspatialindex.h
Expand Down
1 change: 1 addition & 0 deletions src/core/maprenderer/qgsmaprendererjob.cpp
Expand Up @@ -41,6 +41,7 @@
#include "qgssymbollayerutils.h"
#include "qgsmaplayertemporalproperties.h"
#include "qgsmaplayerelevationproperties.h"
#include "qgsmaplayerstyle.h"
#include "qgsvectorlayerrenderer.h"
#include "qgsrendereditemresults.h"
#include "qgsmaskpaintdevice.h"
Expand Down
28 changes: 28 additions & 0 deletions src/core/qgis.h
Expand Up @@ -241,6 +241,34 @@ class CORE_EXPORT Qgis
};
Q_ENUM( SettingsType )

/**
* \brief SLD export options
*
* \since QGIS 3.30
*/
enum class SldExportOption : int
{
NoOptions = 0, //!< Default SLD export
Svg = 1 << 0, //!< Export complex styles to separate SVG files for better compatibility with OGC servers
};
Q_ENUM( SldExportOption )
Q_DECLARE_FLAGS( SldExportOptions, SldExportOption )
Q_FLAG( SldExportOptions )

/**
* \brief SLD export vendor extensions, allow the use of vendor extensions when exporting to SLD.
*
* \since QGIS 3.30
*/
enum class SldExportVendorExtension : int
{
NoVendorExtension = 0, //!< No vendor extensions
GeoServerVendorExtension = 1 << 1, //!< Use GeoServer vendor extensions when required
DeegreeVendorExtension = 1 << 2, //!< Use Deegree vendor extensions when required
};
Q_ENUM( SldExportVendorExtension )


/**
* Settings options
* \since QGIS 3.26
Expand Down
41 changes: 33 additions & 8 deletions src/core/qgsmaplayer.cpp
Expand Up @@ -35,6 +35,7 @@
#include "qgsrasterlayer.h"
#include "qgsreadwritecontext.h"
#include "qgsrectangle.h"
#include "qgssldexportcontext.h"
#include "qgsvectorlayer.h"
#include "qgsxmlutils.h"
#include "qgsstringutils.h"
Expand Down Expand Up @@ -1756,6 +1757,11 @@ QString QgsMapLayer::saveNamedStyle( const QString &uri, bool &resultFlag, Style
}

void QgsMapLayer::exportSldStyle( QDomDocument &doc, QString &errorMsg ) const
{
return exportSldStyleV2( doc, errorMsg, QgsSldExportContext() );
}

void QgsMapLayer::exportSldStyleV2( QDomDocument &doc, QString &errorMsg, const QgsSldExportContext &exportContext ) const
{
QGIS_PROTECT_QOBJECT_THREAD_ACCESS

Expand Down Expand Up @@ -1807,6 +1813,12 @@ void QgsMapLayer::exportSldStyle( QDomDocument &doc, QString &errorMsg ) const
}

QVariantMap props;

QVariant context;
context.setValue( exportContext );

props[ QStringLiteral( "SldExportContext" ) ] = context;

if ( hasScaleBasedVisibility() )
{
props[ QStringLiteral( "scaleMinDenom" ) ] = QString::number( mMinScale );
Expand Down Expand Up @@ -1835,19 +1847,17 @@ void QgsMapLayer::exportSldStyle( QDomDocument &doc, QString &errorMsg ) const
}

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

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

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

QString errorMsg;
QDomDocument myDocument;
mlayer->exportSldStyle( myDocument, errorMsg );
if ( !errorMsg.isNull() )
{
resultFlag = false;
return errorMsg;
}

// check if the uri is a file or ends with .sld,
// which indicates that it should become one
QString filename;
Expand Down Expand Up @@ -1885,6 +1895,20 @@ QString QgsMapLayer::saveSldStyle( const QString &uri, bool &resultFlag ) const
// now construct the file name for our .sld style file
const QString myFileName = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() + ".sld";

QString errorMsg;
QDomDocument myDocument;

QgsSldExportContext context { exportContext };
context.setExportFilePath( myFileName );

mlayer->exportSldStyleV2( myDocument, errorMsg, context );

if ( !errorMsg.isNull() )
{
resultFlag = false;
return errorMsg;
}

QFile myFile( myFileName );
if ( myFile.open( QFile::WriteOnly | QFile::Truncate ) )
{
Expand All @@ -1899,6 +1923,7 @@ QString QgsMapLayer::saveSldStyle( const QString &uri, bool &resultFlag ) const

resultFlag = false;
return tr( "ERROR: Failed to created SLD style file as %1. Check file permissions and retry." ).arg( filename );

}

QString QgsMapLayer::loadSldStyle( const QString &uri, bool &resultFlag )
Expand Down
26 changes: 25 additions & 1 deletion src/core/qgsmaplayer.h
Expand Up @@ -37,7 +37,6 @@
#include "qgsmaplayerdependency.h"
#include "qgslayermetadata.h"
#include "qgsmaplayerserverproperties.h"
#include "qgsmaplayerstyle.h"
#include "qgsreadwritecontext.h"
#include "qgsdataprovider.h"
#include "qgis.h"
Expand All @@ -52,6 +51,7 @@ class QgsProject;
class QgsStyleEntityVisitorInterface;
class QgsMapLayerTemporalProperties;
class QgsMapLayerElevationProperties;
class QgsSldExportContext;

class QDomDocument;
class QKeyEvent;
Expand Down Expand Up @@ -1098,6 +1098,16 @@ class CORE_EXPORT QgsMapLayer : public QObject
*/
virtual void exportSldStyle( QDomDocument &doc, QString &errorMsg ) const;

/**
* 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
* \param exportContext SLD export context
* \since QGIS 3.30
*/
virtual void exportSldStyleV2( QDomDocument &doc, QString &errorMsg, const QgsSldExportContext &exportContext ) const;

/**
* Save the properties of this layer as the default style
* (either as a .qml file on disk or as a
Expand Down Expand Up @@ -1146,9 +1156,23 @@ class CORE_EXPORT QgsMapLayer : public QObject
* the SLD file could not be generated
* \returns a string with any status or error messages
* \see loadSldStyle()
* \see saveSldStyleV2()
*/
virtual QString saveSldStyle( const QString &uri, bool &resultFlag ) const;

/**
* 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
* \returns a string with any status or error messages
*
* \see loadSldStyle()
* \since QGIS 3.30
*/
virtual QString saveSldStyleV2( const QString &uri, bool &resultFlag, const QgsSldExportContext &exportContext ) const;

/**
* Attempts to style the layer using the formatting from an SLD type file.
* \param uri uri of source SLD file
Expand Down

0 comments on commit 0aeb6dc

Please sign in to comment.