def get_temp_filename(self): tmpFile = QTemporaryFile() tmpFile.open() # fileName is no available until open tmpName = tmpFile.fileName() tmpFile.close() tmpFile.setAutoRemove(False) return tmpName
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 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
class ZipWriter: def __init__(self, outputPath, rootDir): self.output = outputPath self.rootDir = rootDir self.zipFile = zipfile.ZipFile(self.output.absoluteFilePath(), 'w') self.tempFile = QTemporaryFile() self.tempFile.setAutoRemove(False) self.tempFile.open(QIODevice.WriteOnly) self.tempFileName = self.tempFile.fileName() self.tempFile.close() def writeTile(self, tile, image, fileFormat, quality): path = '%s/%s/%s' % (self.rootDir, tile.z, tile.x) image.save(self.tempFileName, fileFormat, quality) tilePath = '%s/%s.%s' % (path, tile.y, fileFormat.lower()) self.zipFile.write(self.tempFileName, tilePath) def finalize(self): self.tempFile.close() self.tempFile.remove() self.zipFile.close()