From 124bab8b1784bdd2e76bdeaca17c235a0e651629 Mon Sep 17 00:00:00 2001 From: XANTRONIX Industrial Date: Fri, 14 Feb 2025 11:50:43 -0500 Subject: [PATCH] Fix storm report insertion --- lib/nexrad/db.py | 5 +++++ lib/nexrad/storm.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/lib/nexrad/db.py b/lib/nexrad/db.py index 618331e..3df8ba8 100644 --- a/lib/nexrad/db.py +++ b/lib/nexrad/db.py @@ -111,6 +111,11 @@ class Database(): return ret def add(self, obj): + fn = getattr(obj, '__insert__') + + if fn is not None: + return fn(self.db) + table = type(obj) sql = f"insert into {table.__table__} (" sql += ", ".join([c for c in table.__columns__ if c != table.__key__]) diff --git a/lib/nexrad/storm.py b/lib/nexrad/storm.py index fd78cc7..adde44a 100644 --- a/lib/nexrad/storm.py +++ b/lib/nexrad/storm.py @@ -149,6 +149,50 @@ class StormReport(DatabaseTable): return report + def __insert__(self, db): + columns = [ + 'id', 'timestamp_start', 'timestamp_end', 'episode_id', + 'state', 'event_type', 'wfo', 'locale_start', 'locale_end', + 'tornado_f_rating' + ] + + bindings = ['?' for c in columns] + values = [ + self.id, + self.timestamp_start.isoformat(), + self.timestamp_end.isoformat(), + self.episode_id, + self.state, + self.event_type, + self.wfo, + self.locale_start, + self.locale_end, + self.tornado_f_rating + ] + + if self.coord_start and self.coord_end: + columns.extend(['coord_start', 'coord_end']) + + bindings.extend([ + 'MakePoint(?, ?, {crs})'.format(crs=COORD_SYSTEM), + 'MakePoint(?, ?, {crs})'.format(crs=COORD_SYSTEM) + ]) + + values.extend([ + self.coord_start.lon, + self.coord_start.lat, + self.coord_end.lon, + self.coord_end.lat + ]) + + sql = "insert into nexrad_storm_report (" + sql += ', '.join(columns) + sql += ") values (" + sql += ', '.join(bindings) + sql += ")" + + db.execute(sql, values) + @staticmethod def from_csv_row(row: dict): report = StormReport()