diff --git a/src/Model/GeoTIFF/GeoTIFF.py b/src/Model/GeoTIFF/GeoTIFF.py index d7b6e1d7..b92d9163 100644 --- a/src/Model/GeoTIFF/GeoTIFF.py +++ b/src/Model/GeoTIFF/GeoTIFF.py @@ -62,7 +62,15 @@ class GeoTIFF(SQLSubModel): self._description = description self._file_bytes = b'' - self._coordinates = coordinates + if coordinates is None: + self._coordinates = { + "bottom" : 0.0, + "top": 0.0, + "left": 0.0, + "right": 0.0, + } + else: + self._coordinates = coordinates self._file_name = "" if path != "": diff --git a/src/Model/GeoTIFF/GeoTIFFList.py b/src/Model/GeoTIFF/GeoTIFFList.py index b80a7694..84f3c803 100644 --- a/src/Model/GeoTIFF/GeoTIFFList.py +++ b/src/Model/GeoTIFF/GeoTIFFList.py @@ -43,8 +43,8 @@ class GeoTIFFList(PamhyrModelList): f"WHERE scenario = {self._status.scenario_id}" ) - for af in self._lst: - ok &= af._db_save(execute, data) + for gt in self._lst: + ok &= gt._db_save(execute, data) return ok diff --git a/src/View/GeoTIFF/Edit/Window.py b/src/View/GeoTIFF/Edit/Window.py index c0d08ef7..299c3bdb 100644 --- a/src/View/GeoTIFF/Edit/Window.py +++ b/src/View/GeoTIFF/Edit/Window.py @@ -23,11 +23,7 @@ from View.Tools.PamhyrWindow import PamhyrWindow from PyQt5.QtWidgets import ( QLabel, QPlainTextEdit, QPushButton, - QCheckBox, QFileDialog, -) - -from PyQt5.QtGui import ( - QPixmap, + QCheckBox, QFileDialog, QVBoxLayout, ) from PyQt5.QtCore import ( @@ -39,6 +35,23 @@ from View.GeoTIFF.UndoCommand import ( SetCommand ) +from View.Tools.Plot.PamhyrCanvas import MplCanvas +from View.LateralContribution.PlotXY import PlotXY + +try: + import rasterio + import rasterio.control + import rasterio.crs + import rasterio.sample + import rasterio.vrt + import rasterio._features + + from rasterio.io import MemoryFile + _rasterio_loaded = True +except Exception as e: + print(f"Module 'rasterio' is not available: {e}") + _rasterio_loaded = False + logger = logging.getLogger() @@ -65,9 +78,30 @@ class EditGeoTIFFWindow(PamhyrWindow): self._undo = undo + self.setup_graph() self.setup_values() self.setup_connection() + def setup_graph(self): + self.canvas = MplCanvas(width=5, height=4, dpi=100) + self.canvas.setObjectName("canvas") + self.plot_layout = self.find(QVBoxLayout, "verticalLayout_geotiff") + self.plot_layout.addWidget(self.canvas) + + self.plot = PlotXY( + canvas=self.canvas, + data=None, + trad=self._trad, + toolbar=None, + parent=self + ) + + self._plot_img = None + + memfile = self._geotiff.memfile + if memfile is not None: + self.draw_geotiff(memfile=memfile) + def setup_values(self): self.set_check_box("checkBox", self._geotiff.enabled) self.set_line_edit_text("lineEdit_name", self._geotiff.name) @@ -89,6 +123,16 @@ class EditGeoTIFFWindow(PamhyrWindow): self.set_line_edit_enable("lineEdit_path", False) self.set_plaintext_edit_enable("plainTextEdit", False) + def _set_values_from_bounds(self, bounds): + self._values = { + "bottom": bounds[0], + "top": bounds[2], + "left": bounds[1], + "right": bounds[3], + } + + self._reset_values() + def _reset_values(self): for key in self._values: self._reset_values_key(key) @@ -108,6 +152,34 @@ class EditGeoTIFFWindow(PamhyrWindow): self.find(QPushButton, f"pushButton_{key}")\ .clicked.connect(lambda: self._reset_values_key(key)) + def draw_geotiff(self, memfile=None): + if not _rasterio_loaded: + return + + if memfile is None: + if self._file_name == "": + return + + with rasterio.open(self._file_name) as data: + img = data.read() + b = data.bounds[:] + else: + with memfile.open() as gt: + img = gt.read() + b = gt.bounds[:] + + if self._plot_img is not None: + self._plot_img.remove() + + self._set_values_from_bounds(b) + + self._plot_img = self.canvas.axes.imshow( + img.transpose((1, 2, 0)), + extent=[b[0], b[2], b[1], b[3]] + ) + + self.plot.idle() + def _import(self): options = QFileDialog.Options() settings = QSettings(QSettings.IniFormat, @@ -129,16 +201,13 @@ class EditGeoTIFFWindow(PamhyrWindow): if filename != "": self._file_name = filename - - pixmap = QPixmap(filename) - self.find(QLabel, "label_geotiff")\ - .setPixmap(pixmap) + self. draw_geotiff() def accept(self): if self._study.is_editable(): is_enabled = self.get_check_box("checkBox") name = self.get_line_edit_text("lineEdit_name") - path = self.get_line_edit_text("lineEdit_path") + description = self.get_line_edit_text("lineEdit_description") coord_bottom = self.get_double_spin_box("doubleSpinBox_bottom") coord_top = self.get_double_spin_box("doubleSpinBox_top")