Skip to content

Commit

Permalink
Make sure variables are refreshed when switching layers (#79)
Browse files Browse the repository at this point in the history
Support for node_id != basin feature id
  • Loading branch information
Huite authored Mar 26, 2024
1 parent e5f128c commit a7c8e02
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions imodqgis/timeseries/timeseries_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
In general: plotting with pyqtgraph is fast, collecting data is relatively
slow.
"""

from pathlib import Path
import tempfile
from itertools import compress
Expand Down Expand Up @@ -507,9 +508,9 @@ def load_mesh_data(self, layer):
columns[number] = timeseries_y_data(
layer, geometry, group_index, n_times
)
self.dataframes[
f"{name} point {i + 1} {variable}"
] = pd.DataFrame.from_dict(columns).set_index("time")
self.dataframes[f"{name} point {i + 1} {variable}"] = (
pd.DataFrame.from_dict(columns).set_index("time")
)

def sync_ipf_data(self, layer):
"""Synchronize (load & unload) timeseries data from an IPF dataset"""
Expand Down Expand Up @@ -550,6 +551,7 @@ def load_arrow_data(self, layer):
column = layer.customProperty("arrow_fid_column")
# Don't crash if Ribasim did not yet run
if Path(arrow_path).is_file():
self.stored_dataframes = {}
df = read_arrow(arrow_path)
# Don't crash if the dataframe is empty
if not df.empty:
Expand All @@ -559,14 +561,20 @@ def load_arrow_data(self, layer):

def sync_arrow_data(self, layer):
feature_ids = layer.selectedFeatureIds() # Returns a new list
# Do not read the data if the selection is the same
if self.feature_ids == feature_ids:
return
if len(feature_ids) == 0:
# warn user: no features selected in current layer
return

# FUTURE: maybe we can think of less special casing
column = layer.customProperty("arrow_fid_column")
if column == "node_id":
features = layer.selectedFeatures()
feature_ids = [feature.attributeMap()[column] for feature in features]

feature_ids = set(feature_ids).intersection(self.stored_dataframes.keys())
# Do not read the data if the selection is the same
if self.feature_ids == feature_ids:
return

# Filter names to add and to remove, to prevent loading duplicates
names_to_add = set(feature_ids).difference(self.dataframes.keys())
Expand Down Expand Up @@ -608,9 +616,7 @@ def sync_table_data(self, layer):
index_col=id_column,
)

selection = {
layer.getFeature(fid).attribute(id_column) for fid in feature_ids
}
selection = {layer.getFeature(fid).attribute(id_column) for fid in feature_ids}

# Filter names to add and to remove, to prevent loading duplicates
names_to_add = set(selection).difference(self.dataframes.keys())
Expand Down

0 comments on commit a7c8e02

Please sign in to comment.