Add xmet_city table

This commit is contained in:
Alexandra Hrefna Maheu 2025-03-30 21:12:32 -04:00
parent f7599e8e9b
commit 9dcb6f043e
4 changed files with 49 additions and 0 deletions

View file

@ -8,6 +8,7 @@ SQLITE_DB = xmet.db
DB_INIT = ./bin/xmet-db-init
DB_INIT_RADARS = doc/radars.tsv
DB_INIT_WFO = doc/wfo.tsv
DB_INIT_CITIES = doc/cities.tsv
DB_INIT_IGRA = doc/igra2-station-list.txt
all:
@ -18,6 +19,7 @@ db-init:
$(SQLITE) -init $(SQLITE_SCHEMA) $(SQLITE_DB) .quit
$(DB_INIT) $(SQLITE_DB) --radars-tsv $(DB_INIT_RADARS) \
--wfo-tsv $(DB_INIT_WFO) \
--cities-tsv $(DB_INIT_CITIES) \
--igra-stations $(DB_INIT_IGRA)
clean:

View file

@ -5,6 +5,7 @@ import argparse
from xmet.db import Database
from xmet.radar import Radar
from xmet.wfo import WFO
from xmet.city import City
from xmet.igra import IGRAStation
parser = argparse.ArgumentParser(
@ -15,6 +16,7 @@ parser.add_argument('db', help='Path to SQLite3 database')
parser.add_argument('--radars-tsv', type=str, help='Path to NEXRAD radar station TSV file')
parser.add_argument('--wfo-tsv', type=str, help='Path to forecast office TSV file')
parser.add_argument('--cities-tsv', type=str, help='Path to cities TSV file')
parser.add_argument('--igra-stations', type=str, help='Path to IGRA station list')
args = parser.parse_args()
@ -31,6 +33,10 @@ if args.wfo_tsv is not None:
for wfo in WFO.each_from_tsv(args.wfo_tsv):
db.add(wfo)
if args.cities_tsv is not None:
for city in City.each_from_tsv(args.cities_tsv):
db.add(city)
if args.igra_stations is not None:
for station in IGRAStation.each_from_file(args.igra_stations):
db.add(station)

View file

@ -14,6 +14,17 @@ select
AddGeometryColumn('xmet_wfo', 'location', 4326, 'POINT', 'XY', 1),
CreateSpatialIndex('xmet_wfo', 'location');
create table xmet_city (
id INTEGER PRIMARY KEY NOT NULL,
name TEXT NOT NULL,
state TEXT NOT NULL,
population INTEGER NOT NULL
);
select
AddGeometryColumn('xmet_city', 'location', 4326, 'POINT', 'XY', 1),
CreateSpatialIndex('xmet_city', 'location');
create table xmet_nexrad_radar (
call TEXT PRIMARY KEY NOT NULL,
name TEXT NOT NULL,

View file

@ -1,11 +1,41 @@
import csv
import shapely
from xmet.coord import COORD_SYSTEM
"""
Implements a parser and wrapper class for a TSV list of cities.
"""
class City():
__slots__ = (
'id', 'name', 'state', 'population', 'location'
)
__table__ = 'xmet_city'
__key__ = 'id'
__columns__ = (
'name', 'state', 'population', '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
name: str
state: str
population: int