2929from typing import Union
3030import shutil
3131
32- if Qgis .QGIS_VERSION_INT < 33200 :
33- import tempfile
34- QGIS_VERSION = Qgis .QGIS_VERSION_INT
35-
3632def get_shapefile_as_json_pyqgis (layer , logger = None ):
3733 if logger is not None :
3834 logger .debug ("@get_shapefile_as_json_pyqgis@: ShapeFN id: {}" .format (layer .id ()))
@@ -95,13 +91,8 @@ def process_raster_for_impactmap(source_excavation_poly, dtb_raster_layer, clipp
9591 Returns:
9692 - Path: Path object of the processed raster in TIFF format.
9793 """
98- # Check if the running version of QGIS is lower than the requirement
99- if QGIS_VERSION < 33200 :
100- # Create a temporary directory using Python's tempfile module
101- temp_dir = tempfile .mkdtemp ()
102- temp_folder = Path (temp_dir )
103- else :
104- temp_folder = Path (QgsProcessingContext .temporaryFolder (context ) if context else QgsProcessingUtils .tempFolder ())
94+ # Check if the running version of QGIS is lower than the requirement, and create temp_folder based on that
95+ temp_folder = create_temp_folder_for_version (Qgis .QGIS_VERSION_INT , context if context else None )
10596
10697 # Prepare processing context and feedback
10798 if not context :
@@ -268,13 +259,8 @@ def reproject_layers(keep_interm_layer: bool,
268259 Returns:
269260 - Tuple: (reprojected_vector_path, reprojected_raster_path) Paths to the reprojected layers.
270261 """
271- # Check if the running version of QGIS is lower than the requirement
272- if QGIS_VERSION < 33200 :
273- # Create a temporary directory using Python's tempfile module
274- temp_dir = tempfile .mkdtemp ()
275- temp_folder = Path (temp_dir )
276- else :
277- temp_folder = Path (QgsProcessingContext .temporaryFolder (context ) if context else QgsProcessingUtils .tempFolder ())
262+ # Check if the running version of QGIS is lower than the requirement, and create temp_folder based on that
263+ temp_folder = create_temp_folder_for_version (Qgis .QGIS_VERSION_INT , context if context else None )
278264
279265 # Prepare processing context and feedback
280266 if not context :
@@ -357,4 +343,24 @@ def move_file_components(original_file_path: Path, destination_file_path: Path):
357343 src_file = original_file_path .with_suffix (ext )
358344 dest_file = destination_folder / (destination_base_name + ext )
359345 if src_file .exists ():
360- shutil .move (str (src_file ), str (dest_file ))
346+ shutil .move (str (src_file ), str (dest_file ))
347+
348+ def create_temp_folder_for_version (qgis_version_int : int , context : QgsProcessingContext = None ) -> Path :
349+ """
350+ Creates a temporary folder based on the QGIS version.
351+
352+ Args:
353+ qgis_version_int (int): The integer representation of the QGIS version.
354+ context (QgsProcessingContext, optional): The context for processing. Default is None.
355+
356+ Returns:
357+ Path: The path to the temporary folder.
358+ """
359+ # Check if the running version of QGIS is lower than the requirement
360+ if qgis_version_int >= 33200 and context is not None :
361+ # For QGIS versions 3.32.0 and above
362+ temp_folder = context .temporaryFolder ()
363+ else :
364+ # For older versions, or if no context is provided, use the global Processing temporary folder
365+ temp_folder = QgsProcessingUtils .tempFolder ()
366+ return Path (temp_folder )
0 commit comments