|
| 1 | +"""The thickness calculator algorithm extracts basal contacts from geology. |
| 2 | +
|
| 3 | +When no basal contacts layer is given, the algorithm gets the basal contacts |
| 4 | +from the geology layer and the stratigraphic column. The map2loop calculator |
| 5 | +is replaced with a fake, so these tests examine only the data that the |
| 6 | +algorithm gives to it. |
| 7 | +""" |
| 8 | + |
| 9 | +import unittest |
| 10 | +from unittest.mock import patch |
| 11 | + |
| 12 | +import pandas as pd |
| 13 | +from qgis.core import ( |
| 14 | + QgsFeature, |
| 15 | + QgsGeometry, |
| 16 | + QgsProcessing, |
| 17 | + QgsProcessingContext, |
| 18 | + QgsProcessingFeatureSourceDefinition, |
| 19 | + QgsProcessingFeedback, |
| 20 | + QgsProject, |
| 21 | + QgsVectorLayer, |
| 22 | +) |
| 23 | +from qgis.testing import start_app |
| 24 | + |
| 25 | +from loopstructural.processing.algorithms.thickness_calculator import ( |
| 26 | + ThicknessCalculatorAlgorithm, |
| 27 | +) |
| 28 | + |
| 29 | +MODULE = 'loopstructural.processing.algorithms.thickness_calculator' |
| 30 | + |
| 31 | + |
| 32 | +class FakeCalculator: |
| 33 | + """Keep the arguments of compute and return a thickness for each unit.""" |
| 34 | + |
| 35 | + calls = [] |
| 36 | + |
| 37 | + def __init__(self, **kwargs): |
| 38 | + self.kwargs = kwargs |
| 39 | + |
| 40 | + def compute(self, units, stratigraphic_order, basal_contacts, *args): |
| 41 | + FakeCalculator.calls.append({'kwargs': self.kwargs, 'basal_contacts': basal_contacts}) |
| 42 | + return pd.DataFrame( |
| 43 | + { |
| 44 | + 'name': list(units['name']), |
| 45 | + 'ThicknessMean': 1.0, |
| 46 | + 'ThicknessMedian': 1.0, |
| 47 | + 'ThicknessStdDev': 0.0, |
| 48 | + } |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +class TestThicknessFromGeology(unittest.TestCase): |
| 53 | + |
| 54 | + @classmethod |
| 55 | + def setUpClass(cls): |
| 56 | + cls.qgs = start_app() |
| 57 | + |
| 58 | + def setUp(self): |
| 59 | + FakeCalculator.calls = [] |
| 60 | + self.geology = self._geology() |
| 61 | + QgsProject.instance().addMapLayer(self.geology) |
| 62 | + |
| 63 | + def tearDown(self): |
| 64 | + QgsProject.instance().removeMapLayer(self.geology.id()) |
| 65 | + |
| 66 | + def _geology(self): |
| 67 | + # Two blocks of the layers A, B and C (top to bottom). The first block |
| 68 | + # is at 0 <= x <= 100 and the second block is at 1000 <= x <= 1100. |
| 69 | + layer = QgsVectorLayer("Polygon?crs=EPSG:28350&field=unitname:string", "geology", "memory") |
| 70 | + features = [] |
| 71 | + for x0 in (0, 1000): |
| 72 | + x1 = x0 + 100 |
| 73 | + for name, (y0, y1) in {'A': (200, 300), 'B': (100, 200), 'C': (0, 100)}.items(): |
| 74 | + feature = QgsFeature(layer.fields()) |
| 75 | + feature.setAttribute('unitname', name) |
| 76 | + feature.setGeometry( |
| 77 | + QgsGeometry.fromWkt( |
| 78 | + f"POLYGON(({x0} {y0}, {x1} {y0}, {x1} {y1}, {x0} {y1}, {x0} {y0}))" |
| 79 | + ) |
| 80 | + ) |
| 81 | + features.append(feature) |
| 82 | + layer.dataProvider().addFeatures(features) |
| 83 | + return layer |
| 84 | + |
| 85 | + def _points(self, fields, values): |
| 86 | + layer = QgsVectorLayer(f"Point?crs=EPSG:28350&{fields}", "points", "memory") |
| 87 | + features = [] |
| 88 | + for x, y, attributes in values: |
| 89 | + feature = QgsFeature(layer.fields()) |
| 90 | + feature.setAttributes(attributes) |
| 91 | + feature.setGeometry(QgsGeometry.fromWkt(f"POINT({x} {y})")) |
| 92 | + features.append(feature) |
| 93 | + layer.dataProvider().addFeatures(features) |
| 94 | + return layer |
| 95 | + |
| 96 | + def _run(self, geology): |
| 97 | + structure = self._points("field=DIPDIR:double&field=DIP:double", [(50, 150, [90.0, 10.0])]) |
| 98 | + sampled_contacts = self._points("field=unit:string", [(50, 200, ['A'])]) |
| 99 | + algorithm = ThicknessCalculatorAlgorithm() |
| 100 | + algorithm.initAlgorithm() |
| 101 | + parameters = { |
| 102 | + 'THICKNESS_CALCULATOR_TYPE': 0, |
| 103 | + 'BOUNDING_BOX_TYPE': 0, |
| 104 | + 'MAX_LINE_LENGTH': 1000, |
| 105 | + 'GEOLOGY': geology, |
| 106 | + 'UNIT_NAME_FIELD': 'unitname', |
| 107 | + 'STRATIGRAPHIC_COLUMN': ['A', 'B', 'C'], |
| 108 | + 'SAMPLED_CONTACTS': sampled_contacts, |
| 109 | + 'STRUCTURE_DATA': structure, |
| 110 | + 'ORIENTATION_TYPE': 0, |
| 111 | + 'DIPDIR_FIELD': 'DIPDIR', |
| 112 | + 'DIP_FIELD': 'DIP', |
| 113 | + 'THICKNESS': QgsProcessing.TEMPORARY_OUTPUT, |
| 114 | + } |
| 115 | + context = QgsProcessingContext() |
| 116 | + context.setProject(QgsProject.instance()) |
| 117 | + with patch(f'{MODULE}.InterpolatedStructure', FakeCalculator): |
| 118 | + algorithm.processAlgorithm(parameters, context, QgsProcessingFeedback()) |
| 119 | + self.assertEqual(len(FakeCalculator.calls), 1) |
| 120 | + return FakeCalculator.calls[0] |
| 121 | + |
| 122 | + def _length(self, contacts, unit): |
| 123 | + return contacts[contacts['basal_unit'] == unit].geometry.length.sum() |
| 124 | + |
| 125 | + def test_contacts_from_geology_layer(self): |
| 126 | + call = self._run(self.geology) |
| 127 | + contacts = call['basal_contacts'] |
| 128 | + self.assertFalse(contacts.empty) |
| 129 | + # Each block has one contact of 100 m for A (on B) and for B (on C). |
| 130 | + self.assertAlmostEqual(self._length(contacts, 'A'), 200, delta=5) |
| 131 | + self.assertAlmostEqual(self._length(contacts, 'B'), 200, delta=5) |
| 132 | + self.assertAlmostEqual(call['kwargs']['bounding_box']['maxx'], 1100) |
| 133 | + |
| 134 | + def test_contacts_from_selected_features_only(self): |
| 135 | + # Select only the first block. |
| 136 | + self.geology.selectByExpression('x(centroid($geometry)) < 500') |
| 137 | + self.assertEqual(self.geology.selectedFeatureCount(), 3) |
| 138 | + call = self._run(QgsProcessingFeatureSourceDefinition(self.geology.id(), True)) |
| 139 | + contacts = call['basal_contacts'] |
| 140 | + self.assertFalse(contacts.empty) |
| 141 | + self.assertLessEqual(contacts.total_bounds[2], 100 + 1e-6) |
| 142 | + self.assertAlmostEqual(self._length(contacts, 'A'), 100, delta=5) |
| 143 | + self.assertAlmostEqual(self._length(contacts, 'B'), 100, delta=5) |
| 144 | + self.assertAlmostEqual(call['kwargs']['bounding_box']['maxx'], 100) |
| 145 | + |
| 146 | + |
| 147 | +if __name__ == '__main__': |
| 148 | + unittest.main() |
0 commit comments