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