Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added crs converter function and test #297

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions geest/core/crs_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from qgis.core import QgsCoordinateTransform, QgsCoordinateReferenceSystem, QgsProcessingFeedback, QgsVectorLayer
from qgis import processing

class CRSConverter:
def __init__(self, layer):
"""
Initialize the CRSConverter class with a given layer.
:param layer: The input layer for CRS conversion (QgsVectorLayer or QgsRasterLayer)
"""
self.layer = layer

def convert_to_crs(self, target_crs_epsg):
"""
Converts the layer's CRS to the target CRS based on the EPSG code.
:param target_crs_epsg: EPSG code of the target CRS
"""
# Get the current CRS of the layer
current_crs = self.layer.crs()

# Create the target CRS using the EPSG code
target_crs = QgsCoordinateReferenceSystem(f"EPSG:{target_crs_epsg}")

# Check if the current CRS is the same as the target CRS
if current_crs != target_crs:
print(f"Converting layer from {current_crs.authid()} to {target_crs.authid()}")

layer = processing.run(
"native:reprojectlayer",
{
"INPUT": self.layer,
"TARGET_CRS": target_crs,
"OUTPUT": "memory:",
},
feedback=QgsProcessingFeedback(),
)["OUTPUT"]
print(f"Layer successfully converted to {target_crs.authid()}")
return layer
else:
print(f"Layer is already in the target CRS: {target_crs.authid()}")
return self.layer
61 changes: 61 additions & 0 deletions test/test_crs_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import unittest
import os
from qgis.core import QgsVectorLayer, QgsCoordinateReferenceSystem
from geest.core.crs_converter import CRSConverter


class TestCRSConverter(unittest.TestCase):

def setUp(self):
"""
Setup method that runs before each test. It prepares the environment.
"""
# Define paths for test data
self.working_dir = os.path.dirname(__file__)
self.test_data_dir = os.path.join(self.working_dir, "test_data")

# Load a test layer from the test data directory
self.layer = QgsVectorLayer(
os.path.join(self.test_data_dir, "admin/Admin0.shp"), "test_layer", "ogr"
)
self.assertTrue(self.layer.isValid(), "Layer failed to load!")

def test_crs_conversion(self):
"""
Test CRS conversion to a different CRS.
"""
# Create an instance of CRSConverter with the test layer
converter = CRSConverter(self.layer)

# Convert the layer to a different CRS (EPSG:3857 "Web Mercator")
target_epsg = 3857
reprojected_layer = converter.convert_to_crs(target_epsg)

# Get the new CRS of the reprojected layer
new_crs = reprojected_layer.crs()

# Check if the CRS was converted correctly
expected_crs = QgsCoordinateReferenceSystem(target_epsg)
self.assertEqual(
new_crs.authid(), expected_crs.authid(), "CRS conversion failed!"
)

def test_no_conversion_needed(self):
"""
Test if no conversion is performed when the layer is already in the target CRS.
"""
# Create an instance of CRSConverter with the test layer
converter = CRSConverter(self.layer)

# Convert to the same CRS (EPSG:4326)
target_epsg = 4326
reprojected_layer = converter.convert_to_crs(target_epsg)

# Check that the CRS remains the same
self.assertEqual(
reprojected_layer.crs().authid(), "EPSG:4326", "Layer CRS should remain unchanged!"
)


if __name__ == "__main__":
unittest.main()
4 changes: 1 addition & 3 deletions test/test_polygons_per_grid_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ def test_raster_polygon_grid_score(self):
)
self.country_boundary = os.path.join(self.test_data_dir, "admin/Admin0.shp")

self.assertTrue(
self.polygon_layer.isValid(), "The polygon layer is not valid."
)
self.assertTrue(self.polygon_layer.isValid(), "The polygon layer is not valid.")

# Define output path for the generated raster
self.output_path = os.path.join(
Expand Down
Loading