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))
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)
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)
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))
def on_release(self, event): """ Get asbscissas of new x_min or x_max """ if self.do_change and event.xdata: logger.debug('release') # classic case last_min = self.x_min last_max = self.x_max if self.change_min: self.x_min = event.xdata else: self.x_max = event.xdata # if the user drag the max line under the min line, switch the values if self.x_min > self.x_max: temp = self.x_min self.x_min = self.x_max self.x_max = temp if self.x_min < self.rasterMin: self.x_min = self.rasterMin if self.x_max > self.rasterMax: self.x_max = self.rasterMax logger.info("on release: x_min {}".format(self.x_min)) logger.info("on release: x_max {}".format(self.x_max)) self.axes.clear() self.draw_histogram() # self.axes.plot(self.t, self.s, self.color) # self.axes.plot(self.x1, self.x1, 'g^') self.draw_min_max_percent() # self.axes.vlines(self.x1, [0], self.x1) # self.axes.set_xlabel('time (s)') # self.axes.set_title('Vertical lines demo') self.axes.figure.canvas.draw() self.emit(QtCore.SIGNAL("valueChanged()")) logger.debug("{} {}".format(self.x_min, self.x_max)) if self.multiband: dic = {"r": "red", "g": "green", "b": "blue"} p = TerreImageParamaters() 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))
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
def onProjectLoaded(self): # does not work stops the project reading. # should desactivate all interface and read the project again # self.newProject( True ) # restore mirrors? wl, ok = QgsProject.instance().readEntry("QGISEducation", "/working_layer") if not ok or wl == "": pass if not wl == "": bands, ok = QgsProject.instance().readEntry( "QGISEducation", "/working_layer_bands") logger.debug("is ok" + str(ok)) logger.debug(bands) # working_layer = WorkingLayer(wl, None, bands) # TODO interpreter bands type, ok = QgsProject.instance().readEntry("QGISEducation", "/working_layer_type") working_dir, ok = QgsProject.instance().readEntry( "QGISEducation", "/working_directory") layer, bands = terre_image_utils.restore_working_layer( wl, eval(bands), type) ProcessingManager().working_layer = layer self.show_education_widget(bands, working_dir) # self.qgis_education_manager = TerreImageManager( self.iface ) # self.qgis_education_manager.restore_processing_manager(wl, eval(bands), type, working_dir) # if self.qgis_education_manager: process, ok = QgsProject.instance().readEntry( "QGISEducation", "/process") logger.debug(eval(process)) index_group, ok = QgsProject.instance().readDoubleEntry( "QGISEducation", "/index_group") self.constants.index_group = int(float(index_group)) process = eval(process) for qgis_layer in self.iface.legendInterface().layers(): # print "layer loading ", qgis_layer.name() if qgis_layer.name() in [ "NDVI", "NDTI", "Indice de brillance", "KMEANS" ]: process = TerreImageProcessing( self.iface, working_dir, ProcessingManager().working_layer, self. educationWidget.qgis_education_manager.mirror_map_tool, qgis_layer.name(), None, qgis_layer) elif "Angle Spectral" in qgis_layer.name(): process = TerreImageProcessing( self.iface, working_dir, ProcessingManager().working_layer, self. educationWidget.qgis_education_manager.mirror_map_tool, qgis_layer.name(), qgis_layer.source(), qgis_layer) # ProcessingManager().add_processing(process) elif "couleur_naturelles" in qgis_layer.name(): try: self.do_display_one_band( 'nat', qgis_layer, working_dir, self.educationWidget.qgis_education_manager. mirror_map_tool) except AttributeError: QMessageBox.warning( None, "Erreur", u'Le projet ne peut être lu. Essayez de créer un projet vierge, puis de réouvrir le projet souhaité.', QMessageBox.Ok) # self.newProject( ) return None # ProcessingManager().add_display( process ) else: corres = { 'red': "_bande_rouge", 'green': "_bande_verte", 'blue': "_bande_bleue", 'pir': "_bande_pir", 'mir': "_bande_mir", "nat": "_couleurs_naturelles" } result = [ x for x in corres if qgis_layer.name().endswith(corres[x]) ] # print result if result: # print "the couleur", result[0] try: self.do_display_one_band( result[0], qgis_layer, working_dir, self.educationWidget.qgis_education_manager. mirror_map_tool) except AttributeError: QMessageBox.warning( None, "Erreur", u'Le projet ne peut être lu. Essayez de créer un projet vierge, puis de réouvrir le projet souhaité.', QMessageBox.Ok) # self.newProject( ) return None # ProcessingManager().add_display( process ) angle_spectral_point_x, ok_x = QgsProject.instance( ).readDoubleEntry("QGISEducation", "/angle_spectral_point_x") angle_spectral_point_y, ok_y = QgsProject.instance( ).readDoubleEntry("QGISEducation", "/angle_spectral_point_y") # print "angle_spectral_point_x, angle_spectral_point_y", angle_spectral_point_x, angle_spectral_point_y if ok_x and ok_y: # print "angle_spectral_point_x, angle_spectral_point_y", angle_spectral_point_x, angle_spectral_point_y p = ProcessingManager().processing_from_name("Angle Spectral") if p: rubberband = p[0].rubberband rubberband.setWidth(10) rubberband.setColor(QColor(Qt.yellow)) rubberband.addPoint( QgsPoint(float(angle_spectral_point_x), float(angle_spectral_point_y))) p = TerreImageParamaters() p.red_min = QgsProject.instance().readDoubleEntry( "QGISEducation", "/red_x_min")[0] p.red_max = QgsProject.instance().readDoubleEntry( "QGISEducation", "/red_x_max")[0] p.green_min = QgsProject.instance().readDoubleEntry( "QGISEducation", "/green_x_min")[0] p.green_max = QgsProject.instance().readDoubleEntry( "QGISEducation", "/green_x_max")[0] p.blue_min = QgsProject.instance().readDoubleEntry( "QGISEducation", "/blue_x_min")[0] p.blue_max = QgsProject.instance().readDoubleEntry( "QGISEducation", "/blue_x_max")[0] self.educationWidget.set_combobox_histograms() self.iface.mapCanvas().refresh() self.iface.mapCanvas().repaint()
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