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