Implement WFO database table, class
This commit is contained in:
parent
bbb2a62636
commit
3fa26aca83
2 changed files with 72 additions and 1 deletions
|
@ -3,8 +3,18 @@ select InitSpatialMetadata(1);
|
||||||
|
|
||||||
begin transaction;
|
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 (
|
create table nexrad_radar (
|
||||||
id INTEGER PRIMARY KEY NOT NULL,
|
|
||||||
wban INTEGER,
|
wban INTEGER,
|
||||||
call TEXT NOT NULL,
|
call TEXT NOT NULL,
|
||||||
name TEXT NOT NULL,
|
name TEXT NOT NULL,
|
||||||
|
|
61
lib/nexrad/wfo.py
Normal file
61
lib/nexrad/wfo.py
Normal file
|
@ -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)
|
Loading…
Add table
Reference in a new issue