Skip to content

Commit

Permalink
Do not use CRS operations before initializing QgsApplication [#56]
Browse files Browse the repository at this point in the history
  • Loading branch information
Joonalai committed Apr 26, 2024
1 parent d5227a4 commit 35f3255
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 25 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ ban-relative-imports = "all"
[tool.ruff.per-file-ignores]
"src/pytest_qgis/pytest_qgis.py"=["PLR2004"] # TODO: Fix magic values. Remove this after.
"src/pytest_qgis/qgis_interface.py" = ["N802", "N803"]
"src/pytest_qgis/utils.py" = ["ANN401"]
"tests/*" = [
"ANN001",
"ANN201",
Expand Down
6 changes: 3 additions & 3 deletions src/pytest_qgis/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

DEFAULT_RASTER_FORMAT = "tif"

DEFAULT_CRS = QgsCoordinateReferenceSystem("EPSG:4326")
DEFAULT_EPSG = "EPSG:4326"
LAYER_KEYWORDS = ("layer", "lyr", "raster", "rast", "tif")


Expand Down Expand Up @@ -73,7 +73,7 @@ def set_map_crs_based_on_layers() -> None:
crs_id, _ = crs_counter.most_common(1)[0]
crs = QgsCoordinateReferenceSystem(crs_id)
else:
crs = DEFAULT_CRS
crs = QgsCoordinateReferenceSystem(DEFAULT_EPSG)
QgsProject.instance().setCrs(crs)


Expand Down Expand Up @@ -205,7 +205,7 @@ def ensure_qgis_layer_fixtures_are_cleaned(request: "FixtureRequest") -> None:
_set_layer_owner_to_project(layer)


def _set_layer_owner_to_project(layer: Any) -> None: # noqa: ANN401
def _set_layer_owner_to_project(layer: Any) -> None:
if (
isinstance(layer, QgsMapLayer)
and not isinstance(layer, MagicMock)
Expand Down
19 changes: 4 additions & 15 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
from pathlib import Path

import pytest
from qgis.core import QgsCoordinateReferenceSystem, QgsRasterLayer, QgsVectorLayer

from tests.utils import CRS_3067, DEFAULT_CRS
from qgis.core import QgsRasterLayer, QgsVectorLayer

pytest_plugins = "pytester"

Expand Down Expand Up @@ -65,15 +63,14 @@ def layer_polygon_session(gpkg_session: Path):

@pytest.fixture()
def layer_polygon_3067(gpkg: Path):
return get_gpkg_layer("polygon_3067", gpkg, CRS_3067)
return get_gpkg_layer("polygon_3067", gpkg)


@pytest.fixture()
def raster_3067():
return get_raster_layer(
"small raster 3067",
Path(Path(__file__).parent, "data", "small_raster.tif"),
CRS_3067,
)


Expand All @@ -89,23 +86,15 @@ def get_copied_gpkg(tmp_path: Path) -> Path:
return new_db_path


def get_gpkg_layer(
name: str, gpkg: Path, crs: QgsCoordinateReferenceSystem = DEFAULT_CRS
) -> QgsVectorLayer:
def get_gpkg_layer(name: str, gpkg: Path) -> QgsVectorLayer:
layer = QgsVectorLayer(f"{gpkg!s}|layername={name}", name, "ogr")
layer.setProviderEncoding("utf-8")
assert layer.isValid()
if not layer.crs().isValid():
layer.setCrs(crs)
assert layer.crs().isValid()
return layer


def get_raster_layer(
name: str, path: Path, crs: QgsCoordinateReferenceSystem = DEFAULT_CRS
) -> QgsRasterLayer:
def get_raster_layer(name: str, path: Path) -> QgsRasterLayer:
layer = QgsRasterLayer(str(path), name)
assert layer.isValid()
if not layer.crs().isValid():
layer.setCrs(crs)
return layer
26 changes: 26 additions & 0 deletions tests/test_crs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import unittest
from pathlib import Path

from qgis.core import (
QgsCoordinateReferenceSystem,
QgsRasterLayer,
)


class TestCrs(unittest.TestCase):
"""
These tests make sure CRS works as intended when pytest-qgis is active
"""

def test_crs_copying(self):
crs1 = QgsCoordinateReferenceSystem("EPSG:4326")
assert crs1.isValid()
crs2 = QgsCoordinateReferenceSystem.fromWkt(crs1.toWkt())
assert crs1.toWkt() == crs2.toWkt()
assert crs2.isValid() # <-- raises error

def test_crs_raster(self):
raster_path = Path(Path(__file__).parent, "data", "small_raster.tif")
layer = QgsRasterLayer(raster_path.as_posix())
assert layer.crs().toWkt()
assert layer.crs().isValid()
6 changes: 3 additions & 3 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
replace_layers_with_reprojected_clones,
set_map_crs_based_on_layers,
)
from qgis.core import QgsProject
from qgis.core import QgsCoordinateReferenceSystem, QgsProject

from tests.utils import DEFAULT_CRS, EPSG_3067, EPSG_4326, QGIS_VERSION
from tests.utils import EPSG_3067, EPSG_4326, QGIS_VERSION

QGIS_3_12 = 31200


@pytest.fixture()
def crs():
QgsProject.instance().setCrs(DEFAULT_CRS)
QgsProject.instance().setCrs(QgsCoordinateReferenceSystem(EPSG_4326))


@pytest.fixture()
Expand Down
5 changes: 1 addition & 4 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#
import os

from qgis.core import Qgis, QgsCoordinateReferenceSystem
from qgis.core import Qgis

try:
QGIS_VERSION = Qgis.versionInt()
Expand All @@ -29,6 +29,3 @@

EPSG_4326 = "EPSG:4326"
EPSG_3067 = "EPSG:3067"

DEFAULT_CRS = QgsCoordinateReferenceSystem(EPSG_4326)
CRS_3067 = QgsCoordinateReferenceSystem(EPSG_3067)

0 comments on commit 35f3255

Please sign in to comment.