Sort out adding storm reports to database
This commit is contained in:
parent
55e179658b
commit
9916fc8d9e
1 changed files with 55 additions and 40 deletions
|
@ -102,6 +102,11 @@ class StormReport(DatabaseTable):
|
||||||
'coord_end': 'ST_AsText(coord_end) as coord_end'
|
'coord_end': 'ST_AsText(coord_end) as coord_end'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
__constructors__ = {
|
||||||
|
'coord_start': lambda v: 'null' if v is None else f'MakePoint({v.lon}, {v.lat}, {COORD_SYSTEM})',
|
||||||
|
'coord_end': lambda v: 'null' if v is None else f'MakePoint({v.lon}, {v.lat}, {COORD_SYSTEM})'
|
||||||
|
}
|
||||||
|
|
||||||
id: int
|
id: int
|
||||||
timestamp_start: datetime.datetime
|
timestamp_start: datetime.datetime
|
||||||
timestamp_end: datetime.datetime
|
timestamp_end: datetime.datetime
|
||||||
|
@ -130,11 +135,17 @@ class StormReport(DatabaseTable):
|
||||||
report.locale_end = row['locale_end']
|
report.locale_end = row['locale_end']
|
||||||
report.tornado_f_rating = row['tornado_f_rating']
|
report.tornado_f_rating = row['tornado_f_rating']
|
||||||
|
|
||||||
c = shapely.from_wkt(row['coord_start'])
|
try:
|
||||||
report.coord_start = Coord(c.x, c.y)
|
c = shapely.from_wkt(row['coord_start'])
|
||||||
|
report.coord_start = Coord(c.x, c.y)
|
||||||
|
except:
|
||||||
|
report.coord_start = None
|
||||||
|
|
||||||
c = shapely.from_wkt(row['coord_end'])
|
try:
|
||||||
report.coord_end = Coord(c.x, c.y)
|
c = shapely.from_wkt(row['coord_end'])
|
||||||
|
report.coord_end = Coord(c.x, c.y)
|
||||||
|
except:
|
||||||
|
report.coord_end = None
|
||||||
|
|
||||||
return report
|
return report
|
||||||
|
|
||||||
|
@ -178,44 +189,48 @@ class StormReport(DatabaseTable):
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def add_to_db(self, db):
|
@staticmethod
|
||||||
|
def each_matching(db,
|
||||||
|
coord: Coord=None,
|
||||||
|
radius: float=RADAR_RANGE,
|
||||||
|
timestamp: datetime.datetime=None):
|
||||||
sql = """
|
sql = """
|
||||||
insert into nexrad_storm_report (
|
select * from nexrad_storm_report
|
||||||
id,
|
"""
|
||||||
episode_id,
|
|
||||||
timestamp_start,
|
|
||||||
timestamp_end,
|
|
||||||
state,
|
|
||||||
event_type,
|
|
||||||
wfo,
|
|
||||||
locale_start,
|
|
||||||
locale_end,
|
|
||||||
tornado_f_rating,
|
|
||||||
coord_start,
|
|
||||||
coord_end
|
|
||||||
) values (
|
|
||||||
?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
|
|
||||||
MakePoint(?, ?, {crs}),
|
|
||||||
MakePoint(?, ?, {crs})
|
|
||||||
)
|
|
||||||
""".format(crs=COORD_SYSTEM)
|
|
||||||
|
|
||||||
db.execute(sql, (
|
clauses = list()
|
||||||
self.id,
|
values = list()
|
||||||
self.episode_id,
|
|
||||||
self.timestamp_start.isoformat(),
|
if coord is not None:
|
||||||
self.timestamp_end.isoformat(),
|
clauses.append("""ST_DistanceWithin(MakeLine(coord_start, coord_end),
|
||||||
self.state,
|
MakePoint(?, ?, {csr}),
|
||||||
self.event_type,
|
{radius}) = 1""".format(
|
||||||
self.wfo,
|
csr = COORD_SYSTEM,
|
||||||
self.locale_start,
|
radius = radius
|
||||||
self.locale_end,
|
))
|
||||||
self.tornado_f_rating,
|
|
||||||
self.coord_start.lon,
|
values.append(coord.lon)
|
||||||
self.coord_start.lat,
|
values.append(coord.lat)
|
||||||
self.coord_end.lon,
|
|
||||||
self.coord_end.lat
|
if timestamp is not None:
|
||||||
))
|
clauses.append("timestamp_start >= ? and timestamp_end <= ?")
|
||||||
|
|
||||||
|
values.append(timestamp.isoformat())
|
||||||
|
|
||||||
|
if len(clauses) > 0:
|
||||||
|
sql += " where " + " and ".join(clauses)
|
||||||
|
|
||||||
|
print(f"Got query {sql} coord {coord.lat}, {coord.lon}")
|
||||||
|
|
||||||
|
st = db.query_sql(StormReport, sql, values)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
obj = st.fetchone()
|
||||||
|
|
||||||
|
if obj is None:
|
||||||
|
break
|
||||||
|
|
||||||
|
yield obj
|
||||||
|
|
||||||
RADAR_SIGNIFICANT_EVENT_TYPES = {
|
RADAR_SIGNIFICANT_EVENT_TYPES = {
|
||||||
'Blizzard': True,
|
'Blizzard': True,
|
||||||
|
|
Loading…
Add table
Reference in a new issue