diff --git a/Makefile b/Makefile index 44cc190..a03f9dc 100644 --- a/Makefile +++ b/Makefile @@ -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: diff --git a/bin/xmet-db-init b/bin/xmet-db-init index c92ee31..d1b0764 100755 --- a/bin/xmet-db-init +++ b/bin/xmet-db-init @@ -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) diff --git a/db/xmet.sql b/db/xmet.sql index c90638f..1c800a1 100644 --- a/db/xmet.sql +++ b/db/xmet.sql @@ -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, diff --git a/lib/xmet/city.py b/lib/xmet/city.py index 1dfad3b..505b384 100644 --- a/lib/xmet/city.py +++ b/lib/xmet/city.py @@ -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