Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Move QgsVectorFileWriter::SymbologyExport to Qgis, promote to enum class
  • Loading branch information
nyalldawson committed May 15, 2023
1 parent 256b802 commit b0d67ec
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 59 deletions.
16 changes: 16 additions & 0 deletions python/core/auto_additions/qgis.py
Expand Up @@ -3597,3 +3597,19 @@
Qgis.ExpressionType.__doc__ = 'Expression types\n\n.. versionadded:: 3.32\n\n' + '* ``Qgis``: ' + Qgis.ExpressionType.Qgis.__doc__ + '\n' + '* ``PointCloud``: ' + Qgis.ExpressionType.PointCloud.__doc__
# --
Qgis.ExpressionType.baseClass = Qgis
QgsVectorFileWriter.SymbologyExport = Qgis.FeatureSymbologyExport
# monkey patching scoped based enum
QgsVectorFileWriter.NoSymbology = Qgis.FeatureSymbologyExport.NoSymbology
QgsVectorFileWriter.NoSymbology.is_monkey_patched = True
QgsVectorFileWriter.NoSymbology.__doc__ = "Export only data"
QgsVectorFileWriter.FeatureSymbology = Qgis.FeatureSymbologyExport.PerFeature
QgsVectorFileWriter.SymbologyExport.FeatureSymbology = Qgis.FeatureSymbologyExport.PerFeature
QgsVectorFileWriter.FeatureSymbology.is_monkey_patched = True
QgsVectorFileWriter.FeatureSymbology.__doc__ = "Keeps the number of features and export symbology per feature"
QgsVectorFileWriter.SymbolLayerSymbology = Qgis.FeatureSymbologyExport.PerSymbolLayer
QgsVectorFileWriter.SymbologyExport.SymbolLayerSymbology = Qgis.FeatureSymbologyExport.PerSymbolLayer
QgsVectorFileWriter.SymbolLayerSymbology.is_monkey_patched = True
QgsVectorFileWriter.SymbolLayerSymbology.__doc__ = "Exports one feature per symbol layer (considering symbol levels)"
Qgis.FeatureSymbologyExport.__doc__ = 'Options for exporting features considering their symbology.\n\n.. note::\n\n Prior to QGIS 3.32 this was available as :py:class:`QgsVectorFileWriter`.SymbologyExport.\n\n.. versionadded:: 3.32\n\n' + '* ``NoSymbology``: ' + Qgis.FeatureSymbologyExport.NoSymbology.__doc__ + '\n' + '* ``FeatureSymbology``: ' + Qgis.FeatureSymbologyExport.PerFeature.__doc__ + '\n' + '* ``SymbolLayerSymbology``: ' + Qgis.FeatureSymbologyExport.PerSymbolLayer.__doc__
# --
Qgis.FeatureSymbologyExport.baseClass = Qgis
7 changes: 7 additions & 0 deletions python/core/auto_generated/qgis.sip.in
Expand Up @@ -2040,6 +2040,13 @@ The development version
PointCloud,
};

enum class FeatureSymbologyExport
{
NoSymbology,
PerFeature,
PerSymbolLayer
};

static const double DEFAULT_SEARCH_RADIUS_MM;

static const float DEFAULT_MAPTOPIXEL_THRESHOLD;
Expand Down
30 changes: 17 additions & 13 deletions python/core/auto_generated/qgsvectorfilewriter.sip.in
Expand Up @@ -139,13 +139,6 @@ Constructor for MetaData
Canceled,
};

enum SymbologyExport
{
NoSymbology,
FeatureSymbology,
SymbolLayerSymbology
};

enum FieldNameSource
{
Original,
Expand Down Expand Up @@ -249,7 +242,7 @@ Creates a clone of the FieldValueConverter.
const QStringList &layerOptions = QStringList(),
bool skipAttributeCreation = false,
QString *newFilename = 0,
QgsVectorFileWriter::SymbologyExport symbologyExport = QgsVectorFileWriter::NoSymbology,
Qgis::FeatureSymbologyExport symbologyExport = Qgis::FeatureSymbologyExport::NoSymbology,
double symbologyScale = 1.0,
const QgsRectangle *filterExtent = 0,
Qgis::WkbType overrideGeometryType = Qgis::WkbType::Unknown,
Expand Down Expand Up @@ -298,7 +291,7 @@ Write contents of vector layer to an (OGR supported) vector format
const QStringList &layerOptions = QStringList(),
bool skipAttributeCreation = false,
QString *newFilename = 0,
QgsVectorFileWriter::SymbologyExport symbologyExport = QgsVectorFileWriter::NoSymbology,
Qgis::FeatureSymbologyExport symbologyExport = Qgis::FeatureSymbologyExport::NoSymbology,
double symbologyScale = 1.0,
const QgsRectangle *filterExtent = 0,
Qgis::WkbType overrideGeometryType = Qgis::WkbType::Unknown,
Expand Down Expand Up @@ -379,7 +372,7 @@ Constructor

QStringList attributesExportNames;

QgsVectorFileWriter::SymbologyExport symbologyExport;
Qgis::FeatureSymbologyExport symbologyExport;

double symbologyScale;

Expand Down Expand Up @@ -433,7 +426,7 @@ Writes a layer out to a vector file.
const QStringList &datasourceOptions = QStringList(),
const QStringList &layerOptions = QStringList(),
QString *newFilename = 0,
QgsVectorFileWriter::SymbologyExport symbologyExport = QgsVectorFileWriter::NoSymbology,
Qgis::FeatureSymbologyExport symbologyExport = Qgis::FeatureSymbologyExport::NoSymbology,
QgsFeatureSink::SinkFlags sinkFlags = QgsFeatureSink::SinkFlags()
) /Deprecated/;

Expand Down Expand Up @@ -659,8 +652,19 @@ Delete a shapefile (and its accompanying shx / dbf / prj / qix / qpj / cpg / sbn
:return: bool ``True`` if the file was deleted successfully
%End

QgsVectorFileWriter::SymbologyExport symbologyExport() const;
void setSymbologyExport( QgsVectorFileWriter::SymbologyExport symExport );
Qgis::FeatureSymbologyExport symbologyExport() const;
%Docstring
Returns the feature symbology export handling for the writer.

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

void setSymbologyExport( Qgis::FeatureSymbologyExport symExport );
%Docstring
Sets the feature symbology export handling for the writer.

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

double symbologyScale() const;
%Docstring
Expand Down
2 changes: 1 addition & 1 deletion src/core/pointcloud/qgspointcloudlayerexporter.cpp
Expand Up @@ -264,7 +264,7 @@ void QgsPointCloudLayerExporter::doExport()
saveOptions.datasourceOptions = QgsVectorFileWriter::defaultDatasetOptions( ogrDriver );
saveOptions.layerOptions = QgsVectorFileWriter::defaultLayerOptions( ogrDriver );
saveOptions.layerOptions << layerCreationOptions;
saveOptions.symbologyExport = QgsVectorFileWriter::NoSymbology;
saveOptions.symbologyExport = Qgis::FeatureSymbologyExport::NoSymbology;
saveOptions.actionOnExistingFile = mActionOnExistingFile;
saveOptions.feedback = mFeedback;
mVectorSink = QgsVectorFileWriter::create( mFilename, outputFields(), Qgis::WkbType::PointZ, mTargetCrs, QgsCoordinateTransformContext(), saveOptions );
Expand Down
2 changes: 1 addition & 1 deletion src/core/processing/qgsprocessingutils.cpp
Expand Up @@ -870,7 +870,7 @@ QgsFeatureSink *QgsProcessingUtils::createFeatureSink( QString &destination, Qgs
saveOptions.driverName = format;
saveOptions.datasourceOptions = !datasourceOptions.isEmpty() ? datasourceOptions : QgsVectorFileWriter::defaultDatasetOptions( format );
saveOptions.layerOptions = !layerOptions.isEmpty() ? layerOptions : QgsVectorFileWriter::defaultLayerOptions( format );
saveOptions.symbologyExport = QgsVectorFileWriter::NoSymbology;
saveOptions.symbologyExport = Qgis::FeatureSymbologyExport::NoSymbology;
if ( remappingDefinition )
{
saveOptions.actionOnExistingFile = QgsVectorFileWriter::AppendToLayerNoNewFields;
Expand Down
2 changes: 1 addition & 1 deletion src/core/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -330,7 +330,7 @@ Qgis::VectorExportResult QgsOgrProvider::createEmptyLayer( const QString &uri,
saveOptions.datasourceOptions = dsOptions;
saveOptions.layerOptions = layerOptions;
saveOptions.actionOnExistingFile = action;
saveOptions.symbologyExport = QgsVectorFileWriter::NoSymbology;
saveOptions.symbologyExport = Qgis::FeatureSymbologyExport::NoSymbology;
std::unique_ptr< QgsVectorFileWriter > writer( QgsVectorFileWriter::create( uri, cleanedFields, wkbType, srs, QgsCoordinateTransformContext(), saveOptions, QgsFeatureSink::SinkFlags(), nullptr, &newLayerName ) );
layerName = newLayerName;

Expand Down
15 changes: 15 additions & 0 deletions src/core/qgis.h
Expand Up @@ -3538,6 +3538,21 @@ class CORE_EXPORT Qgis
};
Q_ENUM( ExpressionType )

/**
* Options for exporting features considering their symbology.
*
* \note Prior to QGIS 3.32 this was available as QgsVectorFileWriter::SymbologyExport.
*
* \since QGIS 3.32
*/
enum class FeatureSymbologyExport SIP_MONKEYPATCH_SCOPEENUM_UNNEST( QgsVectorFileWriter, SymbologyExport ) : int
{
NoSymbology = 0, //!< Export only data
PerFeature SIP_MONKEYPATCH_COMPAT_NAME( FeatureSymbology ), //!< Keeps the number of features and export symbology per feature
PerSymbolLayer SIP_MONKEYPATCH_COMPAT_NAME( SymbolLayerSymbology ) //!< Exports one feature per symbol layer (considering symbol levels)
};
Q_ENUM( FeatureSymbologyExport )

/**
* Identify search radius in mm
* \since QGIS 2.3
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsabstractgeopdfexporter.cpp
Expand Up @@ -176,7 +176,7 @@ bool QgsAbstractGeoPdfExporter::saveTemporaryLayers()
QString layerName;
QgsVectorFileWriter::SaveVectorOptions saveOptions;
saveOptions.driverName = QStringLiteral( "GPKG" );
saveOptions.symbologyExport = QgsVectorFileWriter::NoSymbology;
saveOptions.symbologyExport = Qgis::FeatureSymbologyExport::NoSymbology;
std::unique_ptr< QgsVectorFileWriter > writer( QgsVectorFileWriter::create( filePath, features.first().fields(), features.first().geometry().wkbType(), QgsCoordinateReferenceSystem(), QgsCoordinateTransformContext(), saveOptions, QgsFeatureSink::RegeneratePrimaryKey, nullptr, &layerName ) );
if ( writer->hasError() )
{
Expand Down
84 changes: 57 additions & 27 deletions src/core/qgsvectorfilewriter.cpp
Expand Up @@ -78,7 +78,7 @@ QgsVectorFileWriter::QgsVectorFileWriter(
const QStringList &datasourceOptions,
const QStringList &layerOptions,
QString *newFilename,
SymbologyExport symbologyExport,
Qgis::FeatureSymbologyExport symbologyExport,
QgsFeatureSink::SinkFlags sinkFlags,
QString *newLayer,
const QgsCoordinateTransformContext &transformContext,
Expand All @@ -104,7 +104,7 @@ QgsVectorFileWriter::QgsVectorFileWriter(
const QStringList &datasourceOptions,
const QStringList &layerOptions,
QString *newFilename,
QgsVectorFileWriter::SymbologyExport symbologyExport,
Qgis::FeatureSymbologyExport symbologyExport,
FieldValueConverter *fieldValueConverter,
const QString &layerName,
ActionOnExistingFile action,
Expand Down Expand Up @@ -2611,7 +2611,7 @@ bool QgsVectorFileWriter::addFeatureWithStyle( QgsFeature &feature, QgsFeatureRe
return false;

//add OGR feature style type
if ( mSymbologyExport != NoSymbology && renderer )
if ( mSymbologyExport != Qgis::FeatureSymbologyExport::NoSymbology && renderer )
{
mRenderContext.expressionContext().setFeature( feature );
//SymbolLayerSymbology: concatenate ogr styles of all symbollayers
Expand All @@ -2637,33 +2637,49 @@ bool QgsVectorFileWriter::addFeatureWithStyle( QgsFeature &feature, QgsFeatureRe

currentStyle = ( *symbolIt )->symbolLayer( i )->ogrFeatureStyle( mmsf, musf );//"@" + it.value();

if ( mSymbologyExport == FeatureSymbology )
switch ( mSymbologyExport )
{
if ( symbolIt != symbols.constBegin() || i != 0 )
case Qgis::FeatureSymbologyExport::PerFeature:
{
styleString.append( ';' );
if ( symbolIt != symbols.constBegin() || i != 0 )
{
styleString.append( ';' );
}
styleString.append( currentStyle );
break;
}
styleString.append( currentStyle );
}
else if ( mSymbologyExport == SymbolLayerSymbology )
{
OGR_F_SetStyleString( poFeature.get(), currentStyle.toLocal8Bit().constData() );
if ( !writeFeature( mLayer, poFeature.get() ) )
case Qgis::FeatureSymbologyExport::PerSymbolLayer:
{
return false;
OGR_F_SetStyleString( poFeature.get(), currentStyle.toLocal8Bit().constData() );
if ( !writeFeature( mLayer, poFeature.get() ) )
{
return false;
}
break;
}

case Qgis::FeatureSymbologyExport::NoSymbology:
break;
}
}
}
OGR_F_SetStyleString( poFeature.get(), styleString.toLocal8Bit().constData() );
}

if ( mSymbologyExport == NoSymbology || mSymbologyExport == FeatureSymbology )
switch ( mSymbologyExport )
{
if ( !writeFeature( mLayer, poFeature.get() ) )
case Qgis::FeatureSymbologyExport::NoSymbology:
case Qgis::FeatureSymbologyExport::PerFeature:
{
return false;
if ( !writeFeature( mLayer, poFeature.get() ) )
{
return false;
}
break;
}

case Qgis::FeatureSymbologyExport::PerSymbolLayer:
break;
}

return true;
Expand Down Expand Up @@ -3197,7 +3213,7 @@ QgsVectorFileWriter::writeAsVectorFormat( QgsVectorLayer *layer,
const QStringList &layerOptions,
bool skipAttributeCreation,
QString *newFilename,
SymbologyExport symbologyExport,
Qgis::FeatureSymbologyExport symbologyExport,
double symbologyScale,
const QgsRectangle *filterExtent,
Qgis::WkbType overrideGeometryType,
Expand Down Expand Up @@ -3244,7 +3260,7 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat( QgsVe
const QStringList &layerOptions,
bool skipAttributeCreation,
QString *newFilename,
SymbologyExport symbologyExport,
Qgis::FeatureSymbologyExport symbologyExport,
double symbologyScale,
const QgsRectangle *filterExtent,
Qgis::WkbType overrideGeometryType,
Expand Down Expand Up @@ -3531,20 +3547,27 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormatV2( Pre
QgsFeature fet;

//create symbol table if needed
if ( writer->symbologyExport() != NoSymbology )
if ( writer->symbologyExport() != Qgis::FeatureSymbologyExport::NoSymbology )
{
//writer->createSymbolLayerTable( layer, writer->mDS );
}

if ( writer->symbologyExport() == SymbolLayerSymbology )
switch ( writer->symbologyExport() )
{
QgsFeatureRenderer *r = details.renderer.get();
if ( r && r->capabilities() & QgsFeatureRenderer::SymbolLevels
&& r->usingSymbolLevels() )
case Qgis::FeatureSymbologyExport::PerSymbolLayer:
{
QgsVectorFileWriter::WriterError error = writer->exportFeaturesSymbolLevels( details, details.sourceFeatureIterator, options.ct, errorMessage );
return ( error == NoError ) ? NoError : ErrFeatureWriteFailed;
QgsFeatureRenderer *r = details.renderer.get();
if ( r && r->capabilities() & QgsFeatureRenderer::SymbolLevels
&& r->usingSymbolLevels() )
{
QgsVectorFileWriter::WriterError error = writer->exportFeaturesSymbolLevels( details, details.sourceFeatureIterator, options.ct, errorMessage );
return ( error == NoError ) ? NoError : ErrFeatureWriteFailed;
}
break;
}
case Qgis::FeatureSymbologyExport::NoSymbology:
case Qgis::FeatureSymbologyExport::PerFeature:
break;
}

int n = 0, errors = 0;
Expand Down Expand Up @@ -4298,10 +4321,17 @@ void QgsVectorFileWriter::stopRender()

std::unique_ptr<QgsFeatureRenderer> QgsVectorFileWriter::createSymbologyRenderer( QgsFeatureRenderer *sourceRenderer ) const
{
if ( mSymbologyExport == NoSymbology )
switch ( mSymbologyExport )
{
return nullptr;
case Qgis::FeatureSymbologyExport::NoSymbology:
{
return nullptr;
}
case Qgis::FeatureSymbologyExport::PerFeature:
case Qgis::FeatureSymbologyExport::PerSymbolLayer:
break;
}

if ( !sourceRenderer )
{
return nullptr;
Expand Down

0 comments on commit b0d67ec

Please sign in to comment.