xmet/lib/xmet/wfo.py

62 lines
1.4 KiB
Python
Raw Normal View History

2025-02-21 16:29:51 -05:00
import csv
import shapely
2025-02-22 13:53:54 -05:00
from xmet.db import DatabaseTable
from xmet.coord import COORD_SYSTEM
2025-02-21 16:29:51 -05:00
class WFO(DatabaseTable):
__slots__ = (
'code', 'city', 'state', 'address', 'location'
2025-02-21 16:29:51 -05:00
)
2025-02-22 13:53:54 -05:00
__table__ = 'xmet_wfo'
2025-02-21 16:29:51 -05:00
__key__ = 'code'
__columns__ = (
'code', 'city', 'state', 'address', 'location'
2025-02-21 16:29:51 -05:00
)
__columns_read__ = {
'location': 'ST_AsText(location) as location'
2025-02-21 16:29:51 -05:00
}
__values_read__ = {
'location': shapely.from_wkt
2025-02-21 16:29:51 -05:00
}
__columns_write__ = {
'location': 'MakePoint(:location_lon, :location_lat, {crs})'.format(crs=COORD_SYSTEM)
2025-02-21 16:29:51 -05:00
}
__values_write__ = {
'location': lambda v: {'location_lon': v.x, 'location_lat': v.y}
2025-02-21 16:29:51 -05:00
}
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]))
2025-02-21 16:29:51 -05:00
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)