diff --git a/src/qgis_gender_indicator_tool/jobs/rasterization.py b/src/qgis_gender_indicator_tool/jobs/rasterization.py index 6bb5e8c5..c5bbb86e 100644 --- a/src/qgis_gender_indicator_tool/jobs/rasterization.py +++ b/src/qgis_gender_indicator_tool/jobs/rasterization.py @@ -2,8 +2,17 @@ from qgis.core import QgsVectorLayer, QgsProcessingFeedback import processing + class Rasterizer: - def __init__(self, vector_layer_path, output_dir, pixel_size, utm_crs, field=None, dimension="default"): + def __init__( + self, + vector_layer_path, + output_dir, + pixel_size, + utm_crs, + field=None, + dimension="default", + ): """ Initializes the Rasterizer class with relevant parameters. @@ -25,8 +34,10 @@ def __init__(self, vector_layer_path, output_dir, pixel_size, utm_crs, field=Non self.current_script_path = os.path.dirname(os.path.abspath(__file__)) self.temp_dir = os.path.join(self.output_dir, "temp") - self.raster_output_path = os.path.join(self.output_dir, self.dimension, "rasterized_output.tif") - + self.raster_output_path = os.path.join( + self.output_dir, self.dimension, "rasterized_output.tif" + ) + # Create necessary directories self._setup_directories() @@ -41,22 +52,24 @@ def _setup_directories(self): def _load_and_preprocess_vector_layer(self): """Loads and preprocesses the vector layer, including reprojecting if necessary.""" # Load the vector layer - self.vector_layer = QgsVectorLayer(self.vector_layer_path, "vector_layer", "ogr") + self.vector_layer = QgsVectorLayer( + self.vector_layer_path, "vector_layer", "ogr" + ) if not self.vector_layer.isValid(): raise ValueError(f"Invalid vector layer: {self.vector_layer_path}") # Reproject the vector layer if necessary if self.vector_layer.crs() != self.utm_crs: reprojected_result = processing.run( - "native:reprojectlayer", + "native:reprojectlayer", { - 'INPUT': self.vector_layer, - 'TARGET_CRS': self.utm_crs, - 'OUTPUT': 'memory:' - }, - feedback=QgsProcessingFeedback() + "INPUT": self.vector_layer, + "TARGET_CRS": self.utm_crs, + "OUTPUT": "memory:", + }, + feedback=QgsProcessingFeedback(), ) - self.vector_layer = reprojected_result['OUTPUT'] + self.vector_layer = reprojected_result["OUTPUT"] def rasterize_vector_layer(self, nodata_value=-9999, data_type=5): """ @@ -67,26 +80,24 @@ def rasterize_vector_layer(self, nodata_value=-9999, data_type=5): data_type (int): Data type for the raster output (default is Float32). """ rasterize_params = { - 'INPUT': self.vector_layer, - 'FIELD': self.field, # Field to use for rasterization, or None for burn value - 'BURN': None if self.field else 1, # Burn value if no field is provided - 'UNITS': 1, # pixel size is set in units of CRS - 'WIDTH': self.pixel_size, - 'HEIGHT': self.pixel_size, - 'EXTENT': self.vector_layer.extent(), - 'NODATA': nodata_value, - 'DATA_TYPE': data_type, # Data type: Float32 (5) or others - 'OUTPUT': self.raster_output_path + "INPUT": self.vector_layer, + "FIELD": self.field, # Field to use for rasterization, or None for burn value + "BURN": None if self.field else 1, # Burn value if no field is provided + "UNITS": 1, # pixel size is set in units of CRS + "WIDTH": self.pixel_size, + "HEIGHT": self.pixel_size, + "EXTENT": self.vector_layer.extent(), + "NODATA": nodata_value, + "DATA_TYPE": data_type, # Data type: Float32 (5) or others + "OUTPUT": self.raster_output_path, } # Run the rasterization algorithm rasterize_result = processing.run( - "gdal:rasterize", - rasterize_params, - feedback=QgsProcessingFeedback() + "gdal:rasterize", rasterize_params, feedback=QgsProcessingFeedback() ) - self.rasterized_layer = rasterize_result['OUTPUT'] + self.rasterized_layer = rasterize_result["OUTPUT"] if not os.path.exists(self.rasterized_layer): raise ValueError("Rasterization failed. Output file not created.") diff --git a/test/test_rasterizer.py b/test/test_rasterizer.py index 11c0d736..4068965f 100644 --- a/test/test_rasterizer.py +++ b/test/test_rasterizer.py @@ -25,7 +25,7 @@ def tearDownClass(cls): def setUp(self): # Setup real parameters for the Rasterizer class - self.vector_layer_path = "/data/admin/Admin0.shp" # Use a real shapefile path + self.vector_layer_path = "data/admin/Admin0.shp" # Use a real shapefile path self.output_dir = "/output" self.pixel_size = 100 self.utm_crs = QgsCoordinateReferenceSystem("EPSG:32620") # UTM Zone 20N