Skip to content

Commit

Permalink
format and vectory layer path
Browse files Browse the repository at this point in the history
  • Loading branch information
osundwajeff committed Sep 12, 2024
1 parent 286c64c commit f16a265
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
61 changes: 36 additions & 25 deletions src/qgis_gender_indicator_tool/jobs/rasterization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()

Expand All @@ -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):
"""
Expand All @@ -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.")

Expand Down
2 changes: 1 addition & 1 deletion test/test_rasterizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f16a265

Please sign in to comment.