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