diff --git a/requirements.txt b/requirements.txt index 81ad600..30cf11e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,4 +9,5 @@ SQLAlchemy==1.4.46 streamlit==1.17.0 testcontainers==3.2.0 uvicorn==0.17.6 +geopandas==0.11.1 diff --git a/scripts/get_gsp_and_dno.py b/scripts/get_gsp_and_dno.py new file mode 100644 index 0000000..18f8d5d --- /dev/null +++ b/scripts/get_gsp_and_dno.py @@ -0,0 +1,5 @@ +from src.data.dno import download_dno +from src.data.gsp import download_gsp + +download_dno() +download_gsp() \ No newline at end of file diff --git a/src/data/__init__.py b/src/data/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/data/dno.py b/src/data/dno.py new file mode 100644 index 0000000..3b3248e --- /dev/null +++ b/src/data/dno.py @@ -0,0 +1,76 @@ +""" This script adds the relevant GSP to the sites + +Might need to install nowcasting_dataset + +and we want to added the gsp as {gsp_id}|{gsp_nam} into the database + +1. Load in dno data from NG +2. Load all sites +3. For each site add dno + +""" +import ssl +import os + +import geopandas as gpd +from shapely.geometry import Point + +from data.utils import lat_lon_to_osgb + + +cwd = os.getcwd() +if "src" not in cwd: + dno_local_file = "./src/data/dno" +else: + dno_local_file = "./data/dno" + + +def download_dno(): + + print("Getting dno file") + ssl._create_default_https_context = ssl._create_unverified_context + url = "https://data.nationalgrideso.com/backend/dataset/0e377f16-95e9-4c15-a1fc-49e06a39cfa0/resource/e96db306-aaa8-45be-aecd-65b34d38923a/download/dno_license_areas_20200506.geojson" + dno_shapes = gpd.read_file(url) + + print("Saving dno file") + dno_shapes.to_file(dno_local_file) + + +def get_dno(latitude, longitude) -> dict: + """ + This function takes a latitude and longitude and returns the dno + + :param latitude: + :param longitude: + + :return: dno is this format {"dno_id": dno_id, "name": dno_name, "long_name": dno_long_name}= + """ + + # load file + dno = gpd.read_file(dno_local_file) + + # change lat lon to osgb + x, y = lat_lon_to_osgb(lat=latitude, lon=longitude) + point = Point(x, y) + + # select dno + mask = dno.contains(point) + dno = dno[mask] + + # format dno + if len(dno) == 1: + dno = dno.iloc[0] + + dno_id = dno["ID"] + name = dno["Name"] + long_name = dno["LongName"] + + dno_dict = {"dno_id": str(dno_id), "name": name, "long_name": long_name} + print(dno_dict) + else: + dno_dict = {"dno_id": "999", "name": "unknown", "long_name": "unknown"} + + return dno_dict + + +# diff --git a/src/data/dno/dno.cpg b/src/data/dno/dno.cpg new file mode 100644 index 0000000..cd89cb9 --- /dev/null +++ b/src/data/dno/dno.cpg @@ -0,0 +1 @@ +ISO-8859-1 \ No newline at end of file diff --git a/src/data/dno/dno.dbf b/src/data/dno/dno.dbf new file mode 100644 index 0000000..c3d2d9f Binary files /dev/null and b/src/data/dno/dno.dbf differ diff --git a/src/data/dno/dno.prj b/src/data/dno/dno.prj new file mode 100644 index 0000000..fec0ee2 --- /dev/null +++ b/src/data/dno/dno.prj @@ -0,0 +1 @@ +PROJCS["British_National_Grid",GEOGCS["GCS_OSGB_1936",DATUM["D_OSGB_1936",SPHEROID["Airy_1830",6377563.396,299.3249646]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",400000.0],PARAMETER["False_Northing",-100000.0],PARAMETER["Central_Meridian",-2.0],PARAMETER["Scale_Factor",0.9996012717],PARAMETER["Latitude_Of_Origin",49.0],UNIT["Meter",1.0]] \ No newline at end of file diff --git a/src/data/dno/dno.shp b/src/data/dno/dno.shp new file mode 100644 index 0000000..0270352 Binary files /dev/null and b/src/data/dno/dno.shp differ diff --git a/src/data/dno/dno.shx b/src/data/dno/dno.shx new file mode 100644 index 0000000..48af78d Binary files /dev/null and b/src/data/dno/dno.shx differ diff --git a/src/data/gsp.py b/src/data/gsp.py new file mode 100644 index 0000000..2a7293c --- /dev/null +++ b/src/data/gsp.py @@ -0,0 +1,67 @@ +import os + +import geopandas as gpd +import pandas as pd +from shapely.geometry import Point + +from data.utils import lat_lon_to_osgb + +cwd = os.getcwd() +if "src" not in cwd: + dir = "./src/data" +else: + dir = "./data" + +gsp_names = pd.read_csv(f"{dir}/gsp_new_ids_and_names-edited.csv") +gsp_local_file = f"{dir}/gsp" + + +def download_gsp(): + + print("Getting gsp file") + + url = ( + "https://data.nationalgrideso.com/backend/dataset/2810092e-d4b2-472f-b955-d8bea01f9ec0/" + "resource/08534dae-5408-4e31-8639-b579c8f1c50b/download/gsp_regions_20220314.geojson" + ) + gsp_shapes = gpd.read_file(url) + + print("Saving gsp file") + gsp_shapes.to_file(gsp_local_file) + + +def get_gsp(latitude, longitude) -> dict: + """ + This function takes a latitude and longitude and returns the dno + + :param latitude: + :param longitude: + + :return: dno is this format {"dno_id": dno_id, "name": dno_name, "long_name": dno_long_name}= + """ + + # load file + gsp = gpd.read_file(gsp_local_file) + + # change lat lon to osgb + x, y = lat_lon_to_osgb(lat=latitude, lon=longitude) + point = Point(x, y) + + # select gsp + mask = gsp.contains(point) + gsp = gsp[mask] + + # format gsp + if len(gsp) == 1: + gsp = gsp.iloc[0] + gsp_details = gsp_names[gsp_names["gsp_name"] == gsp.GSPs] + gsp_id = gsp_details.index[0] + gsp_details = gsp_details.iloc[0] + name = gsp_details["region_name"] + + gsp_dict = {"gsp_id": str(gsp_id), "name": name} + print(gsp_dict) + else: + gsp_dict = {"gsp_id": "999", "name": "unknown"} + + return gsp_dict diff --git a/src/data/gsp/gsp.cpg b/src/data/gsp/gsp.cpg new file mode 100644 index 0000000..cd89cb9 --- /dev/null +++ b/src/data/gsp/gsp.cpg @@ -0,0 +1 @@ +ISO-8859-1 \ No newline at end of file diff --git a/src/data/gsp/gsp.dbf b/src/data/gsp/gsp.dbf new file mode 100644 index 0000000..113aba1 Binary files /dev/null and b/src/data/gsp/gsp.dbf differ diff --git a/src/data/gsp/gsp.prj b/src/data/gsp/gsp.prj new file mode 100644 index 0000000..fec0ee2 --- /dev/null +++ b/src/data/gsp/gsp.prj @@ -0,0 +1 @@ +PROJCS["British_National_Grid",GEOGCS["GCS_OSGB_1936",DATUM["D_OSGB_1936",SPHEROID["Airy_1830",6377563.396,299.3249646]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",400000.0],PARAMETER["False_Northing",-100000.0],PARAMETER["Central_Meridian",-2.0],PARAMETER["Scale_Factor",0.9996012717],PARAMETER["Latitude_Of_Origin",49.0],UNIT["Meter",1.0]] \ No newline at end of file diff --git a/src/data/gsp/gsp.shp b/src/data/gsp/gsp.shp new file mode 100644 index 0000000..5fee419 Binary files /dev/null and b/src/data/gsp/gsp.shp differ diff --git a/src/data/gsp/gsp.shx b/src/data/gsp/gsp.shx new file mode 100644 index 0000000..cf0979a Binary files /dev/null and b/src/data/gsp/gsp.shx differ diff --git a/src/data/gsp_new_ids_and_names-edited.csv b/src/data/gsp_new_ids_and_names-edited.csv new file mode 100644 index 0000000..c2de170 --- /dev/null +++ b/src/data/gsp_new_ids_and_names-edited.csv @@ -0,0 +1,319 @@ +,gsp_id_x,gsp_name,region_name +0,0,NATIONAL, National +1,1,ABHA1,Abham +2,2,ABNE_P,Abernethy +3,3,ABTH_1,Aberthaw +4,4,ACTL_2|CBNK_H|GREE_H|PERI_H,Willesden (Zone 1) +5,5,ACTL_C|WISD_1|WISD_6,Willesden (Zone 2) +6,6,ALNE_P,Alness +7,7,ALST_3|USKM_1,Uskmouth +8,8,ALVE1,Alverdiscott +9,9,AMEM_1,Amersham Main +10,10,ARBR_P,Arbroath +11,11,ARDK_P|CLAC_P,Clachan +12,12,ARMO_P,Ardmore +13,13,AXMI1,Axminster +14,14,AYRR,Ayr +15,15,BAGA,Bathgate +16,16,BAIN,Bainsford +17,17,BARKC1|BARKW3,Barking +18,18,BEAU_P|ORRI_P,Beauly +19,19,BEDDT1,Beddington (Zone 1) +20,20,BEDD_1,Beddington (Zone 2) +21,21,BERB_P|CAIF_P|DALL_P|GLEF_P|KEIT_P,Keith +22,22,BERW,Berwick +23,23,BESW_1,Berkswell +24,24,BICF_1,Bicker Fen +25,25,BIRK_1,Birkenhead +26,26,BISW_1,Bishops Wood +27,27,BLYTB1,Blyth (Zone 1) +28,28,BLYTH132,Blyth (Zone 2) +30,29,BOAG_P,Boat of Garten +31,30,BOLN_1,Bolney +32,31,BONN,Bonnybridge +33,32,BOTW_1,Botley Wood +34,33,BRAC_P,Braco West +35,34,BRAI_1,Braintree +36,35,BRAP,Braehead Park +37,36,BRAW_1,Bradford West +38,37,BRED_1,Bredbury +39,38,BRFO_1|CLT03,Bramford +40,39,BRID_P,Bridge of Dun +41,40,BRIM_1,Brimsdown +42,41,BRLE_1|FLEE_1,Bramley +43,42,BROA_P,Broadford +44,43,BROR_P,Brora +45,44,BROX,Broxburn +46,45,BRWA1,Bridgewater +47,46,BUMU_P,Burghmuir +48,47,BURM_1,Burwell Main +49,48,BUSH_1,Bushbury +50,49,BUST_1,Bustleholme +51,50,CAAD_P,Carradale +52,51,CAFA|EAST|GLLE|KEOO|TONG,Tongland +53,52,CAMB_01,Camblesforth +54,53,CANTN1|RICH_J|RICH1,Canterbury +55,54,CAPEA1,Capenhurst +57,55,CARE_1,Cardiff East +58,56,CARR_1,Carrington +60,57,CASS_P,Cassley +61,58,CATY,Carntyne +62,59,CEAN_P,Ceannacroc +63,60,CELL_1,Cellarhead +64,61,CHAP,Chapelcross +65,62,CHAR_P,Charleston +66,63,CHAS,Charlotte Street +67,64,CHIC_1,Chickerell +68,65,CHSI_1,Chessington +70,66,CHTE_1,Chesterfield +71,67,CITR_1,City Road +72,68,CLAY_P,Clayhills +73,69,CLYM,Clyde’s Mill +74,70,COAT,Coatbridge +76,71,COCK,Cockenzie +77,72,CONQA1|SASA,Connahs Quay +78,73,COUA_P,Coupar Angus +79,74,COVE_1,Coventry +80,75,COWL_1|ECLA_H,Cowley +81,76,COYL,Coylton +82,77,CRAI_P,Craigiebuckler +83,78,CREB_1,Creyke Beck +84,79,CROO,Crookston +86,80,CUMB,Cumbernauld +87,81,CUPA,Cupar +88,82,CURR,Currie +89,83,DALM3,Dalmarnock +90,84,DEVM,Devol Moor +91,85,DEVO,Devonside +92,86,DEWP,Dewar Place +93,87,DOUN_P,Dounreay +94,88,DRAK_1,Drakelow +95,89,DRAX_1,Drax +96,90,DRCR,Drumcross +97,91,DRUM,Drumchapel +98,92,DUBE_P,Dunbeath +99,93,DUDH_P,Dudhope +100,94,DUGR_P,Dunvegan +101,95,DUMF,Dumfries +104,96,DUNB,Dunbar +105,97,DUNF,Dunfermline +106,98,DUNO_P,Dunoon +107,99,DYCE_P,Dyce +108,100,EALI_6,Ealing +109,101,EASO_1,Eaton Socon +110,102,ECCL,Eccles +111,103,ECLA_1,East Claydon +112,104,EERH,Easterhouse +113,105,EKIL,East Kilbride +114,106,EKIS,East Kilbride South +115,107,ELDE,Elderslie +116,108,ELGI_P,Elgin +117,109,ELLA_1,Elland +118,110,ELST_1,Elstree +121,111,ENDE_1,Enderby +122,112,ERSK,Erskine +123,113,EXET1,Exeter +124,114,FASN_P,Fasnakyle +125,115,FAUG_P,Fort Augustus +126,116,FAWL_1,Fawley +127,117,FECK_6,Feckenham +128,118,FERRA2,Ferrybridge (Zone 1) +129,119,FERRB_M|FERRB1,Ferrybridge (Zone 2) +130,120,FETT_P|KINT_P,Kintore +131,121,FIDD_P,Fiddes +132,122,FIDF_1,Fiddlers Ferry +133,123,FINN,Finnieston +134,124,FOUR_1,Fourstones +135,125,FRAS_P,Fraserburgh +136,126,FROD_1,Frodsham +137,127,FWIL_P,Fort William +138,128,GALA,Galashiels +139,129,GIFF,Giffnock +140,130,GLLU,Glenluce +141,131,GLNI,Glenniston +142,132,GLRO,Glenrothes +143,133,GORG,Gorgie +144,134,GOVA,Govan +145,135,GREN_1,Grendon +146,136,GRIW_1,Grimsby West +147,137,GRMO,Grangemouth +149,138,GRUB_P,Grudie Bridge +150,139,G_EXTRA_12,G_EXTRA_12 +151,140,HACK_1|HACK_6,Hackney +152,141,HAGR,Haggs Road +153,142,HAMHC1,Hams Hall +154,143,HARK_1|HUTT_1|RRIG,Hutton +155,144,HARM_6,Hart Moor +156,145,HAWI,Hawick +157,146,HAWP_6,Hawthorn Pit +158,147,HELE,Helensburgh +159,148,HEYS_1|HEYS1|ORMO,Heysham +160,149,HUER,Hunterston Farm +161,150,HURS_1,Hurst +162,151,IMPK_1,Imperial Park +163,152,INDQ1,Indian Queens +164,153,INKE,Inverkeithing +165,154,INNE_P,Inverness +166,155,IROA1,Iron Acton +167,156,IRONB1,Ironbridge +168,157,ISLI_1|WHAM_1,West Ham +169,158,IVER_1|IVER_6,Iver +170,159,JOHN,Johnstone +171,160,JORD_3,Jordanthorpe +172,161,KAIM,Kaimes +173,162,KEAD_1,Keadby +174,163,KEAR_1|KEAR_3,Kearsley +175,164,KEMS_1,Kemsley +177,165,KIBY_1,Kirkby (Zone 1) +178,166,KIBY_G|WASF_1,Kirkby (Zone 2) +179,167,KIER,Killermont +180,168,KIIN_P,Killin +181,169,KILB,Kilbowie +183,170,KILS,Kilmarnock South +184,171,KILT,Kilmarnock Town +185,172,KILW,Kilwinning +186,173,KINL_P,Kinlochleven +187,174,KINO_1,Kingsnorth +188,175,KIRKB1,Kirkstall +189,176,KITW_1,Kitwell +190,177,KNAR,Knaresborough +191,178,LACK_6,Lackenby +192,179,LAIR_P,Lairg +193,180,LALE1SG003,Laleham +194,181,LAND1,Landulph +195,182,LEGA_1,Legacy +196,183,LEVE,Leven +197,184,LING,Livingston East +198,185,LINM,Linnmill +199,186,LISD_1,Lister Drive +200,187,LITT_C,Littlebrook (Zone 1) +201,188,LITT_J,Littlebrook (Zone 2) +202,189,LODR_6,Lodge Road +203,190,LOUD_H,Loudwater +204,191,LOVE_1,Lovedean +205,192,LUNA_P,Lunanhead +206,193,LYND_P,Lyndhurst +207,194,MACC_3,Macclesfield +208,195,MACD_P,Macduff +209,196,MAGA_6,Margam +210,197,MANN_1,Mannington +211,198,MAYB,Maybole +212,199,MELK_1,Melksham +213,200,MILC_P,Milton of Craigie +214,201,MILH_1,Mill Hill +215,202,MITY_1,Minety +216,203,MYBS_P,Mybster +217,204,NAIR_P,Nairn +218,205,NEAR,Newarthill +219,206,NECE_1,Nechells +221,207,NEEP_3,Neepsend +222,208,NETS,Newton Stewart +223,209,NEWX_6,New Cross +225,210,NFLE,Northfleet East +226,211,NHYD_6,North Hyde +227,212,NINF_1,Ninfield +228,213,NORL_3,Norton Lees +229,214,NORM_1|SALL1,Norwich Main +230,215,NORT_1,Norton +231,216,NURS_1,Nursling +232,217,OCKH_1,Ocker Hill +233,218,OFFE_3,Offerton +234,219,OLDB_1,Oldbury +235,220,OSBA_1,Osbaldwick +236,221,PADIB1,Padiham +237,222,PAIS,Paisley +238,223,PART,Partick +239,224,PEHG_P,Peterhead Grange +240,225,PEHS_P|STRI_P,Peterhead Shell +241,226,PELH_1,Pelham +242,227,PEMB_1,Pembroke +243,228,PENE_1,Penwortham +244,229,PENN_1,Penn +245,230,PENT_1,Pentir +246,231,PENW_1|STAH_1|WABO,Penwortham +247,232,PERS_P,Persley +248,233,PITS_3,Pitsmoor +250,234,POOB,Portobello +251,235,POPP_3,Poppleton +252,236,PORA_P,Port Ann +253,237,PORD,Port Dundas +254,238,PYLE_1,Pyle +255,239,QUOI_P,Quoich +256,240,RAIN_1,Rainhill +257,241,RANN_P,Rannoch +258,242,RASS_1,Rassau +259,243,RATS_1,Ratcliffe +260,244,RAVE|WISH,Ravenscraig +261,245,RAYL_1,Rayleigh +262,246,REBR_3,Redbridge +263,247,REDH,Redhouse +264,248,REDM_P,Redmoss +265,249,ROCH_1,Rochdale +266,250,RUGEB1,Rugeley +267,251,RYEH_1,Rye House +268,252,SACO,Saltcoats +270,253,SALH_1,Saltholme +271,254,SALT_1,Saltend North +272,255,SANX,St Andrews Cross +273,256,SBAR,Stoke Bardolph +274,257,SEAB1,Seabank +275,258,SELL_1,Sellindge +277,259,SFEG_P,St Fergus Gas Terminal +278,260,SFIL_P,St Fillans +279,261,SHEC_3,Sheffield City +280,262,SHIN_P,Shin +281,263,SHRU,Shrubhill +282,264,SHRW_1,Shrewsbury +283,265,SIGH,Sighthill +284,266,SJOW_1,St Johns Wood +285,267,SKLGB1,Skelton Grange +286,268,SLOY_P,Sloy +287,269,SMAN_1,South Manchester +288,270,SPAV,Spango Valley +289,271,SPEN_1,Spennymoor +290,272,SSHI_3,South Shields +291,273,STAL_1,Stalybridge +292,274,STAY_1,Staythorpe +293,275,STEN_1,Stella North +294,276,STES_1,Stella South +295,277,STHA,Strathaven +296,278,STIR,Stirling +297,279,STLE,Strathleven +298,280,SUND_1,Sundon +299,281,SWAN_1,Swansea North +300,282,TARL_P,Tarland +301,283,TAUN1,Taunton +302,284,TAYN_P,Taynuilt +303,285,TELR,Telford Road +304,286,TEMP_3,Templeborough +305,287,THOM_6,Thorpe Marsh +306,288,THSO_P,Thurso +307,289,THUR_6,Thurcroft +308,290,TILBB_1,Tilbury +310,291,TOTT_1,Tottenham +311,292,TRAW_1,Trawsfynydd +312,293,TUMB_P,Tummel Bridge +313,294,TYNE_1|TYNE_2,Tynemouth +314,295,UPPB_1|UPPB_3,Upper Boat +315,296,WALH_1,Walham +316,297,WALP_1,Walpole (Zone 1) +317,298,WALP_B,Walpole (Zone 2) +318,299,WARL_1,Warley +319,300,WATFS_1,Watford South +320,301,WBOL_6,West Boldon +321,302,WBUR_1,West Burton +322,303,WFIE,Westfield +323,304,WGEO,West George Street +324,305,WHGA_1,Whitegate +325,306,WHHO,Whitehouse +326,307,WIBA_3,Wincobank +327,308,WIEN_1,Willenhall +328,309,WILL_1,Willington +329,310,WIMBN1|WIMBS1,Wimbledon +330,311,WIOW_P,Willowdale +331,312,WMEL_1,West Melton +332,313,WOHI_P,Woodhill +333,314,WTHU31,West Thurrock +334,315,WWEY_1,West Weybridge +335,316,WYLF_1,Wylfa +336,317,WYMOM_1,Wymondley diff --git a/src/data/utils.py b/src/data/utils.py new file mode 100644 index 0000000..b429448 --- /dev/null +++ b/src/data/utils.py @@ -0,0 +1,71 @@ +import pyproj + +# OSGB is also called "OSGB 1936 / British National Grid -- United +# Kingdom Ordnance Survey". OSGB is used in many UK electricity +# system maps, and is used by the UK Met Office UKV model. OSGB is a +# Transverse Mercator projection, using 'easting' and 'northing' +# coordinates which are in meters. See https://epsg.io/27700 + +# WGS84 is short for "World Geodetic System 1984", used in GPS. Uses +# latitude and longitude. + +OSGB = 27700 +WGS84 = 4326 +WGS84_CRS = f"EPSG:{WGS84}" + + +def lat_lon_to_osgb(lat: float, lon: float) -> [float, float]: + """ + Change lat, lon to a OSGB coordinates + + Args: + lat: latitude + lon: longitude + + Return: 2-tuple of x (east-west), y (north-south). + + """ + return transformers.lat_lon_to_osgb.transform(lat, lon) + + +class Transformers: + """ + Class to store transformation from one Grid to another. + + Its good to make this only once, but need the + option of updating them, due to out of data grids. + """ + + def __init__(self): + """Init""" + self._osgb_to_lat_lon = None + self._lat_lon_to_osgb = None + self._osgb_to_geostationary = None + self.make_transformers() + + def make_transformers(self): + """ + Make transformers + + Nice to only make these once, as it makes calling the functions below quicker + """ + self._osgb_to_lat_lon = pyproj.Transformer.from_crs(crs_from=OSGB, crs_to=WGS84) + self._lat_lon_to_osgb = pyproj.Transformer.from_crs(crs_from=WGS84, crs_to=OSGB) + + @property + def osgb_to_lat_lon(self): + """OSGB to lat-lon property""" + return self._osgb_to_lat_lon + + @property + def lat_lon_to_osgb(self): + """lat-lon to OSGB property""" + return self._lat_lon_to_osgb + + @property + def osgb_to_geostationary(self): + """Convert from OSGB to geostationary coordinates.""" + return self._osgb_to_geostationary + + +transformers = Transformers() diff --git a/src/get_data.py b/src/get_data.py index 34a23c0..6c8a02a 100644 --- a/src/get_data.py +++ b/src/get_data.py @@ -6,6 +6,7 @@ # TODO move to nowcasting_datamodel """ import logging +import json from datetime import datetime, timezone from typing import List, Optional @@ -21,6 +22,8 @@ ) from pvsite_datamodel.sqlmodels import UserSQL, SiteGroupSQL, SiteSQL +from data.gsp import get_gsp +from data.dno import get_dno logger = logging.getLogger(__name__) @@ -217,27 +220,28 @@ def create_new_site( if max_ml_id is None: max_ml_id = 0 - if region is None: + if region in [None, ""]: region = "uk" - if orientation is None: + if orientation in [None, ""]: orientation = 180 - if tilt is None: + if tilt in [None, ""]: tilt = 35 - if inverter_capacity_kw is None: + if inverter_capacity_kw in [None, ""]: inverter_capacity_kw = capacity_kw - if module_capacity_kw is None: + if module_capacity_kw in [None, ""]: module_capacity_kw = capacity_kw if gsp is None: - pass + gsp = get_gsp(latitude=latitude, longitude=longitude) + gsp = json.dumps(gsp) if dno is None: - pass - + dno = get_dno(latitude=latitude, longitude=longitude) + dno = json.dumps(dno) site = SiteSQL( ml_id=max_ml_id + 1, diff --git a/src/sites_toolbox.py b/src/sites_toolbox.py index a51a564..01178d5 100644 --- a/src/sites_toolbox.py +++ b/src/sites_toolbox.py @@ -44,9 +44,7 @@ def get_site_details(session, site_uuid: str): "site_uuid": str(site.site_uuid), "client_site_id": str(site.client_site_id), "client_site_name": str(site.client_site_name), - "site_group_names": [ - site_group.site_group_name for site_group in site.site_groups - ], + "site_group_names": [site_group.site_group_name for site_group in site.site_groups], "latitude": str(site.latitude), "longitude": str(site.longitude), "region": str(site.region), @@ -69,13 +67,9 @@ def select_site_id(dbsession, query_method: str): site_uuids = [str(site.site_uuid) for site in get_all_sites(session=dbsession)] selected_uuid = st.selectbox("Sites by site_uuid", site_uuids) elif query_method == "client_site_id": - client_site_ids = [ - str(site.client_site_id) for site in get_all_sites(session=dbsession) - ] + client_site_ids = [str(site.client_site_id) for site in get_all_sites(session=dbsession)] client_site_id = st.selectbox("Sites by client_site_id", client_site_ids) - site = get_site_by_client_site_id( - session=dbsession, client_site_id=client_site_id - ) + site = get_site_by_client_site_id(session=dbsession, client_site_id=client_site_id) selected_uuid = str(site.site_uuid) elif query_method not in ["site_uuid", "client_site_id"]: raise ValueError("Please select a valid query_method.") @@ -85,9 +79,7 @@ def select_site_id(dbsession, query_method: str): # get details for one site group def get_site_group_details(session, site_group_name: str): """Get the site group details from the database""" - site_group_uuid = get_site_group_by_name( - session=session, site_group_name=site_group_name - ) + site_group_uuid = get_site_group_by_name(session=session, site_group_name=site_group_name) site_group_sites = [ {"site_uuid": str(site.site_uuid), "client_site_id": str(site.client_site_id)} for site in site_group_uuid.sites @@ -99,9 +91,7 @@ def get_site_group_details(session, site_group_name: str): # update a site's site groups def update_site_group(session, site_uuid: str, site_group_name: str): """Add a site to a site group""" - site_group = get_site_group_by_name( - session=session, site_group_name=site_group_name - ) + site_group = get_site_group_by_name(session=session, site_group_name=site_group_name) site_group_sites = add_site_to_site_group( session=session, site_uuid=site_uuid, site_group_name=site_group_name ) @@ -120,9 +110,7 @@ def change_user_site_group(session, email: str, site_group_name: str): Change user to a specific site group name :param session: the database session :param email: the email of the user""" - update_user_site_group( - session=session, email=email, site_group_name=site_group_name - ) + update_user_site_group(session=session, email=email, site_group_name=site_group_name) user = get_user_by_email(session=session, email=email) user_site_group = user.site_group.site_group_name user = user.email @@ -136,9 +124,7 @@ def add_all_sites_to_ocf_group(session, site_group_name="ocf"): :param site_group_name: the name of the site group""" all_sites = get_all_sites(session=session) - ocf_site_group = get_site_group_by_name( - session=session, site_group_name=site_group_name - ) + ocf_site_group = get_site_group_by_name(session=session, site_group_name=site_group_name) site_uuids = [site.site_uuid for site in ocf_site_group.sites] @@ -200,9 +186,7 @@ def sites_toolbox_page(): user_site_count, "sites.", ) - st.write( - "Here are the site_uuids and client_site_ids for this group:", user_sites - ) + st.write("Here are the site_uuids and client_site_ids for this group:", user_sites) if st.button("Close user details"): st.empty() @@ -273,9 +257,7 @@ def sites_toolbox_page(): "sites: ", site_group_sites, ) - st.write( - "The following site groups include site", site_uuid, ":", site_site_groups - ) + st.write("The following site groups include site", site_uuid, ":", site_site_groups) if st.button("Close details"): st.empty() @@ -322,7 +304,7 @@ def sites_toolbox_page(): unsafe_allow_html=True, ) # ml_id = max_ml_id + 1 - client_site_id = st.number_input("Client Site Id *",step=1) + client_site_id = st.number_input("Client Site Id *", step=1) client_site_name = st.text_input("Client Site Name *") st.markdown( @@ -334,7 +316,6 @@ def sites_toolbox_page(): longitude = st.text_input("longitude *") region = st.text_input("region") - st.markdown( f'

{"PV Information"}

', unsafe_allow_html=True, @@ -346,14 +327,13 @@ def sites_toolbox_page(): module_capacity_kw = st.text_input("Module Capacity [kwp]") if st.button(f"Create new site"): - if ( - None in [client_site_id ,client_site_name,region,orientation, - tilt, latitude,longitude, inverter_capacity_kw,module_capacity_kw,capacity_kw] - ): - st.write(f"Please check that you've entered data for each field." - f" {client_site_id} {client_site_name} {region} " - f"{orientation} {tilt} {latitude} {longitude} " - f"{inverter_capacity_kw} {module_capacity_kw} {capacity_kw}") + if "" in [client_site_id, client_site_name, latitude, longitude, capacity_kw]: + error = ( + f"Please check that you've entered data for each field. " + f"{client_site_id=} {client_site_name=} " + f"{latitude=} {longitude=} {capacity_kw=}" + ) + st.write(error) else: # create new site, message = create_new_site( session=session, @@ -374,8 +354,7 @@ def sites_toolbox_page(): "client_site_id": str(site.client_site_id), "client_site_name": str(site.client_site_name), "site_group_names": [ - site_group.site_group_name - for site_group in site.site_groups + site_group.site_group_name for site_group in site.site_groups ], "latitude": str(site.latitude), "longitude": str(site.longitude), diff --git a/tests/data/test_dno_and_gsp.py b/tests/data/test_dno_and_gsp.py new file mode 100644 index 0000000..81f338f --- /dev/null +++ b/tests/data/test_dno_and_gsp.py @@ -0,0 +1,27 @@ +from data.dno import get_dno +from data.gsp import get_gsp + + +def test_get_dno(): + + dno = get_dno(latitude=51.5074, longitude=0.1278) + + assert dno == {'dno_id': '12','name':'_C','long_name': 'UKPN (London)'} + + +def test_get_dno_outside_uk(): + dno = get_dno(latitude=0.0, longitude=0.0) + + assert dno == {"dno_id": "999", "name": "unknown", "long_name": "unknown"} + + +def test_get_gsp(): + gsp = get_gsp(latitude=51.5074, longitude=0.1278) + + assert gsp == {'gsp_id': '17', 'name': 'Barking'} + + +def test_get_gsp_outside_uk(): + gsp = get_gsp(latitude=0.0, longitude=0.0) + + assert gsp == {"gsp_id": "999", "name": "unknown"} \ No newline at end of file