diff --git a/src/qgis_gender_indicator_tool/jobs/create_grids.py b/src/qgis_gender_indicator_tool/jobs/create_grids.py index 24d4902b..8c782b8b 100644 --- a/src/qgis_gender_indicator_tool/jobs/create_grids.py +++ b/src/qgis_gender_indicator_tool/jobs/create_grids.py @@ -81,6 +81,17 @@ def create_grids(self, layer, output_dir, crs, merged_output_path): grid_layer = QgsVectorLayer( grid_output_path, "grid_layer", "ogr" ) # Load the existing grid layer + # Clip the grid to the polygon feature (to restrict it to the boundaries) + clipped_grid_output_path = ( + f"{output_dir}/clipped_grid_{feature.id()}_part_{part_id}.gpkg" + ) + clip_params = { + "INPUT": grid_layer, # The grid we just created + "OVERLAY": layer, # The layer we're clipping to + "OUTPUT": clipped_grid_output_path, + } + clip_result = processing.run("native:clip", clip_params) + grid_layer = clip_result["OUTPUT"] # The clipped grid else: print(f"Creating grid: {grid_output_path}") # Define grid creation parameters @@ -116,12 +127,5 @@ def create_grids(self, layer, output_dir, crs, merged_output_path): print(f"Merging grids into: {merged_output_path}") merge_params = {"LAYERS": all_grids, "CRS": crs, "OUTPUT": merged_output_path} merged_grid = processing.run("native:mergevectorlayers", merge_params)["OUTPUT"] - lock_files = [ - f"{merged_output_path}-journal", - f"{merged_output_path}-wal", - f"{merged_output_path}-shm", - ] - for lock_file in lock_files: - if os.path.exists(lock_file): - os.remove(lock_file) + return merged_grid diff --git a/src/qgis_gender_indicator_tool/jobs/points_per_grid_cell.py b/src/qgis_gender_indicator_tool/jobs/points_per_grid_cell.py index e0bdd13e..589dc2b1 100644 --- a/src/qgis_gender_indicator_tool/jobs/points_per_grid_cell.py +++ b/src/qgis_gender_indicator_tool/jobs/points_per_grid_cell.py @@ -11,7 +11,7 @@ from .extents import Extents -class RasterFromScore: +class RasterPointGridScore: def __init__(self, country_boundary, pixel_size, output_path, crs, input_points): self.country_boundary = country_boundary self.pixel_size = pixel_size @@ -19,7 +19,7 @@ def __init__(self, country_boundary, pixel_size, output_path, crs, input_points) self.crs = crs self.input_points = input_points - def raster_from_score(self): + def raster_point_grid_score(self): """ Generates a raster based on the number of input points within each grid cell. :param country_boundary: Layer defining the country boundary to clip the grid. @@ -89,6 +89,13 @@ def raster_from_score(self): ) grid_layer.commitChanges() + Merge = processing.run( + "native:mergevectorlayers", + {"LAYERS": [grid_layer], "CRS": None, "OUTPUT": "memory:"}, + ) + + merge = Merge["OUTPUT"] + extents_processor = Extents( output_dir, self.country_boundary, self.pixel_size, self.crs ) @@ -104,7 +111,7 @@ def raster_from_score(self): # Rasterize the clipped grid layer to generate the raster rasterize_params = { - "INPUT": grid_layer, + "INPUT": merge, "FIELD": field_name, "BURN": 0, "USE_Z": False, diff --git a/test/test_points_per_grid_cell.py b/test/test_points_per_grid_cell.py index f4955905..3631f5b4 100644 --- a/test/test_points_per_grid_cell.py +++ b/test/test_points_per_grid_cell.py @@ -2,21 +2,20 @@ import os from qgis.core import ( QgsVectorLayer, + QgsRasterLayer, QgsCoordinateReferenceSystem, - QgsProject, - QgsRectangle, ) from qgis_gender_indicator_tool.jobs.points_per_grid_cell import ( - RasterFromScore, + RasterPointGridScore, ) # Adjust the path to your class -class TestRasterFromScore(unittest.TestCase): - """Test the RasterFromScore class.""" +class TestRasterPointGridScore(unittest.TestCase): + """Test the RasterPointGridScore class.""" - def test_raster_from_score(self): + def test_raster_point_grid_score(self): """ - Test raster generation using the RasterFromScore class. + Test raster generation using the RasterPointGridScore class. """ self.working_dir = os.path.dirname(__file__) self.test_data_dir = os.path.join(self.working_dir, "test_data") @@ -39,8 +38,8 @@ def test_raster_from_score(self): self.crs = QgsCoordinateReferenceSystem("EPSG:32620") self.pixel_size = 100 # 100m grid - # Create an instance of the RasterFromScore class - rasterizer = RasterFromScore( + # Create an instance of the RasterPointGridScore class + rasterizer = RasterPointGridScore( country_boundary=self.country_boundary, pixel_size=self.pixel_size, output_path=self.output_path, @@ -48,15 +47,15 @@ def test_raster_from_score(self): input_points=self.point_layer, ) - # Run the raster_from_score method - rasterizer.raster_from_score() + # Run the raster_point_grid_score method + rasterizer.raster_point_grid_score() # Load the generated raster layer to verify its validity # Verify that the raster file was created self.assertTrue( os.path.exists(self.output_path), "The raster output file was not created." ) - raster_layer = QgsVectorLayer(self.output_path, "test_raster", "gdal") + raster_layer = QgsRasterLayer(self.output_path, "test_raster", "gdal") self.assertTrue( raster_layer.isValid(), "The generated raster layer is not valid." )