From c12d250d3e970d429fb448fae50869b6cc826288 Mon Sep 17 00:00:00 2001 From: XANTRONIX Industrial Date: Wed, 19 Feb 2025 23:14:21 -0500 Subject: [PATCH] Abandon Coord class in favor of shapely.Point --- lib/nexrad/afos.py | 6 ++++++ lib/nexrad/coord.py | 21 --------------------- lib/nexrad/radar.py | 11 ++++++----- lib/nexrad/storm.py | 27 ++++++++++++++------------- 4 files changed, 26 insertions(+), 39 deletions(-) diff --git a/lib/nexrad/afos.py b/lib/nexrad/afos.py index e1df7c8..cb5d415 100644 --- a/lib/nexrad/afos.py +++ b/lib/nexrad/afos.py @@ -204,3 +204,9 @@ class AFOSMessage(DatabaseTable): event.forecaster = line return event + + def is_watch(self): + return self.sig is not None and self.sig == 'A' + + def is_warning(self): + return self.sig is not None and self.sig == 'W' diff --git a/lib/nexrad/coord.py b/lib/nexrad/coord.py index 85f1a76..ce69f4c 100644 --- a/lib/nexrad/coord.py +++ b/lib/nexrad/coord.py @@ -1,22 +1 @@ -import shapely - COORD_SYSTEM = 4326 - -class Coord(): - __slots__ = 'lon', 'lat', - - def __init__(self, lon: float, lat: float): - self.lon: float = lon - self.lat: float = lat - - def __str__(self): - return '%f, %f' % (self.lon, self.lat) - - @staticmethod - def from_wkt(wkt: str): - try: - point = shapely.from_wkt(wkt) - - return Coord(point.x, point.y) - except: - return None diff --git a/lib/nexrad/radar.py b/lib/nexrad/radar.py index bcfcbce..451f2e7 100644 --- a/lib/nexrad/radar.py +++ b/lib/nexrad/radar.py @@ -1,8 +1,9 @@ import csv import re +import shapely from nexrad.db import DatabaseTable -from nexrad.coord import Coord, COORD_SYSTEM +from nexrad.coord import COORD_SYSTEM """ Implements a parser and wrapper class for the WSR-88D radar list @@ -37,7 +38,7 @@ def parse(text: str): lon = parse_int(match[3]) lat = parse_int(match[1]) - return Coord(sign_lon * lon, sign_lat * lat) + return shapely.Point(sign_lon * lon, sign_lat * lat) RADAR_RANGE = 230000 @@ -58,7 +59,7 @@ class Radar(DatabaseTable): } __values_read__ = { - 'coord': Coord.from_wkt + 'coord': shapely.from_wkt } __columns_write__ = { @@ -66,13 +67,13 @@ class Radar(DatabaseTable): } __values_write__ = { - 'coord': lambda v: {'coord_lon': v.lon, 'coord_lat': v.lat} + 'coord': lambda v: {'coord_lon': v.x, 'coord_lat': v.y} } call: str wban: int name: str - coord: Coord + coord: shapely.Point site_elevation: float tower_height: float diff --git a/lib/nexrad/storm.py b/lib/nexrad/storm.py index a8d9de8..d7d34a3 100644 --- a/lib/nexrad/storm.py +++ b/lib/nexrad/storm.py @@ -2,9 +2,10 @@ import re import gzip import csv import datetime +import shapely from nexrad.db import DatabaseTable -from nexrad.coord import Coord, COORD_SYSTEM +from nexrad.coord import COORD_SYSTEM from nexrad.radar import RADAR_RANGE def time_from_str(time: str): @@ -78,7 +79,7 @@ def coord_from_str(text_lon: str, text_lat: str): if text_lon == '' or text_lat == '': return - return Coord(float(text_lon), float(text_lat)) + return shapely.Point(float(text_lon), float(text_lat)) class StormEvent(DatabaseTable): __slots__ = ( @@ -104,8 +105,8 @@ class StormEvent(DatabaseTable): __values_read__ = { 'timestamp_start': datetime.datetime.fromisoformat, 'timestamp_end': datetime.datetime.fromisoformat, - 'coord_start': Coord.from_wkt, - 'coord_end': Coord.from_wkt + 'coord_start': shapely.from_wkt, + 'coord_end': shapely.from_wkt } __columns_write__ = { @@ -114,8 +115,8 @@ class StormEvent(DatabaseTable): } __values_write__ = { - 'coord_start': lambda v: {'coord_start_lon': v.lon, 'coord_start_lat': v.lat}, - 'coord_end': lambda v: {'coord_end_lon': v.lon, 'coord_end_lat': v.lat} + 'coord_start': lambda v: {'coord_start_lon': v.x, 'coord_start_lat': v.y}, + 'coord_end': lambda v: {'coord_end_lon': v.x, 'coord_end_lat': v.y} } id: int @@ -128,8 +129,8 @@ class StormEvent(DatabaseTable): locale_start: str locale_end: str tornado_f_rating: str - coord_start: Coord - coord_end: Coord + coord_start: shapely.Point + coord_end: shapely.Point @staticmethod def from_csv_row(row: dict): @@ -173,7 +174,7 @@ class StormEvent(DatabaseTable): @staticmethod def each_matching(db, - coord: Coord=None, + coord: shapely.Point=None, radius: float=RADAR_RANGE, timestamp: datetime.datetime=None): columns = StormEvent.__format_columns_select__(StormEvent) @@ -192,8 +193,8 @@ class StormEvent(DatabaseTable): ]) values.update({ - 'lon': coord.lon, - 'lat': coord.lat, + 'lon': coord.x, + 'lat': coord.y, 'crs': COORD_SYSTEM, 'radius': radius }) @@ -271,8 +272,8 @@ class StormEvent(DatabaseTable): radars = list() st = db.execute(sql, ( - self.coord_start.lon, self.coord_start.lat, - self.coord_end.lon, self.coord_end.lat, + self.coord_start.x, self.coord_start.y, + self.coord_end.x, self.coord_end.y, RADAR_RANGE)) while True: