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}
Beispiel #2
0
    def load_layer(self, layer):
        r = layer.renderer().clone()
        r.setRedBand(self._find_band(layer, "red", 0))
        r.setGreenBand(self._find_band(layer, "green", 1))
        r.setBlueBand(self._find_band(layer, "blue", 2))

        usedBands = r.usesBands()
        for b in range(3):
            typ = layer.renderer().dataType(b)
            enhancement = QgsContrastEnhancement(typ)
            enhancement.setContrastEnhancementAlgorithm(
                QgsContrastEnhancement.StretchToMinimumMaximum, True)
            bandMin, bandMax = layer.dataProvider().cumulativeCut(
                usedBands[b], 0.02, 0.98, sampleSize=10000)
            enhancement.setMinimumValue(bandMin)
            enhancement.setMaximumValue(bandMax)
            if b == 0:
                r.setRedContrastEnhancement(enhancement)
            elif b == 1:
                r.setGreenContrastEnhancement(enhancement)
            elif b == 2:
                r.setBlueContrastEnhancement(enhancement)

        layer.setRenderer(r)

        QgsProject.instance().addMapLayer(layer)
Beispiel #3
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
Beispiel #4
0
def set_raster_renderer_to_singleband(layer: QgsRasterLayer, band: int = 1) -> None:
    """
    Set raster renderer to singleband
    :param layer: raster layer
    :param band: band number starting from 1
    """
    # https://gis.stackexchange.com/a/377631/123927 and
    # https://gis.stackexchange.com/a/157573/123927
    provider: QgsRasterDataProvider = layer.dataProvider()
    renderer: QgsSingleBandGrayRenderer = QgsSingleBandGrayRenderer(
        layer.dataProvider(), band
    )

    stats: QgsRasterBandStats = provider.bandStatistics(
        band, QgsRasterBandStats.All, layer.extent(), 0
    )
    min_val = max(stats.minimumValue, 0)
    max_val = max(stats.maximumValue, 0)

    enhancement = QgsContrastEnhancement(renderer.dataType(band))
    contrast_enhancement = QgsContrastEnhancement.StretchToMinimumMaximum
    enhancement.setContrastEnhancementAlgorithm(contrast_enhancement, True)
    enhancement.setMinimumValue(min_val)
    enhancement.setMaximumValue(max_val)
    layer.setRenderer(renderer)
    layer.renderer().setContrastEnhancement(enhancement)
    layer.triggerRepaint()
Beispiel #5
0
def contrastForRasters(the_raster_layer, min_layer, max_layer, band = None):
    """
    Applies a contrast between min and max. If given min and max are 0, then calculates the min and max from gdal.
    """
    # type of layer : raster, vector, other
    type_of_layer = the_raster_layer.type()

    # take the layer renderer to get the min and max
    layer_renderer = the_raster_layer.renderer()  # for qgis > 1.9
    data_provider = the_raster_layer.data_provider()

    # the layer has to be a raster layer
    if type_of_layer == 1:
        if the_raster_layer.rasterType() == 0 and layer_renderer:
            # gray band
            # layer_renderer <qgis.core.QgsSingleBandGrayRenderer object at 0x514caf0>
            layerCE = layer_renderer.contrastEnhancement()
            # take the contrast enhancement of the layer threw the renderer
            if layerCE:
                layerCE.setContrastEnhancementAlgorithm(3)  # qgis 1.9
                layerCE.setMinimumValue(min_layer)
                layerCE.setMaximumValue(max_layer)
        elif the_raster_layer.rasterType() == 2 and layer_renderer:
            if min_layer == 0 and max_layer == 0:
                if band:
                    min1, max1 = get_min_max_via_qgis(the_raster_layer, band[0])
                    min2, max2 = get_min_max_via_qgis(the_raster_layer, band[1])
                    min3, max3 = get_min_max_via_qgis(the_raster_layer, band[2])

# #                     min1, max1, _, _ = terre_image_utils.computeStatistics(the_raster_layer.source(),0, band[0])
# #                     min2, max2, _, _ = terre_image_utils.computeStatistics(the_raster_layer.source(),0, band[1])
# #                     min3, max3, _, _ = terre_image_utils.computeStatistics(the_raster_layer.source(),0, band[2])
# #                     #print min1, max1, min2, max2, min3, max3
                else:
                    min1, max1, _, _ = OTBApplications.computeStatistics(the_raster_layer.source(), 0, 1)
                    min2, max2, _, _ = OTBApplications.computeStatistics(the_raster_layer.source(), 0, 2)
                    min3, max3, _, _ = OTBApplications.computeStatistics(the_raster_layer.source(), 0, 3)

                red_enhancement = QgsContrastEnhancement(data_provider.dataType(0))
                green_enhancement = QgsContrastEnhancement(data_provider.dataType(1))
                blue_enhancement = QgsContrastEnhancement(data_provider.dataType(2))
                # set stretch to min max
                red_enhancement.setMinimumValue(min1)
                red_enhancement.setMaximumValue(max1)
                green_enhancement.setMinimumValue(min2)
                green_enhancement.setMaximumValue(max2)
                blue_enhancement.setMinimumValue(min3)
                blue_enhancement.setMaximumValue(max3)
                red_enhancement.setContrastEnhancementAlgorithm(3)
                green_enhancement.setContrastEnhancementAlgorithm(3)
                blue_enhancement.setContrastEnhancementAlgorithm(3)
                layer_renderer.setRedContrastEnhancement(red_enhancement)  # , QgsRaster.ContrastEnhancementCumulativeCut  )
                layer_renderer.setGreenContrastEnhancement(green_enhancement)  # , QgsRaster.ContrastEnhancementCumulativeCut  )
                layer_renderer.setBlueContrastEnhancement(blue_enhancement)  # , QgsRaster.ContrastEnhancementCumulativeCut  )
        the_raster_layer.triggerRepaint()
    def testRenderWithContrast(self):
        layer = QgsPointCloudLayer(
            unitTestDataPath() + '/point_clouds/ept/rgb/ept.json', 'test',
            'ept')
        self.assertTrue(layer.isValid())

        layer.renderer().setPointSize(2)
        layer.renderer().setPointSizeUnit(QgsUnitTypes.RenderMillimeters)

        redce = QgsContrastEnhancement()
        redce.setMinimumValue(100)
        redce.setMaximumValue(120)
        redce.setContrastEnhancementAlgorithm(
            QgsContrastEnhancement.StretchToMinimumMaximum)
        layer.renderer().setRedContrastEnhancement(redce)

        greence = QgsContrastEnhancement()
        greence.setMinimumValue(130)
        greence.setMaximumValue(150)
        greence.setContrastEnhancementAlgorithm(
            QgsContrastEnhancement.StretchToMinimumMaximum)
        layer.renderer().setGreenContrastEnhancement(greence)

        bluece = QgsContrastEnhancement()
        bluece.setMinimumValue(170)
        bluece.setMaximumValue(190)
        bluece.setContrastEnhancementAlgorithm(
            QgsContrastEnhancement.StretchToMinimumMaximum)
        layer.renderer().setBlueContrastEnhancement(bluece)

        mapsettings = QgsMapSettings()
        mapsettings.setOutputSize(QSize(400, 400))
        mapsettings.setOutputDpi(96)
        mapsettings.setDestinationCrs(layer.crs())
        mapsettings.setExtent(
            QgsRectangle(497753.5, 7050887.5, 497754.6, 7050888.6))
        mapsettings.setLayers([layer])

        renderchecker = QgsMultiRenderChecker()
        renderchecker.setMapSettings(mapsettings)
        renderchecker.setControlPathPrefix('pointcloudrenderer')
        renderchecker.setControlName('expected_rgb_contrast')
        result = renderchecker.runTest('expected_rgb_contrast')
        TestQgsPointCloudRgbRenderer.report += renderchecker.report()
        self.assertTrue(result)
Beispiel #7
0
def custom_stretch(the_raster_layer, values, canvas, mono = False):
    """
    Applies a contrast between min and max. If given min and max are 0, then calculates the min and max from gdal.
    """
#     logger.info("#######################")
    logger.info("custom stretch: values")
    logger.info(values)

    # print "custom stretch"
    # print canvas
    # print "layer :", the_raster_layer

    # type of layer : raster, vector, other
    type_of_layer = the_raster_layer.type()

    # take the layer renderer to get the min and max
    layer_renderer = the_raster_layer.renderer()  # for qgis > 1.9
    data_provider = the_raster_layer.dataProvider()

    # print "values", values
    # the layer has to be a raster layer
    if type_of_layer == 1:
        if (the_raster_layer.rasterType() == 0 or mono) and layer_renderer:
            min_layer, max_layer = values[0]
            # gray band
            # layer_renderer <qgis.core.QgsSingleBandGrayRenderer object at 0x514caf0>
            gray_enhancement = QgsContrastEnhancement(data_provider.dataType(1))
            # take the contrast enhancement of the layer threw the renderer
            if gray_enhancement:
                gray_enhancement.setContrastEnhancementAlgorithm(1)  # qgis 1.9
                gray_enhancement.setMinimumValue(min_layer)
                gray_enhancement.setMaximumValue(max_layer)
                layer_renderer.setContrastEnhancement(gray_enhancement)

        elif the_raster_layer.rasterType() == 2 and layer_renderer:
            # print "layer 3 bandes"
            min_red, max_red = values[0]
            min_green, max_green = values[1]
            min_blue, max_blue = values[2]
            logger.debug("red : " + str(min_red) + " " + str(max_red))
            logger.debug("green : " + str(min_green) + " " + str(max_green))
            logger.debug("blue : " + str(min_blue) + " " + str(max_blue))

            red_enhancement = QgsContrastEnhancement(data_provider.dataType(1))
            green_enhancement = QgsContrastEnhancement(data_provider.dataType(2))
            blue_enhancement = QgsContrastEnhancement(data_provider.dataType(3))
            logger.debug("red_enhancement : " + str(red_enhancement))
            logger.debug("green_enhancement : " + str(green_enhancement))
            logger.debug("blue_enhancement : " + str(blue_enhancement))

            # set stretch to min max
            red_enhancement.setMinimumValue(min_red)
            red_enhancement.setMaximumValue(max_red)
            green_enhancement.setMinimumValue(min_green)
            green_enhancement.setMaximumValue(max_green)
            blue_enhancement.setMinimumValue(min_blue)
            blue_enhancement.setMaximumValue(max_blue)
            logger.debug("red (1): " + str(red_enhancement.minimumValue()) + " " + str(red_enhancement.maximumValue()))
            logger.debug("green (1): " + str(green_enhancement.minimumValue()) + " " + str(green_enhancement.maximumValue()))
            logger.debug("blue (1): " + str(blue_enhancement.minimumValue()) + " " + str(blue_enhancement.maximumValue()))
            red_enhancement.setContrastEnhancementAlgorithm(1)
            green_enhancement.setContrastEnhancementAlgorithm(1)
            blue_enhancement.setContrastEnhancementAlgorithm(1)
            logger.debug("red (2): " + str(red_enhancement.minimumValue()) + " " + str(red_enhancement.maximumValue()))
            logger.debug("green (2): " + str(green_enhancement.minimumValue()) + " " + str(green_enhancement.maximumValue()))
            logger.debug("blue (2): " + str(blue_enhancement.minimumValue()) + " " + str(blue_enhancement.maximumValue()))

            # print "blue enhancement", blue_enhancement
            # print "blue max", blue_enhancement.maximumValue()
            # print "blue min", blue_enhancement.minimumValue()

            layer_renderer.setRedContrastEnhancement(red_enhancement)  # , QgsRaster.ContrastEnhancementCumulativeCut  )
            layer_renderer.setGreenContrastEnhancement(green_enhancement)  # , QgsRaster.ContrastEnhancementCumulativeCut  )
            layer_renderer.setBlueContrastEnhancement(blue_enhancement)  # , QgsRaster.ContrastEnhancementCumulativeCut  )

            # print "layer renderer"
            red_enhancement_debug = layer_renderer.redContrastEnhancement()
            green_enhancement_debug = layer_renderer.greenContrastEnhancement()
            blue_enhancement_debug = layer_renderer.blueContrastEnhancement()
            logger.debug("red (3): " + str(red_enhancement_debug.minimumValue()) + " " + str(red_enhancement_debug.maximumValue()))
            logger.debug("green (3): " + str(green_enhancement_debug.minimumValue()) + " " + str(green_enhancement_debug.maximumValue()))
            logger.debug("blue (3): " + str(blue_enhancement_debug.minimumValue()) + " " + str(blue_enhancement_debug.maximumValue()))

        # print "end"
        the_raster_layer.setCacheImage(None)
        the_raster_layer.triggerRepaint()
        # print "2"
    canvas.refresh()
    canvas.repaint()
    def testBasic(self):
        renderer = QgsPointCloudRgbRenderer()
        renderer.setBlueAttribute('b')
        self.assertEqual(renderer.blueAttribute(), 'b')
        renderer.setGreenAttribute('g')
        self.assertEqual(renderer.greenAttribute(), 'g')
        renderer.setRedAttribute('r')
        self.assertEqual(renderer.redAttribute(), 'r')

        redce = QgsContrastEnhancement()
        redce.setMinimumValue(100)
        redce.setMaximumValue(120)
        redce.setContrastEnhancementAlgorithm(
            QgsContrastEnhancement.StretchAndClipToMinimumMaximum)
        renderer.setRedContrastEnhancement(redce)

        greence = QgsContrastEnhancement()
        greence.setMinimumValue(130)
        greence.setMaximumValue(150)
        greence.setContrastEnhancementAlgorithm(
            QgsContrastEnhancement.StretchToMinimumMaximum)
        renderer.setGreenContrastEnhancement(greence)

        bluece = QgsContrastEnhancement()
        bluece.setMinimumValue(170)
        bluece.setMaximumValue(190)
        bluece.setContrastEnhancementAlgorithm(
            QgsContrastEnhancement.ClipToMinimumMaximum)
        renderer.setBlueContrastEnhancement(bluece)

        renderer.setMaximumScreenError(18)
        renderer.setMaximumScreenErrorUnit(QgsUnitTypes.RenderInches)
        renderer.setPointSize(13)
        renderer.setPointSizeUnit(QgsUnitTypes.RenderPoints)
        renderer.setPointSizeMapUnitScale(QgsMapUnitScale(1000, 2000))

        rr = renderer.clone()
        self.assertEqual(rr.maximumScreenError(), 18)
        self.assertEqual(rr.maximumScreenErrorUnit(),
                         QgsUnitTypes.RenderInches)
        self.assertEqual(rr.pointSize(), 13)
        self.assertEqual(rr.pointSizeUnit(), QgsUnitTypes.RenderPoints)
        self.assertEqual(rr.pointSizeMapUnitScale().minScale, 1000)
        self.assertEqual(rr.pointSizeMapUnitScale().maxScale, 2000)

        self.assertEqual(rr.blueAttribute(), 'b')
        self.assertEqual(rr.greenAttribute(), 'g')
        self.assertEqual(rr.redAttribute(), 'r')
        self.assertEqual(rr.redContrastEnhancement().minimumValue(), 100)
        self.assertEqual(rr.redContrastEnhancement().maximumValue(), 120)
        self.assertEqual(
            rr.redContrastEnhancement().contrastEnhancementAlgorithm(),
            QgsContrastEnhancement.StretchAndClipToMinimumMaximum)
        self.assertEqual(rr.greenContrastEnhancement().minimumValue(), 130)
        self.assertEqual(rr.greenContrastEnhancement().maximumValue(), 150)
        self.assertEqual(
            rr.greenContrastEnhancement().contrastEnhancementAlgorithm(),
            QgsContrastEnhancement.StretchToMinimumMaximum)
        self.assertEqual(rr.blueContrastEnhancement().minimumValue(), 170)
        self.assertEqual(rr.blueContrastEnhancement().maximumValue(), 190)
        self.assertEqual(
            rr.blueContrastEnhancement().contrastEnhancementAlgorithm(),
            QgsContrastEnhancement.ClipToMinimumMaximum)

        doc = QDomDocument("testdoc")
        elem = renderer.save(doc, QgsReadWriteContext())

        r2 = QgsPointCloudRgbRenderer.create(elem, QgsReadWriteContext())
        self.assertEqual(r2.maximumScreenError(), 18)
        self.assertEqual(r2.maximumScreenErrorUnit(),
                         QgsUnitTypes.RenderInches)
        self.assertEqual(r2.pointSize(), 13)
        self.assertEqual(r2.pointSizeUnit(), QgsUnitTypes.RenderPoints)
        self.assertEqual(r2.pointSizeMapUnitScale().minScale, 1000)
        self.assertEqual(r2.pointSizeMapUnitScale().maxScale, 2000)

        self.assertEqual(r2.blueAttribute(), 'b')
        self.assertEqual(r2.greenAttribute(), 'g')
        self.assertEqual(r2.redAttribute(), 'r')
        self.assertEqual(r2.redContrastEnhancement().minimumValue(), 100)
        self.assertEqual(r2.redContrastEnhancement().maximumValue(), 120)
        self.assertEqual(
            r2.redContrastEnhancement().contrastEnhancementAlgorithm(),
            QgsContrastEnhancement.StretchAndClipToMinimumMaximum)
        self.assertEqual(r2.greenContrastEnhancement().minimumValue(), 130)
        self.assertEqual(r2.greenContrastEnhancement().maximumValue(), 150)
        self.assertEqual(
            r2.greenContrastEnhancement().contrastEnhancementAlgorithm(),
            QgsContrastEnhancement.StretchToMinimumMaximum)
        self.assertEqual(r2.blueContrastEnhancement().minimumValue(), 170)
        self.assertEqual(r2.blueContrastEnhancement().maximumValue(), 190)
        self.assertEqual(
            r2.blueContrastEnhancement().contrastEnhancementAlgorithm(),
            QgsContrastEnhancement.ClipToMinimumMaximum)
Beispiel #9
0
    def dynamics_multi_band(self, layer_renderer, list_canvas_layer):
        """
        Applies the dynamic of the selected layer to all multi band layers
        :param layer_renderer:
        :param list_canvas_layer:
        :return:
        """
        #       layer_renderer
        #       <qgis.core.QgsMultiBandColorRenderer object at 0x514c9e0>
        layer_ce_red = layer_renderer.redContrastEnhancement()
        layer_ce_green = layer_renderer.greenContrastEnhancement()
        layer_ce_blue = layer_renderer.blueContrastEnhancement()

        if layer_ce_red and layer_ce_green and layer_ce_blue:
            # set stretch to min max
            layer_ce_red.setContrastEnhancementAlgorithm(1)
            layer_ce_green.setContrastEnhancementAlgorithm(1)
            layer_ce_blue.setContrastEnhancementAlgorithm(1)

            logger.debug("red :" +
                         str(layer_ce_red.contrastEnhancementAlgorithm()))
            logger.debug("green:" +
                         str(layer_ce_green.contrastEnhancementAlgorithm()))
            logger.debug("blue :" +
                         str(layer_ce_blue.contrastEnhancementAlgorithm()))

            # get the min and max of RGB bands
            max_current_red = layer_ce_red.maximumValue()
            min_current_red = layer_ce_red.minimumValue()

            logger.debug("min red:" + str(min_current_red) + "max red:" +
                         str(max_current_red))

            max_current_green = layer_ce_green.maximumValue()
            min_current_green = layer_ce_green.minimumValue()

            logger.debug("min green:" + str(min_current_green) + "max green:" +
                         str(max_current_green))

            max_current_blue = layer_ce_blue.maximumValue()
            min_current_blue = layer_ce_blue.minimumValue()

            logger.debug("min blue:" + str(min_current_blue) + "max blue:" +
                         str(max_current_blue))

            # for each layer
            logger.debug("there are" + str(len(list_canvas_layer)) +
                         " layers in the canvas")
            for layer_from_list in list_canvas_layer:
                logger.debug("layer from list name :" +
                             str(layer_from_list.name()))

                # which is raster
                if layer_from_list.type() == QGIS_LAYER_TYPE:
                    logger.debug("layer from list is a raster file")

                    # which is multi
                    if layer_from_list.rasterType() == 2:
                        logger.debug("multiband file")

                        data_provider = layer_from_list.dataProvider()
                        # take the layer renderer to set the min and max
                        renderer_from_list = layer_from_list.renderer()
                        # <qgis.core.QgsMultiBandColorRenderer object at 0x3f8a8d0>
                        logger.debug("renderer from list" +
                                     str(renderer_from_list))

                        logger.debug("green band" +
                                     str(renderer_from_list.greenBand()))
                        logger.debug("blue band" +
                                     str(renderer_from_list.blueBand()))
                        logger.debug("red band" +
                                     str(renderer_from_list.redBand()))

                        logger.info(renderer_from_list)

                        red_enhancement = QgsContrastEnhancement(
                            data_provider.dataType(1))

                        if red_enhancement:
                            red_enhancement.setMinimumValue(min_current_red)
                            red_enhancement.setMaximumValue(max_current_red)
                            red_enhancement.setContrastEnhancementAlgorithm(1)
                            renderer_from_list.setRedContrastEnhancement(
                                red_enhancement)

                        green_enhancement = QgsContrastEnhancement(
                            data_provider.dataType(2))

                        if green_enhancement:
                            green_enhancement.setMinimumValue(
                                min_current_green)
                            green_enhancement.setMaximumValue(
                                max_current_green)
                            green_enhancement.setContrastEnhancementAlgorithm(
                                1)
                            renderer_from_list.setGreenContrastEnhancement(
                                green_enhancement)

                        blue_enhancement = QgsContrastEnhancement(
                            data_provider.dataType(3))

                        if blue_enhancement:
                            blue_enhancement.setMinimumValue(min_current_blue)
                            blue_enhancement.setMaximumValue(max_current_blue)
                            blue_enhancement.setContrastEnhancementAlgorithm(1)
                            renderer_from_list.setBlueContrastEnhancement(
                                blue_enhancement)

                        layer_from_list.triggerRepaint()
    def dynamicsMultiBand(self, layerRenderer, listCanvasLayer):
        """
        Applies the dynamic of the selected layer to all multi band layers
        :param layerRenderer:
        :param listCanvasLayer:
        :return:
        """
        #       layerRenderer
        #       <qgis.core.QgsMultiBandColorRenderer object at 0x514c9e0>
        layerCERed = layerRenderer.redContrastEnhancement()
        layerCEGreen = layerRenderer.greenContrastEnhancement()
        layerCEBlue = layerRenderer.blueContrastEnhancement()

        if layerCERed and layerCEGreen and layerCEBlue:
            #set stretch to min max
            layerCERed.setContrastEnhancementAlgorithm(1)
            layerCEGreen.setContrastEnhancementAlgorithm(1)
            layerCEBlue.setContrastEnhancementAlgorithm(1)

            logger.debug("red :" +
                         str(layerCERed.contrastEnhancementAlgorithm()))
            logger.debug("green:" +
                         str(layerCEGreen.contrastEnhancementAlgorithm()))
            logger.debug("blue :" +
                         str(layerCEBlue.contrastEnhancementAlgorithm()))

            #get the min and max of RGB bands
            maxCurrentRed = layerCERed.maximumValue()
            minCurrentRed = layerCERed.minimumValue()

            logger.debug("min red:" + str(minCurrentRed) + "max red:" +
                         str(maxCurrentRed))

            maxCurrentGreen = layerCEGreen.maximumValue()
            minCurrentGreen = layerCEGreen.minimumValue()

            logger.debug("min green:" + str(minCurrentGreen) + "max green:" +
                         str(maxCurrentGreen))

            maxCurrentBlue = layerCEBlue.maximumValue()
            minCurrentBlue = layerCEBlue.minimumValue()

            logger.debug("min blue:" + str(minCurrentBlue) + "max blue:" +
                         str(maxCurrentBlue))

            # for each layer
            logger.debug("there are" + str(len(listCanvasLayer)) +
                         " layers in the canvas")
            for layerFromList in listCanvasLayer:
                logger.debug("layer from list name :" +
                             str(layerFromList.name()))

                # which is raster
                if layerFromList.type() == 1:
                    logger.debug("layer from list is a raster file")

                    # which is multi
                    if layerFromList.rasterType() == 2:
                        logger.debug("multiband file")

                        dataProvider = layerFromList.dataProvider()
                        #take the layer renderer to set the min and max
                        rendererFromList = layerFromList.renderer()
                        #<qgis.core.QgsMultiBandColorRenderer object at 0x3f8a8d0>
                        logger.debug("renderer from list" +
                                     str(rendererFromList))

                        logger.debug("green band" +
                                     str(rendererFromList.greenBand()))
                        logger.debug("blue band" +
                                     str(rendererFromList.blueBand()))
                        logger.debug("red band" +
                                     str(rendererFromList.redBand()))

                        logger.info(rendererFromList)

                        redEnhancement = None
                        if QGis.QGIS_VERSION_INT == 10800:  # for QGIS = 1.8
                            redEnhancement = QgsContrastEnhancement(
                                QgsContrastEnhancement.QgsRasterDataType(
                                    dataProvider.dataType(0)))
                        elif QGis.QGIS_VERSION_INT >= 10900:  # for QGIS = 1.9
                            redEnhancement = QgsContrastEnhancement(
                                dataProvider.dataType(1))

                        if redEnhancement:
                            redEnhancement.setMinimumValue(minCurrentRed)
                            redEnhancement.setMaximumValue(maxCurrentRed)
                            redEnhancement.setContrastEnhancementAlgorithm(1)
                            rendererFromList.setRedContrastEnhancement(
                                redEnhancement)

                        greenEnhancement = None
                        if QGis.QGIS_VERSION_INT == 10800:  # for QGIS = 1.8
                            greenEnhancement = QgsContrastEnhancement(
                                QgsContrastEnhancement.QgsRasterDataType(
                                    dataProvider.dataType(1)))
                        elif QGis.QGIS_VERSION_INT >= 10900:  # for QGIS = 1.9
                            greenEnhancement = QgsContrastEnhancement(
                                dataProvider.dataType(2))

                        if greenEnhancement:
                            greenEnhancement.setMinimumValue(minCurrentGreen)
                            greenEnhancement.setMaximumValue(maxCurrentGreen)
                            greenEnhancement.setContrastEnhancementAlgorithm(1)
                            rendererFromList.setGreenContrastEnhancement(
                                greenEnhancement)

                        blueEnhancement = None
                        if QGis.QGIS_VERSION_INT == 10800:  # for QGIS = 1.8
                            blueEnhancement = QgsContrastEnhancement(
                                QgsContrastEnhancement.QgsRasterDataType(
                                    dataProvider.dataType(2)))
                        elif QGis.QGIS_VERSION_INT >= 10900:  # for QGIS = 1.9
                            blueEnhancement = QgsContrastEnhancement(
                                dataProvider.dataType(3))

                        if blueEnhancement:
                            blueEnhancement.setMinimumValue(minCurrentBlue)
                            blueEnhancement.setMaximumValue(maxCurrentBlue)
                            blueEnhancement.setContrastEnhancementAlgorithm(1)
                            rendererFromList.setBlueContrastEnhancement(
                                blueEnhancement)

                        layerFromList.setCacheImage(None)
                        layerFromList.triggerRepaint()