|
1 | 1 | """Widget for thickness calculator.""" |
2 | 2 |
|
3 | 3 | import os |
| 4 | +import pandas as pd |
4 | 5 |
|
5 | 6 | from PyQt5.QtWidgets import QMessageBox, QWidget |
6 | 7 | from qgis.core import QgsMapLayerProxyModel |
@@ -325,8 +326,61 @@ def _run_calculator(self): |
325 | 326 | thicknesses = result.get('thicknesses') |
326 | 327 | lines = result.get('lines') |
327 | 328 | location_tracking = result.get('location_tracking') |
328 | | - if thicknesses is not None: |
329 | | - addGeoDataFrameToproject(thicknesses, "Thickness Results") |
| 329 | + # If thicknesses were calculated, update the stratigraphic column units |
| 330 | + try: |
| 331 | + if thicknesses is not None and getattr(self, 'data_manager', None): |
| 332 | + # Prefer median thickness if available, fallback to mean |
| 333 | + thickness_col = ( |
| 334 | + 'ThicknessMedian' |
| 335 | + if 'ThicknessMedian' in getattr(thicknesses, 'columns', []) |
| 336 | + else ( |
| 337 | + 'ThicknessMean' |
| 338 | + if 'ThicknessMean' in getattr(thicknesses, 'columns', []) |
| 339 | + else None |
| 340 | + ) |
| 341 | + ) |
| 342 | + if thickness_col is not None: |
| 343 | + for _, row in thicknesses.iterrows(): |
| 344 | + unit_name = row.get('name') or row.get('UNITNAME') |
| 345 | + if not unit_name: |
| 346 | + continue |
| 347 | + try: |
| 348 | + value = row.get(thickness_col) |
| 349 | + except Exception: |
| 350 | + value = None |
| 351 | + # Skip invalid values (e.g. -1 means not calculated) |
| 352 | + try: |
| 353 | + is_invalid = pd.isna(value) or float(value) == -1 |
| 354 | + except Exception: |
| 355 | + is_invalid = value is None |
| 356 | + if is_invalid: |
| 357 | + continue |
| 358 | + # Find unit in stratigraphic column and update thickness |
| 359 | + try: |
| 360 | + strat_col = self.data_manager.get_stratigraphic_column() |
| 361 | + unit = strat_col.get_unit_by_name(unit_name) |
| 362 | + if unit is not None: |
| 363 | + unit.thickness = float(value) |
| 364 | + except Exception as err: |
| 365 | + # Log but don't fail the widget |
| 366 | + try: |
| 367 | + if getattr(self, '_debug', None): |
| 368 | + self._debug.plugin.log( |
| 369 | + message=f"Failed to update stratigraphic unit thickness for {unit_name}: {err}", |
| 370 | + log_level=2, |
| 371 | + ) |
| 372 | + except Exception: |
| 373 | + pass |
| 374 | + # Notify any stratigraphic column callbacks |
| 375 | + try: |
| 376 | + if getattr(self.data_manager, 'stratigraphic_column_callback', None): |
| 377 | + self.data_manager.stratigraphic_column_callback() |
| 378 | + except Exception: |
| 379 | + pass |
| 380 | + except Exception: |
| 381 | + pass |
| 382 | + # if thicknesses is not None: |
| 383 | + # addGeoDataFrameToproject(thicknesses, "Thickness Results") |
330 | 384 | if lines is not None: |
331 | 385 | addGeoDataFrameToproject(lines, "Thickness Lines") |
332 | 386 | if location_tracking is not None: |
@@ -388,5 +442,6 @@ def get_parameters(self): |
388 | 442 | if self.data_manager and hasattr(self.data_manager, 'get_stratigraphic_unit_names') |
389 | 443 | else None |
390 | 444 | ), |
| 445 | + 'debug_manager': self._debug, |
391 | 446 | } |
392 | 447 | return params |
0 commit comments