Skip to content

Commit 295c212

Browse files
committed
Add a QgsMapSettingsUtils::worldFileContent() function
1 parent a188d4f commit 295c212

9 files changed

+194
-35
lines changed

python/core/core.sip

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
%Include qgsmaprenderersequentialjob.sip
101101
%Include qgsmaprenderertask.sip
102102
%Include qgsmapsettings.sip
103+
%Include qgsmapsettingsutils.sip
103104
%Include qgsmaptopixel.sip
104105
%Include qgsmapunitscale.sip
105106
%Include qgsmargins.sip

python/core/qgsmapsettingsutils.sip

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/core/qgsmapsettingsutils.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
12+
13+
class QgsMapSettingsUtils
14+
{
15+
%Docstring
16+
Utilities for map settings.
17+
.. versionadded:: 3.0
18+
%End
19+
20+
%TypeHeaderCode
21+
#include "qgsmapsettingsutils.h"
22+
%End
23+
public:
24+
25+
static QString worldFileContent( const QgsMapSettings &mapSettings );
26+
%Docstring
27+
Creates the content of a world file.
28+
\param mapSettings map settings
29+
.. note::
30+
31+
Uses 17 places of precision for all numbers output
32+
:rtype: str
33+
%End
34+
35+
};
36+
37+
/************************************************************************
38+
* This file has been generated automatically from *
39+
* *
40+
* src/core/qgsmapsettingsutils.h *
41+
* *
42+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
43+
************************************************************************/

src/core/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ SET(QGIS_CORE_SRCS
184184
qgsmaprenderersequentialjob.cpp
185185
qgsmaprenderertask.cpp
186186
qgsmapsettings.cpp
187+
qgsmapsettingsutils.cpp
187188
qgsmaptopixel.cpp
188189
qgsmaptopixelgeometrysimplifier.cpp
189190
qgsmapunitscale.cpp
@@ -751,6 +752,7 @@ SET(QGIS_CORE_HDRS
751752
qgsmaplayerrenderer.h
752753
qgsmaplayerstylemanager.h
753754
qgsmapsettings.h
755+
qgsmapsettingsutils.h
754756
qgsmaptopixel.h
755757
qgsmaptopixelgeometrysimplifier.h
756758
qgsmapunitscale.h

src/core/qgsmaprenderertask.cpp

+3-17
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "qgsannotation.h"
1919
#include "qgsannotationmanager.h"
2020
#include "qgsmaprenderertask.h"
21+
#include "qgsmapsettingsutils.h"
2122

2223
#include <QFile>
2324
#include <QTextStream>
@@ -159,23 +160,8 @@ bool QgsMapRendererTask::run()
159160

160161
if ( mSaveWorldFile )
161162
{
162-
QString content;
163-
// note: use 17 places of precision for all numbers output
164-
//Pixel XDim
165-
content += qgsDoubleToString( mMapSettings.mapUnitsPerPixel() ) + "\r\n";
166-
//Rotation on y axis - hard coded
167-
content += QLatin1String( "0 \r\n" );
168-
//Rotation on x axis - hard coded
169-
content += QLatin1String( "0 \r\n" );
170-
//Pixel YDim - almost always negative - see
171-
//http://en.wikipedia.org/wiki/World_file#cite_note-2
172-
content += '-' + qgsDoubleToString( mMapSettings.mapUnitsPerPixel() ) + "\r\n";
173-
//Origin X (center of top left cell)
174-
content += qgsDoubleToString( mMapSettings.visibleExtent().xMinimum() + ( mMapSettings.mapUnitsPerPixel() / 2 ) ) + "\r\n";
175-
//Origin Y (center of top left cell)
176-
content += qgsDoubleToString( mMapSettings.visibleExtent().yMaximum() - ( mMapSettings.mapUnitsPerPixel() / 2 ) ) + "\r\n";
177-
178163
QFileInfo info = QFileInfo( mFileName );
164+
179165
// build the world file name
180166
QString outputSuffix = info.suffix();
181167
QString worldFileName = info.absolutePath() + '/' + info.baseName() + '.'
@@ -185,7 +171,7 @@ bool QgsMapRendererTask::run()
185171
if ( worldFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) ) //don't use QIODevice::Text
186172
{
187173
QTextStream stream( &worldFile );
188-
stream << content;
174+
stream << QgsMapSettingsUtils::worldFileContent( mMapSettings );
189175
}
190176
}
191177
}

src/core/qgsmapsettingsutils.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/***************************************************************************
2+
qgsmapsettingsutils.cpp
3+
-------------------
4+
begin : May 2017
5+
copyright : (C) 2017 by Mathieu Pellerin
6+
email : nirvn dot asia 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 "qgsmapsettings.h"
19+
#include "qgsmapsettingsutils.h"
20+
21+
#include <QString>
22+
23+
QString QgsMapSettingsUtils::worldFileContent( const QgsMapSettings &mapSettings )
24+
{
25+
double xOrigin = mapSettings.visiblePolygon().at( 0 ).x() + ( mapSettings.mapUnitsPerPixel() / 2 );
26+
double yOrigin = mapSettings.visiblePolygon().at( 0 ).y() - ( mapSettings.mapUnitsPerPixel() / 2 );
27+
28+
QString content;
29+
// Pixel XDim
30+
content += qgsDoubleToString( mapSettings.mapUnitsPerPixel() ) + "\r\n";
31+
// Rotation on y axis
32+
content += QString( "%1\r\n" ).arg( mapSettings.rotation() );
33+
// Rotation on x axis
34+
content += QString( "%1\r\n" ).arg( mapSettings.rotation() );
35+
// Pixel YDim - almost always negative
36+
// See https://en.wikipedia.org/wiki/World_file#cite_ref-3
37+
content += '-' + qgsDoubleToString( mapSettings.mapUnitsPerPixel() ) + "\r\n";
38+
// Origin X (center of top left cell)
39+
content += qgsDoubleToString( xOrigin ) + "\r\n";
40+
// Origin Y (center of top left cell)
41+
content += qgsDoubleToString( yOrigin ) + "\r\n";
42+
43+
return content;
44+
}

src/core/qgsmapsettingsutils.h

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/***************************************************************************
2+
qgsmapsettingsutils.h
3+
-------------------
4+
begin : May 2017
5+
copyright : (C) 2017 by Mathieu Pellerin
6+
email : nirvn dot asia 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+
#ifndef QGSMAPSETTINGSUTILS_H
19+
#define QGSMAPSETTINGSUTILS_H
20+
21+
#include "qgis_core.h"
22+
#include "qgsmapsettings.h"
23+
24+
#include <QString>
25+
26+
/** \ingroup core
27+
* Utilities for map settings.
28+
* \since QGIS 3.0
29+
*/
30+
class CORE_EXPORT QgsMapSettingsUtils
31+
{
32+
33+
public:
34+
35+
/** Creates the content of a world file.
36+
* \param mapSettings map settings
37+
* \note Uses 17 places of precision for all numbers output
38+
*/
39+
static QString worldFileContent( const QgsMapSettings &mapSettings );
40+
41+
};
42+
43+
#endif

src/gui/qgsmapcanvas.cpp

+3-18
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ email : sherman at mrcc.com
5656
#include "qgsmaprenderercustompainterjob.h"
5757
#include "qgsmaprendererparalleljob.h"
5858
#include "qgsmaprenderersequentialjob.h"
59+
#include "qgsmapsettingsutils.h"
5960
#include "qgsmessagelog.h"
6061
#include "qgsmessageviewer.h"
6162
#include "qgspallabeling.h"
@@ -723,24 +724,8 @@ void QgsMapCanvas::saveAsImage( const QString &fileName, QPixmap *theQPixmap, co
723724
painter.end();
724725
image.save( fileName, format.toLocal8Bit().data() );
725726

726-
//create a world file to go with the image...
727-
QgsRectangle myRect = mapSettings().visibleExtent();
728-
QString myHeader;
729-
// note: use 17 places of precision for all numbers output
730-
//Pixel XDim
731-
myHeader += qgsDoubleToString( mapUnitsPerPixel() ) + "\r\n";
732-
//Rotation on y axis - hard coded
733-
myHeader += QLatin1String( "0 \r\n" );
734-
//Rotation on x axis - hard coded
735-
myHeader += QLatin1String( "0 \r\n" );
736-
//Pixel YDim - almost always negative - see
737-
//http://en.wikipedia.org/wiki/World_file#cite_note-2
738-
myHeader += '-' + qgsDoubleToString( mapUnitsPerPixel() ) + "\r\n";
739-
//Origin X (center of top left cell)
740-
myHeader += qgsDoubleToString( myRect.xMinimum() + ( mapUnitsPerPixel() / 2 ) ) + "\r\n";
741-
//Origin Y (center of top left cell)
742-
myHeader += qgsDoubleToString( myRect.yMaximum() - ( mapUnitsPerPixel() / 2 ) ) + "\r\n";
743727
QFileInfo myInfo = QFileInfo( fileName );
728+
744729
// build the world file name
745730
QString outputSuffix = myInfo.suffix();
746731
QString myWorldFileName = myInfo.absolutePath() + '/' + myInfo.baseName() + '.'
@@ -751,7 +736,7 @@ void QgsMapCanvas::saveAsImage( const QString &fileName, QPixmap *theQPixmap, co
751736
return;
752737
}
753738
QTextStream myStream( &myWorldFile );
754-
myStream << myHeader;
739+
myStream << QgsMapSettingsUtils::worldFileContent( mapSettings() );
755740
} // saveAsImage
756741

757742

tests/src/core/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ SET(TESTS
128128
testqgsmaprendererjob.cpp
129129
testqgsmaprotation.cpp
130130
testqgsmapsettings.cpp
131+
testqgsmapsettingsutils.cpp
131132
testqgsmaptopixelgeometrysimplifier.cpp
132133
testqgsmaptopixel.cpp
133134
testqgsmarkerlinesymbol.cpp
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/***************************************************************************
2+
testqgsmapsettingsutils.cpp
3+
-----------------------
4+
begin : May 2017
5+
copyright : (C) 2017 by Mathieu Pellerin
6+
email : nirvn dot asia 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 "qgstest.h"
19+
20+
#include "qgsmapsettings.h"
21+
#include "qgsmapsettingsutils.h"
22+
23+
#include <QString>
24+
25+
class TestQgsMapSettingsUtils : public QObject
26+
{
27+
Q_OBJECT
28+
29+
private slots:
30+
void initTestCase();// will be called before the first testfunction is executed.
31+
void cleanupTestCase() {} // will be called after the last testfunction was executed.
32+
void init() {} // will be called before each testfunction is executed.
33+
void cleanup() {} // will be called after each testfunction was executed.
34+
35+
void createWorldFileContent(); //test world file content function
36+
37+
private:
38+
39+
QgsMapSettings mMapSettings;
40+
41+
};
42+
43+
void TestQgsMapSettingsUtils::initTestCase()
44+
{
45+
mMapSettings.setExtent( QgsRectangle( 0, 0, 1, 1 ) );
46+
}
47+
48+
void TestQgsMapSettingsUtils::createWorldFileContent()
49+
{
50+
QCOMPARE( QgsMapSettingsUtils::worldFileContent( mMapSettings ), QString( "1\r\n0\r\n0\r\n-1\r\n0.5\r\n0.5\r\n" ) );
51+
}
52+
53+
QGSTEST_MAIN( TestQgsMapSettingsUtils )
54+
#include "testqgsmapsettingsutils.moc"

0 commit comments

Comments
 (0)