Skip to content

Commit

Permalink
fix: fix for points per grid score(error reading grid)
Browse files Browse the repository at this point in the history
  • Loading branch information
osundwajeff committed Sep 19, 2024
1 parent 920f7e5 commit 4f2b106
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
20 changes: 12 additions & 8 deletions src/qgis_gender_indicator_tool/jobs/create_grids.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
13 changes: 10 additions & 3 deletions src/qgis_gender_indicator_tool/jobs/points_per_grid_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
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
self.output_path = output_path
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.
Expand Down Expand Up @@ -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
)
Expand All @@ -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,
Expand Down
23 changes: 11 additions & 12 deletions test/test_points_per_grid_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -39,24 +38,24 @@ 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,
crs=self.crs,
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."
)
Expand Down

0 comments on commit 4f2b106

Please sign in to comment.