xmet/lib/xmet/wfo.py

61 lines
1.4 KiB
Python

import csv
import shapely
from xmet.db import DatabaseTable
from xmet.coord import COORD_SYSTEM
class WFO(DatabaseTable):
__slots__ = (
'code', 'city', 'state', 'address', 'location'
)
__table__ = 'xmet_wfo'
__key__ = 'code'
__columns__ = (
'code', 'city', 'state', 'address', 'location'
)
__columns_read__ = {
'location': 'ST_AsText(location) as location'
}
__values_read__ = {
'location': shapely.from_wkt
}
__columns_write__ = {
'location': 'MakePoint(:location_lon, :location_lat, {crs})'.format(crs=COORD_SYSTEM)
}
__values_write__ = {
'location': lambda v: {'location_lon': v.x, 'location_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.location = 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)