Skip to content

Commit

Permalink
Merge pull request #444 from nens/eli-fix-epsg-ref-code-attributes
Browse files Browse the repository at this point in the history
fix epsg ref code attributes
  • Loading branch information
elisalle authored Feb 27, 2025
2 parents 80dee3b + a1a2b1d commit aedebe8
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 23 deletions.
8 changes: 7 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
Changelog of threedi-modelchecker
=================================

2.17.6 (unreleased)
2.17.7 (unreleased)
-------------------

- Nothing changed yet.


2.17.6.dev0 (2025-02-27)
------------------------

- Store epsg name and code in context instead of session.


2.17.5 (2025-02-18)
-------------------

Expand Down
2 changes: 1 addition & 1 deletion threedi_modelchecker/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .model_checks import * # NOQA

# fmt: off
__version__ = '2.17.6.dev0'
__version__ = '2.17.7.dev0'
# fmt: on
6 changes: 3 additions & 3 deletions threedi_modelchecker/checks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,12 +425,12 @@ def __init__(
self.epsg_ref_name = ""

def get_invalid(self, session: Session) -> List[NamedTuple]:
self.epsg_ref_name = session.epsg_ref_name
if session.epsg_ref_code is None:
self.epsg_ref_name = session.model_checker_context.epsg_ref_name
if session.model_checker_context.epsg_ref_code is None:
return []
return (
self.to_check(session)
.filter(ST_SRID(self.column) != session.epsg_ref_code)
.filter(ST_SRID(self.column) != session.model_checker_context.epsg_ref_code)
.all()
)

Expand Down
6 changes: 3 additions & 3 deletions threedi_modelchecker/checks/other.py
Original file line number Diff line number Diff line change
Expand Up @@ -1129,7 +1129,7 @@ def __init__(self, *args, **kwargs):
self.epsg_code = None

def get_invalid(self, session: Session) -> List[NamedTuple]:
self.epsg_code = session.epsg_ref_code
self.epsg_code = session.model_checker_context.epsg_ref_code
if self.epsg_code is not None:
try:
pyproj.CRS.from_epsg(self.epsg_code)
Expand All @@ -1147,7 +1147,7 @@ def __init__(self, *args, **kwargs):
self.epsg_code = None

def get_invalid(self, session: Session) -> List[NamedTuple]:
self.epsg_code = session.epsg_ref_code
self.epsg_code = session.model_checker_context.epsg_ref_code
if self.epsg_code is not None:
try:
crs = pyproj.CRS.from_epsg(self.epsg_code)
Expand All @@ -1168,7 +1168,7 @@ def __init__(self, *args, **kwargs):
self.epsg_code = None

def get_invalid(self, session: Session) -> List[NamedTuple]:
self.epsg_code = session.epsg_ref_code
self.epsg_code = session.model_checker_context.epsg_ref_code
if self.epsg_code is not None:
try:
crs = pyproj.CRS.from_epsg(self.epsg_code)
Expand Down
10 changes: 7 additions & 3 deletions threedi_modelchecker/checks/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ class Context:
class ServerContext(Context):
available_rasters: Dict[str, str]
raster_interface: Type[RasterInterface] = GDALRasterInterface
epsg_ref_code: int = None
epsg_ref_name: str = ""


@dataclass
class LocalContext(Context):
base_path: Path
raster_interface: Type[RasterInterface] = GDALRasterInterface
epsg_ref_code: int = None
epsg_ref_name: str = ""


class BaseRasterCheck(BaseCheck):
Expand Down Expand Up @@ -154,10 +158,10 @@ def __init__(
self.epsg_ref_code = None

def get_invalid(self, session):
if session.epsg_ref_code is None:
if session.model_checker_context.epsg_ref_code is None:
return []
self.epsg_ref_name = session.epsg_ref_name
self.epsg_ref_code = session.epsg_ref_code
self.epsg_ref_name = session.model_checker_context.epsg_ref_name
self.epsg_ref_code = session.model_checker_context.epsg_ref_code
return super().get_invalid(session)

def is_valid(self, path: str, interface_cls: Type[RasterInterface]):
Expand Down
19 changes: 12 additions & 7 deletions threedi_modelchecker/model_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ def __init__(
)
context = {} if context is None else context.copy()
context_type = context.pop("context_type", "local")
session = self.db.get_session()
if self.db.schema.epsg_code is not None:
context["epsg_ref_code"] = self.db.schema.epsg_code
context["epsg_ref_name"] = self.db.schema.epsg_source
else:
epsg_ref_code, epsg_ref_name = get_epsg_data_from_raster(session)
context["epsg_ref_code"] = epsg_ref_code
context["epsg_ref_name"] = epsg_ref_name

if context_type == "local":
context.setdefault("base_path", self.db.base_path)
self.context = LocalContext(**context)
Expand All @@ -68,6 +77,8 @@ def __init__(
else:
raise ValueError(f"Unknown context_type '{context_type}'")

session.model_checker_context = self.context

@property
def models(self):
"""Returns a list of declared models"""
Expand All @@ -82,15 +93,9 @@ def errors(
:return: Tuple of the applied check and the failing row.
"""

session = self.db.get_session()
session.model_checker_context = self.context
if self.db.schema.epsg_code is not None:
session.epsg_ref_code = self.db.schema.epsg_code
session.epsg_ref_name = self.db.schema.epsg_source
else:
epsg_ref_code, epsg_ref_name = get_epsg_data_from_raster(session)
session.epsg_ref_code = epsg_ref_code
session.epsg_ref_name = epsg_ref_name
for check in self.checks(level=level, ignore_checks=ignore_checks):
model_errors = check.get_invalid(session)
for error_row in model_errors:
Expand Down
4 changes: 2 additions & 2 deletions threedi_modelchecker/tests/test_checks_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,8 +603,8 @@ def test_list_of_inst_check(session, tag_ids_string, nof_invalid_expected):
)
def test_epsg_geom_check(session, ref_epsg, valid):
factories.ConnectionNodeFactory()
session.epsg_ref_code = ref_epsg
session.epsg_ref_name = "foo"
session.model_checker_context.epsg_ref_code = ref_epsg
session.model_checker_context.epsg_ref_name = "foo"
check = EPSGGeomCheck(column=models.ConnectionNode.geom)
invalids = check.get_invalid(session)
assert (len(invalids) == 0) == valid
6 changes: 3 additions & 3 deletions threedi_modelchecker/tests/test_checks_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ def test_dwf_distribution_sum_check(session, distribution, valid):
)
def test_model_epsg_check_valid(session, ref_epsg, valid):
factories.ModelSettingsFactory()
session.epsg_ref_code = ref_epsg
session.model_checker_context.epsg_ref_code = ref_epsg
check = ModelEPSGCheckValid()
invalids = check.get_invalid(session)
assert (len(invalids) == 0) == valid
Expand All @@ -953,7 +953,7 @@ def test_model_epsg_check_valid(session, ref_epsg, valid):
)
def test_model_epsg_check_projected(session, ref_epsg, valid):
factories.ModelSettingsFactory()
session.epsg_ref_code = ref_epsg
session.model_checker_context.epsg_ref_code = ref_epsg
check = ModelEPSGCheckProjected()
invalids = check.get_invalid(session)
assert (len(invalids) == 0) == valid
Expand All @@ -969,7 +969,7 @@ def test_model_epsg_check_projected(session, ref_epsg, valid):
)
def test_model_epsg_check_units(session, ref_epsg, valid):
factories.ModelSettingsFactory()
session.epsg_ref_code = ref_epsg
session.model_checker_context.epsg_ref_code = ref_epsg
check = ModelEPSGCheckUnits()
invalids = check.get_invalid(session)
assert (len(invalids) == 0) == valid

0 comments on commit aedebe8

Please sign in to comment.