/
test_qgsrasterfilewriter.py
112 lines (88 loc) · 3.72 KB
/
test_qgsrasterfilewriter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsRasterFileWriter.
.. note:: 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.
"""
__author__ = 'Radim Blazek'
__date__ = '20/08/2012'
__copyright__ = 'Copyright 2012, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import qgis # NOQA
import os
import glob
from qgis.PyQt.QtCore import QTemporaryFile, QDir
from qgis.core import (QgsRasterLayer,
QgsRasterChecker,
QgsRasterPipe,
QgsRasterFileWriter,
QgsRasterProjector)
from qgis.testing import start_app, unittest
from utilities import unitTestDataPath
start_app()
class TestQgsRasterFileWriter(unittest.TestCase):
def __init__(self, methodName):
unittest.TestCase.__init__(self, methodName)
self.testDataDir = unitTestDataPath()
self.report = "<h1>Python Raster File Writer Tests</h1>\n"
def write(self, theRasterName):
print(theRasterName)
path = "%s/%s" % (self.testDataDir, theRasterName)
rasterLayer = QgsRasterLayer(path, "test")
if not rasterLayer.isValid():
return False
provider = rasterLayer.dataProvider()
tmpFile = QTemporaryFile()
tmpFile.open() # fileName is no avialable until open
tmpName = tmpFile.fileName()
tmpFile.close()
# do not remove when class is destroyed so that we can read
# the file and see difference
tmpFile.setAutoRemove(False)
fileWriter = QgsRasterFileWriter(tmpName)
pipe = QgsRasterPipe()
if not pipe.set(provider.clone()):
print("Cannot set pipe provider")
return False
projector = QgsRasterProjector()
projector.setCrs(provider.crs(), provider.crs())
if not pipe.insert(2, projector):
print("Cannot set pipe projector")
return False
fileWriter.writeRaster(
pipe,
provider.xSize(),
provider.ySize(),
provider.extent(),
provider.crs())
checker = QgsRasterChecker()
ok = checker.runTest("gdal", tmpName, "gdal", path)
self.report += checker.report()
# All OK, we can delete the file
tmpFile.setAutoRemove(ok)
return ok
def testWrite(self):
for name in glob.glob("%s/raster/*.tif" % self.testDataDir):
baseName = os.path.basename(name)
allOk = True
ok = self.write("raster/%s" % baseName)
if not ok:
allOk = False
reportFilePath = "%s/qgistest.html" % QDir.tempPath()
reportFile = open(reportFilePath, 'a')
reportFile.write(self.report)
reportFile.close()
assert allOk, "Raster file writer test failed"
def testDriverForExtension(self):
self.assertEqual(QgsRasterFileWriter.driverForExtension('tif'), 'GTiff')
self.assertEqual(QgsRasterFileWriter.driverForExtension('TIF'), 'GTiff')
self.assertEqual(QgsRasterFileWriter.driverForExtension('tIf'), 'GTiff')
self.assertEqual(QgsRasterFileWriter.driverForExtension('.tif'), 'GTiff')
self.assertEqual(QgsRasterFileWriter.driverForExtension('img'), 'HFA')
self.assertEqual(QgsRasterFileWriter.driverForExtension('.vrt'), 'VRT')
self.assertEqual(QgsRasterFileWriter.driverForExtension('not a format'), '')
self.assertEqual(QgsRasterFileWriter.driverForExtension(''), '')
if __name__ == '__main__':
unittest.main()