def _testGeneratePyramids(self, pyramidFormat): tmpName = tempfile.mktemp(suffix='.tif') source = QgsRasterLayer( os.path.join(self.testDataDir, 'raster', 'byte.tif'), 'my', 'gdal') self.assertTrue(source.isValid()) provider = source.dataProvider() fw = QgsRasterFileWriter(tmpName) fw.setBuildPyramidsFlag(QgsRaster.PyramidsFlagYes) fw.setPyramidsFormat(pyramidFormat) fw.setPyramidsList([2]) pipe = QgsRasterPipe() self.assertTrue(pipe.set(provider.clone())) projector = QgsRasterProjector() projector.setCrs(provider.crs(), provider.crs()) self.assertTrue(pipe.insert(2, projector)) self.assertEqual( fw.writeRaster(pipe, provider.xSize(), provider.ySize(), provider.extent(), provider.crs()), 0) del fw ds = gdal.Open(tmpName) self.assertEqual(ds.GetRasterBand(1).GetOverviewCount(), 1) fl = ds.GetFileList() if pyramidFormat == QgsRaster.PyramidsGTiff: self.assertEqual(len(fl), 2, fl) self.assertIn('.ovr', fl[1]) elif pyramidFormat == QgsRaster.PyramidsInternal: self.assertEqual(len(fl), 1, fl) elif pyramidFormat == QgsRaster.PyramidsErdas: self.assertEqual(len(fl), 2, fl) self.assertIn('.aux', fl[1]) os.unlink(tmpName)
def testImportIntoGpkg(self): # init target file test_gpkg = tempfile.mktemp(suffix='.gpkg', dir=self.testDataDir) gdal.GetDriverByName('GPKG').Create(test_gpkg, 1, 1, 1) source = QgsRasterLayer(os.path.join(self.testDataDir, 'raster', 'band3_byte_noct_epsg4326.tif'), 'my', 'gdal') self.assertTrue(source.isValid()) provider = source.dataProvider() fw = QgsRasterFileWriter(test_gpkg) fw.setOutputFormat('gpkg') fw.setCreateOptions(['RASTER_TABLE=imported_table', 'APPEND_SUBDATASET=YES']) pipe = QgsRasterPipe() self.assertTrue(pipe.set(provider.clone())) projector = QgsRasterProjector() projector.setCrs(provider.crs(), provider.crs()) self.assertTrue(pipe.insert(2, projector)) self.assertEqual(fw.writeRaster(pipe, provider.xSize(), provider.ySize(), provider.extent(), provider.crs()), 0) # Check that the test geopackage contains the raster layer and compare rlayer = QgsRasterLayer('GPKG:%s:imported_table' % test_gpkg) self.assertTrue(rlayer.isValid()) out_provider = rlayer.dataProvider() self.assertEqual(provider.block(1, provider.extent(), source.width(), source.height()).data(), out_provider.block(1, out_provider.extent(), rlayer.width(), rlayer.height()).data()) # remove result file os.unlink(test_gpkg)
def write(self, theRasterName): print theRasterName path = "%s/%s" % (self.testDataDir, theRasterName) #myFileInfo = QFileInfo( path ) #myBaseName = myFileInfo.baseName() 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 destroyd 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 #nuller = QgsRasterNuller() #nuller.setNoData( ... ) #if not pipe.insert( 1, nuller ): # print "Cannot set pipe nuller" # 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) # myFileInfo = QFileInfo( path ) # myBaseName = myFileInfo.baseName() 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 destroyd 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 # nuller = QgsRasterNuller() # nuller.setNoData( ... ) # if not pipe.insert( 1, nuller ): # print "Cannot set pipe nuller" # 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 _testGeneratePyramids(self, pyramidFormat): tmpName = tempfile.mktemp(suffix='.tif') source = QgsRasterLayer(os.path.join(self.testDataDir, 'raster', 'byte.tif'), 'my', 'gdal') self.assertTrue(source.isValid()) provider = source.dataProvider() fw = QgsRasterFileWriter(tmpName) fw.setBuildPyramidsFlag(QgsRaster.PyramidsFlagYes) fw.setPyramidsFormat(pyramidFormat) fw.setPyramidsList([2]) pipe = QgsRasterPipe() self.assertTrue(pipe.set(provider.clone())) projector = QgsRasterProjector() projector.setCrs(provider.crs(), provider.crs()) self.assertTrue(pipe.insert(2, projector)) self.assertEqual(fw.writeRaster(pipe, provider.xSize(), provider.ySize(), provider.extent(), provider.crs()), 0) del fw ds = gdal.Open(tmpName) self.assertEqual(ds.GetRasterBand(1).GetOverviewCount(), 1) fl = ds.GetFileList() if pyramidFormat == QgsRaster.PyramidsGTiff: self.assertEqual(len(fl), 2, fl) self.assertIn('.ovr', fl[1]) elif pyramidFormat == QgsRaster.PyramidsInternal: self.assertEqual(len(fl), 1, fl) elif pyramidFormat == QgsRaster.PyramidsErdas: self.assertEqual(len(fl), 2, fl) self.assertIn('.aux', fl[1]) os.unlink(tmpName)
def write_raster(raster_layer, raster_writer, write_path): """Write raster to specified file and update the layer's data source.""" dp = raster_layer.dataProvider() pipe = QgsRasterPipe() if not pipe.set(dp.clone()): raise PackagingError( f"Couldn't set raster pipe projector for layer {write_path}") projector = QgsRasterProjector() projector.setCrs(raster_layer.crs(), raster_layer.crs()) if not pipe.insert(2, projector): raise PackagingError( f"Couldn't set raster pipe provider for layer {write_path}") res = raster_writer.writeRaster(pipe, dp.xSize(), dp.ySize(), dp.extent(), raster_layer.crs()) if not res == QgsRasterFileWriter.NoError: raise PackagingError( f"Couldn't save raster {write_path} - write error: {res}") provider_opts = QgsDataProvider.ProviderOptions() provider_opts.layerName = raster_layer.name() raster_layer.setDataSource(write_path, raster_layer.name(), "gdal", provider_opts)