diff --git a/db/nexrad.sql b/db/nexrad.sql index 5b8a872..04f78eb 100644 --- a/db/nexrad.sql +++ b/db/nexrad.sql @@ -3,8 +3,18 @@ select InitSpatialMetadata(1); begin transaction; +create table nexrad_wfo ( + code TEXT NOT NULL, + city TEXT NOT NULL, + state TEXT NOT NULL, + address TEXT NOT NULL +); + +select + AddGeometryColumn('nexrad_wfo', 'coord', 4326, 'POINT', 'XY', 1), + CreateSpatialIndex('nexrad_wfo', 'coord'); + create table nexrad_radar ( - id INTEGER PRIMARY KEY NOT NULL, wban INTEGER, call TEXT NOT NULL, name TEXT NOT NULL, diff --git a/lib/nexrad/wfo.py b/lib/nexrad/wfo.py new file mode 100644 index 0000000..5485c8a --- /dev/null +++ b/lib/nexrad/wfo.py @@ -0,0 +1,61 @@ +import csv +import shapely + +from nexrad.db import DatabaseTable +from nexrad.coord import COORD_SYSTEM + +class WFO(DatabaseTable): + __slots__ = ( + 'code', 'city', 'state', 'address', 'coord' + ) + + __table__ = 'nexrad_wfo' + __key__ = 'code' + + __columns__ = ( + 'code', 'city', 'state', 'address', 'coord' + ) + + __columns_read__ = { + 'coord': 'ST_AsText(coord) as coord' + } + + __values_read__ = { + 'coord': shapely.from_wkt + } + + __columns_write__ = { + 'coord': 'MakePoint(:coord_lon, :coord_lat, {crs})'.format(crs=COORD_SYSTEM) + } + + __values_write__ = { + 'coord': lambda v: {'coord_lon': v.x, 'coord_lat': v.y} + } + + id: int + code: str + city: str + state: str + adadress: str + + @staticmethod + def from_tsv_row(row: list): + wfo = WFO() + wfo.code = row[2] + wfo.city = row[0] + wfo.state = row[1] + wfo.address = row[3] + wfo.coord = shapely.Point(float(row[5]), float(row[4])) + + return wfo + + @staticmethod + def each_from_tsv(file: str): + with open(file) as fh: + reader = csv.reader(fh, delimiter='\t') + + for row in reader: + for i in range(0, len(row)): + row[i] = row[i].rstrip() + + yield WFO.from_tsv_row(row)