QGIS API Documentation
4.3.0-Master (16eddb2670e)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
core
qgsscaleutils.cpp
Go to the documentation of this file.
1
/***************************************************************************
2
qgsscaleutils.cpp
3
---------------------
4
begin : July 2012
5
copyright : (C) 2012 by Alexander Bruy
6
email : alexander dot bruy at gmail dot com
7
***************************************************************************
8
* *
9
* This program is free software; you can redistribute it and/or modify *
10
* it under the terms of the GNU General Public License as published by *
11
* the Free Software Foundation; either version 2 of the License, or *
12
* (at your option) any later version. *
13
* *
14
***************************************************************************/
15
16
#include "
qgsscaleutils.h
"
17
18
#include "
qgis.h
"
19
20
#include <QDomDocument>
21
#include <QFile>
22
#include <QString>
23
#include <QTextStream>
24
25
using namespace
Qt::StringLiterals;
26
27
bool
QgsScaleUtils::saveScaleList
(
const
QString &fileName,
const
QStringList &scales, QString &errorMessage )
28
{
29
QDomDocument doc;
30
QDomElement root = doc.createElement( u
"qgsScales"
_s );
31
root.setAttribute( u
"version"
_s, u
"1.0"
_s );
32
doc.appendChild( root );
33
34
for
(
int
i = 0; i < scales.count(); ++i )
35
{
36
QDomElement el = doc.createElement( u
"scale"
_s );
37
el.setAttribute( u
"value"
_s, scales.at( i ) );
38
root.appendChild( el );
39
}
40
41
QFile file( fileName );
42
if
( !file.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
43
{
44
errorMessage = u
"Cannot write file %1:\n%2."
_s.arg( fileName, file.errorString() );
45
return
false
;
46
}
47
48
QTextStream out( &file );
49
doc.save( out, 4 );
50
return
true
;
51
}
52
53
bool
QgsScaleUtils::loadScaleList
(
const
QString &fileName, QStringList &scales, QString &errorMessage )
54
{
55
QFile file( fileName );
56
if
( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
57
{
58
errorMessage = u
"Cannot read file %1:\n%2."
_s.arg( fileName, file.errorString() );
59
return
false
;
60
}
61
62
QDomDocument doc;
63
QString errorStr;
64
int
errorLine;
65
int
errorColumn;
66
67
if
( !doc.setContent( &file,
true
, &errorStr, &errorLine, &errorColumn ) )
68
{
69
errorMessage = u
"Parse error at line %1, column %2:\n%3"
_s.arg( errorLine ).arg( errorColumn ).arg( errorStr );
70
return
false
;
71
}
72
73
const
QDomElement root = doc.documentElement();
74
if
( root.tagName() !=
"qgsScales"
_L1 )
75
{
76
errorMessage = u
"The file is not an scales exchange file."
_s;
77
return
false
;
78
}
79
80
QDomElement child = root.firstChildElement();
81
while
( !child.isNull() )
82
{
83
scales.append( child.attribute( u
"value"
_s ) );
84
child = child.nextSiblingElement();
85
}
86
87
return
true
;
88
}
89
90
bool
QgsScaleUtils::equalToOrGreaterThanMinimumScale
(
const
double
scale,
const
double
minScale )
91
{
92
return
scale > minScale ||
qgsDoubleNear
( scale, minScale, 1E-8 );
93
}
94
95
bool
QgsScaleUtils::lessThanMaximumScale
(
const
double
scale,
const
double
maxScale )
96
{
97
return
scale < maxScale && !
qgsDoubleNear
( scale, maxScale, 1E-8 );
98
}
QgsScaleUtils::equalToOrGreaterThanMinimumScale
static bool equalToOrGreaterThanMinimumScale(const double scale, const double minScale)
Returns whether the scale is equal to or greater than the minScale, taking non-round numbers into acc...
Definition
qgsscaleutils.cpp:90
QgsScaleUtils::lessThanMaximumScale
static bool lessThanMaximumScale(const double scale, const double maxScale)
Returns whether the scale is less than the maxScale, taking non-round numbers into account.
Definition
qgsscaleutils.cpp:95
QgsScaleUtils::loadScaleList
static bool loadScaleList(const QString &fileName, QStringList &scales, QString &errorMessage)
Load scales from the given file.
Definition
qgsscaleutils.cpp:53
QgsScaleUtils::saveScaleList
static bool saveScaleList(const QString &fileName, const QStringList &scales, QString &errorMessage)
Save scales to the given file.
Definition
qgsscaleutils.cpp:27
qgis.h
qgsDoubleNear
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition
qgis.h:7743
qgsscaleutils.h
Generated on
for QGIS API Documentation by
1.17.0