Skip to content

Commit 495c1ce

Browse files
author
timlinux
committed
Fix image export so that it is georeferenced.
git-svn-id: http://svn.osgeo.org/qgis/branches/Version-1_0@10169 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent a350ffb commit 495c1ce

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/gui/qgsmapcanvas.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ email : sherman at mrcc.com
2020
#include <QtGlobal>
2121
#include <QApplication>
2222
#include <QCursor>
23+
#include <QDir>
24+
#include <QFile>
2325
#include <QGraphicsItem>
2426
#include <QGraphicsScene>
2527
#include <QGraphicsView>
@@ -32,6 +34,7 @@ email : sherman at mrcc.com
3234
#include <QResizeEvent>
3335
#include <QString>
3436
#include <QStringList>
37+
#include <QTextStream>
3538
#include <QWheelEvent>
3639

3740
#include "qgis.h"
@@ -412,6 +415,31 @@ void QgsMapCanvas::saveAsImage( QString theFileName, QPixmap * theQPixmap, QStri
412415
{
413416
mMap->pixmap().save( theFileName, theFormat.toLocal8Bit().data() );
414417
}
418+
//create a world file to go with the image...
419+
QgsRectangle myRect = mMapRenderer->extent();
420+
QString myHeader;
421+
//Pixel XDim
422+
myHeader += QString::number( mapUnitsPerPixel() ) + "\r\n";
423+
//Rotation on y axis - hard coded
424+
myHeader += "0 \r\n";
425+
//Rotation on x axis - hard coded
426+
myHeader += "0 \r\n";
427+
//Pixel YDim - almost always negative - see
428+
//http://en.wikipedia.org/wiki/World_file#cite_note-2
429+
myHeader += "-" + QString::number( mapUnitsPerPixel() ) + "\r\n";
430+
//Origin X (top left corner)
431+
myHeader += QString::number( myRect.xMinimum() ) + "\r\n";
432+
//Origin Y (top left corner)
433+
myHeader += QString::number( myRect.yMaximum() ) + "\r\n";
434+
QFileInfo myInfo = QFileInfo( theFileName );
435+
QString myWorldFileName = myInfo.absolutePath() + QDir::separator() + myInfo.baseName() + "." + theFormat + "w";
436+
QFile myWorldFile( myWorldFileName );
437+
if ( !myWorldFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
438+
{
439+
return;
440+
}
441+
QTextStream myStream( &myWorldFile );
442+
myStream << myHeader;
415443
} // saveAsImage
416444

417445

0 commit comments

Comments
 (0)