Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion m2l/processing/algorithms/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"""
# Python imports
from typing import Any, Optional
from qgis.PyQt.QtCore import QMetaType, QVariant
from qgis.PyQt.QtCore import QVariant
from osgeo import gdal
import pandas as pd

Expand Down
33 changes: 21 additions & 12 deletions m2l/processing/algorithms/sorter.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def initAlgorithm(self, config: Optional[dict[str, Any]] = None) -> None:
self.INPUT_GEOLOGY,
"Geology polygons",
[QgsProcessing.TypeVectorPolygon],
optional=True
optional=False
)
)

Expand Down Expand Up @@ -161,7 +161,7 @@ def initAlgorithm(self, config: Optional[dict[str, Any]] = None) -> None:
optional=True
)
)

self.addParameter(
QgsProcessingParameterField(
'GROUP_FIELD',
Expand Down Expand Up @@ -225,7 +225,7 @@ def initAlgorithm(self, config: Optional[dict[str, Any]] = None) -> None:
"CONTACTS_LAYER",
"Contacts Layer",
[QgsProcessing.TypeVectorLine],
optional=False,
optional=True,
)
)

Expand Down Expand Up @@ -256,9 +256,14 @@ def processAlgorithm(

algo_index: int = self.parameterAsEnum(parameters, self.SORTING_ALGORITHM, context)
sorter_cls = list(SORTER_LIST.values())[algo_index]
sorter_name = list(SORTER_LIST.keys())[algo_index]
requires_contacts = sorter_cls in [SorterAlpha, SorterMaximiseContacts, SorterObservationProjections]
contacts_layer = self.parameterAsVectorLayer(parameters, self.CONTACTS_LAYER, context)
in_layer = self.parameterAsVectorLayer(parameters, self.INPUT_GEOLOGY, context)
output_file = self.parameterAsFileOutput(parameters, 'JSON_OUTPUT', context)

if requires_contacts and not contacts_layer or not contacts_layer.isValid():
raise QgsProcessingException(f"{sorter_name} requires a contacts layer")

units_df, relationships_df, contacts_df= build_input_frames(in_layer,contacts_layer, feedback,parameters)

Expand Down Expand Up @@ -384,26 +389,30 @@ def build_input_frames(layer: QgsVectorLayer,contacts_layer: QgsVectorLayer, fee
if not group_field:
raise QgsProcessingException("Group Field is required")

unique_units = {}
units_records = []
for f in layer.getFeatures():
units_records.append(
dict(
layerId=f.id(),
name=f[unit_name_field],
minAge=qvariantToFloat(f, min_age_field),
maxAge=qvariantToFloat(f, max_age_field),
group=f[group_field],
unit_name = f[unit_name_field]
if unit_name not in unique_units:
unique_units[unit_name] = len(unique_units)
units_records.append(
dict(
layerId=len(units_records),
name=f[unit_name_field],
minAge=qvariantToFloat(f, min_age_field),
maxAge=qvariantToFloat(f, max_age_field),
group=f[group_field],
)
)
)
units_df = pd.DataFrame.from_records(units_records)

feedback.pushInfo(f"Units → {len(units_df)} records")
# map_data can be mocked if you only use Age-based sorter

if not contacts_layer or not contacts_layer.isValid():
raise QgsProcessingException("No contacts layer provided")

contacts_df = qgsLayerToGeoDataFrame(contacts_layer) if contacts_layer else pd.DataFrame()

if not contacts_df.empty:
relationships_df = contacts_df.copy()
if 'length' in contacts_df.columns:
Expand Down