Example #1
0
    def Color(self, file_in, calcType=None):
        """
        Color the Inbound file (Essentially the File we JUST exported) and display it to screen)

        :param file_in: The file that was just exported
        :type file_in: FileImport

        :return: TO SCREEN Rendered Image
        :rtype: None
        """
        k = self.iface.addRasterLayer(file_in.filePath, file_in.baseName)
        stats = k.dataProvider().bandStatistics(1, QgsRasterBandStats.All,
                                                k.extent(), 0)
        minimum = stats.minimumValue
        maximum = stats.maximumValue

        self.com.log("Color func: [Min val: {0} | Max val: {1}".format(
            str(minimum), str(maximum)),
                     level=0)

        ramp_shader = QgsColorRampShader()
        ramp_shader.setColorRampType(QgsColorRampShader.INTERPOLATED)

        if calcType is None:
            color_list = [
                QgsColorRampShader.ColorRampItem(minimum, QColor(255, 0, 0)),
                QgsColorRampShader.ColorRampItem(0, QColor(255, 207, 74, 255)),
                QgsColorRampShader.ColorRampItem(maximum, QColor(0, 255, 0))
            ]

        elif calcType == "EVI":
            color_list = [
                QgsColorRampShader.ColorRampItem(-2, QColor(255, 0, 0)),
                QgsColorRampShader.ColorRampItem(0, QColor(255, 207, 74, 255)),
                QgsColorRampShader.ColorRampItem(2, QColor(0, 255, 0))
            ]

        else:
            color_list = [
                QgsColorRampShader.ColorRampItem(minimum, QColor(255, 0, 0)),
                QgsColorRampShader.ColorRampItem(0, QColor(255, 207, 74, 255)),
                QgsColorRampShader.ColorRampItem(maximum, QColor(0, 255, 0))
            ]

        ramp_shader.setColorRampItemList(color_list)

        shader = QgsRasterShader()
        shader.setRasterShaderFunction(ramp_shader)

        renderer = QgsSingleBandPseudoColorRenderer(k.dataProvider(), 1,
                                                    shader)
        k.setRenderer(renderer)
        """
        Export colored image to file
        """
        export_path = file_in.filePath + ".colored.tif"
        file_writer = QgsRasterFileWriter(export_path)
        pipe = QgsRasterPipe()
        provide = k.dataProvider()

        # Pipe Setter
        if not pipe.set(provide.clone()):
            self.com.error(Bold="PipeProviderError:",
                           String="Cannot set pipe provider",
                           level=1,
                           duration=3)
            self.com.log(
                "mainPlug - Color: Pipe provider error on line 473, Continuing...",
                level=1)

        self.com.log(str(pipe.renderer()), level=0)
        pipe.set(renderer.clone())
        file_writer.writeRaster(pipe, provide.xSize(), provide.ySize(),
                                provide.extent(), provide.crs())