def postProcessAlgorithm(self, context, feedback):

        output = QgsProcessingUtils.mapLayerFromString(self.output_model,
                                                       context)
        provider = output.dataProvider()

        stats = provider.bandStatistics(1, QgsRasterBandStats.All,
                                        output.extent(), 0)
        mean, sd = stats.mean, stats.stdDev

        rnd = QgsSingleBandGrayRenderer(provider, 1)
        ce = QgsContrastEnhancement(provider.dataType(1))
        ce.setContrastEnhancementAlgorithm(
            QgsContrastEnhancement.StretchToMinimumMaximum)

        ce.setMinimumValue(mean - 3 * sd)
        ce.setMaximumValue(min(1, mean + 2 * sd))

        rnd.setContrastEnhancement(ce)

        output.setRenderer(rnd)

        output.triggerRepaint()

        return {self.OUTPUT: self.output_model}
Пример #2
0
 def update_pc_style(self):
     """Update the grey style using mean+-5*std for all principal components
     """
     for view_widget in MainAnalysisDialog.view_widgets:
         if view_widget.pc_id is not None:
             src_ds = gdal.Open(
                 str(get_file_path_of_layer(
                     view_widget.render_widget.layer)), gdal.GA_ReadOnly)
             ds = src_ds.GetRasterBand(1).ReadAsArray().flatten().astype(
                 np.float32)
             ds = ds[ds != 0]
             try:
                 mean = np.mean(ds)
                 std = np.std(ds)
             except:
                 continue
             renderer = QgsSingleBandGrayRenderer(
                 view_widget.render_widget.layer.dataProvider(), 1)
             ce = QgsContrastEnhancement(
                 view_widget.render_widget.layer.dataProvider().dataType(0))
             ce.setContrastEnhancementAlgorithm(
                 QgsContrastEnhancement.StretchToMinimumMaximum)
             ce.setMinimumValue(mean - 5 * std)
             ce.setMaximumValue(mean + 5 * std)
             renderer.setContrastEnhancement(ce)
             view_widget.render_widget.layer.setRenderer(renderer)
             del src_ds, ds