Coverage for C: \ Users \ peaco \ OneDrive \ Documents \ GitHub \ mt_metadata \ mt_metadata \ transfer_functions \ io \ zonge \ metadata \ gps.py: 100%

26 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-10 00:11 -0800

1# ===================================================== 

2# Imports 

3# ===================================================== 

4from typing import Annotated 

5 

6from pydantic import Field, field_validator, ValidationInfo 

7from pyproj import CRS 

8 

9from mt_metadata.base import MetadataBase 

10from mt_metadata.utils import location_helpers 

11 

12 

13# ===================================================== 

14 

15 

16class GPS(MetadataBase): 

17 lat: Annotated[ 

18 float, 

19 Field( 

20 default=0.0, 

21 description="latitude", 

22 alias=None, 

23 json_schema_extra={ 

24 "units": None, 

25 "required": True, 

26 "examples": ["10.3"], 

27 }, 

28 ), 

29 ] 

30 

31 lon: Annotated[ 

32 float, 

33 Field( 

34 default=0.0, 

35 description="longitude", 

36 alias=None, 

37 json_schema_extra={ 

38 "units": None, 

39 "required": True, 

40 "examples": ["10.3"], 

41 }, 

42 ), 

43 ] 

44 

45 datum: Annotated[ 

46 str, 

47 Field( 

48 default="WGS84", 

49 description="Datum of the location", 

50 alias=None, 

51 json_schema_extra={ 

52 "units": None, 

53 "required": True, 

54 "examples": ["WGS84"], 

55 }, 

56 ), 

57 ] 

58 

59 u_t_m_zone: Annotated[ 

60 int, 

61 Field( 

62 default=0, 

63 description="UTM zone of location", 

64 alias=None, 

65 json_schema_extra={ 

66 "units": None, 

67 "required": True, 

68 "examples": ["12"], 

69 }, 

70 ), 

71 ] 

72 

73 @field_validator("datum", mode="before") 

74 @classmethod 

75 def validate_datum(cls, value: str | int) -> str: 

76 """ 

77 Validate the datum value and convert it to the appropriate enum type. 

78 """ 

79 try: 

80 datum_crs = CRS.from_user_input(value) 

81 return datum_crs.name 

82 except Exception: 

83 raise ValueError( 

84 f"Invalid datum value: {value}. Must be a valid CRS string or identifier." 

85 ) 

86 

87 @field_validator("lat", "lon", mode="before") 

88 @classmethod 

89 def validate_position(cls, value, info: ValidationInfo): 

90 if info.field_name in ["lat"]: 

91 field_name = "latitude" 

92 elif info.field_name in ["lon"]: 

93 field_name = "longitude" 

94 return location_helpers.validate_position(value, field_name)