GeoTIFF: Factorise draw geotiff function.

scenario-dev-pa
Pierre-Antoine 2025-11-14 15:04:31 +01:00
parent 97ece018aa
commit ae08642116
3 changed files with 60 additions and 65 deletions

View File

@ -86,17 +86,14 @@ class GeoTIFFListWindow(PamhyrWindow):
self.plot = PlotXY( self.plot = PlotXY(
canvas=self.canvas, canvas=self.canvas,
data=self._study.river.enable_edges(), data=self._study.river.enable_edges(),
geotiff=self._study.river.geotiff,
trad=self._trad, trad=self._trad,
toolbar=None, toolbar=None,
parent=self parent=self
) )
self.plot.update() self.plot.update()
self._plot_img = {}
for geotiff in self._study.river.geotiff.lst:
self.draw_geotiff(geotiff)
def setup_connections(self): def setup_connections(self):
if self._study.is_editable(): if self._study.is_editable():
self.find(QAction, "action_add").triggered.connect(self.add) self.find(QAction, "action_add").triggered.connect(self.add)
@ -104,37 +101,9 @@ class GeoTIFFListWindow(PamhyrWindow):
self.find(QAction, "action_edit").triggered.connect(self.edit) self.find(QAction, "action_edit").triggered.connect(self.edit)
def draw_geotiff(self, geotiff):
if not _rasterio_loaded:
return
memfile = geotiff.memfile
if memfile is None:
return
with memfile.open() as gt:
img = gt.read()
coords = geotiff.coordinates
if geotiff in self._plot_img:
self._plot_img[geotiff].remove()
self._plot_img[geotiff] = self.canvas.axes.imshow(
img.transpose((1, 2, 0)),
extent=list(coords.values())
)
def update(self): def update(self):
self._list.update() self._list.update()
for geotiff in self._plot_img:
self._plot_img[geotiff].remove()
self._plot_img = {}
for geotiff in self._study.river.geotiff.files:
self.draw_geotiff(geotiff)
def selected_rows(self): def selected_rows(self):
lst = self.find(QListView, f"listView") lst = self.find(QListView, f"listView")
return list(map(lambda i: i.row(), lst.selectedIndexes())) return list(map(lambda i: i.row(), lst.selectedIndexes()))

View File

@ -104,8 +104,6 @@ class WidgetInfo(PamhyrWidget):
parent=self parent=self
) )
self._plot_img = {}
def update(self): def update(self):
if self._study is None: if self._study is None:
self.set_initial_values() self.set_initial_values()
@ -120,37 +118,14 @@ class WidgetInfo(PamhyrWidget):
self.plot = PlotXY( self.plot = PlotXY(
canvas=self.canvas, canvas=self.canvas,
data=self._study.river.enable_edges(), data=self._study.river.enable_edges(),
geotiff=self._study.river.geotiff,
trad=self.parent._trad, trad=self.parent._trad,
toolbar=self._toolbar_xy, toolbar=self._toolbar_xy,
parent=self parent=self
) )
self.draw_all_geotiff()
self.plot.update() self.plot.update()
def draw_all_geotiff(self):
if not _rasterio_loaded:
return
for img in self._plot_img:
self._plot_img[img].remove()
self._plot_img = {}
for geotiff in self._study.river.geotiff.lst:
memfile = geotiff.memfile
if memfile is None:
return
with memfile.open() as gt:
img = gt.read()
coords = geotiff.coordinates
self._plot_img[geotiff] = self.canvas.axes.imshow(
img.transpose((1, 2, 0)),
extent=list(coords.values())
)
def set_network_values(self): def set_network_values(self):
river = self._study.river river = self._study.river

View File

@ -27,12 +27,26 @@ from PyQt5.QtCore import (
) )
from PyQt5.QtWidgets import QApplication, QTableView from PyQt5.QtWidgets import QApplication, QTableView
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
_translate = QCoreApplication.translate _translate = QCoreApplication.translate
class PlotXY(PamhyrPlot): class PlotXY(PamhyrPlot):
def __init__(self, canvas=None, trad=None, data=None, toolbar=None, def __init__(self, canvas=None, trad=None, data=None, geotiff=None,
table=None, parent=None): toolbar=None, table=None, parent=None):
super(PlotXY, self).__init__( super(PlotXY, self).__init__(
canvas=canvas, canvas=canvas,
trad=trad, trad=trad,
@ -43,10 +57,14 @@ class PlotXY(PamhyrPlot):
) )
self._data = data self._data = data
self._geotiff = geotiff
self.label_x = self._trad["x"] self.label_x = self._trad["x"]
self.label_y = self._trad["y"] self.label_y = self._trad["y"]
self.parent = parent self.parent = parent
self._plot_img = {}
self._isometric_axis = True self._isometric_axis = True
self._auto_relim_update = True self._auto_relim_update = True
@ -69,7 +87,12 @@ class PlotXY(PamhyrPlot):
if data.reach.number_profiles != 0: if data.reach.number_profiles != 0:
self.draw_xy(data.reach) self.draw_xy(data.reach)
self.draw_lr(data.reach) self.draw_lr(data.reach)
self.idle()
if self._geotiff is not None:
self.draw_geotiff(self._geotiff.files)
self.idle()
return return
def draw_xy(self, reach): def draw_xy(self, reach):
@ -78,9 +101,9 @@ class PlotXY(PamhyrPlot):
line_xy.append(np.column_stack(xy)) line_xy.append(np.column_stack(xy))
line_xy_collection = collections.LineCollection( line_xy_collection = collections.LineCollection(
line_xy, line_xy,
colors=self.color_plot_river_bottom colors=self.color_plot_river_bottom
) )
self.canvas.axes.add_collection(line_xy_collection) self.canvas.axes.add_collection(line_xy_collection)
def draw_lr(self, reach): def draw_lr(self, reach):
@ -113,6 +136,34 @@ class PlotXY(PamhyrPlot):
) )
self.line_lr.append(line) self.line_lr.append(line)
def draw_geotiff(self, lst):
if not _rasterio_loaded:
return
for img in self._plot_img:
self._plot_img[img].remove()
self._plot_img = {}
for geotiff in lst:
memfile = geotiff.memfile
if memfile is None:
return
with memfile.open() as gt:
img = gt.read()
coords = geotiff.coordinates
self._plot_img[geotiff] = self.canvas.axes.imshow(
img.transpose((1, 2, 0)),
extent=list(coords.values())
)
if not geotiff.is_enabled():
self._plot_img[geotiff].set(alpha=0.5)
self.idle()
@timer @timer
def update(self): def update(self):
self.draw() self.draw()