Skip to content

Commit 569f39b

Browse files
committed
add json file output for sorter
1 parent 170c1b8 commit 569f39b

1 file changed

Lines changed: 32 additions & 15 deletions

File tree

m2l/processing/algorithms/sorter.py

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Any, Optional
22
from osgeo import gdal
33
import pandas as pd
4+
import json
45

56
from PyQt5.QtCore import QMetaType
67
from qgis import processing
@@ -17,6 +18,7 @@
1718
QgsProcessingException,
1819
QgsProcessingFeedback,
1920
QgsProcessingParameterEnum,
21+
QgsProcessingParameterFileDestination,
2022
QgsProcessingParameterFeatureSink,
2123
QgsProcessingParameterFeatureSource,
2224
QgsProcessingParameterField,
@@ -255,6 +257,14 @@ def initAlgorithm(self, config: Optional[dict[str, Any]] = None) -> None:
255257
)
256258
)
257259

260+
self.addParameter(
261+
QgsProcessingParameterFileDestination(
262+
"JSON_OUTPUT",
263+
"Stratigraphic column json",
264+
fileFilter="JSON files (*.json)"
265+
)
266+
)
267+
258268
# ----------------------------------------------------------
259269
# Core
260270
# ----------------------------------------------------------
@@ -270,6 +280,7 @@ def processAlgorithm(
270280
sorter_cls = list(SORTER_LIST.values())[algo_index]
271281
contacts_layer = self.parameterAsVectorLayer(parameters, self.CONTACTS_LAYER, context)
272282
in_layer = self.parameterAsVectorLayer(parameters, self.INPUT_GEOLOGY, context)
283+
output_file = self.parameterAsFileOutput(parameters, 'JSON_OUTPUT', context)
273284

274285
if method == 0: # User-Defined
275286
strati_column_matrix = self.parameterAsMatrix(parameters, self.INPUT_STRATI_COLUMN, context)
@@ -317,20 +328,20 @@ def processAlgorithm(
317328
)
318329
elif orientation_type_name == 'Dip Direction':
319330
structure_gdf = structure_gdf.rename(columns={dipdir_field: 'DIPDIR'})
331+
order = sorter_cls().sort(
332+
units_df,
333+
relationships_df,
334+
contacts_df,
335+
geology_gdf,
336+
structure_gdf,
337+
dtm_gdal
338+
)
320339
else:
321-
geology_gdf = None
322-
structure_gdf = None
323-
dtm_gdal = None
324-
325-
sorter = sorter_cls()
326-
order = sorter.sort(
327-
units_df,
328-
relationships_df,
329-
contacts_df,
330-
geology_gdf,
331-
structure_gdf,
332-
dtm_gdal
333-
)
340+
order = sorter_cls().sort(
341+
units_df,
342+
relationships_df,
343+
contacts_df
344+
)
334345

335346
# 4 ► write an in-memory table with the result
336347
sink_fields = QgsFields()
@@ -350,8 +361,14 @@ def processAlgorithm(
350361
f = QgsFeature(sink_fields)
351362
f.setAttributes([pos, name])
352363
sink.addFeature(f, QgsFeatureSink.FastInsert)
353-
354-
return {self.OUTPUT: dest_id}
364+
try:
365+
with open(output_file, 'w') as f:
366+
json.dump(order, f)
367+
except Exception as e:
368+
with open(output_file, 'w') as f:
369+
json.dump([], f)
370+
371+
return {self.OUTPUT: dest_id, 'JSON_OUTPUT': output_file}
355372

356373
# ----------------------------------------------------------
357374
def createInstance(self) -> QgsProcessingAlgorithm:

0 commit comments

Comments
 (0)