Skip to content

Commit

Permalink
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
nyalldawson committed Sep 15, 2020
1 parent 4668927 commit afd7698
Show file tree
Hide file tree
Showing 6 changed files with 297 additions and 125 deletions.
54 changes: 54 additions & 0 deletions python/core/auto_generated/raster/qgsrasterrendererutils.sip.in
@@ -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 *
************************************************************************/
1 change: 1 addition & 0 deletions python/core/core_auto.sip
Expand Up @@ -512,6 +512,7 @@
%Include auto_generated/raster/qgsrasterpyramid.sip
%Include auto_generated/raster/qgsrasterrange.sip
%Include auto_generated/raster/qgsrasterrenderer.sip
%Include auto_generated/raster/qgsrasterrendererutils.sip
%Include auto_generated/raster/qgsrasterresamplefilter.sip
%Include auto_generated/raster/qgsrasterresampler.sip
%Include auto_generated/raster/qgsrastershader.sip
Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -590,6 +590,7 @@ SET(QGIS_CORE_SRCS
raster/qgsrasterfilewriter.cpp
raster/qgsrasterrenderer.cpp
raster/qgsrasterrendererregistry.cpp
raster/qgsrasterrendererutils.cpp
raster/qgsrasterresamplefilter.cpp
raster/qgssinglebandcolordatarenderer.cpp
raster/qgssinglebandgrayrenderer.cpp
Expand Down Expand Up @@ -1377,6 +1378,7 @@ SET(QGIS_CORE_HDRS
raster/qgsrasterrange.h
raster/qgsrasterrenderer.h
raster/qgsrasterrendererregistry.h
raster/qgsrasterrendererutils.h
raster/qgsrasterresamplefilter.h
raster/qgsrasterresampler.h
raster/qgsrastershader.h
Expand Down
142 changes: 142 additions & 0 deletions src/core/raster/qgsrasterrendererutils.cpp
@@ -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;
}
}

56 changes: 56 additions & 0 deletions src/core/raster/qgsrasterrendererutils.h
@@ -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

0 comments on commit afd7698

Please sign in to comment.