Ejemplo n.º 1
0
    def get_2_98_percent(self, sizeX, sizeY, histogram):
        # get 2 - 98 %
        logger.debug("sizeX: {}, sizeY: {}".format(sizeX, sizeY))
        nb_pixels = sizeX * sizeY
        logger.debug("nb_pixels: {}".format(nb_pixels))

        nb_pixels_2 = int(float(nb_pixels * 0.02))
        nb_pixels_98 = int(float(nb_pixels * 0.98))

        logger.debug("nb_pixels_2: {}, nb_pixels_98: {}".format(
            nb_pixels_2, nb_pixels_98))

        hist_cum = cumsum(histogram)
        # logger.debug("histogram {}".format(histogram))
        # logger.debug("hist cum {}".format(hist_cum))
        # logger.debug("len(hist_cum) {}".format(len(hist_cum)))

        self.x_min = 0
        self.x_max = len(hist_cum)
        hist_cum[-1] + self.rasterMin
        for index in range(0, len(hist_cum) - 1):
            if hist_cum[index + 1] > nb_pixels_2 and self.x_min == 0:
                self.x_min = index + self.rasterMin
            if hist_cum[index + 1] > nb_pixels_98:
                self.x_max = index + self.rasterMin
                break

        # logger.debug("self.x_min {}, self.x_max {}".format(self.x_min, self.x_max))

        logger.debug("self.x_min: {}, self.x_max: {}".format(
            self.x_min, self.x_max))
        self.x_min = (self.x_min -
                      self.rasterMin) * self.bin_witdh + self.rasterMin
        self.x_max = (self.x_max -
                      self.rasterMin) * self.bin_witdh + self.rasterMin
        self.two_min = self.x_min
        self.ninety_eight_max = self.x_max

        # logger.debug("self.x_min {}, self.x_max {}".format(self.x_min, self.x_max))

        if self.multiband:
            dic = {"r": "red", "g": "green", "b": "blue"}
            p = TerreImageParamaters()
            if not p.is_complete():
                exec("p." + dic[self.color] + "_min=" + str(self.x_min))
                # logger.debug("p.{} _min= {}".format(dic[self.color], str(self.x_min)))
                exec("p." + dic[self.color] + "_max=" + str(self.x_max))
Ejemplo n.º 2
0
    def onWriteProject(self, domproject):
        if ProcessingManager().working_layer is None:
            return

#         QgsProject.instance().writeEntry( "QGISEducation", "/working_layer", self.qgis_education_manager.layer.source_file )
#         # write band orders
#         QgsProject.instance().writeEntry( "QGISEducation", "/working_layer_bands", str(self.qgis_education_manager.layer.bands) )
#         logger.debug( str(self.qgis_education_manager.layer.bands) )
#         QgsProject.instance().writeEntry( "QGISEducation", "/working_layer_type", self.qgis_education_manager.layer.type )
#         QgsProject.instance().writeEntry( "QGISEducation", "/working_directory", self.qgis_education_manager.working_directory )
        QgsProject.instance().writeEntry("QGISEducation", "/working_layer", ProcessingManager().working_layer.source_file)
        # write band orders
        QgsProject.instance().writeEntry("QGISEducation", "/working_layer_bands", str(ProcessingManager().working_layer.bands))
        logger.debug(str(ProcessingManager().working_layer.bands))
        QgsProject.instance().writeEntry("QGISEducation", "/working_layer_type", ProcessingManager().working_layer.type)
        QgsProject.instance().writeEntry("QGISEducation", "/working_directory", self.educationWidget.qgis_education_manager.working_directory)
        p = []
        for process in ProcessingManager().get_processings():
            p.append((process.processing_name, process.output_working_layer.get_source()))
        # print "process", p

        QgsProject.instance().writeEntry("QGISEducation", "/process", str(p))
        QgsProject.instance().writeEntry("QGISEducation", "/index_group", self.constants.index_group)

        if "Angle Spectral" in ProcessingManager().get_processings_name():
            # delete rubberband
            for item in self.iface.mapCanvas().scene().items():
                # item is a rubberband
                if isinstance(item, QgsRubberBand):
                    # get point
                    if item.size() > 0:
                        point = item.getPoint(0)
                        # print point
                        QgsProject.instance().writeEntryDouble("QGISEducation", "/angle_spectral_point_x", point.x())
                        QgsProject.instance().writeEntryDouble("QGISEducation", "/angle_spectral_point_y", point.y())

        p = TerreImageParamaters()
        if p.is_complete():
            QgsProject.instance().writeEntryDouble("QGISEducation", "/red_x_min", p.red_min)
            QgsProject.instance().writeEntryDouble("QGISEducation", "/red_x_max", p.red_max)
            QgsProject.instance().writeEntryDouble("QGISEducation", "/green_x_min", p.green_min)
            QgsProject.instance().writeEntryDouble("QGISEducation", "/green_x_max", p.green_max)
            QgsProject.instance().writeEntryDouble("QGISEducation", "/blue_x_min", p.blue_min)
            QgsProject.instance().writeEntryDouble("QGISEducation", "/blue_x_max", p.blue_max)
Ejemplo n.º 3
0
    def onWriteProject(self, domproject):
        if ProcessingManager().working_layer is None:
            return

#         QgsProject.instance().writeEntry( "QGISEducation", "/working_layer", self.qgis_education_manager.layer.source_file )
#         # write band orders
#         QgsProject.instance().writeEntry( "QGISEducation", "/working_layer_bands", str(self.qgis_education_manager.layer.bands) )
#         logger.debug( str(self.qgis_education_manager.layer.bands) )
#         QgsProject.instance().writeEntry( "QGISEducation", "/working_layer_type", self.qgis_education_manager.layer.type )
#         QgsProject.instance().writeEntry( "QGISEducation", "/working_directory", self.qgis_education_manager.working_directory )
        QgsProject.instance().writeEntry("QGISEducation", "/working_layer", ProcessingManager().working_layer.source_file)
        # write band orders
        QgsProject.instance().writeEntry("QGISEducation", "/working_layer_bands", str(ProcessingManager().working_layer.bands))
        logger.debug("{}".format(ProcessingManager().working_layer.bands))
        QgsProject.instance().writeEntry("QGISEducation", "/working_layer_type", ProcessingManager().working_layer.type)
        QgsProject.instance().writeEntry("QGISEducation", "/working_directory", self.educationWidget.qgis_education_manager.working_directory)
        p = []
        for process in ProcessingManager().get_processings():
            p.append((process.processing_name, process.output_working_layer.get_source()))
        # logger.debug("process{}".format(p))

        QgsProject.instance().writeEntry("QGISEducation", "/process", str(p))
        QgsProject.instance().writeEntry("QGISEducation", "/index_group", self.constants.index_group)

        if "Angle Spectral" in ProcessingManager().get_processings_name():
            # delete rubberband
            for item in self.iface.mapCanvas().scene().items():
                # item is a rubberband
                if isinstance(item, QgsRubberBand):
                    # get point
                    if item.size() > 0:
                        point = item.getPoint(0)
                        # logger.debug(point)
                        QgsProject.instance().writeEntryDouble("QGISEducation", "/angle_spectral_point_x", point.x())
                        QgsProject.instance().writeEntryDouble("QGISEducation", "/angle_spectral_point_y", point.y())

        p = TerreImageParamaters()
        if p.is_complete():
            QgsProject.instance().writeEntryDouble("QGISEducation", "/red_x_min", p.red_min)
            QgsProject.instance().writeEntryDouble("QGISEducation", "/red_x_max", p.red_max)
            QgsProject.instance().writeEntryDouble("QGISEducation", "/green_x_min", p.green_min)
            QgsProject.instance().writeEntryDouble("QGISEducation", "/green_x_max", p.green_max)
            QgsProject.instance().writeEntryDouble("QGISEducation", "/blue_x_min", p.blue_min)
            QgsProject.instance().writeEntryDouble("QGISEducation", "/blue_x_max", p.blue_max)
Ejemplo n.º 4
0
    def get_2_98_percent(self, sizeX, sizeY, histogram):
        # get 2 - 98 %
        logger.debug("sizeX, sizeY" + str(sizeX) + " " + str(sizeY))
        nb_pixels = sizeX * sizeY
        logger.debug("nb_pixels: " + str(nb_pixels))

        nb_pixels_2 = int(float(nb_pixels * 0.02))
        nb_pixels_98 = int(float(nb_pixels * 0.98))

        logger.debug("nb_pixels_2, nb_pixels_98: " + str(nb_pixels_2) + " " + str(nb_pixels_98))

        hist_cum = cumsum(histogram)
        # print "histogram", histogram
        # print "hist cum", hist_cum
        # print "len(hist_cum)", len(hist_cum)

        self.x_min = 0
        self.x_max = len(hist_cum)
        hist_cum[-1] + self.rasterMin
        for index in range(0, len(hist_cum) - 1):
            if hist_cum[index + 1] > nb_pixels_2 and self.x_min == 0:
                self.x_min = index + self.rasterMin
            if hist_cum[index + 1] > nb_pixels_98:
                self.x_max = index + self.rasterMin
                break

        # print "self.x_min, self.x_max", self.x_min, self.x_max

        logger.debug("self.x_min, self.x_max" + str(self.x_min) + " " + str(self.x_max))
        self.x_min = (self.x_min - self.rasterMin) * self.bin_witdh + self.rasterMin
        self.x_max = (self.x_max - self.rasterMin) * self.bin_witdh + self.rasterMin
        self.two_min = self.x_min
        self.ninety_eight_max = self.x_max

        # print "self.x_min, self.x_max", self.x_min, self.x_max

        if self.multiband:
            dic = {"r": "red", "g": "green", "b": "blue"}
            p = TerreImageParamaters()
            if not p.is_complete():
                exec("p." + dic[self.color] + "_min=" + str(self.x_min))
                # print "p." + dic[self.color] + "_min=" + str(self.x_min)
                exec("p." + dic[self.color] + "_max=" + str(self.x_max))
Ejemplo n.º 5
0
    def get_GDAL_histogram(self, image, band_number, qgis_layer, no_data=-1):
        """
        From the given binary image, compute histogram and return the number of 1
        """
        histogram = []

        # logger.debug( "image: " + str(image) + " band: " + str(band_number) )
        dataset = gdal.Open(image, gdal.GA_ReadOnly)
        if dataset is None:
            logger.error(u"Error: Opening file {}".format(image))
        else:
            # get raster band
            band = dataset.GetRasterBand(band_number)
            if no_data != -1:
                band.SetNoDataValue(no_data)

            # get raster and overview if available
            overview = 2
            if overview < band.GetOverviewCount():
                band_overview = band.GetOverview(overview)
            else:
                band_overview = band

            # get overview statistics
            self.rasterMin, self.rasterMax, mean, stddev = band_overview.ComputeStatistics(
                False)
            # logger.debug(self.rasterMin, self.rasterMax, mean, stddev
            logger.debug("self.rasterMax: {}, self.rasterMin: {}".format(
                self.rasterMax, self.rasterMin))
            nbVal = self.rasterMax - self.rasterMin
            # logger.debug("nbVal {}".format(nbVal))

            # taking the size of the raster
            sizeX = float(band_overview.XSize)
            sizeY = float(band_overview.YSize)
            # logger.debug( "totalXSize {}".format(sizeX))
            # logger.debug( "totalYSize {}".format(sizeY))

            # computing nb bins
            stddev_part = 3.5 * stddev
            sizexy_part = math.pow(sizeX * sizeY, 1. / 3.)
            # logger.debug( "stddev {}".format(stddev_part))
            # logger.debug( "sizexy {}".format(sizexy_part))

            # warning stddev
            nb_bin_part = (3.5 * stddev) / (math.pow(sizeX * sizeY, 1. / 3.))
            # logger.debug(nb_bin_part)
            self.nb_bin = int(math.ceil(nbVal / nb_bin_part))
            # logger.debug("nb_bin {}".format(self.nb_bin))

            self.bin_witdh = float(self.rasterMax -
                                   self.rasterMin) / self.nb_bin
            # logger.debug("bin_width {}".format(self.bin_witdh))

            histogram = band_overview.GetHistogram(
                self.rasterMin - self.bin_witdh / 2,
                self.rasterMax + self.bin_witdh / 2,
                self.nb_bin,
                approx_ok=0)

            # logger.debug("histogram {}".format(histogram))

            self.get_2_98_percent(sizeX, sizeY, histogram)

            if self.multiband:
                p = TerreImageParamaters()
                if p.is_complete():
                    # logger.debug("here")
                    dic = {"r": "red", "g": "green", "b": "blue"}
                    exec("self.x_min=int(p." + dic[self.color] + "_min)")
                    exec("self.x_max=int(p." + dic[self.color] + "_max)")
                    # logger.debug(self.x_min, self.x_max)

            return histogram
Ejemplo n.º 6
0
    def get_GDAL_histogram(self, image, band_number, qgis_layer, no_data=-1):
        """
        From the given binary image, compute histogram and return the number of 1
        """
        histogram = []

        # logger.debug( "image: " + str(image) + " band: " + str(band_number) )
        dataset = gdal.Open(image, gdal.GA_ReadOnly)
        if dataset is None:
            print "Error: Opening file ", image
        else:
            # get raster band
            band = dataset.GetRasterBand(band_number)
            if no_data != -1:
                band.SetNoDataValue(no_data)

            # get raster and overview if available
            overview = 2
            if overview < band.GetOverviewCount():
                band_overview = band.GetOverview(overview)
            else:
                band_overview = band

            # get overview statistics
            self.rasterMin, self.rasterMax, mean, stddev = band_overview.ComputeStatistics(False)
            # print self.rasterMin, self.rasterMax, mean, stddev
            logger.debug("self.rasterMax, self.rasterMin" + str(self.rasterMax) + " " + str(self.rasterMin))
            nbVal = self.rasterMax - self.rasterMin
            # print "nbVal", nbVal

            # taking the size of the raster
            sizeX = float(band_overview.XSize)
            sizeY = float(band_overview.YSize)
            # print "totalXSize", sizeX
            # print "totalYSize", sizeY

            # computing nb bins
            stddev_part = 3.5 * stddev
            sizexy_part = math.pow(sizeX * sizeY, 1. / 3.)
            # print "stddev", stddev_part
            # print "sizexy", sizexy_part

            # warning stddev
            nb_bin_part = (3.5 * stddev) / (math.pow(sizeX * sizeY, 1. / 3.))
            # print nb_bin_part
            self.nb_bin = int(math.ceil(nbVal / nb_bin_part))
            # print "nb_bin", self.nb_bin

            self.bin_witdh = float(self.rasterMax - self.rasterMin) / self.nb_bin
            # print "bin_witdh", self.bin_witdh

            histogram = band_overview.GetHistogram(self.rasterMin - self.bin_witdh / 2, self.rasterMax + self.bin_witdh / 2, self.nb_bin, approx_ok=0)

            # print "histogram", histogram

            self.get_2_98_percent(sizeX, sizeY, histogram)

            if self.multiband:
                p = TerreImageParamaters()
                if p.is_complete():
                    # print "here"
                    dic = {"r": "red", "g": "green", "b": "blue"}
                    exec("self.x_min=int(p." + dic[self.color] + "_min)")
                    exec("self.x_max=int(p." + dic[self.color] + "_max)")
                    # print self.x_min, self.x_max

            return histogram