def clipping_thematic_raster(self): # first check input files requirements if not valid_file_selected_in(self.QCBox_ThematicRaster, "thematic raster"): return if not valid_file_selected_in(self.QCBox_AreaOfInterest, "area of interest shape"): return # first select the target dir for save the clipping file filename, ext = os.path.splitext(get_current_file_path_in(self.QCBox_ThematicRaster)) ext = ext if ext in [".tif", ".TIF", ".img", ".IMG"] else ".tif" suggested_filename = filename + "_clip" + ext file_out, _ = QFileDialog.getSaveFileName(self, self.tr("Select the output file to save the clipping file"), suggested_filename, self.tr("GeoTiff files (*.tif);;Img files (*.img);;All files (*.*)")) if file_out == '': return # clipping clip_file = do_clipping_with_shape( self.QCBox_ThematicRaster.currentLayer(), self.QCBox_AreaOfInterest.currentLayer(), file_out, get_nodata_value(self.QCBox_ThematicRaster.currentLayer())) # copy the style thematic_basename = os.path.splitext(get_current_file_path_in(self.QCBox_ThematicRaster))[0] if os.path.isfile(thematic_basename + ".qml"): copyfile(thematic_basename + ".qml", os.path.splitext(file_out)[0] + ".qml") # unload old thematic file unload_layer(get_current_file_path_in(self.QCBox_ThematicRaster)) # load to qgis and update combobox list load_and_select_filepath_in(self.QCBox_ThematicRaster, clip_file) self.select_thematic_raster(self.QCBox_ThematicRaster.currentLayer()) iface.messageBar().pushMessage("AcATaMa", "Clipping the thematic raster with shape, completed", level=Qgis.Success)
def save_config(self, file_out): import yaml from AcATaMa.gui.acatama_dockwidget import AcATaMaDockWidget as AcATaMa def setup_yaml(): """ Keep dump ordered with orderedDict """ represent_dict_order = lambda self, data: self.represent_mapping('tag:yaml.org,2002:map', list(data.items())) yaml.add_representer(OrderedDict, represent_dict_order) setup_yaml() data = OrderedDict() data["thematic_raster"] = \ {"path": get_current_file_path_in(AcATaMa.dockwidget.QCBox_ThematicRaster, show_message=False), "band": int(AcATaMa.dockwidget.QCBox_band_ThematicRaster.currentText()) if AcATaMa.dockwidget.QCBox_band_ThematicRaster.currentText() else None, "nodata": AcATaMa.dockwidget.nodata_ThematicRaster.value()} data["sampling_layer"] = get_file_path_of_layer(self.sampling_layer) data["dialog_size"] = self.dialog_size data["grid_view_widgets"] = {"columns": self.grid_columns, "rows": self.grid_rows} data["current_sample_idx"] = self.current_sample_idx data["fit_to_sample"] = self.fit_to_sample data["is_completed"] = self.is_completed data["view_widgets_config"] = self.view_widgets_config data["classification_buttons"] = self.buttons_config # save samples status points_config = {} for pnt_idx, point in enumerate(self.points): if point.is_classified: points_config[pnt_idx] = {"classif_id": point.classif_id, "shape_id": point.shape_id} data["points"] = points_config # save the samples order data["points_order"] = [p.shape_id for p in self.points] # save sampling selected in accuracy assessment data["accuracy_assessment_sampling_file"] = get_current_file_path_in(AcATaMa.dockwidget.QCBox_SamplingFile_AA, show_message=False) # save config of the accuracy assessment dialog if exists if self.accuracy_assessment: data["accuracy_assessment_dialog"] = { "area_unit": QgsUnitTypes.toString(self.accuracy_assessment.area_unit), "z_score": self.accuracy_assessment.z_score, "csv_separator": self.accuracy_assessment.csv_separator, "csv_decimal": self.accuracy_assessment.csv_decimal, } with open(file_out, 'w') as yaml_file: yaml.dump(data, yaml_file)
def closing(self): """ Do this before close the classification dialog """ from AcATaMa.gui.acatama_dockwidget import AcATaMaDockWidget as AcATaMa # save some config of classification dialog for this sampling file self.classification.fit_to_sample = self.radiusFitToSample.value() # save view widgets status view_widgets_config = {} for view_widget in ClassificationDialog.view_widgets: # {N: {"view_name", "layer_name", "render_file_path", "scale_factor"}, ...} view_widgets_config[view_widget.id] = \ {"view_name": view_widget.QLabel_ViewName.text(), "layer_name": view_widget.QCBox_RenderFile.currentLayer().name() if view_widget.QCBox_RenderFile.currentLayer() else None, "render_file_path": get_current_file_path_in(view_widget.QCBox_RenderFile, show_message=False), # "view_size": (view_widget.size().width(), view_widget.size().height()), "scale_factor": view_widget.current_scale_factor} self.classification.view_widgets_config = view_widgets_config self.classification.dialog_size = (self.size().width(), self.size().height()) ClassificationDialog.is_opened = False # restore the states for some objects in the dockwidget AcATaMa.dockwidget.QGBox_SamplingFile.setEnabled(True) AcATaMa.dockwidget.QGBox_GridSettings.setEnabled(True) AcATaMa.dockwidget.QGBox_ClassificationStatus.setEnabled(True) AcATaMa.dockwidget.QGBox_saveSamplingClassified.setEnabled(True) AcATaMa.dockwidget.QPBtn_OpenClassificationDialog.setText( "Open the classification dialog") self.reject(is_ok_to_close=True)
def __init__(self, file_selected_combo_box, band=1, nodata=None): from AcATaMa.utils.qgis_utils import get_current_file_path_in self.file_path = get_current_file_path_in(file_selected_combo_box) self.qgs_layer = file_selected_combo_box.currentLayer() self.band = band self.nodata = nodata if nodata != -1 else None self.pixel_counts_by_value = None