QGIS API Documentation 3.39.0-Master (47f7b3a4989)
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#include "qgis.h"
20#include <QFile>
21#include <QTextStream>
22#include <QRegularExpression>
23
24bool QgsRasterRendererUtils::parseColorMapFile( const QString &path, QList<QgsColorRampShader::ColorRampItem> &items, Qgis::ShaderInterpolationMethod &type, QStringList &errors )
25{
27 errors.clear();
28 items.clear();
29
30 QFile inputFile( path );
31 if ( !inputFile.open( QFile::ReadOnly ) )
32 {
33 errors.append( QObject::tr( "Read access denied. Adjust the file permissions and try again.\n\n" ) );
34 return false;
35 }
36
37 bool res = true;
38
39 QTextStream inputStream( &inputFile );
40 int lineCounter = 0;
41 const thread_local QRegularExpression itemRegex( QStringLiteral( "^(.+?),(.+?),(.+?),(.+?),(.+?),(.+)$" ) );
42
43 //read through the input looking for valid data
44 while ( !inputStream.atEnd() )
45 {
46 lineCounter++;
47 const QString inputLine = inputStream.readLine();
48 if ( !inputLine.isEmpty() )
49 {
50 if ( !inputLine.simplified().startsWith( '#' ) )
51 {
52 if ( inputLine.contains( QLatin1String( "INTERPOLATION" ), Qt::CaseInsensitive ) )
53 {
54 QStringList inputStringComponents = inputLine.split( ':' );
55 if ( inputStringComponents.size() == 2 )
56 {
57 if ( inputStringComponents[1].trimmed().toUpper().compare( QLatin1String( "INTERPOLATED" ), Qt::CaseInsensitive ) == 0 )
58 {
60 }
61 else if ( inputStringComponents[1].trimmed().toUpper().compare( QLatin1String( "DISCRETE" ), Qt::CaseInsensitive ) == 0 )
62 {
64 }
65 else
66 {
68 }
69 }
70 else
71 {
72 res = false;
73 errors << QObject::tr( "Unknown interpolation type at line %1: %2" ).arg( lineCounter ).arg( inputLine );
74 }
75 }
76 else
77 {
78 const QRegularExpressionMatch match = itemRegex.match( inputLine );
79 if ( match.hasMatch() )
80 {
81 const QgsColorRampShader::ColorRampItem currentItem( match.captured( 1 ).toDouble(),
82 QColor::fromRgb( match.captured( 2 ).toInt(), match.captured( 3 ).toInt(), match.captured( 4 ).toInt(), match.captured( 5 ).toInt() ),
83 match.captured( 6 ) );
84 items.push_back( currentItem );
85 }
86 else
87 {
88 res = false;
89 errors << QObject::tr( "Invalid entry at line %1: %2" ).arg( lineCounter ).arg( inputLine );
90 }
91 }
92 }
93 }
94 lineCounter++;
95 }
96
97 return res;
98}
99
100bool QgsRasterRendererUtils::saveColorMapFile( const QString &path, const QList<QgsColorRampShader::ColorRampItem> &items, Qgis::ShaderInterpolationMethod type )
101{
102 QFile outputFile( path );
103 if ( outputFile.open( QFile::WriteOnly | QIODevice::Truncate ) )
104 {
105 QTextStream outputStream( &outputFile );
106 outputStream << "# " << QObject::tr( "QGIS Generated Color Map Export File" ) << '\n';
107 outputStream << "INTERPOLATION:";
108 switch ( type )
109 {
111 outputStream << "INTERPOLATED\n";
112 break;
114 outputStream << "DISCRETE\n";
115 break;
117 outputStream << "EXACT\n";
118 break;
119 }
120
121 int i = 0;
122 for ( const QgsColorRampShader::ColorRampItem &item : items )
123 {
124 outputStream << qgsDoubleToString( item.value ) << ',';
125 outputStream << item.color.red() << ',' << item.color.green() << ',' << item.color.blue() << ',' << item.color.alpha() << ',';
126 if ( item.label.isEmpty() )
127 {
128 outputStream << "Color entry " << i + 1 << '\n';
129 }
130 else
131 {
132 outputStream << item.label << '\n';
133 }
134 i++;
135 }
136 outputStream.flush();
137 outputFile.close();
138 return true;
139 }
140 else
141 {
142 return false;
143 }
144}
145
ShaderInterpolationMethod
Color ramp shader interpolation methods.
Definition qgis.h:1158
@ Exact
Assigns the color of the exact matching value in the color ramp item list.
@ Linear
Interpolates the color between two class breaks linearly.
@ Discrete
Assigns the color of the higher class for every pixel between two class breaks.
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:5382