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)