Skip to content

Commit 16328cf

Browse files
authored
feat: move the stratigraphic layers to step 2 (6.2) (#138)
* feat: move the stratigraphic layers to step 2 The geology, contacts and structure pickers are in step 2, in a Source layers tab next to the Column tab. The first picker follows the contacts source: polygon layers for the geology source, line and point layers for the contacts layer source. Each source keeps its own selection. * fix: save the guessed contacts layer and show one geology picker The guessed contacts layer was set while _loading was set, so the guards of the pickers did not save it. Save it after _loading is cleared. Set the layer of the unit name picker before its field. On step 2, hide the geology picker of the column tab when the contacts come from the geology polygons. The source layers tab has that picker.
1 parent 81a8dbb commit 16328cf

8 files changed

Lines changed: 239 additions & 51 deletions

File tree

‎docs/development/usability-plan.md‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -412,30 +412,30 @@ input is the polygon geology layer, so the user cannot select it there.
412412

413413
Tasks:
414414

415-
- [ ] Remove `StratigraphicLayersWidget` from `ModelDefinitionTab`. Step 1 has
415+
- [x] Remove `StratigraphicLayersWidget` from `ModelDefinitionTab`. Step 1 has
416416
only the bounding box, the CRS and the DEM.
417-
- [ ] Add the widget to `StratigraphyStep`, as a collapsible group (see 6.3).
418-
- [ ] The first layer picker depends on the contacts source:
417+
- [x] Add the widget to `StratigraphyStep`, as a collapsible group (see 6.3).
418+
- [x] The first layer picker depends on the contacts source:
419419
- "Calculate from geology polygons": the picker shows polygon layers. It
420420
reads and writes the `geology` and `geology_unit_field` roles. It does not
421421
call `set_basal_contacts`. The Z-coordinate check box is hidden.
422422
- "Use a contacts layer": the picker shows line and point layers, as now. It
423423
reads and writes the `basal_contacts` role.
424424
- Change the group title and the label to match ("Geology layer" or
425425
"Contacts layer").
426-
- [ ] When the user changes the source, do not write the old selection to the
426+
- [x] When the user changes the source, do not write the old selection to the
427427
other role. Keep one selection for each source.
428-
- [ ] The `set_basal_contacts` callback (a tool or a build made a new contacts
428+
- [x] The `set_basal_contacts` callback (a tool or a build made a new contacts
429429
layer) does not change the picker in the "geology" source. The layer is for
430430
display.
431-
- [ ] Keep the geology picker of the column group in sync with the new picker
431+
- [x] Keep the geology picker of the column group in sync with the new picker
432432
(both use the same roles). Decide in the review if one of them must be
433433
removed (see the open questions).
434434
- [x] Move the "Select the geology layer" and "Select the structure layer"
435435
items from `check_data` to `check_stratigraphy`.
436-
- [ ] Update the text that says "in step 1" for these layers
436+
- [x] Update the text that says "in step 1" for these layers
437437
(`derived_refresh.py`, `checks.py`, `pages.py`, the docs).
438-
- [ ] Keep the saved widget settings key `stratigraphic_layers_widget`, so old
438+
- [x] Keep the saved widget settings key `stratigraphic_layers_widget`, so old
439439
state files load.
440440

441441
Acceptance: with "Calculate from geology polygons", the user selects the

‎docs/usage/interface.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ The faults trace layer is usually a line layer that contains the trace of the fa
4747

4848
![Fault Layer](../static/fault_layers.png)
4949
### Stratigraphy
50-
Two layers can be used to constrain the stratigraphy of the model:
50+
The **Source layers** group is at the top of step 2. Two layers can be used to constrain the stratigraphy of the model:
5151
1. Basal contacts - this layer defines the basal contacts of the stratigraphy. The layer should contain a line layer with the contact traces. The attributes can be used to define the name of the contact.
5252
2. Structural data - this layer defines the structural data that is used to constrain the model. The layer should contain a point layer with the structural data. The attributes can be used to define the orientation of the data, such as dip and dip direction.
5353

@@ -58,9 +58,9 @@ You select each layer one time. The plugin keeps the geology layer, the unit nam
5858

5959
The geology layer is the layer that you select in the Stratigraphic Column tab. If you select a layer in one of the tools and no geology layer is set, the plugin uses that layer for the other tools. The plugin saves the shared layers with the application state.
6060

61-
The **Source** setting in the basal contacts group selects where the basal contacts come from:
62-
- **Calculate from geology polygons** (default). The Basal Contacts tool extracts the contacts from the geology layer and the stratigraphic column. When it finishes, the new layer becomes the basal contacts layer of the model.
63-
- **Use a contacts layer**. Your own layer is an input. The plugin does not change it, and the Thickness Calculator uses it.
61+
The **Source** setting in the **Source layers** group of step 2 selects where the basal contacts come from. The first layer picker follows the setting. Each source keeps its own selection:
62+
- **Calculate from geology polygons** (default). The picker shows polygon layers. It sets the geology layer and the unit name field. The Basal Contacts tool extracts the contacts from the geology layer and the stratigraphic column. When it finishes, the new layer becomes the basal contacts layer of the model.
63+
- **Use a contacts layer**. The picker shows line and point layers. Your own layer is an input. The plugin does not change it, and the Thickness Calculator uses it.
6464

6565
## Stratigraphic Column
6666
The stratigraphic column defines the order of the contacts and any unconformable relationships between them. The column is defined by a list of units - these units are ordered from oldest at the bottom to youngest at the top. Unconformities can be inserted between units to define an unconformable relationship. The thicknesses define the true thickness of each unit and are used to parameterise the interpolation. The unit names should match the names of the contacts in the basal contacts layer. Units without basal contacts can be included in the stratigraphic column but will not be constrained by any data.

‎loopstructural/gui/modelling/model_definition/model_definition_tab.py‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,24 @@
44

55
from .bounding_box import BoundingBoxWidget
66
from .dem import DEMWidget
7-
from .stratigraphic_layers import StratigraphicLayersWidget
87

98

109
class ModelDefinitionTab(BaseTab):
11-
"""The area, the elevation and the source layers (step 1 of the dock).
10+
"""The area and the elevation (step 1 of the dock).
1211
13-
The fault layers are in step 3. Save, Open and Reset are in the header of
14-
the dock.
12+
The geology, contacts and structure layers are in step 2. The fault layers
13+
are in step 3. Save, Open and Reset are in the header of the dock.
1514
"""
1615

1716
def __init__(self, parent=None, data_manager=None):
1817
super().__init__(parent, data_manager, scrollable=True)
1918
# Add widgets to the QToolBox
2019
self.bounding_box = BoundingBoxWidget(self, data_manager)
2120
self.dem = DEMWidget(self, data_manager)
22-
self.stratigraphy_layers = StratigraphicLayersWidget(self, data_manager)
2321

2422
# Set uniform size policy for all widgets
25-
for widget in [self.bounding_box, self.dem, self.stratigraphy_layers]:
23+
for widget in [self.bounding_box, self.dem]:
2624
widget.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
2725

2826
self.add_widget(self.bounding_box, 'Bounding Box')
2927
self.add_widget(self.dem, 'DEM')
30-
self.add_widget(self.stratigraphy_layers, 'Stratigraphic Layers')

‎loopstructural/gui/modelling/model_definition/stratigraphic_layers.py‎

Lines changed: 111 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def __init__(self, parent=None, data_manager=None):
1515
if data_manager is None:
1616
raise ValueError("data_manager must be provided")
1717
self.data_manager = data_manager
18+
self._loading = False
1819
super().__init__(parent)
1920
ui_path = os.path.join(os.path.dirname(__file__), "stratigraphic_layers.ui")
2021
uic.loadUi(ui_path, self)
@@ -55,8 +56,65 @@ def __init__(self, parent=None, data_manager=None):
5556
self.useStructuralPointsZCoordinatesCheckBox.stateChanged.connect(
5657
self.onStructuralDataFieldChanged
5758
)
58-
self._guess_layers_and_fields()
59+
self._guess_structure_layer()
5960
self._restore_selection()
61+
self._apply_contacts_source(self.data_manager.layer_roles.contacts_source)
62+
63+
@property
64+
def _from_geology(self):
65+
return self.data_manager.layer_roles.contacts_source == layer_roles.CONTACTS_FROM_GEOLOGY
66+
67+
def _apply_contacts_source(self, source):
68+
"""Set the filter, the labels and the selection of the first picker for a source.
69+
70+
The geology source has one selection (the `geology` roles) and the layer
71+
source has another (the `basal_contacts` role). Each one is kept.
72+
"""
73+
from_geology = source == layer_roles.CONTACTS_FROM_GEOLOGY
74+
guessed = False
75+
self._loading = True
76+
try:
77+
if from_geology:
78+
configure_layer_combo(
79+
self.basalContactsLayer,
80+
QgsMapLayerProxyModel.Filter.PolygonLayer,
81+
allow_empty=True,
82+
)
83+
self.groupBox_basalContacts.setTitle("Geology")
84+
self.basalContactsLabel.setText("Geology layer")
85+
self.label.setText("Unit name field")
86+
roles = self.data_manager.layer_roles
87+
layer = roles.get(layer_roles.GEOLOGY)
88+
field = roles.get(layer_roles.GEOLOGY_UNIT_FIELD)
89+
else:
90+
configure_layer_combo(
91+
self.basalContactsLayer,
92+
QgsMapLayerProxyModel.Filter.LineLayer | QgsMapLayerProxyModel.Filter.PointLayer,
93+
allow_empty=True,
94+
)
95+
self.groupBox_basalContacts.setTitle("Basal contacts")
96+
self.basalContactsLabel.setText("Contacts layer")
97+
self.label.setText("Unit name field")
98+
layer = self.data_manager.layer_roles.get(layer_roles.BASAL_CONTACTS)
99+
field = (self.data_manager._basal_contacts or {}).get('unitname_field')
100+
if layer is None:
101+
self._guess_basal_contacts()
102+
layer = self.basalContactsLayer.currentLayer()
103+
field = self.unitNameField.currentField() or field
104+
guessed = layer is not None
105+
self.basalContactsLayer.setLayer(layer)
106+
self.unitNameField.setLayer(layer)
107+
if field:
108+
self.unitNameField.setField(field)
109+
self.useBasalContactsZCoordinatesCheckBox.setVisible(not from_geology)
110+
self.useZCoordinateLabel.setVisible(not from_geology)
111+
finally:
112+
self._loading = False
113+
if guessed:
114+
# The guards of the pickers skip a change while `_loading` is set,
115+
# so save the guessed layer here
116+
self._write_first_picker(layer, field)
117+
self._persist_selection()
60118

61119
def _add_contacts_source_combo(self):
62120
"""Add the choice of where the basal contacts come from."""
@@ -87,9 +145,14 @@ def _on_contacts_source_selected(self, index):
87145
self.data_manager.layer_roles.contacts_source = self.contactsSourceComboBox.itemData(index)
88146

89147
def _on_layer_role_changed(self, role, value):
90-
"""Show a contacts source that was set by a loaded state."""
148+
"""Show a contacts source or a geology layer that something else set."""
91149
if role == 'contacts_source':
92150
self._show_contacts_source(value)
151+
self._apply_contacts_source(value)
152+
elif role in (layer_roles.GEOLOGY, layer_roles.GEOLOGY_UNIT_FIELD) and self._from_geology:
153+
self._apply_contacts_source(layer_roles.CONTACTS_FROM_GEOLOGY)
154+
elif role == layer_roles.BASAL_CONTACTS and not self._from_geology:
155+
self._apply_contacts_source(layer_roles.CONTACTS_FROM_LAYER)
93156

94157
def enableBasalContactsZCheckBox(self, enable):
95158
self.useBasalContactsZCoordinatesCheckBox.setEnabled(enable)
@@ -106,6 +169,9 @@ def enableStructuralPointsZCheckBox(self, enable):
106169
self.useStructuralPointsZCoordinatesCheckBox.setChecked(False)
107170

108171
def set_basal_contacts(self, layer, unitname_field=None, use_z_coordinate=False):
172+
if self._from_geology:
173+
# The contacts layer is for display. The picker shows the geology layer.
174+
return
109175
self.basalContactsLayer.setLayer(layer)
110176
if layer is not None and layer.isValid():
111177
if layer.wkbType() != QgsWkbTypes.Type.Unknown:
@@ -153,9 +219,22 @@ def set_orientations_layer(
153219
self.structural_points_use_z = use_z_coordinate
154220
self.useStructuralPointsZCoordinatesCheckBox.setChecked(use_z_coordinate)
155221

222+
def _write_first_picker(self, layer, field):
223+
"""Write the first picker to the role of the current source."""
224+
if self._from_geology:
225+
roles = self.data_manager.layer_roles
226+
roles.set(layer_roles.GEOLOGY, layer)
227+
roles.set(layer_roles.GEOLOGY_UNIT_FIELD, field or None)
228+
else:
229+
self.data_manager.set_basal_contacts(
230+
layer, field, use_z_coordinate=self.basal_contacts_use_z
231+
)
232+
156233
def onBasalContactsChanged(self, layer):
234+
if self._loading:
235+
return
157236
self.unitNameField.setLayer(layer)
158-
self.data_manager.set_basal_contacts(layer, self.unitNameField.currentField())
237+
self._write_first_picker(layer, self.unitNameField.currentField())
159238
self._persist_selection()
160239

161240
def onOrientationTypeChanged(self, index):
@@ -198,16 +277,13 @@ def onStructuralDataFieldChanged(self, field):
198277
# self.updateDataManager()
199278

200279
def onUnitFieldChanged(self, field):
201-
self.data_manager.set_basal_contacts(
202-
self.basalContactsLayer.currentLayer(),
203-
field,
204-
use_z_coordinate=self.basal_contacts_use_z,
205-
)
280+
if self._loading:
281+
return
282+
self._write_first_picker(self.basalContactsLayer.currentLayer(), field)
206283
self._persist_selection()
207284

208-
# self.updateDataManager()
209-
210-
def _guess_layers_and_fields(self):
285+
def _guess_basal_contacts(self):
286+
"""Select a layer that is named like a contacts layer, in the layer source."""
211287
if not self.data_manager:
212288
return
213289
# Basal contacts
@@ -218,11 +294,16 @@ def _guess_layers_and_fields(self):
218294
layer = self.data_manager.find_layer_by_name(basal_match)
219295
if layer:
220296
self.basalContactsLayer.setLayer(layer)
297+
# setField does nothing for a field that is not in the layer of the picker
298+
self.unitNameField.setLayer(layer)
221299
fields = [f.name() for f in layer.fields()]
222300
fmatcher = ColumnMatcher(fields)
223301
if unit_match := fmatcher.find_match('UNITNAME'):
224302
self.unitNameField.setField(unit_match)
225-
# Structural data
303+
304+
def _guess_structure_layer(self):
305+
if not self.data_manager:
306+
return
226307
structural_names = get_layer_names(self.structuralDataLayer)
227308
structural_matcher = ColumnMatcher(structural_names)
228309
structural_match = structural_matcher.find_match(
@@ -244,23 +325,27 @@ def _guess_layers_and_fields(self):
244325
def _persist_selection(self):
245326
if not self.data_manager:
246327
return
247-
settings = {
248-
'basal_layer': (
328+
settings = dict(self.data_manager.get_widget_settings('stratigraphic_layers_widget', {}))
329+
if not self._from_geology:
330+
# In the geology source the first picker holds the geology layer. The
331+
# `geology` roles save it. Keep the contacts layer of the other source.
332+
settings['basal_layer'] = (
249333
self.basalContactsLayer.currentLayer().name()
250334
if self.basalContactsLayer.currentLayer()
251335
else None
252-
),
336+
)
337+
settings['unit_name_field'] = self.unitNameField.currentField()
338+
settings['use_basal_z'] = self.useBasalContactsZCoordinatesCheckBox.isChecked()
339+
settings |= {
253340
'structural_layer': (
254341
self.structuralDataLayer.currentLayer().name()
255342
if self.structuralDataLayer.currentLayer()
256343
else None
257344
),
258-
'unit_name_field': self.unitNameField.currentField(),
259345
'orientation_field': self.orientationField.currentField(),
260346
'dip_field': self.dipField.currentField(),
261347
'structural_unit_field': self.structuralDataUnitName.currentField(),
262348
'orientation_type': self.orientationType.currentText(),
263-
'use_basal_z': self.useBasalContactsZCoordinatesCheckBox.isChecked(),
264349
'use_structural_z': self.useStructuralPointsZCoordinatesCheckBox.isChecked(),
265350
}
266351
self.data_manager.set_widget_settings('stratigraphic_layers_widget', settings)
@@ -271,16 +356,18 @@ def _restore_selection(self):
271356
settings = self.data_manager.get_widget_settings('stratigraphic_layers_widget', {})
272357
if not settings:
273358
return
274-
if layer_name := settings.get('basal_layer'):
275-
layer = self.data_manager.find_layer_by_name(layer_name)
276-
if layer:
277-
self.basalContactsLayer.setLayer(layer)
359+
# The first picker of the geology source comes from the `geology` roles
360+
if not self._from_geology:
361+
if layer_name := settings.get('basal_layer'):
362+
layer = self.data_manager.find_layer_by_name(layer_name)
363+
if layer:
364+
self.basalContactsLayer.setLayer(layer)
365+
if field := settings.get('unit_name_field'):
366+
self.unitNameField.setField(field)
278367
if layer_name := settings.get('structural_layer'):
279368
layer = self.data_manager.find_layer_by_name(layer_name)
280369
if layer:
281370
self.structuralDataLayer.setLayer(layer)
282-
if field := settings.get('unit_name_field'):
283-
self.unitNameField.setField(field)
284371
if field := settings.get('orientation_field'):
285372
self.orientationField.setField(field)
286373
if field := settings.get('dip_field'):
@@ -293,7 +380,7 @@ def _restore_selection(self):
293380
)
294381
if idx >= 0:
295382
self.orientationType.setCurrentIndex(idx)
296-
if 'use_basal_z' in settings:
383+
if 'use_basal_z' in settings and not self._from_geology:
297384
self.useBasalContactsZCoordinatesCheckBox.setChecked(settings['use_basal_z'])
298385
if 'use_structural_z' in settings:
299386
self.useStructuralPointsZCoordinatesCheckBox.setChecked(settings['use_structural_z'])

0 commit comments

Comments
 (0)