Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Extract utility functions for saving and reading raster color map def…
…initions to file to new QgsRasterRendererUtils class
- Loading branch information
1 parent
4668927
commit afd7698
Showing
6 changed files
with
297 additions
and
125 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
python/core/auto_generated/raster/qgsrasterrendererutils.sip.in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/raster/qgsrasterrendererutils.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ | ||
|
||
|
||
|
||
|
||
class QgsRasterRendererUtils | ||
{ | ||
%Docstring | ||
Utility functions for raster renderers. | ||
|
||
.. versionadded:: 3.16 | ||
%End | ||
|
||
%TypeHeaderCode | ||
#include "qgsrasterrendererutils.h" | ||
%End | ||
public: | ||
|
||
static bool parseColorMapFile( const QString &path, QList<QgsColorRampShader::ColorRampItem> &items /Out/, | ||
QgsColorRampShader::Type &type /Out/, | ||
QStringList &errors /Out/ ); | ||
%Docstring | ||
Parses an exported color map file at the specified ``path`` and extracts the stored color ramp ``items`` | ||
and ramp shader ``type``. | ||
|
||
Returns ``True`` if the parsing was successful. If not, a list of ``errors`` will be generated. | ||
|
||
.. seealso:: :py:func:`saveColorMapFile` | ||
%End | ||
|
||
static bool saveColorMapFile( const QString &path, const QList<QgsColorRampShader::ColorRampItem> &items, QgsColorRampShader::Type type ); | ||
%Docstring | ||
Exports a list of color ramp ``items`` and ramp shader ``type`` to a color map file at the specified | ||
``path``. | ||
|
||
Returns ``True`` if the save was successful. | ||
|
||
.. seealso:: :py:func:`parseColorMapFile` | ||
%End | ||
}; | ||
|
||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/raster/qgsrasterrendererutils.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/*************************************************************************** | ||
qgsrasterrendererutils.cpp | ||
------------------- | ||
begin : September 2020 | ||
copyright : (C) 2020 by Nyall Dawson | ||
email : nyall dawson dawson at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgsrasterrendererutils.h" | ||
#include "qgis.h" | ||
#include <QFile> | ||
#include <QTextStream> | ||
|
||
bool QgsRasterRendererUtils::parseColorMapFile( const QString &path, QList<QgsColorRampShader::ColorRampItem> &items, QgsColorRampShader::Type &type, QStringList &errors ) | ||
{ | ||
QFile inputFile( path ); | ||
if ( !inputFile.open( QFile::ReadOnly ) ) | ||
{ | ||
errors.append( QObject::tr( "Read access denied. Adjust the file permissions and try again.\n\n" ) ); | ||
return false; | ||
} | ||
|
||
bool res = true; | ||
|
||
QTextStream inputStream( &inputFile ); | ||
QString inputLine; | ||
QStringList inputStringComponents; | ||
int lineCounter = 0; | ||
|
||
//read through the input looking for valid data | ||
while ( !inputStream.atEnd() ) | ||
{ | ||
lineCounter++; | ||
inputLine = inputStream.readLine(); | ||
if ( !inputLine.isEmpty() ) | ||
{ | ||
if ( !inputLine.simplified().startsWith( '#' ) ) | ||
{ | ||
if ( inputLine.contains( QLatin1String( "INTERPOLATION" ), Qt::CaseInsensitive ) ) | ||
{ | ||
inputStringComponents = inputLine.split( ':' ); | ||
if ( inputStringComponents.size() == 2 ) | ||
{ | ||
if ( inputStringComponents[1].trimmed().toUpper().compare( QLatin1String( "INTERPOLATED" ), Qt::CaseInsensitive ) == 0 ) | ||
{ | ||
type = QgsColorRampShader::Interpolated; | ||
} | ||
else if ( inputStringComponents[1].trimmed().toUpper().compare( QLatin1String( "DISCRETE" ), Qt::CaseInsensitive ) == 0 ) | ||
{ | ||
type = QgsColorRampShader::Discrete; | ||
} | ||
else | ||
{ | ||
type = QgsColorRampShader::Exact; | ||
} | ||
} | ||
else | ||
{ | ||
res = false; | ||
errors << QObject::tr( "Unknown interpolation type at line %1: %2" ).arg( lineCounter ).arg( inputLine ); | ||
} | ||
} | ||
else | ||
{ | ||
inputStringComponents = inputLine.split( ',' ); | ||
if ( inputStringComponents.size() == 6 ) | ||
{ | ||
QgsColorRampShader::ColorRampItem currentItem( QLocale().toDouble( inputStringComponents[0] ), | ||
QColor::fromRgb( inputStringComponents[1].toInt(), inputStringComponents[2].toInt(), | ||
inputStringComponents[3].toInt(), inputStringComponents[4].toInt() ), | ||
inputStringComponents[5] ); | ||
items.push_back( currentItem ); | ||
} | ||
else | ||
{ | ||
res = false; | ||
errors << QObject::tr( "Invalid entry at line %1: %2" ).arg( lineCounter ).arg( inputLine ); | ||
} | ||
} | ||
} | ||
} | ||
lineCounter++; | ||
} | ||
|
||
return res; | ||
} | ||
|
||
bool QgsRasterRendererUtils::saveColorMapFile( const QString &path, const QList<QgsColorRampShader::ColorRampItem> &items, QgsColorRampShader::Type type ) | ||
{ | ||
QFile outputFile( path ); | ||
if ( outputFile.open( QFile::WriteOnly | QIODevice::Truncate ) ) | ||
{ | ||
QTextStream outputStream( &outputFile ); | ||
outputStream << "# " << QObject::tr( "QGIS Generated Color Map Export File" ) << '\n'; | ||
outputStream << "INTERPOLATION:"; | ||
switch ( type ) | ||
{ | ||
case QgsColorRampShader::Interpolated: | ||
outputStream << "INTERPOLATED\n"; | ||
break; | ||
case QgsColorRampShader::Discrete: | ||
outputStream << "DISCRETE\n"; | ||
break; | ||
case QgsColorRampShader::Exact: | ||
outputStream << "EXACT\n"; | ||
break; | ||
} | ||
|
||
int i = 0; | ||
for ( const QgsColorRampShader::ColorRampItem &item : items ) | ||
{ | ||
outputStream << qgsDoubleToString( item.value ) << ','; | ||
outputStream << item.color.red() << ',' << item.color.green() << ',' << item.color.blue() << ',' << item.color.alpha() << ','; | ||
if ( item.label.isEmpty() ) | ||
{ | ||
outputStream << "Color entry " << i + 1 << '\n'; | ||
} | ||
else | ||
{ | ||
outputStream << item.label << '\n'; | ||
} | ||
i++; | ||
} | ||
outputStream.flush(); | ||
outputFile.close(); | ||
return true; | ||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/*************************************************************************** | ||
qgsrasterrendererutils.h | ||
------------------- | ||
begin : September 2020 | ||
copyright : (C) 2020 by Nyall Dawson | ||
email : nyall dawson dawson at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef QGSRASTERRENDERERUTILS_H | ||
#define QGSRASTERRENDERERUTILS_H | ||
|
||
#include "qgscolorrampshader.h" | ||
|
||
/** | ||
* \ingroup core | ||
* Utility functions for raster renderers. | ||
* | ||
* \since QGIS 3.16 | ||
*/ | ||
class CORE_EXPORT QgsRasterRendererUtils | ||
{ | ||
public: | ||
|
||
/** | ||
* Parses an exported color map file at the specified \a path and extracts the stored color ramp \a items | ||
* and ramp shader \a type. | ||
* | ||
* Returns TRUE if the parsing was successful. If not, a list of \a errors will be generated. | ||
* | ||
* \see saveColorMapFile() | ||
*/ | ||
static bool parseColorMapFile( const QString &path, QList<QgsColorRampShader::ColorRampItem> &items SIP_OUT, | ||
QgsColorRampShader::Type &type SIP_OUT, | ||
QStringList &errors SIP_OUT ); | ||
|
||
/** | ||
* Exports a list of color ramp \a items and ramp shader \a type to a color map file at the specified | ||
* \a path. | ||
* | ||
* Returns TRUE if the save was successful. | ||
* | ||
* \see parseColorMapFile() | ||
*/ | ||
static bool saveColorMapFile( const QString &path, const QList<QgsColorRampShader::ColorRampItem> &items, QgsColorRampShader::Type type ); | ||
}; | ||
|
||
#endif // QGSRASTERRENDERERUTILS_H |
Oops, something went wrong.