|
| 1 | +from typing import Any, Optional |
| 2 | +from osgeo import gdal |
| 3 | +import numpy as np |
| 4 | +import json |
| 5 | + |
| 6 | +from PyQt5.QtCore import QVariant |
| 7 | +from qgis import processing |
| 8 | +from qgis.core import ( |
| 9 | + QgsFeatureSink, |
| 10 | + QgsFields, |
| 11 | + QgsField, |
| 12 | + QgsFeature, |
| 13 | + QgsGeometry, |
| 14 | + QgsRasterLayer, |
| 15 | + QgsProcessing, |
| 16 | + QgsProcessingAlgorithm, |
| 17 | + QgsProcessingContext, |
| 18 | + QgsProcessingException, |
| 19 | + QgsProcessingFeedback, |
| 20 | + QgsProcessingParameterEnum, |
| 21 | + QgsProcessingParameterFileDestination, |
| 22 | + QgsProcessingParameterFeatureSink, |
| 23 | + QgsProcessingParameterFeatureSource, |
| 24 | + QgsProcessingParameterField, |
| 25 | + QgsProcessingParameterRasterLayer, |
| 26 | + QgsProcessingParameterMatrix, |
| 27 | + QgsCoordinateReferenceSystem, |
| 28 | + QgsVectorLayer, |
| 29 | + QgsWkbTypes, |
| 30 | + QgsSettings |
| 31 | +) |
| 32 | + |
| 33 | + |
| 34 | +from qgis.core import ( |
| 35 | + QgsFields, QgsField, QgsFeature, QgsFeatureSink, QgsWkbTypes, |
| 36 | + QgsCoordinateReferenceSystem, QgsProcessingAlgorithm, QgsProcessingContext, |
| 37 | + QgsProcessingFeedback, QgsProcessingParameterFeatureSink, QgsProcessingParameterMatrix, |
| 38 | + QgsSettings |
| 39 | +) |
| 40 | +from PyQt5.QtCore import QVariant |
| 41 | +import numpy as np |
| 42 | + |
| 43 | +class UserDefinedStratigraphyAlgorithm(QgsProcessingAlgorithm): |
| 44 | + INPUT_STRATI_COLUMN = "INPUT_STRATI_COLUMN" |
| 45 | + OUTPUT = "OUTPUT" |
| 46 | + |
| 47 | + def name(self): return "loop_sorter_2" |
| 48 | + def displayName(self): return "User-Defined Stratigraphic Column" |
| 49 | + def group(self): return "Stratigraphy" |
| 50 | + def groupId(self): return "Stratigraphy_Column" |
| 51 | + |
| 52 | + def initAlgorithm(self, config=None): |
| 53 | + strati_settings = QgsSettings() |
| 54 | + last_strati_column = strati_settings.value("m2l/strati_column", "") |
| 55 | + self.addParameter( |
| 56 | + QgsProcessingParameterMatrix( |
| 57 | + name=self.INPUT_STRATI_COLUMN, |
| 58 | + description="Stratigraphic Order", |
| 59 | + headers=["Unit"], |
| 60 | + numberRows=0, |
| 61 | + defaultValue=last_strati_column |
| 62 | + ) |
| 63 | + ) |
| 64 | + self.addParameter( |
| 65 | + QgsProcessingParameterFeatureSink( |
| 66 | + self.OUTPUT, |
| 67 | + "Stratigraphic column", |
| 68 | + ) |
| 69 | + ) |
| 70 | + |
| 71 | + def processAlgorithm(self, parameters, context, feedback): |
| 72 | + # 1) Read the matrix; it may be a list of lists (rows) or a flat list depending on input source. |
| 73 | + matrix = self.parameterAsMatrix(parameters, self.INPUT_STRATI_COLUMN, context) |
| 74 | + |
| 75 | + # Normalize to a list of unit strings (one column: "Unit") |
| 76 | + units = [] |
| 77 | + for row in matrix: |
| 78 | + if isinstance(row, (list, tuple)): |
| 79 | + unit = row[0] if row else "" |
| 80 | + else: |
| 81 | + unit = row |
| 82 | + unit = (unit or "").strip() |
| 83 | + if unit: # skip empty rows to avoid writing "" into fields |
| 84 | + units.append(unit) |
| 85 | + |
| 86 | + # 2) Build sequential order (1-based), cast to native int |
| 87 | + order_vals = [int(i) for i in (np.arange(len(units)) + 1)] |
| 88 | + |
| 89 | + # 3) Prepare sink |
| 90 | + sink_fields = QgsFields() |
| 91 | + sink_fields.append(QgsField("order", QVariant.Int)) # or QVariant.LongLong |
| 92 | + sink_fields.append(QgsField("unit_name", QVariant.String)) |
| 93 | + |
| 94 | + crs = context.project().crs() if context and context.project() else QgsCoordinateReferenceSystem() |
| 95 | + sink, dest_id = self.parameterAsSink( |
| 96 | + parameters, self.OUTPUT, context, |
| 97 | + sink_fields, QgsWkbTypes.NoGeometry, crs |
| 98 | + ) |
| 99 | + |
| 100 | + # 4) Insert features |
| 101 | + for pos, unit_name in zip(order_vals, units): |
| 102 | + f = QgsFeature(sink_fields) |
| 103 | + # Ensure correct types: int for "order", str for "unit_name" |
| 104 | + f.setAttributes([int(pos), str(unit_name)]) |
| 105 | + ok = sink.addFeature(f, QgsFeatureSink.FastInsert) |
| 106 | + if not ok: |
| 107 | + feedback.reportError(f"Failed to add feature for unit '{unit_name}' (order={pos}).") |
| 108 | + |
| 109 | + return {self.OUTPUT: dest_id} |
| 110 | + |
| 111 | + def createInstance(self): |
| 112 | + return __class__() |
0 commit comments