Skip to content

Commit

Permalink
change geometry warning to error (like in DAVIE)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidvlaminck committed Dec 7, 2024
1 parent 2b9963f commit ca1abe4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
13 changes: 7 additions & 6 deletions UnitTests/GATests/test_WKTPlusGeoType.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from otlmow_model.OtlmowModel.BaseClasses.OTLAsset import OTLAsset
from otlmow_model.OtlmowModel.Exceptions.WrongGeometryTypeError import WrongGeometryTypeError
from otlmow_model.OtlmowModel.Exceptions.WrongGeometryWarning import WrongGeometryWarning
from otlmow_model.OtlmowModel.GeometrieTypes.PuntGeometrie import PuntGeometrie
from otlmow_model.OtlmowModel.GeometrieTypes.VlakGeometrie import VlakGeometrie
Expand Down Expand Up @@ -28,7 +29,7 @@ def test_point(subtests):
assert puntclass.geometry is not None

with subtests.test(msg='invalid points'):
with pytest.warns(WrongGeometryWarning):
with pytest.raises(WrongGeometryTypeError):
puntclass.geometry = 'POINT (200001 200002)'
with pytest.raises(ValueError):
puntclass.geometry = 'POINT Z (200001 200002,0 3)'
Expand All @@ -52,28 +53,28 @@ def test_point_polygon(subtests):
assert puntvlakclass.geometry is not None

with subtests.test(msg='invalid points'):
with pytest.warns(WrongGeometryWarning):
with pytest.raises(WrongGeometryTypeError):
puntvlakclass.geometry = 'POINT (200001 220000)'
with pytest.raises(ValueError):
puntvlakclass.geometry = 'POINT Z (200001 200002,0 3)'

with subtests.test(msg='invalid points (outside box)'):
with pytest.warns(WrongGeometryWarning):
with pytest.raises(WrongGeometryTypeError):
puntvlakclass.geometry = 'POINT (200001 200002)'
with pytest.raises(ValueError):
puntvlakclass.geometry = 'POINT Z (1 2,0 3)'

with subtests.test(msg='invalid polygons'):
with pytest.warns(WrongGeometryWarning):
with pytest.raises(WrongGeometryTypeError):
puntvlakclass.geometry = 'POLYGON ((200010 200020, 200030 200040, 200050 200060))'
with pytest.raises(ValueError):
puntvlakclass.geometry = 'POLYGON Z ((200010.0 200020.0, 200030.0 200040.0 2, 200050.0 200060.0))'


def test_invalid_geometry_based_on_geometry_artefact():
instance = PointPolygonTestClass()
with pytest.warns(WrongGeometryWarning) as wrong_geometry_warning:
with pytest.raises(WrongGeometryTypeError) as wrong_geometry_exception:
instance.geometry = 'LINESTRING Z (200001 200002 10, 200003 200004 20)'
expected_msg = "Asset type PointPolygonTestClass shouldn't be assigned a LINESTRING Z as geometry, " \
"valid types are POINT Z and POLYGON Z"
assert expected_msg == wrong_geometry_warning[0].message.args[0]
assert expected_msg == wrong_geometry_exception.value.args[0]
11 changes: 5 additions & 6 deletions otlmow_model/OtlmowModel/BaseClasses/WKTField.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import warnings
from typing import Any

from otlmow_model.OtlmowModel.BaseClasses.OTLField import OTLField
from otlmow_model.OtlmowModel.BaseClasses.StringField import StringField
from otlmow_model.OtlmowModel.BaseClasses.WKTValidator import WKTValidator
from otlmow_model.OtlmowModel.Exceptions.WrongGeometryWarning import WrongGeometryWarning
from otlmow_model.OtlmowModel.Exceptions.WrongGeometryTypeError import WrongGeometryTypeError


class WKTField(StringField):
Expand All @@ -17,9 +16,9 @@ class WKTField(StringField):

@classmethod
def convert_to_correct_type(cls, value: str, log_warnings: bool = True) -> str:
value = (value.replace(' Z(', ' Z (').replace('T(', 'T (')
.replace('G(', 'G (').replace('N(', 'N ('))
return value
return (value.replace(' Z(', ' Z (').replace('T(', 'T (')
.replace('G(', 'G (').replace('N(', 'N ('))


@classmethod
def validate(cls, value: Any, attribuut) -> bool:
Expand All @@ -34,7 +33,7 @@ def validate(cls, value: Any, attribuut) -> bool:
verkorte_uri = attribuut.owner.typeURI.split('#')[1]
error_msg = f"Asset type {verkorte_uri} shouldn't be assigned a {geo_type} as geometry, " \
f"valid types are {expected_types}"
warnings.warn(message=error_msg, category=WrongGeometryWarning)
raise WrongGeometryTypeError(error_msg)
return True

def __str__(self) -> str:
Expand Down
2 changes: 2 additions & 0 deletions otlmow_model/OtlmowModel/Exceptions/WrongGeometryTypeError.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class WrongGeometryTypeError(Exception):
pass
7 changes: 6 additions & 1 deletion otlmow_model/OtlmowModel/Exceptions/WrongGeometryWarning.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
import warnings


class WrongGeometryWarning(Warning):
pass
def __init__(self, message: str = ''):
self.message = message
warnings.warn('WrongGeometryWarning is now deprecated and replaced by an error', DeprecationWarning)

0 comments on commit ca1abe4

Please sign in to comment.