Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add a QgsMapSettingsUtils::worldFileContent() function
  • Loading branch information
nirvn committed May 2, 2017
1 parent a188d4f commit 295c212
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 35 deletions.
1 change: 1 addition & 0 deletions python/core/core.sip
Expand Up @@ -100,6 +100,7 @@
%Include qgsmaprenderersequentialjob.sip
%Include qgsmaprenderertask.sip
%Include qgsmapsettings.sip
%Include qgsmapsettingsutils.sip
%Include qgsmaptopixel.sip
%Include qgsmapunitscale.sip
%Include qgsmargins.sip
Expand Down
43 changes: 43 additions & 0 deletions python/core/qgsmapsettingsutils.sip
@@ -0,0 +1,43 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgsmapsettingsutils.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/





class QgsMapSettingsUtils
{
%Docstring
Utilities for map settings.
.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgsmapsettingsutils.h"
%End
public:

static QString worldFileContent( const QgsMapSettings &mapSettings );
%Docstring
Creates the content of a world file.
\param mapSettings map settings
.. note::

Uses 17 places of precision for all numbers output
:rtype: str
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgsmapsettingsutils.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -184,6 +184,7 @@ SET(QGIS_CORE_SRCS
qgsmaprenderersequentialjob.cpp
qgsmaprenderertask.cpp
qgsmapsettings.cpp
qgsmapsettingsutils.cpp
qgsmaptopixel.cpp
qgsmaptopixelgeometrysimplifier.cpp
qgsmapunitscale.cpp
Expand Down Expand Up @@ -751,6 +752,7 @@ SET(QGIS_CORE_HDRS
qgsmaplayerrenderer.h
qgsmaplayerstylemanager.h
qgsmapsettings.h
qgsmapsettingsutils.h
qgsmaptopixel.h
qgsmaptopixelgeometrysimplifier.h
qgsmapunitscale.h
Expand Down
20 changes: 3 additions & 17 deletions src/core/qgsmaprenderertask.cpp
Expand Up @@ -18,6 +18,7 @@
#include "qgsannotation.h"
#include "qgsannotationmanager.h"
#include "qgsmaprenderertask.h"
#include "qgsmapsettingsutils.h"

#include <QFile>
#include <QTextStream>
Expand Down Expand Up @@ -159,23 +160,8 @@ bool QgsMapRendererTask::run()

if ( mSaveWorldFile )
{
QString content;
// note: use 17 places of precision for all numbers output
//Pixel XDim
content += qgsDoubleToString( mMapSettings.mapUnitsPerPixel() ) + "\r\n";
//Rotation on y axis - hard coded
content += QLatin1String( "0 \r\n" );
//Rotation on x axis - hard coded
content += QLatin1String( "0 \r\n" );
//Pixel YDim - almost always negative - see
//http://en.wikipedia.org/wiki/World_file#cite_note-2
content += '-' + qgsDoubleToString( mMapSettings.mapUnitsPerPixel() ) + "\r\n";
//Origin X (center of top left cell)
content += qgsDoubleToString( mMapSettings.visibleExtent().xMinimum() + ( mMapSettings.mapUnitsPerPixel() / 2 ) ) + "\r\n";
//Origin Y (center of top left cell)
content += qgsDoubleToString( mMapSettings.visibleExtent().yMaximum() - ( mMapSettings.mapUnitsPerPixel() / 2 ) ) + "\r\n";

QFileInfo info = QFileInfo( mFileName );

// build the world file name
QString outputSuffix = info.suffix();
QString worldFileName = info.absolutePath() + '/' + info.baseName() + '.'
Expand All @@ -185,7 +171,7 @@ bool QgsMapRendererTask::run()
if ( worldFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) ) //don't use QIODevice::Text
{
QTextStream stream( &worldFile );
stream << content;
stream << QgsMapSettingsUtils::worldFileContent( mMapSettings );
}
}
}
Expand Down
44 changes: 44 additions & 0 deletions src/core/qgsmapsettingsutils.cpp
@@ -0,0 +1,44 @@
/***************************************************************************
qgsmapsettingsutils.cpp
-------------------
begin : May 2017
copyright : (C) 2017 by Mathieu Pellerin
email : nirvn dot asia at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsmapsettings.h"
#include "qgsmapsettingsutils.h"

#include <QString>

QString QgsMapSettingsUtils::worldFileContent( const QgsMapSettings &mapSettings )
{
double xOrigin = mapSettings.visiblePolygon().at( 0 ).x() + ( mapSettings.mapUnitsPerPixel() / 2 );
double yOrigin = mapSettings.visiblePolygon().at( 0 ).y() - ( mapSettings.mapUnitsPerPixel() / 2 );

QString content;
// Pixel XDim
content += qgsDoubleToString( mapSettings.mapUnitsPerPixel() ) + "\r\n";
// Rotation on y axis
content += QString( "%1\r\n" ).arg( mapSettings.rotation() );
// Rotation on x axis
content += QString( "%1\r\n" ).arg( mapSettings.rotation() );
// Pixel YDim - almost always negative
// See https://en.wikipedia.org/wiki/World_file#cite_ref-3
content += '-' + qgsDoubleToString( mapSettings.mapUnitsPerPixel() ) + "\r\n";
// Origin X (center of top left cell)
content += qgsDoubleToString( xOrigin ) + "\r\n";
// Origin Y (center of top left cell)
content += qgsDoubleToString( yOrigin ) + "\r\n";

return content;
}
43 changes: 43 additions & 0 deletions src/core/qgsmapsettingsutils.h
@@ -0,0 +1,43 @@
/***************************************************************************
qgsmapsettingsutils.h
-------------------
begin : May 2017
copyright : (C) 2017 by Mathieu Pellerin
email : nirvn dot asia at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSMAPSETTINGSUTILS_H
#define QGSMAPSETTINGSUTILS_H

#include "qgis_core.h"
#include "qgsmapsettings.h"

#include <QString>

/** \ingroup core
* Utilities for map settings.
* \since QGIS 3.0
*/
class CORE_EXPORT QgsMapSettingsUtils
{

public:

/** Creates the content of a world file.
* \param mapSettings map settings
* \note Uses 17 places of precision for all numbers output
*/
static QString worldFileContent( const QgsMapSettings &mapSettings );

};

#endif
21 changes: 3 additions & 18 deletions src/gui/qgsmapcanvas.cpp
Expand Up @@ -56,6 +56,7 @@ email : sherman at mrcc.com
#include "qgsmaprenderercustompainterjob.h"
#include "qgsmaprendererparalleljob.h"
#include "qgsmaprenderersequentialjob.h"
#include "qgsmapsettingsutils.h"
#include "qgsmessagelog.h"
#include "qgsmessageviewer.h"
#include "qgspallabeling.h"
Expand Down Expand Up @@ -723,24 +724,8 @@ void QgsMapCanvas::saveAsImage( const QString &fileName, QPixmap *theQPixmap, co
painter.end();
image.save( fileName, format.toLocal8Bit().data() );

//create a world file to go with the image...
QgsRectangle myRect = mapSettings().visibleExtent();
QString myHeader;
// note: use 17 places of precision for all numbers output
//Pixel XDim
myHeader += qgsDoubleToString( mapUnitsPerPixel() ) + "\r\n";
//Rotation on y axis - hard coded
myHeader += QLatin1String( "0 \r\n" );
//Rotation on x axis - hard coded
myHeader += QLatin1String( "0 \r\n" );
//Pixel YDim - almost always negative - see
//http://en.wikipedia.org/wiki/World_file#cite_note-2
myHeader += '-' + qgsDoubleToString( mapUnitsPerPixel() ) + "\r\n";
//Origin X (center of top left cell)
myHeader += qgsDoubleToString( myRect.xMinimum() + ( mapUnitsPerPixel() / 2 ) ) + "\r\n";
//Origin Y (center of top left cell)
myHeader += qgsDoubleToString( myRect.yMaximum() - ( mapUnitsPerPixel() / 2 ) ) + "\r\n";
QFileInfo myInfo = QFileInfo( fileName );

// build the world file name
QString outputSuffix = myInfo.suffix();
QString myWorldFileName = myInfo.absolutePath() + '/' + myInfo.baseName() + '.'
Expand All @@ -751,7 +736,7 @@ void QgsMapCanvas::saveAsImage( const QString &fileName, QPixmap *theQPixmap, co
return;
}
QTextStream myStream( &myWorldFile );
myStream << myHeader;
myStream << QgsMapSettingsUtils::worldFileContent( mapSettings() );
} // saveAsImage


Expand Down
1 change: 1 addition & 0 deletions tests/src/core/CMakeLists.txt
Expand Up @@ -128,6 +128,7 @@ SET(TESTS
testqgsmaprendererjob.cpp
testqgsmaprotation.cpp
testqgsmapsettings.cpp
testqgsmapsettingsutils.cpp
testqgsmaptopixelgeometrysimplifier.cpp
testqgsmaptopixel.cpp
testqgsmarkerlinesymbol.cpp
Expand Down
54 changes: 54 additions & 0 deletions tests/src/core/testqgsmapsettingsutils.cpp
@@ -0,0 +1,54 @@
/***************************************************************************
testqgsmapsettingsutils.cpp
-----------------------
begin : May 2017
copyright : (C) 2017 by Mathieu Pellerin
email : nirvn dot asia at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgstest.h"

#include "qgsmapsettings.h"
#include "qgsmapsettingsutils.h"

#include <QString>

class TestQgsMapSettingsUtils : public QObject
{
Q_OBJECT

private slots:
void initTestCase();// will be called before the first testfunction is executed.
void cleanupTestCase() {} // will be called after the last testfunction was executed.
void init() {} // will be called before each testfunction is executed.
void cleanup() {} // will be called after each testfunction was executed.

void createWorldFileContent(); //test world file content function

private:

QgsMapSettings mMapSettings;

};

void TestQgsMapSettingsUtils::initTestCase()
{
mMapSettings.setExtent( QgsRectangle( 0, 0, 1, 1 ) );
}

void TestQgsMapSettingsUtils::createWorldFileContent()
{
QCOMPARE( QgsMapSettingsUtils::worldFileContent( mMapSettings ), QString( "1\r\n0\r\n0\r\n-1\r\n0.5\r\n0.5\r\n" ) );
}

QGSTEST_MAIN( TestQgsMapSettingsUtils )
#include "testqgsmapsettingsutils.moc"

0 comments on commit 295c212

Please sign in to comment.