diff --git a/lib/nexrad/vtec.py b/lib/nexrad/vtec.py index 3558505..56f4fee 100644 --- a/lib/nexrad/vtec.py +++ b/lib/nexrad/vtec.py @@ -55,13 +55,33 @@ RE_HYDRO = re.compile(r''' /$ ''', re.X) -RE_POLY = re.compile(r'^LAT\.\.\.LON(?P \d{4})+') +RE_POLY = re.compile(r'^LAT\.\.\.LON (?P\d{4}(?: \d{4})+)') def parse_timestamp(text: str, post_2016_05_11: bool): return datetime.datetime.strptime( text, '%y%m%dT%H%M%SZ' ).astimezone(datetime.UTC) +def parse_lon(text: str): + return 0 - float(text[0:2]) + (float(text[3:4]) / 100) + +def parse_lat(text: str): + return float(text[0:2]) + (float(text[3:4]) / 100) + +def parse_shape(text: str): + points = list() + coords = text.split(' ') + + for i in range(0, len(coords), 2): + lat = coords[i] + lon = coords[i+1] + + points.append([parse_lon(lon), parse_lat(lat)]) + + points.append([parse_lon(coords[0]), parse_lat(coords[1])]) + + return shapely.Polygon(points) + class VTECEventType(enum.StrEnum): OPERATIONAL = 'O' TEST = 'T' @@ -120,9 +140,6 @@ class VTECEvent(DatabaseTable): forecaster: str poly: shapely.Geometry - def parse_shape(self, coords: str): - pass - @staticmethod def parse(text: str): event = VTECEvent() @@ -185,7 +202,7 @@ class VTECEvent(DatabaseTable): match = RE_POLY.match(line) if match is not None: - event.parse_shape(match['coords']) + event.poly = parse_shape(match['coords']) elif line == '$$': state = VTECEventParserState.FOOTER else: