Skip to content

Commit

Permalink
Promote Qgis::DataType to enum class
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 27, 2021
1 parent d80ebab commit b4fea52
Show file tree
Hide file tree
Showing 81 changed files with 743 additions and 676 deletions.
31 changes: 31 additions & 0 deletions python/core/auto_additions/qgis.py
Expand Up @@ -17,6 +17,37 @@
QgsMapLayer.PointCloudLayer.__doc__ = "Added in 3.18"
QgsMapLayerType.__doc__ = 'Types of layers that can be added to a map\n\n.. versionadded:: 3.8\n\n' + '* ``VectorLayer``: ' + QgsMapLayerType.VectorLayer.__doc__ + '\n' + '* ``RasterLayer``: ' + QgsMapLayerType.RasterLayer.__doc__ + '\n' + '* ``PluginLayer``: ' + QgsMapLayerType.PluginLayer.__doc__ + '\n' + '* ``MeshLayer``: ' + QgsMapLayerType.MeshLayer.__doc__ + '\n' + '* ``VectorTileLayer``: ' + QgsMapLayerType.VectorTileLayer.__doc__ + '\n' + '* ``AnnotationLayer``: ' + QgsMapLayerType.AnnotationLayer.__doc__ + '\n' + '* ``PointCloudLayer``: ' + QgsMapLayerType.PointCloudLayer.__doc__
# --
# monkey patching scoped based enum
Qgis.UnknownDataType = Qgis.DataType.UnknownDataType
Qgis.UnknownDataType.__doc__ = "Unknown or unspecified type"
Qgis.Byte = Qgis.DataType.Byte
Qgis.Byte.__doc__ = "Eight bit unsigned integer (quint8)"
Qgis.UInt16 = Qgis.DataType.UInt16
Qgis.UInt16.__doc__ = "Sixteen bit unsigned integer (quint16)"
Qgis.Int16 = Qgis.DataType.Int16
Qgis.Int16.__doc__ = "Sixteen bit signed integer (qint16)"
Qgis.UInt32 = Qgis.DataType.UInt32
Qgis.UInt32.__doc__ = "Thirty two bit unsigned integer (quint32)"
Qgis.Int32 = Qgis.DataType.Int32
Qgis.Int32.__doc__ = "Thirty two bit signed integer (qint32)"
Qgis.Float32 = Qgis.DataType.Float32
Qgis.Float32.__doc__ = "Thirty two bit floating point (float)"
Qgis.Float64 = Qgis.DataType.Float64
Qgis.Float64.__doc__ = "Sixty four bit floating point (double)"
Qgis.CInt16 = Qgis.DataType.CInt16
Qgis.CInt16.__doc__ = "Complex Int16"
Qgis.CInt32 = Qgis.DataType.CInt32
Qgis.CInt32.__doc__ = "Complex Int32"
Qgis.CFloat32 = Qgis.DataType.CFloat32
Qgis.CFloat32.__doc__ = "Complex Float32"
Qgis.CFloat64 = Qgis.DataType.CFloat64
Qgis.CFloat64.__doc__ = "Complex Float64"
Qgis.ARGB32 = Qgis.DataType.ARGB32
Qgis.ARGB32.__doc__ = "Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32"
Qgis.ARGB32_Premultiplied = Qgis.DataType.ARGB32_Premultiplied
Qgis.ARGB32_Premultiplied.__doc__ = "Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied"
Qgis.DataType.__doc__ = 'Raster data types.\nThis is modified and extended copy of GDALDataType.\n\n' + '* ``UnknownDataType``: ' + Qgis.DataType.UnknownDataType.__doc__ + '\n' + '* ``Byte``: ' + Qgis.DataType.Byte.__doc__ + '\n' + '* ``UInt16``: ' + Qgis.DataType.UInt16.__doc__ + '\n' + '* ``Int16``: ' + Qgis.DataType.Int16.__doc__ + '\n' + '* ``UInt32``: ' + Qgis.DataType.UInt32.__doc__ + '\n' + '* ``Int32``: ' + Qgis.DataType.Int32.__doc__ + '\n' + '* ``Float32``: ' + Qgis.DataType.Float32.__doc__ + '\n' + '* ``Float64``: ' + Qgis.DataType.Float64.__doc__ + '\n' + '* ``CInt16``: ' + Qgis.DataType.CInt16.__doc__ + '\n' + '* ``CInt32``: ' + Qgis.DataType.CInt32.__doc__ + '\n' + '* ``CFloat32``: ' + Qgis.DataType.CFloat32.__doc__ + '\n' + '* ``CFloat64``: ' + Qgis.DataType.CFloat64.__doc__ + '\n' + '* ``ARGB32``: ' + Qgis.DataType.ARGB32.__doc__ + '\n' + '* ``ARGB32_Premultiplied``: ' + Qgis.DataType.ARGB32_Premultiplied.__doc__
# --
Qgis.DataType.baseClass = Qgis
Qgis.PythonMacroMode.baseClass = Qgis
QgsVectorDataProvider.FeatureCountState = Qgis.FeatureCountState
Expand Down
6 changes: 3 additions & 3 deletions python/core/auto_generated/qgis.sip.in
Expand Up @@ -81,11 +81,11 @@ The development version
Warning,
Critical,
Success,
None
None,
};

enum DataType
{
enum class DataType
{
UnknownDataType,
Byte,
UInt16,
Expand Down
Expand Up @@ -33,7 +33,10 @@ ContrastEnhancementAlgorithm.
UserDefinedEnhancement
};

QgsContrastEnhancement( Qgis::DataType datatype = Qgis::Byte );
QgsContrastEnhancement( Qgis::DataType datatype = Qgis::DataType::Byte );
%Docstring
Constructor for QgsContrastEnhancement, for the specified data type.
%End
QgsContrastEnhancement( const QgsContrastEnhancement &ce );
~QgsContrastEnhancement();

Expand Down
2 changes: 1 addition & 1 deletion python/core/auto_generated/raster/qgsrasterblock.sip.in
Expand Up @@ -64,7 +64,7 @@ This method does not return ``True`` if size is not zero and all values are
'no data' (null).
%End

static int typeSize( int dataType ) /HoldGIL/;
static int typeSize( Qgis::DataType dataType ) /HoldGIL/;
%Docstring
Returns the size in bytes for the specified ``dataType``.
%End
Expand Down
1 change: 1 addition & 0 deletions python/core/auto_generated/raster/qgsrasterchecker.sip.in
Expand Up @@ -8,6 +8,7 @@




class QgsRasterChecker
{
%Docstring(signature="appended")
Expand Down
4 changes: 3 additions & 1 deletion scripts/sipify.pl
Expand Up @@ -1049,7 +1049,9 @@ sub detect_non_method_member{
$enum_mk_base = $+{emkb} if defined $+{emkb};
if (defined $+{emkf} and $monkeypatch eq "1"){
if ( $ACTUAL_CLASS ne "" ) {
push @OUTPUT_PYTHON, "$enum_mk_base.$+{emkf} = $ACTUAL_CLASS.$enum_qualname\n";
if ($enum_mk_base.$+{emkf} ne $ACTUAL_CLASS.$enum_qualname) {
push @OUTPUT_PYTHON, "$enum_mk_base.$+{emkf} = $ACTUAL_CLASS.$enum_qualname\n";
}
} else {
push @OUTPUT_PYTHON, "$enum_mk_base.$+{emkf} = $enum_qualname\n";
}
Expand Down
6 changes: 3 additions & 3 deletions src/3d/symbols/qgspointcloud3dsymbol.cpp
Expand Up @@ -274,7 +274,7 @@ void QgsRgbPointCloud3DSymbol::readXml( const QDomElement &elem, const QgsReadWr
QDomElement redContrastElem = elem.firstChildElement( QStringLiteral( "redContrastEnhancement" ) );
if ( !redContrastElem.isNull() )
{
redContrastEnhancement = new QgsContrastEnhancement( Qgis::UnknownDataType );
redContrastEnhancement = new QgsContrastEnhancement( Qgis::DataType::UnknownDataType );
redContrastEnhancement->readXml( redContrastElem );
setRedContrastEnhancement( redContrastEnhancement );
}
Expand All @@ -283,7 +283,7 @@ void QgsRgbPointCloud3DSymbol::readXml( const QDomElement &elem, const QgsReadWr
QDomElement greenContrastElem = elem.firstChildElement( QStringLiteral( "greenContrastEnhancement" ) );
if ( !greenContrastElem.isNull() )
{
greenContrastEnhancement = new QgsContrastEnhancement( Qgis::UnknownDataType );
greenContrastEnhancement = new QgsContrastEnhancement( Qgis::DataType::UnknownDataType );
greenContrastEnhancement->readXml( greenContrastElem );
setGreenContrastEnhancement( greenContrastEnhancement );
}
Expand All @@ -292,7 +292,7 @@ void QgsRgbPointCloud3DSymbol::readXml( const QDomElement &elem, const QgsReadWr
QDomElement blueContrastElem = elem.firstChildElement( QStringLiteral( "blueContrastEnhancement" ) );
if ( !blueContrastElem.isNull() )
{
blueContrastEnhancement = new QgsContrastEnhancement( Qgis::UnknownDataType );
blueContrastEnhancement = new QgsContrastEnhancement( Qgis::DataType::UnknownDataType );
blueContrastEnhancement->readXml( blueContrastElem );
setBlueContrastEnhancement( blueContrastEnhancement );
}
Expand Down
4 changes: 2 additions & 2 deletions src/3d/terrain/qgsdemterraintileloader_p.cpp
Expand Up @@ -186,7 +186,7 @@ static QByteArray _readDtmData( QgsRasterDataProvider *provider, const QgsRectan
QByteArray data;
if ( block )
{
block->convert( Qgis::Float32 ); // currently we expect just floats
block->convert( Qgis::DataType::Float32 ); // currently we expect just floats
data = block->data();
data.detach(); // this should make a deep copy

Expand Down Expand Up @@ -275,7 +275,7 @@ void QgsDemHeightMapGenerator::lazyLoadDtmCoarseData( int res, const QgsRectangl
if ( mDtmCoarseData.isEmpty() )
{
std::unique_ptr< QgsRasterBlock > block( mDtm->dataProvider()->block( 1, rect, res, res ) );
block->convert( Qgis::Float32 );
block->convert( Qgis::DataType::Float32 );
mDtmCoarseData = block->data();
mDtmCoarseData.detach(); // make a deep copy
}
Expand Down
8 changes: 4 additions & 4 deletions src/analysis/processing/qgsalgorithmcellstatistics.cpp
Expand Up @@ -103,7 +103,7 @@ bool QgsCellStatisticsAlgorithmBase::prepareAlgorithm( const QVariantMap &parame

//determine output raster data type
//initially raster data type to most primitive data type that is possible
mDataType = Qgis::Byte;
mDataType = Qgis::DataType::Byte;
for ( const QgsRasterAnalysisUtils::RasterLogicInput &i : std::as_const( mInputs ) )
{
for ( int band : i.bands )
Expand Down Expand Up @@ -245,12 +245,12 @@ bool QgsCellStatisticsAlgorithm::prepareSpecificAlgorithmParameters( const QVari
)
{
if ( static_cast<int>( mDataType ) < 6 )
mDataType = Qgis::Float32; //force float on mean, stddev and median with equal number of input layers if all inputs are integer
mDataType = Qgis::DataType::Float32; //force float on mean, stddev and median with equal number of input layers if all inputs are integer
}
else if ( mMethod == QgsRasterAnalysisUtils::Count || mMethod == QgsRasterAnalysisUtils::Variety ) //count, variety
{
if ( static_cast<int>( mDataType ) > 5 ) //if is floating point type
mDataType = Qgis::Int32; //force integer on variety if all inputs are float or complex
mDataType = Qgis::DataType::Int32; //force integer on variety if all inputs are float or complex
}
return true;
}
Expand Down Expand Up @@ -425,7 +425,7 @@ bool QgsCellStatisticsPercentileAlgorithm::prepareSpecificAlgorithmParameters( c

//default percentile output data type to float32 raster if interpolation method is chosen
//otherwise use the most potent data type in the intput raster stack (see prepareAlgorithm() in base class)
if ( mMethod != QgsRasterAnalysisUtils::CellValuePercentileMethods::NearestRankPercentile && mDataType < 6 )
if ( mMethod != QgsRasterAnalysisUtils::CellValuePercentileMethods::NearestRankPercentile && static_cast< int >( mDataType ) < 6 )
mDataType = Qgis::DataType::Float32;

return true;
Expand Down
32 changes: 16 additions & 16 deletions src/analysis/processing/qgsalgorithmconstantraster.cpp
Expand Up @@ -100,49 +100,49 @@ QVariantMap QgsConstantRasterAlgorithm::processAlgorithm( const QVariantMap &par
double intpart;
fractpart = abs( std::modf( value, &intpart ) ); //@abs: negative values may be entered

Qgis::DataType rasterDataType = Qgis::Float32; //standard output type
Qgis::DataType rasterDataType = Qgis::DataType::Float32; //standard output type
switch ( typeId )
{
case 0:
rasterDataType = Qgis::Byte;
rasterDataType = Qgis::DataType::Byte;
if ( value < std::numeric_limits<quint8>::min() || value > std::numeric_limits<quint8>::max() )
throw QgsProcessingException( QObject::tr( "Raster datasets of type %3 only accept positive values between %1 and %2" ).arg( std::numeric_limits<quint8>::min() ).arg( std::numeric_limits<quint8>::max() ).arg( QLatin1String( "Byte" ) ) );
if ( fractpart > 0 )
feedback->reportError( QObject::tr( "The entered constant value has decimals but will be written to a raster dataset of type %1. The decimals of the constant value will be omitted." ).arg( QLatin1String( "Byte" ) ) );
break;
case 1:
rasterDataType = Qgis::Int16;
rasterDataType = Qgis::DataType::Int16;
if ( value < std::numeric_limits<qint16>::min() || value > std::numeric_limits<qint16>::max() )
throw QgsProcessingException( QObject::tr( "Raster datasets of type %3 only accept values between %1 and %2" ).arg( std::numeric_limits<qint16>::min() ).arg( std::numeric_limits<qint16>::max() ).arg( QLatin1String( "Integer16" ) ) );
if ( fractpart > 0 )
feedback->reportError( QObject::tr( "The entered constant value has decimals but will be written to a raster dataset of type %1. The decimals of the constant value will be omitted." ).arg( QLatin1String( "Integer16" ) ) );
break;
case 2:
rasterDataType = Qgis::UInt16;
rasterDataType = Qgis::DataType::UInt16;
if ( value < std::numeric_limits<quint16>::min() || value > std::numeric_limits<quint16>::max() )
throw QgsProcessingException( QObject::tr( "Raster datasets of type %3 only accept positive values between %1 and %2" ).arg( std::numeric_limits<quint16>::min() ).arg( std::numeric_limits<quint16>::max() ).arg( "Unsigned Integer16" ) );
if ( fractpart > 0 )
feedback->reportError( QObject::tr( "The entered constant value has decimals but will be written to a raster dataset of type %1. The decimals of the constant value will be omitted." ).arg( QLatin1String( "Unsigned Integer16" ) ) );
break;
case 3:
rasterDataType = Qgis::Int32;
rasterDataType = Qgis::DataType::Int32;
if ( value < std::numeric_limits<qint32>::min() || value > std::numeric_limits<qint32>::max() )
throw QgsProcessingException( QObject::tr( "Raster datasets of type %3 only accept values between %1 and %2" ).arg( std::numeric_limits<qint32>::min() ).arg( std::numeric_limits<qint32>::max() ).arg( QLatin1String( "Integer32" ) ) );
if ( fractpart > 0 )
feedback->reportError( QObject::tr( "The entered constant value has decimals but will be written to a raster dataset of type %1. The decimals of the constant value will be omitted." ).arg( QLatin1String( "Integer32" ) ) );
break;
case 4:
rasterDataType = Qgis::UInt32;
rasterDataType = Qgis::DataType::UInt32;
if ( value < std::numeric_limits<quint32>::min() || value > std::numeric_limits<quint32>::max() )
throw QgsProcessingException( QObject::tr( "Raster datasets of type %3 only accept positive values between %1 and %2" ).arg( std::numeric_limits<quint32>::min() ).arg( std::numeric_limits<quint32>::max() ).arg( QLatin1String( "Unsigned Integer32" ) ) );
if ( fractpart > 0 )
feedback->reportError( QObject::tr( "The entered constant value has decimals but will be written to a raster dataset of type %1. The decimals of the constant value will be omitted." ).arg( QLatin1String( "Unsigned Integer32" ) ) );
break;
case 5:
rasterDataType = Qgis::Float32;
rasterDataType = Qgis::DataType::Float32;
break;
case 6:
rasterDataType = Qgis::Float64;
rasterDataType = Qgis::DataType::Float64;
break;
default:
break;
Expand Down Expand Up @@ -179,56 +179,56 @@ QVariantMap QgsConstantRasterAlgorithm::processAlgorithm( const QVariantMap &par
{
std::vector<quint8> byteRow( cols );
std::fill( byteRow.begin(), byteRow.end(), value );
block.setData( QByteArray::fromRawData( ( char * )&byteRow[0], QgsRasterBlock::typeSize( Qgis::Byte ) * cols ) );
block.setData( QByteArray::fromRawData( ( char * )&byteRow[0], QgsRasterBlock::typeSize( Qgis::DataType::Byte ) * cols ) );
break;
}
case 1:
{
std::vector<qint16> int16Row( cols );
std::fill( int16Row.begin(), int16Row.end(), value );
block.setData( QByteArray::fromRawData( ( char * )&int16Row[0], QgsRasterBlock::typeSize( Qgis::Int16 ) * cols ) );
block.setData( QByteArray::fromRawData( ( char * )&int16Row[0], QgsRasterBlock::typeSize( Qgis::DataType::Int16 ) * cols ) );
break;
}
case 2:
{
std::vector<quint16> uInt16Row( cols );
std::fill( uInt16Row.begin(), uInt16Row.end(), value );
block.setData( QByteArray::fromRawData( ( char * )&uInt16Row[0], QgsRasterBlock::typeSize( Qgis::UInt16 ) * cols ) );
block.setData( QByteArray::fromRawData( ( char * )&uInt16Row[0], QgsRasterBlock::typeSize( Qgis::DataType::UInt16 ) * cols ) );
break;
}
case 3:
{
std::vector<qint32> int32Row( cols );
std::fill( int32Row.begin(), int32Row.end(), value );
block.setData( QByteArray::fromRawData( ( char * )&int32Row[0], QgsRasterBlock::typeSize( Qgis::Int32 ) * cols ) );
block.setData( QByteArray::fromRawData( ( char * )&int32Row[0], QgsRasterBlock::typeSize( Qgis::DataType::Int32 ) * cols ) );
break;
}
case 4:
{
std::vector<quint32> uInt32Row( cols );
std::fill( uInt32Row.begin(), uInt32Row.end(), value );
block.setData( QByteArray::fromRawData( ( char * )&uInt32Row[0], QgsRasterBlock::typeSize( Qgis::UInt32 ) * cols ) );
block.setData( QByteArray::fromRawData( ( char * )&uInt32Row[0], QgsRasterBlock::typeSize( Qgis::DataType::UInt32 ) * cols ) );
break;
}
case 5:
{
std::vector<float> float32Row( cols );
std::fill( float32Row.begin(), float32Row.end(), value );
block.setData( QByteArray::fromRawData( ( char * )&float32Row[0], QgsRasterBlock::typeSize( Qgis::Float32 ) * cols ) );
block.setData( QByteArray::fromRawData( ( char * )&float32Row[0], QgsRasterBlock::typeSize( Qgis::DataType::Float32 ) * cols ) );
break;
}
case 6:
{
std::vector<double> float64Row( cols );
std::fill( float64Row.begin(), float64Row.end(), value );
block.setData( QByteArray::fromRawData( ( char * )&float64Row[0], QgsRasterBlock::typeSize( Qgis::Float64 ) * cols ) );
block.setData( QByteArray::fromRawData( ( char * )&float64Row[0], QgsRasterBlock::typeSize( Qgis::DataType::Float64 ) * cols ) );
break;
}
default:
{
std::vector<float> float32Row( cols );
std::fill( float32Row.begin(), float32Row.end(), value );
block.setData( QByteArray::fromRawData( ( char * )&float32Row[0], QgsRasterBlock::typeSize( Qgis::Float32 ) * cols ) );
block.setData( QByteArray::fromRawData( ( char * )&float32Row[0], QgsRasterBlock::typeSize( Qgis::DataType::Float32 ) * cols ) );
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/analysis/processing/qgsalgorithmexportmesh.cpp
Expand Up @@ -825,7 +825,7 @@ QVariantMap QgsMeshRasterizeAlgorithm::processAlgorithm( const QVariantMap &para
rasterFileWriter.setOutputFormat( outputFormat );

std::unique_ptr<QgsRasterDataProvider> rasterDataProvider(
rasterFileWriter.createMultiBandRaster( Qgis::Float64, width, height, extent, mTransform.destinationCrs(), mDataPerGroup.count() ) );
rasterFileWriter.createMultiBandRaster( Qgis::DataType::Float64, width, height, extent, mTransform.destinationCrs(), mDataPerGroup.count() ) );
rasterDataProvider->setEditable( true );

for ( int i = 0; i < mDataPerGroup.count(); ++i )
Expand Down
2 changes: 1 addition & 1 deletion src/analysis/processing/qgsalgorithmfuzzifyraster.h
Expand Up @@ -71,7 +71,7 @@ class QgsFuzzifyRasterAlgorithmBase : public QgsProcessingAlgorithm
int mNbCellsXProvider = 0;
int mNbCellsYProvider = 0;

Qgis::DataType mDataType = Qgis::Float32;
Qgis::DataType mDataType = Qgis::DataType::Float32;
const double mNoDataValue = -9999;
QgsRasterDataProvider *mDestinationRasterProvider;
};
Expand Down
4 changes: 2 additions & 2 deletions src/analysis/processing/qgsalgorithmlinedensity.cpp
Expand Up @@ -140,7 +140,7 @@ QVariantMap QgsLineDensityAlgorithm::processAlgorithm( const QVariantMap &parame
QgsRasterFileWriter writer = QgsRasterFileWriter( outputFile );
writer.setOutputProviderKey( QStringLiteral( "gdal" ) );
writer.setOutputFormat( outputFormat );
std::unique_ptr<QgsRasterDataProvider > provider( writer.createOneBandRaster( Qgis::Float32, cols, rows, rasterExtent, mCrs ) );
std::unique_ptr<QgsRasterDataProvider > provider( writer.createOneBandRaster( Qgis::DataType::Float32, cols, rows, rasterExtent, mCrs ) );
if ( !provider )
throw QgsProcessingException( QObject::tr( "Could not create raster output: %1" ).arg( outputFile ) );
if ( !provider->isValid() )
Expand All @@ -151,7 +151,7 @@ QVariantMap QgsLineDensityAlgorithm::processAlgorithm( const QVariantMap &parame
qgssize totalCellcnt = static_cast<qgssize>( rows ) * cols;
int cellcnt = 0;

std::unique_ptr< QgsRasterBlock > rasterDataLine = std::make_unique< QgsRasterBlock >( Qgis::Float32, cols, 1 );
std::unique_ptr< QgsRasterBlock > rasterDataLine = std::make_unique< QgsRasterBlock >( Qgis::DataType::Float32, cols, 1 );

for ( int row = 0; row < rows; row++ )
{
Expand Down

0 comments on commit b4fea52

Please sign in to comment.