QGIS API Documentation 3.99.0-Master (26c88405ac0)
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 <QTextStream>
23
24bool QgsScaleUtils::saveScaleList( const QString &fileName, const QStringList &scales, QString &errorMessage )
25{
26 QDomDocument doc;
27 QDomElement root = doc.createElement( QStringLiteral( "qgsScales" ) );
28 root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
29 doc.appendChild( root );
30
31 for ( int i = 0; i < scales.count(); ++i )
32 {
33 QDomElement el = doc.createElement( QStringLiteral( "scale" ) );
34 el.setAttribute( QStringLiteral( "value" ), scales.at( i ) );
35 root.appendChild( el );
36 }
37
38 QFile file( fileName );
39 if ( !file.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
40 {
41 errorMessage = QStringLiteral( "Cannot write file %1:\n%2." ).arg( fileName, file.errorString() );
42 return false;
43 }
44
45 QTextStream out( &file );
46 doc.save( out, 4 );
47 return true;
48}
49
50bool QgsScaleUtils::loadScaleList( const QString &fileName, QStringList &scales, QString &errorMessage )
51{
52 QFile file( fileName );
53 if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
54 {
55 errorMessage = QStringLiteral( "Cannot read file %1:\n%2." ).arg( fileName, file.errorString() );
56 return false;
57 }
58
59 QDomDocument doc;
60 QString errorStr;
61 int errorLine;
62 int errorColumn;
63
64 if ( !doc.setContent( &file, true, &errorStr, &errorLine, &errorColumn ) )
65 {
66 errorMessage = QStringLiteral( "Parse error at line %1, column %2:\n%3" )
67 .arg( errorLine )
68 .arg( errorColumn )
69 .arg( errorStr );
70 return false;
71 }
72
73 const QDomElement root = doc.documentElement();
74 if ( root.tagName() != QLatin1String( "qgsScales" ) )
75 {
76 errorMessage = QStringLiteral( "The file is not an scales exchange file." );
77 return false;
78 }
79
80 QDomElement child = root.firstChildElement();
81 while ( !child.isNull() )
82 {
83 scales.append( child.attribute( QStringLiteral( "value" ) ) );
84 child = child.nextSiblingElement();
85 }
86
87 return true;
88}
89
90bool QgsScaleUtils::equalToOrGreaterThanMinimumScale( const double scale, const double minScale )
91{
92 return scale > minScale || qgsDoubleNear( scale, minScale, 1E-8 );
93}
94
95bool QgsScaleUtils::lessThanMaximumScale( const double scale, const double maxScale )
96{
97 return scale < maxScale && !qgsDoubleNear( scale, maxScale, 1E-8 );
98}
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:6607