diff --git a/src/Model/GeoTIFF/GeoTIFF.py b/src/Model/GeoTIFF/GeoTIFF.py index 221b2acc..d7b6e1d7 100644 --- a/src/Model/GeoTIFF/GeoTIFF.py +++ b/src/Model/GeoTIFF/GeoTIFF.py @@ -42,6 +42,8 @@ except Exception as e: print(f"Module 'rasterio' is not available: {e}") _rasterio_loaded = False +logger = logging.getLogger() + class GeoTIFF(SQLSubModel): _sub_classes = [] @@ -57,10 +59,11 @@ class GeoTIFF(SQLSubModel): self._enabled = enabled self._name = f"GeoTIFF #{self._pamhyr_id}" if name == "" else name - self._description = text + self._description = description self._file_bytes = b'' self._coordinates = coordinates + self._file_name = "" if path != "": self.read_file(path) @@ -101,7 +104,7 @@ class GeoTIFF(SQLSubModel): elif key == "description": self.description = value elif key == "file_name": - self.file_name = value + self.read_file(value) elif key == "coordinates": self.coordinates = value elif key == "coordinates_bottom": @@ -206,17 +209,25 @@ class GeoTIFF(SQLSubModel): return self._memfile def read_file(self, path): + logger.debug(f"Read GeoTIFF file at : '{path}'") + self._file_name = path self._file_bytes = b'' self._memfile = None + nbytes = 0 + with open(path, "rb") as f: while True: data = f.read(4096) if not data: break + + nbytes += len(data) self._file_bytes += data + logger.debug(f"Read GeoTIFF: {nbytes} bytes readed") + def write_file(self, path): with open(path, "w+b") as f: f.write(self._file_bytes) diff --git a/src/Model/River.py b/src/Model/River.py index b9418026..7520f838 100644 --- a/src/Model/River.py +++ b/src/Model/River.py @@ -507,7 +507,7 @@ class River(Graph): self._D90AdisTS = D90AdisTSList(status=self._status) self._DIFAdisTS = DIFAdisTSList(status=self._status) - self._geo_tiff = GeoTIFFList(status=self._status) + self._geotiff = GeoTIFFList(status=self._status) self._results = {} @@ -621,7 +621,7 @@ class River(Graph): new._DIFAdisTS = DIFAdisTSList._db_load(execute, data) - new._geo_tiff = GeoTIFFList._db_load(execute, data) + new._geotiff = GeoTIFFList._db_load(execute, data) return new @@ -732,7 +732,7 @@ class River(Graph): self._BoundaryConditionsAdisTS, self._LateralContributionsAdisTS, self._D90AdisTS, self._DIFAdisTS, - self._geo_tiff, + self._geotiff, ] for solver in self._parameters: @@ -825,6 +825,10 @@ Last export at: @date.""" def additional_files(self): return self._additional_files + @property + def geotiff(self): + return self._geotiff + @property def rep_lines(self): return self._rep_lines diff --git a/src/View/GeoTIFF/Edit/Window.py b/src/View/GeoTIFF/Edit/Window.py index 08532128..c0d08ef7 100644 --- a/src/View/GeoTIFF/Edit/Window.py +++ b/src/View/GeoTIFF/Edit/Window.py @@ -23,7 +23,15 @@ from View.Tools.PamhyrWindow import PamhyrWindow from PyQt5.QtWidgets import ( QLabel, QPlainTextEdit, QPushButton, - QCheckBox, + QCheckBox, QFileDialog, +) + +from PyQt5.QtGui import ( + QPixmap, +) + +from PyQt5.QtCore import ( + QSettings ) from View.GeoTIFF.Translate import GeoTIFFTranslate @@ -35,22 +43,24 @@ logger = logging.getLogger() class EditGeoTIFFWindow(PamhyrWindow): - _pamhyr_ui = "EditGeoTIFF" + _pamhyr_ui = "GeoTIFF" _pamhyr_name = "Edit GeoTIFF" - def __init__(self, study=None, config=None, add_file=None, + def __init__(self, study=None, config=None, geotiff=None, trad=None, undo=None, parent=None): - name = trad[self._pamhyr_name] + " - " + study.name super(EditGeoTIFFWindow, self).__init__( - title=name, + title=self._pamhyr_name, study=study, config=config, + trad=trad, options=[], parent=parent ) self._geotiff = geotiff + self._file_name = geotiff.file_name + self._hash_data.append(self._geotiff) self._undo = undo @@ -61,12 +71,17 @@ class EditGeoTIFFWindow(PamhyrWindow): def setup_values(self): self.set_check_box("checkBox", self._geotiff.enabled) self.set_line_edit_text("lineEdit_name", self._geotiff.name) - self.set_line_edit_text("lineEdit_description", self._geotiff.description) + self.set_line_edit_text("lineEdit_description", + self._geotiff.description) - self.set_double_spin_box("doubleSpinBox_bottom", self._geotiff.coord_bottom) - self.set_double_spin_box("doubleSpinBox_top", self._geotiff.coord_top) - self.set_double_spin_box("doubleSpinBox_left", self._geotiff.coord_left) - self.set_double_spin_box("doubleSpinBox_right", self._geotiff.coord_right) + self._values = { + "bottom": self._geotiff.coord_bottom, + "top": self._geotiff.coord_top, + "left": self._geotiff.coord_left, + "right": self._geotiff.coord_right, + } + + self._reset_values() if self._study.is_read_only(): self.set_check_box_enable("checkBox", False) @@ -74,11 +89,50 @@ class EditGeoTIFFWindow(PamhyrWindow): self.set_line_edit_enable("lineEdit_path", False) self.set_plaintext_edit_enable("plainTextEdit", False) + def _reset_values(self): + for key in self._values: + self._reset_values_key(key) + + def _reset_values_key(self, key): + self.set_double_spin_box(f"doubleSpinBox_{key}", self._values[key]) + def setup_connection(self): self.find(QPushButton, "pushButton_cancel")\ .clicked.connect(self.close) self.find(QPushButton, "pushButton_ok")\ .clicked.connect(self.accept) + self.find(QPushButton, "pushButton_import")\ + .clicked.connect(self._import) + + for key in self._values: + self.find(QPushButton, f"pushButton_{key}")\ + .clicked.connect(lambda: self._reset_values_key(key)) + + def _import(self): + options = QFileDialog.Options() + settings = QSettings(QSettings.IniFormat, + QSettings.UserScope, 'MyOrg', ) + options |= QFileDialog.DontUseNativeDialog + + file_types = [ + self._trad["file_geotiff"], + self._trad["file_all"], + ] + + filename, _ = QFileDialog.getOpenFileName( + self, + self._trad["open_file"], + "", + ";; ".join(file_types), + options=options + ) + + if filename != "": + self._file_name = filename + + pixmap = QPixmap(filename) + self.find(QLabel, "label_geotiff")\ + .setPixmap(pixmap) def accept(self): if self._study.is_editable(): @@ -99,6 +153,7 @@ class EditGeoTIFFWindow(PamhyrWindow): coordinates_top=coord_top, coordinates_left=coord_left, coordinates_right=coord_right, + file_name=self._file_name, ) ) diff --git a/src/View/GeoTIFF/List.py b/src/View/GeoTIFF/List.py index 8bb158aa..d604d72e 100644 --- a/src/View/GeoTIFF/List.py +++ b/src/View/GeoTIFF/List.py @@ -68,7 +68,7 @@ class ListModel(PamhyrListModel): return QBrush(color) if role == Qt.ItemDataRole.DisplayRole: - text = f"{file.name}: '{file.path}'" + text = f"{file.name}: '{file.description}'" if not file.is_enabled(): text += " (disabled)" diff --git a/src/View/GeoTIFF/Translate.py b/src/View/GeoTIFF/Translate.py index fe39460f..3e328230 100644 --- a/src/View/GeoTIFF/Translate.py +++ b/src/View/GeoTIFF/Translate.py @@ -24,7 +24,7 @@ _translate = QCoreApplication.translate class GeoTIFFTranslate(MainTranslate): def __init__(self): - super(AddFileTranslate, self).__init__() + super(GeoTIFFTranslate, self).__init__() self._dict["GeoTIFF files"] = _translate( "GeoTIFF", "GeoTIFF files" diff --git a/src/View/GeoTIFF/Window.py b/src/View/GeoTIFF/Window.py index 98d1d058..3113c8d3 100644 --- a/src/View/GeoTIFF/Window.py +++ b/src/View/GeoTIFF/Window.py @@ -93,18 +93,18 @@ class GeoTIFFListWindow(PamhyrWindow): rows = self.selected_rows() for row in rows: - add_file = self._study.river.geotiff.files[row] + geotiff= self._study.river.geotiff.files[row] if self.sub_window_exists( EditGeoTIFFWindow, - data=[self._study, self._config, add_file] + data=[self._study, self._config, geotiff] ): continue win = EditGeoTIFFWindow( study=self._study, config=self._config, - add_file=add_file, + geotiff=geotiff, trad=self._trad, undo=self._undo_stack, parent=self, diff --git a/src/View/MainWindow.py b/src/View/MainWindow.py index 8f0f54ac..96c123ae 100644 --- a/src/View/MainWindow.py +++ b/src/View/MainWindow.py @@ -79,6 +79,7 @@ from View.Frictions.Window import FrictionsWindow from View.SedimentLayers.Window import SedimentLayersWindow from View.SedimentLayers.Reach.Window import ReachSedimentLayersWindow from View.AdditionalFiles.Window import AddFileListWindow +from View.GeoTIFF.Window import GeoTIFFListWindow from View.REPLines.Window import REPLineListWindow from View.SolverParameters.Window import SolverParametersWindow from View.RunSolver.Window import ( @@ -1360,6 +1361,19 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit): ) self.additonal_files.show() + def open_geotiff(self): + if self._study is not None: + if self.sub_window_exists( + GeoTIFFListWindow, + data=[self._study, None] + ): + return + + self.geotiff = GeoTIFFListWindow( + study=self._study, parent=self + ) + self.geotiff.show() + def open_rep_lines(self): if self._study is not None: if self.sub_window_exists( diff --git a/src/View/Results/translate.py b/src/View/Results/translate.py index 7a1e7e9b..95ddd9a4 100644 --- a/src/View/Results/translate.py +++ b/src/View/Results/translate.py @@ -53,11 +53,6 @@ class ResultsTranslate(MainTranslate): "Results", "Max water elevation" ) - self._dict["file_all"] = _translate("Results", "All files (*)") - self._dict["file_geotiff"] = _translate( - "Results", "GeoTIFF file (*.tiff *.tif)") - self._dict["file_csv"] = _translate( - "Results", "CSV file (*.csv)") self._dict["ImageCoordinates"] = _translate( "Results", "Image coordinates" ) diff --git a/src/View/Translate.py b/src/View/Translate.py index 463b1057..e288e196 100644 --- a/src/View/Translate.py +++ b/src/View/Translate.py @@ -54,6 +54,18 @@ class CommonWordTranslate(PamhyrTranslate): self._dict["method"] = _translate("CommonWord", "Method") + # Files + self._dict["open_file"] = _translate( + "CommonWord", "Open file" + ) + self._dict["file_all"] = _translate("CommonWord", "All files (*)") + self._dict["file_geotiff"] = _translate( + "CommonWord", "GeoTIFF file (*.tiff *.tif)" + ) + self._dict["file_csv"] = _translate( + "CommonWord", "CSV file (*.csv)" + ) + class UnitTranslate(CommonWordTranslate): def __init__(self):