Fix bugs, make SPC database bindings work

This commit is contained in:
XANTRONIX 2025-03-15 23:04:11 -04:00
parent 5b09570b70
commit 547545a665

View file

@ -3,9 +3,9 @@ import enum
import shapely import shapely
import datetime import datetime
from typing import Self from xmet.db import DatabaseTable
from xmet.coord import COORD_SYSTEM
from xmet.afos import MONTHS, TIMEZONES from xmet.afos import MONTHS, TIMEZONES
RE_HEADER = re.compile(r''' RE_HEADER = re.compile(r'''
^DAY ^DAY
@ -71,26 +71,54 @@ def parse_coord(coord: str) -> tuple[float, float]:
def parse_poly(points: list[str]) -> shapely.Polygon: def parse_poly(points: list[str]) -> shapely.Polygon:
return shapely.Polygon([parse_coord(p) for p in points]) return shapely.Polygon([parse_coord(p) for p in points])
class SPCOutlookArea(): class SPCOutlookArea(DatabaseTable):
__slots__ = ('id', 'outlook_id', 'poly') __slots__ = ('id', 'outlook_id', 'poly')
__columns_read__ = {
'poly': 'ST_AsText(poly) as poly'
}
__values_read__ = {
'poly': shapely.from_wkt
}
__columns_write__ = {
'poly': 'ST_GeomFromText(:poly, {crs})'.format(crs=COORD_SYSTEM)
}
__values_write__ = {
'poly': lambda v: {'poly': shapely.to_wkt(v)}
}
def __init__(self):
super().__init__()
self.id = None
self.outlook_id = None
self.poly = None
class SPCOutlookProbabilityArea(SPCOutlookArea): class SPCOutlookProbabilityArea(SPCOutlookArea):
__slots__ = ( __slots__ = (
'hazard', 'probability', 'hazard', 'probability',
) )
__table__ = 'xmet_spc_outlook_probability_area'
__key__ = 'id'
__columns__ = (
'id', 'outlook_id', 'hazard', 'probability', 'poly'
)
class SPCOutlookCategoryArea(SPCOutlookArea): class SPCOutlookCategoryArea(SPCOutlookArea):
__slots__ = ( __slots__ = (
'category' 'category'
) )
class SPCOutlookParserState(enum.Enum): __table__ = 'xmet_spc_outlook_category_area'
HEADER = 1 __key__ = 'id'
OFFICE = enum.auto()
ISSUANCE = enum.auto() __columns__ = (
VALIDITY = enum.auto() 'id', 'outlook_id', 'category', 'poly'
AREA_THREAT = enum.auto() )
BODY = enum.auto()
class SPCOutlook(): class SPCOutlook():
__slots__ = ( __slots__ = (
@ -98,8 +126,16 @@ class SPCOutlook():
'text_raw', 'body', 'poly', 'probabilities', 'categories' 'text_raw', 'body', 'poly', 'probabilities', 'categories'
) )
__table__ = 'xmet_spc_outlook'
__key__ = 'id'
__columns__ = (
'id', 'timestamp_issued', 'timestamp_start', 'timestamp_end',
'day', 'text_raw', 'body'
)
def __init__(self): def __init__(self):
self.id = None self.id = None
self.timestamp_issued = None self.timestamp_issued = None
self.timestamp_start = None self.timestamp_start = None
self.timestamp_end = None self.timestamp_end = None
@ -111,6 +147,14 @@ class SPCOutlook():
self.probabilities = list() self.probabilities = list()
self.categories = list() self.categories = list()
class SPCOutlookParserState(enum.Enum):
HEADER = 1
OFFICE = enum.auto()
ISSUANCE = enum.auto()
VALIDITY = enum.auto()
AREA_THREAT = enum.auto()
BODY = enum.auto()
class SPCOutlookParser(): class SPCOutlookParser():
outlook: SPCOutlook outlook: SPCOutlook
state: SPCOutlookParserState state: SPCOutlookParserState
@ -246,7 +290,6 @@ class SPCOutlookParser():
self.outlook.categories.append(area) self.outlook.categories.append(area)
self.hazard = None
self.category = None self.category = None
self.points = list() self.points = list()
@ -312,6 +355,8 @@ class SPCOutlookParser():
def parse(self, text: str) -> SPCOutlook: def parse(self, text: str) -> SPCOutlook:
self.reset() self.reset()
self.outlook.text_raw = text
for line in text.split('\n'): for line in text.split('\n'):
if line is None: if line is None:
break break