QGIS API Documentation
4.3.0-Master (16eddb2670e)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
core
raster
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
20
#include "
qgis.h
"
21
22
#include <QFile>
23
#include <QRegularExpression>
24
#include <QString>
25
#include <QTextStream>
26
27
using namespace
Qt::StringLiterals;
28
29
bool
QgsRasterRendererUtils::parseColorMapFile
(
const
QString &path, QList<QgsColorRampShader::ColorRampItem> &items,
Qgis::ShaderInterpolationMethod
&type, QStringList &errors )
30
{
31
type =
Qgis::ShaderInterpolationMethod::Discrete
;
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
{
64
type =
Qgis::ShaderInterpolationMethod::Linear
;
65
}
66
else
if
( inputStringComponents[1].trimmed().toUpper().compare(
"DISCRETE"
_L1, Qt::CaseInsensitive ) == 0 )
67
{
68
type =
Qgis::ShaderInterpolationMethod::Discrete
;
69
}
70
else
71
{
72
type =
Qgis::ShaderInterpolationMethod::Exact
;
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
87
currentItem( match.captured( 1 ).toDouble(), QColor::fromRgb( match.captured( 2 ).toInt(), match.captured( 3 ).toInt(), match.captured( 4 ).toInt(), match.captured( 5 ).toInt() ), match.captured( 6 ) );
88
items.push_back( currentItem );
89
}
90
else
91
{
92
res =
false
;
93
errors << QObject::tr(
"Invalid entry at line %1: %2"
).arg( lineCounter ).arg( inputLine );
94
}
95
}
96
}
97
}
98
lineCounter++;
99
}
100
101
return
res;
102
}
103
104
bool
QgsRasterRendererUtils::saveColorMapFile
(
const
QString &path,
const
QList<QgsColorRampShader::ColorRampItem> &items,
Qgis::ShaderInterpolationMethod
type )
105
{
106
QFile outputFile( path );
107
if
( outputFile.open( QFile::WriteOnly | QIODevice::Truncate ) )
108
{
109
QTextStream outputStream( &outputFile );
110
outputStream <<
"# "
<< QObject::tr(
"QGIS Generated Color Map Export File"
) <<
'\n'
;
111
outputStream <<
"INTERPOLATION:"
;
112
switch
( type )
113
{
114
case
Qgis::ShaderInterpolationMethod::Linear
:
115
outputStream <<
"INTERPOLATED\n"
;
116
break
;
117
case
Qgis::ShaderInterpolationMethod::Discrete
:
118
outputStream <<
"DISCRETE\n"
;
119
break
;
120
case
Qgis::ShaderInterpolationMethod::Exact
:
121
outputStream <<
"EXACT\n"
;
122
break
;
123
}
124
125
int
i = 0;
126
for
(
const
QgsColorRampShader::ColorRampItem
&item : items )
127
{
128
outputStream <<
qgsDoubleToString
( item.value ) <<
','
;
129
outputStream << item.color.red() <<
','
<< item.color.green() <<
','
<< item.color.blue() <<
','
<< item.color.alpha() <<
','
;
130
if
( item.label.isEmpty() )
131
{
132
outputStream <<
"Color entry "
<< i + 1 <<
'\n'
;
133
}
134
else
135
{
136
outputStream << item.label <<
'\n'
;
137
}
138
i++;
139
}
140
outputStream.flush();
141
outputFile.close();
142
return
true
;
143
}
144
else
145
{
146
return
false
;
147
}
148
}
Qgis::ShaderInterpolationMethod
ShaderInterpolationMethod
Color ramp shader interpolation methods.
Definition
qgis.h:1594
Qgis::ShaderInterpolationMethod::Exact
@ Exact
Assigns the color of the exact matching value in the color ramp item list.
Definition
qgis.h:1597
Qgis::ShaderInterpolationMethod::Linear
@ Linear
Interpolates the color between two class breaks linearly.
Definition
qgis.h:1595
Qgis::ShaderInterpolationMethod::Discrete
@ Discrete
Assigns the color of the higher class for every pixel between two class breaks.
Definition
qgis.h:1596
QgsRasterRendererUtils::saveColorMapFile
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.
Definition
qgsrasterrendererutils.cpp:104
QgsRasterRendererUtils::parseColorMapFile
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 ...
Definition
qgsrasterrendererutils.cpp:29
qgis.h
qgsDoubleToString
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition
qgis.h:7650
qgsrasterrendererutils.h
QgsColorRampShader::ColorRampItem
Definition
qgscolorrampshader.h:89
Generated on
for QGIS API Documentation by
1.17.0