Exemple #1
0
    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)
Exemple #2
0
    def exportRasterLayer(self, layer, destinationFile, options=None):
        outputFormat = self.settings['rasterFormat'] if 'rasterFormat' in self.settings else 'GTiff'

        if os.path.splitext(destinationFile)[1] == '':
            formats = QgsRasterFileWriter.extensionsForFormat(outputFormat)
            if len(formats) > 0:
                destinationFile = '{}.{}'.format(destinationFile, formats[0])

        provider = layer.dataProvider()

        cols = provider.xSize()
        rows = provider.ySize()
        if not provider.capabilities() & QgsRasterDataProvider.Size:
            k = float(provider.extent().width()) / float(provider.extent().height())
            cols = RASTER_SIZE * k
            rows = RASTER_SIZE

        pipe = QgsRasterPipe()
        if not pipe.set(provider.clone()):
            QgsMessageLog.logMessage(self.tr('Failed to export layer "{layer}": Cannot set pipe provider.'.format(layer=layer.name())), 'QConsolidate', Qgis.Warning)
            return False, None

        nodata = {}
        for i in range(1, provider.bandCount() + 1):
            if provider.sourceHasNoDataValue(i):
                value = provider.sourceNoDataValue(i)
                nodata[i] = QgsRasterRange(value, value)

        nuller = QgsRasterNuller()
        for band, value in nodata.items():
            nuller.setNoData(band, [value])

        if not pipe.insert(1, nuller):
            QgsMessageLog.logMessage(self.tr('Failed to export layer "{layer}": Cannot set pipe nuller.'.format(layer=layer.name())), 'QConsolidate', Qgis.Warning)
            return False, None

        writer = QgsRasterFileWriter(destinationFile)
        writer.setOutputFormat(outputFormat)

        if options is not None:
            writer.setCreateOptions(options)

        success = True
        error = writer.writeRaster(pipe, cols, rows, provider.extent(), provider.crs())
        if error != QgsRasterFileWriter.NoError:
            QgsMessageLog.logMessage(self.tr('Failed to export layer "{layer}": {message}.'.format(layer=layer.name(), message=error)), 'QConsolidate', Qgis.Warning)
            success = False

        return success, destinationFile
Exemple #3
0
    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)
        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
Exemple #5
0
    def _exportRaster(self, filePath, rawMode=True):
        provider = self.data.dataProvider()
        cols = provider.xSize()
        rows = provider.ySize()

        pipe = None
        if rawMode:
            pipe = QgsRasterPipe()
            if not pipe.set(provider.clone()):
                self.error = self.tr(
                    'Failed to export layer "{layer}": Cannot set pipe provider.'
                    .format(layer=self.data.name()))
                return False

            nodata = {}
            for i in range(1, provider.bandCount() + 1):
                if provider.sourceHasNoDataValue(i):
                    value = provider.sourceNoDataValue(i)
                    nodata[i] = QgsRasterRange(value, value)

            nuller = QgsRasterNuller()
            for band, value in nodata.items():
                nuller.setNoData(band, [value])

            if not pipe.insert(1, nuller):
                self.error = self.tr(
                    'Failed to export layer "{layer}": Cannot set pipe nuller.'
                    .format(layer=self.data.name()))
                return False
        else:
            pipe = self.data.pipe()

        writer = QgsRasterFileWriter(filePath)
        writer.setOutputFormat('GTiff')

        error = writer.writeRaster(pipe, cols, rows, provider.extent(),
                                   provider.crs())
        if error != QgsRasterFileWriter.NoError:
            self.error = self.tr(
                'Failed to export layer "{layer}": {message}.'.format(
                    layer=self.data.name(), message=error))
            return False

        return True
    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 _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)
Exemple #8
0
    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)
Exemple #10
0
    def exportRasterLayer(self, layer, destinationFile, options=None):
        outputFormat = self.settings[
            'rasterFormat'] if 'rasterFormat' in self.settings else 'GTiff'

        if os.path.splitext(destinationFile)[1] == '':
            formats = QgsRasterFileWriter.extensionsForFormat(outputFormat)
            if len(formats) > 0:
                destinationFile = '{}.{}'.format(destinationFile, formats[0])

        provider = layer.dataProvider()

        cols = provider.xSize()
        rows = provider.ySize()
        if not provider.capabilities() & QgsRasterDataProvider.Size:
            k = float(provider.extent().width()) / float(
                provider.extent().height())
            cols = RASTER_SIZE * k
            rows = RASTER_SIZE

        pipe = QgsRasterPipe()
        if not pipe.set(provider.clone()):
            QgsMessageLog.logMessage(
                self.
                tr('Failed to export layer "{layer}": Cannot set pipe provider.'
                   ).format(layer=layer.name()), 'QConsolidate', Qgis.Warning)
            return False, None

        nodata = {}
        for i in range(1, provider.bandCount() + 1):
            if provider.sourceHasNoDataValue(i):
                value = provider.sourceNoDataValue(i)
                nodata[i] = QgsRasterRange(value, value)

        nuller = QgsRasterNuller()
        for band, value in nodata.items():
            nuller.setNoData(band, [value])

        if not pipe.insert(1, nuller):
            QgsMessageLog.logMessage(
                self.tr(
                    'Failed to export layer "{layer}": Cannot set pipe nuller.'
                ).format(layer=layer.name()), 'QConsolidate', Qgis.Warning)
            return False, None

        writer = QgsRasterFileWriter(destinationFile)
        writer.setOutputFormat(outputFormat)

        if options is not None:
            writer.setCreateOptions(options)

        success = True
        error = writer.writeRaster(pipe, cols, rows, provider.extent(),
                                   provider.crs())
        if error != QgsRasterFileWriter.NoError:
            QgsMessageLog.logMessage(
                self.tr('Failed to export layer "{layer}": {message}.').format(
                    layer=layer.name(), message=error), 'QConsolidate',
                Qgis.Warning)
            success = False

        return success, destinationFile