Skip to content
Permalink
Browse files

Port some more world file generation related code

  • Loading branch information
nyalldawson committed Dec 11, 2017
1 parent 56383e4 commit f08ff15feb535916a33940eec0894046237e3488
@@ -214,15 +214,14 @@ QgsLayoutExporter::ExportResult QgsLayoutExporter::exportToImage( const QString
{
georeferenceOutput( outputFilePath, nullptr, bounds, settings.dpi );

#if 0
if ( settings.generateWorldFile )
{
// should generate world file for this page
double a, b, c, d, e, f;
if ( bounds.isValid() )
mLayout->computeWorldFileParameters( bounds, a, b, c, d, e, f );
computeWorldFileParameters( bounds, a, b, c, d, e, f, settings.dpi );
else
mLayout->computeWorldFileParameters( a, b, c, d, e, f );
computeWorldFileParameters( a, b, c, d, e, f, settings.dpi );

QFileInfo fi( outputFilePath );
// build the world file name
@@ -232,7 +231,6 @@ QgsLayoutExporter::ExportResult QgsLayoutExporter::exportToImage( const QString

writeWorldFile( worldFileName, a, b, c, d, e, f );
}
#endif
}

}
@@ -323,6 +321,25 @@ double *QgsLayoutExporter::computeGeoTransform( const QgsLayoutItemMap *map, con
return t;
}

void QgsLayoutExporter::writeWorldFile( const QString &worldFileName, double a, double b, double c, double d, double e, double f ) const
{
QFile worldFile( worldFileName );
if ( !worldFile.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
{
return;
}
QTextStream fout( &worldFile );

// QString::number does not use locale settings (for the decimal point)
// which is what we want here
fout << QString::number( a, 'f', 12 ) << "\r\n";
fout << QString::number( d, 'f', 12 ) << "\r\n";
fout << QString::number( b, 'f', 12 ) << "\r\n";
fout << QString::number( e, 'f', 12 ) << "\r\n";
fout << QString::number( c, 'f', 12 ) << "\r\n";
fout << QString::number( f, 'f', 12 ) << "\r\n";
}

bool QgsLayoutExporter::georeferenceOutput( const QString &file, QgsLayoutItemMap *map, const QRectF &exportRegion, double dpi ) const
{
if ( !map )
@@ -230,6 +230,10 @@ class CORE_EXPORT QgsLayoutExporter
*/
double *computeGeoTransform( const QgsLayoutItemMap *referenceMap = nullptr, const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;

//! Write a world file
void writeWorldFile( const QString &fileName, double a, double b, double c, double d, double e, double f ) const;


friend class TestQgsLayout;

};
@@ -276,6 +276,45 @@ def testExportToImage(self):
page2_path = os.path.join(self.basetestpath, 'test_exporttoimagesize_2.png')
self.assertTrue(self.checkImage('exporttoimagesize_page2', 'exporttoimagesize_page2', page2_path))

def testExportWorldFile(self):
l = QgsLayout(QgsProject.instance())
l.initializeDefaults()

# add some items
map = QgsLayoutItemMap(l)
map.attemptSetSceneRect(QRectF(30, 60, 200, 100))
extent = QgsRectangle(2000, 2800, 2500, 2900)
map.setExtent(extent)
l.addLayoutItem(map)

exporter = QgsLayoutExporter(l)
# setup settings
settings = QgsLayoutExporter.ImageExportSettings()
settings.dpi = 80
settings.generateWorldFile = False

rendered_file_path = os.path.join(self.basetestpath, 'test_exportwithworldfile.png')
world_file_path = os.path.join(self.basetestpath, 'test_exportwithworldfile.pgw')
self.assertEqual(exporter.exportToImage(rendered_file_path, settings), QgsLayoutExporter.Success)
self.assertTrue(os.path.exists(rendered_file_path))
self.assertFalse(os.path.exists(world_file_path))

# with world file
settings.generateWorldFile = True
rendered_file_path = os.path.join(self.basetestpath, 'test_exportwithworldfile.png')
self.assertEqual(exporter.exportToImage(rendered_file_path, settings), QgsLayoutExporter.Success)
self.assertTrue(os.path.exists(rendered_file_path))
self.assertTrue(os.path.exists(world_file_path))

lines = tuple(open(world_file_path, 'r'))
values = [float(f) for f in lines]
self.assertAlmostEqual(values[0], 0.794117647059, 2)
self.assertAlmostEqual(values[1], 0.0, 2)
self.assertAlmostEqual(values[2], 0.0, 2)
self.assertAlmostEqual(values[3], -0.794251134644, 2)
self.assertAlmostEqual(values[4], 1925.000000000000, 2)
self.assertAlmostEqual(values[5], 3050.000000000000, 2)


if __name__ == '__main__':
unittest.main()

0 comments on commit f08ff15

Please sign in to comment.
You can’t perform that action at this time.