Exemple #1
0
    def test11_rescaleNormaliseRaster(self):
        in_file = self.gridextract_files[-1]
        rast_crs = crs.getCRSfromRasterFile(in_file)

        with rasterio.open(os.path.normpath(in_file)) as src:
            rescaled = rescale(src, 0, 255)
            rescaled2 = rescale(src, 0, 5)
            norm = normalise(src)
            out_meta = src.meta.copy()

        out_meta['crs'] = rast_crs.crs_wkt
        out_meta['count'] = 1  # contains only one band
        out_meta['dtype'] = np.float32

        out_rescale = os.path.join(TmpDir,
                                   os.path.basename(in_file).replace('.tif', '_rescale0-255.tif'))
        with rasterio.open(os.path.normpath(out_rescale), 'w', **out_meta) as out:
            out.write_band(1, rescaled)

        out_normalised = os.path.join(TmpDir,
                                      os.path.basename(in_file).replace('.tif', '_normalised.tif'))
        with rasterio.open(os.path.normpath(out_normalised), 'w', **out_meta) as out:
            out.write_band(1, rescaled2)

        self.assertAlmostEqual(2.0000722408294678, float(np.nanmax(norm)), 4)
        self.assertAlmostEqual(-2.266947031021118, float(np.nanmin(norm)), 4)

        self.assertEqual(0, np.nanmin(rescaled) )
        self.assertEqual(255, np.nanmax(rescaled))

        self.assertEqual(0, np.nanmin(rescaled2))
        self.assertEqual(5, np.nanmax(rescaled2))
Exemple #2
0
    def test_rescaleMultiBand(self):
        with rasterio.open(os.path.normpath(self.multitif)) as src:
            rescaled = raster_ops.rescale(src, 0, 255, band_num=3)
            rescaled2 = raster_ops.rescale(src, 0, 5, band_num=3)

        self.assertEqual(int(np.nanmin(rescaled)), 0)
        self.assertEqual(int(np.nanmax(rescaled)), 255)

        self.assertEqual(int(np.nanmin(rescaled2)), 0)
        self.assertEqual(int(np.nanmax(rescaled2)), 5)
Exemple #3
0
    def test_rescaleSingleBand(self):
        with rasterio.open(os.path.normpath(self.singletif)) as src:
            rescaled = raster_ops.rescale(src, 0, 255)
            rescaled2 = raster_ops.rescale(src, 0, 5)
            outMeta = src.meta.copy()

        self.assertEqual(np.nanmin(rescaled), 0)
        self.assertEqual(np.nanmax(rescaled), 255)

        self.assertEqual(np.nanmin(rescaled2), 0)
        self.assertEqual(np.nanmax(rescaled2), 5)
    def accept(self, *args, **kwargs):
        if not self.validate():
            return False
        try:

            QApplication.setOverrideCursor(QtCore.Qt.WaitCursor)

            LOGGER.info('{st}\nProcessing {} Raster'.format(
                self.cboMethod.currentText(), st='*' * 50))
            self.iface.mainWindow().statusBar().showMessage(
                'Processing {} Raster'.format(self.cboMethod.currentText()))

            # Add settings to log
            settingsStr = 'Parameters:---------------------------------------'
            settingsStr += '\n    {:30}\t{}'.format(
                'Layer:',
                self.mcboTargetLayer.currentLayer().name())
            settingsStr += '\n    {:30}\t{}'.format('For Band: ',
                                                    self.cboBand.currentText())
            settingsStr += '\n    {:30}\t{}'.format(
                'Method: ', self.cboMethod.currentText())
            if self.cboMethod.currentText() == 'Rescale':
                settingsStr += '\n    {:30}\t{} - {}'.format(
                    'Between:', self.dsbRescaleLower.value(),
                    self.dsbRescaleUpper.value())
            settingsStr += '\n    {:30}\t{}'.format(
                'Output Raster File:', self.lneSaveRasterFile.text())

            LOGGER.info(settingsStr)

            lyrTarget = self.mcboTargetLayer.currentLayer()
            rasterOut = self.lneSaveRasterFile.text()
            removeFileFromQGIS(rasterOut)

            rasterIn = get_layer_source(lyrTarget)
            # need this to maintain correct wkt otherwise gda/mga defaults to utm zonal
            in_crswkt = lyrTarget.crs().toWkt()

            band_num = int(self.cboBand.currentText().replace('Band ', ''))
            with rasterio.open(os.path.normpath(rasterIn)) as src:
                if self.cboMethod.currentText() == 'Rescale':
                    rast_result = rescale(src,
                                          self.dsbRescaleLower.value(),
                                          self.dsbRescaleUpper.value(),
                                          band_num=band_num,
                                          ignore_nodata=True)
                else:
                    rast_result = normalise(src,
                                            band_num=band_num,
                                            ignore_nodata=True)
                meta = src.meta.copy()

                meta['crs'] = str(in_crswkt)
                meta['count'] = 1
                meta['dtype'] = rasterio.float32

            with rasterio.open(os.path.normpath(rasterOut), 'w',
                               **meta) as dst:
                dst.write_band(1, rast_result)

            rasterLyr = addRasterFileToQGIS(rasterOut, atTop=False)

            QApplication.restoreOverrideCursor()
            self.iface.mainWindow().statusBar().clearMessage()
            return super(RescaleNormaliseDialog, self).accept(*args, **kwargs)

        except Exception as err:
            QApplication.restoreOverrideCursor()
            self.cleanMessageBars(True)
            self.iface.mainWindow().statusBar().clearMessage()

            self.send_to_messagebar(str(err),
                                    level=Qgis.Critical,
                                    duration=0,
                                    addToLog=True,
                                    exc_info=sys.exc_info())
            return False  # leave dialog open