-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasal_contacts_widget.py
More file actions
321 lines (282 loc) · 12.7 KB
/
basal_contacts_widget.py
File metadata and controls
321 lines (282 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
"""Widget for extracting basal contacts."""
import os
from PyQt5.QtWidgets import QMessageBox, QWidget
from qgis.core import QgsProject, QgsVectorFileWriter
from qgis.PyQt import uic
from ...main.helpers import ColumnMatcher, get_layer_names
from ...main.m2l_api import extract_basal_contacts
from ...main.vectorLayerWrapper import addGeoDataFrameToproject
class BasalContactsWidget(QWidget):
"""Widget for configuring and running the basal contacts extractor.
This widget provides a GUI interface for extracting basal contacts
from geology layers.
"""
def __init__(self, parent=None, data_manager=None, debug_manager=None):
"""Initialize the basal contacts widget.
Parameters
----------
parent : QWidget, optional
Parent widget.
data_manager : object, optional
Data manager for accessing shared data.
"""
super().__init__(parent)
self.data_manager = data_manager
self._debug = debug_manager
# Load the UI file
ui_path = os.path.join(os.path.dirname(__file__), "basal_contacts_widget.ui")
uic.loadUi(ui_path, self)
# Move layer filter setup out of the .ui (QgsMapLayerProxyModel values in .ui
# can cause import errors outside QGIS). Set filters programmatically
# and preserve the allowEmptyLayer setting for the faults combobox.
try:
from qgis.core import QgsMapLayerProxyModel
# geology layer should only show polygon layers
self.geologyLayerComboBox.setFilters(QgsMapLayerProxyModel.PolygonLayer)
# faults should show line layers and allow empty selection (as set in .ui)
self.faultsLayerComboBox.setFilters(QgsMapLayerProxyModel.LineLayer)
try:
# QgsMapLayerComboBox has setAllowEmptyLayer method in newer QGIS versions
self.faultsLayerComboBox.setAllowEmptyLayer(True)
except Exception:
# Older QGIS bindings may use allowEmptyLayer property; ignore if unavailable
pass
except Exception:
# If QGIS isn't available (e.g. editing the UI outside QGIS), skip setting filters
pass
# Connect signals
self.geologyLayerComboBox.layerChanged.connect(self._on_geology_layer_changed)
self.runButton.clicked.connect(self._run_extractor)
self._guess_layers()
# Set up field combo boxes
self._setup_field_combo_boxes()
def set_debug_manager(self, debug_manager):
"""Attach a debug manager instance."""
self._debug = debug_manager
def _export_layer_for_debug(self, layer, name_prefix: str):
if not (self._debug and self._debug.is_debug()):
return None
try:
debug_dir = self._debug.get_effective_debug_dir()
out_path = debug_dir / f"{name_prefix}.gpkg"
options = QgsVectorFileWriter.SaveVectorOptions()
options.driverName = "GPKG"
options.layerName = layer.name()
res = QgsVectorFileWriter.writeAsVectorFormatV3(
layer,
str(out_path),
QgsProject.instance().transformContext(),
options,
)
if res[0] == QgsVectorFileWriter.NoError:
return str(out_path)
except Exception as err:
self._debug.plugin.log(
message=f"[map2loop] Failed to export layer '{name_prefix}': {err}",
log_level=2,
)
return None
def _serialize_layer(self, layer, name_prefix: str):
try:
export_path = self._export_layer_for_debug(layer, name_prefix)
return {
"name": layer.name(),
"id": layer.id(),
"provider": layer.providerType() if hasattr(layer, "providerType") else None,
"source": layer.source() if hasattr(layer, "source") else None,
"export_path": export_path,
}
except Exception:
return str(layer)
def _serialize_params_for_logging(self, params, context_label: str):
serialized = {}
for key, value in params.items():
if hasattr(value, "source") or hasattr(value, "id"):
serialized[key] = self._serialize_layer(value, f"{context_label}_{key}")
else:
serialized[key] = value
return serialized
def _log_params(self, context_label: str):
if getattr(self, "_debug", None):
try:
self._debug.log_params(
context_label=context_label,
params=self._serialize_params_for_logging(self.get_parameters(), context_label),
)
except Exception:
pass
def _guess_layers(self):
"""Attempt to auto-select layers based on common naming conventions."""
if not self.data_manager:
return
# Attempt to find geology layer
geology_layer_names = get_layer_names(self.geologyLayerComboBox)
geology_matcher = ColumnMatcher(geology_layer_names)
geology_layer_match = geology_matcher.find_match('GEOLOGY')
if geology_layer_match:
geology_layer = self.data_manager.find_layer_by_name(geology_layer_match)
self.geologyLayerComboBox.setLayer(geology_layer)
# Attempt to find faults layer
fault_layer_names = get_layer_names(self.faultsLayerComboBox)
fault_layer_matcher = ColumnMatcher(fault_layer_names)
fault_layer_match = fault_layer_matcher.find_match('FAULTS')
if fault_layer_match:
faults_layer = self.data_manager.find_layer_by_name(fault_layer_match)
self.faultsLayerComboBox.setLayer(faults_layer)
def _setup_field_combo_boxes(self):
"""Set up field combo boxes to link to their respective layers."""
geology = self.geologyLayerComboBox.currentLayer()
if geology is not None:
self.unitNameFieldComboBox.setLayer(geology)
else:
# Ensure combo boxes are cleared if no geology layer selected
self.unitNameFieldComboBox.setLayer(None)
def _on_geology_layer_changed(self):
"""Update field combo boxes when geology layer changes."""
from ...main.helpers import ColumnMatcher
layer = self.geologyLayerComboBox.currentLayer()
self.unitNameFieldComboBox.setLayer(layer)
# Auto-detect appropriate fields
if layer:
fields = [field.name() for field in layer.fields()]
matcher = ColumnMatcher(fields)
# Auto-select UNITNAME field
if unit_match := matcher.find_match('UNITNAME'):
self.unitNameFieldComboBox.setField(unit_match)
def _run_extractor(self):
"""Run the basal contacts extraction algorithm."""
self._log_params("basal_contacts_widget_run")
# Validate inputs
if not self.geologyLayerComboBox.currentLayer():
QMessageBox.warning(self, "Missing Input", "Please select a geology layer.")
return False
try:
result, contact_type = self._extract_contacts()
if result:
QMessageBox.information(
self,
"Success",
f"Successfully extracted {contact_type}!",
)
if self._debug and self._debug.is_debug():
try:
self._debug.save_debug_file(
"basal_contacts_result.txt", str(result).encode("utf-8")
)
except Exception as err:
self._debug.plugin.log(
message=f"[map2loop] Failed to save basal contacts debug output: {err}",
log_level=2,
)
return True
except Exception as err:
if self._debug:
self._debug.plugin.log(
message=f"[map2loop] Basal contacts extraction failed: {err}",
log_level=2,
)
raise err
QMessageBox.critical(self, "Error", f"An error occurred: {err}")
return False
def get_parameters(self):
"""Get current widget parameters.
Returns
-------
dict
Dictionary of current widget parameters.
"""
ignore_units = []
if self.ignoreUnitsLineEdit.text().strip():
ignore_units = [
unit.strip() for unit in self.ignoreUnitsLineEdit.text().split(',') if unit.strip()
]
return {
'geology_layer': self.geologyLayerComboBox.currentLayer(),
'unit_name_field': self.unitNameFieldComboBox.currentField(),
'faults_layer': self.faultsLayerComboBox.currentLayer(),
'ignore_units': ignore_units,
'all_contacts': self.allContactsCheckBox.isChecked(),
}
def set_parameters(self, params):
"""Set widget parameters.
Parameters
----------
params : dict
Dictionary of parameters to set.
"""
if 'geology_layer' in params and params['geology_layer']:
self.geologyLayerComboBox.setLayer(params['geology_layer'])
if 'faults_layer' in params and params['faults_layer']:
self.faultsLayerComboBox.setLayer(params['faults_layer'])
if 'ignore_units' in params and params['ignore_units']:
self.ignoreUnitsLineEdit.setText(', '.join(params['ignore_units']))
if 'all_contacts' in params:
self.allContactsCheckBox.setChecked(params['all_contacts'])
def _extract_contacts(self):
"""Execute basal contacts extraction."""
# Parse ignore units
ignore_units = []
if self.ignoreUnitsLineEdit.text().strip():
ignore_units = [
unit.strip() for unit in self.ignoreUnitsLineEdit.text().split(',') if unit.strip()
]
geology = self.geologyLayerComboBox.currentLayer()
unit_name_field = self.unitNameFieldComboBox.currentField()
faults = self.faultsLayerComboBox.currentLayer()
stratigraphic_order = (
self.data_manager.get_stratigraphic_unit_names() if self.data_manager else []
)
# Check if user wants all contacts or just basal contacts
all_contacts = self.allContactsCheckBox.isChecked()
if all_contacts:
def _is_null_like(v):
# Python None
if v is None:
return True
# PyQGIS QVariant null check
if hasattr(v, "isNull") and callable(getattr(v, "isNull")) and v.isNull():
return True
# Empty strings or literal "NULL" (case-insensitive)
if isinstance(v, str):
s = v.strip()
if s == "" or s.upper() == "NULL":
return True
return False
values = []
for feat in geology.getFeatures():
try:
val = feat[unit_name_field]
except Exception:
val = None
if _is_null_like(val):
continue
if val not in values:
values.append(val)
stratigraphic_order = values
print(f"Extracting all contacts for units: {stratigraphic_order}")
self.data_manager.logger(f"Extracting all contacts for units: {stratigraphic_order}")
result = extract_basal_contacts(
geology=geology,
stratigraphic_order=stratigraphic_order,
faults=faults,
ignore_units=ignore_units,
unit_name_field=unit_name_field,
all_contacts=all_contacts,
updater=lambda message: QMessageBox.information(self, "Extraction Progress", message),
debug_manager=self._debug,
)
self.data_manager.logger(f'All contacts extracted: {all_contacts}')
contact_type = "basal contacts"
if result:
if all_contacts and result['all_contacts'].empty is False:
addGeoDataFrameToproject(result['all_contacts'], "All contacts")
contact_type = "all contacts and basal contacts"
elif not all_contacts and result['basal_contacts'].empty is False:
addGeoDataFrameToproject(result['basal_contacts'], "Basal contacts")
else:
QMessageBox.information(
self,
"No Contacts Found",
"No contacts were found with the given parameters.",
)
return result, contact_type