QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
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
25using namespace Qt::StringLiterals;
26
27bool 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
53bool 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
70 .arg( errorLine )
71 .arg( errorColumn )
72 .arg( errorStr );
73 return false;
74 }
75
76 const QDomElement root = doc.documentElement();
77 if ( root.tagName() != "qgsScales"_L1 )
78 {
79 errorMessage = u"The file is not an scales exchange file."_s;
80 return false;
81 }
82
83 QDomElement child = root.firstChildElement();
84 while ( !child.isNull() )
85 {
86 scales.append( child.attribute( u"value"_s ) );
87 child = child.nextSiblingElement();
88 }
89
90 return true;
91}
92
93bool QgsScaleUtils::equalToOrGreaterThanMinimumScale( const double scale, const double minScale )
94{
95 return scale > minScale || qgsDoubleNear( scale, minScale, 1E-8 );
96}
97
98bool QgsScaleUtils::lessThanMaximumScale( const double scale, const double maxScale )
99{
100 return scale < maxScale && !qgsDoubleNear( scale, maxScale, 1E-8 );
101}
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...
static bool lessThanMaximumScale(const double scale, const double maxScale)
Returns whether the scale is less than the maxScale, taking non-round numbers into account.
static bool loadScaleList(const QString &fileName, QStringList &scales, QString &errorMessage)
Load scales from the given file.
static bool saveScaleList(const QString &fileName, const QStringList &scales, QString &errorMessage)
Save scales to the given file.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6900