QGIS API Documentation 3.99.0-Master (e9821da5c6b)
Loading...
Searching...
No Matches
qgsrasterrendererutils.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsrasterrendererutils.cpp
3 -------------------
4 begin : September 2020
5 copyright : (C) 2020 by Nyall Dawson
6 email : nyall dawson dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include "qgis.h"
21
22#include <QFile>
23#include <QRegularExpression>
24#include <QString>
25#include <QTextStream>
26
27using namespace Qt::StringLiterals;
28
29bool QgsRasterRendererUtils::parseColorMapFile( const QString &path, QList<QgsColorRampShader::ColorRampItem> &items, Qgis::ShaderInterpolationMethod &type, QStringList &errors )
30{
32 errors.clear();
33 items.clear();
34
35 QFile inputFile( path );
36 if ( !inputFile.open( QFile::ReadOnly ) )
37 {
38 errors.append( QObject::tr( "Read access denied. Adjust the file permissions and try again.\n\n" ) );
39 return false;
40 }
41
42 bool res = true;
43
44 QTextStream inputStream( &inputFile );
45 int lineCounter = 0;
46 const thread_local QRegularExpression itemRegex( u"^(.+?),(.+?),(.+?),(.+?),(.+?),(.+)$"_s );
47
48 //read through the input looking for valid data
49 while ( !inputStream.atEnd() )
50 {
51 lineCounter++;
52 const QString inputLine = inputStream.readLine();
53 if ( !inputLine.isEmpty() )
54 {
55 if ( !inputLine.simplified().startsWith( '#' ) )
56 {
57 if ( inputLine.contains( "INTERPOLATION"_L1, Qt::CaseInsensitive ) )
58 {
59 QStringList inputStringComponents = inputLine.split( ':' );
60 if ( inputStringComponents.size() == 2 )
61 {
62 if ( inputStringComponents[1].trimmed().toUpper().compare( "INTERPOLATED"_L1, Qt::CaseInsensitive ) == 0 )
63 {
65 }
66 else if ( inputStringComponents[1].trimmed().toUpper().compare( "DISCRETE"_L1, Qt::CaseInsensitive ) == 0 )
67 {
69 }
70 else
71 {
73 }
74 }
75 else
76 {
77 res = false;
78 errors << QObject::tr( "Unknown interpolation type at line %1: %2" ).arg( lineCounter ).arg( inputLine );
79 }
80 }
81 else
82 {
83 const QRegularExpressionMatch match = itemRegex.match( inputLine );
84 if ( match.hasMatch() )
85 {
86 const QgsColorRampShader::ColorRampItem currentItem( match.captured( 1 ).toDouble(),
87 QColor::fromRgb( match.captured( 2 ).toInt(), match.captured( 3 ).toInt(), match.captured( 4 ).toInt(), match.captured( 5 ).toInt() ),
88 match.captured( 6 ) );
89 items.push_back( currentItem );
90 }
91 else
92 {
93 res = false;
94 errors << QObject::tr( "Invalid entry at line %1: %2" ).arg( lineCounter ).arg( inputLine );
95 }
96 }
97 }
98 }
99 lineCounter++;
100 }
101
102 return res;
103}
104
105bool QgsRasterRendererUtils::saveColorMapFile( const QString &path, const QList<QgsColorRampShader::ColorRampItem> &items, Qgis::ShaderInterpolationMethod type )
106{
107 QFile outputFile( path );
108 if ( outputFile.open( QFile::WriteOnly | QIODevice::Truncate ) )
109 {
110 QTextStream outputStream( &outputFile );
111 outputStream << "# " << QObject::tr( "QGIS Generated Color Map Export File" ) << '\n';
112 outputStream << "INTERPOLATION:";
113 switch ( type )
114 {
116 outputStream << "INTERPOLATED\n";
117 break;
119 outputStream << "DISCRETE\n";
120 break;
122 outputStream << "EXACT\n";
123 break;
124 }
125
126 int i = 0;
127 for ( const QgsColorRampShader::ColorRampItem &item : items )
128 {
129 outputStream << qgsDoubleToString( item.value ) << ',';
130 outputStream << item.color.red() << ',' << item.color.green() << ',' << item.color.blue() << ',' << item.color.alpha() << ',';
131 if ( item.label.isEmpty() )
132 {
133 outputStream << "Color entry " << i + 1 << '\n';
134 }
135 else
136 {
137 outputStream << item.label << '\n';
138 }
139 i++;
140 }
141 outputStream.flush();
142 outputFile.close();
143 return true;
144 }
145 else
146 {
147 return false;
148 }
149}
150
ShaderInterpolationMethod
Color ramp shader interpolation methods.
Definition qgis.h:1483
@ Exact
Assigns the color of the exact matching value in the color ramp item list.
Definition qgis.h:1486
@ Linear
Interpolates the color between two class breaks linearly.
Definition qgis.h:1484
@ Discrete
Assigns the color of the higher class for every pixel between two class breaks.
Definition qgis.h:1485
static bool saveColorMapFile(const QString &path, const QList< QgsColorRampShader::ColorRampItem > &items, Qgis::ShaderInterpolationMethod type)
Exports a list of color ramp items and ramp shader type to a color map file at the specified path.
static bool parseColorMapFile(const QString &path, QList< QgsColorRampShader::ColorRampItem > &items, Qgis::ShaderInterpolationMethod &type, QStringList &errors)
Parses an exported color map file at the specified path and extracts the stored color ramp items and ...
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:6841