Skip to content

Commit

Permalink
Merge pull request #81 from openclimatefix/create-site-fixes
Browse files Browse the repository at this point in the history
add method to get gsp and dno automatically
  • Loading branch information
rachel-labri-tipton authored Oct 10, 2023
2 parents 2a22842 + cb7670f commit f46950c
Show file tree
Hide file tree
Showing 20 changed files with 600 additions and 47 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ SQLAlchemy==1.4.46
streamlit==1.17.0
testcontainers==3.2.0
uvicorn==0.17.6
geopandas==0.11.1

5 changes: 5 additions & 0 deletions scripts/get_gsp_and_dno.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from src.data.dno import download_dno
from src.data.gsp import download_gsp

download_dno()
download_gsp()
Empty file added src/data/__init__.py
Empty file.
76 changes: 76 additions & 0 deletions src/data/dno.py
Original file line number Diff line number Diff line change
@@ -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


#
1 change: 1 addition & 0 deletions src/data/dno/dno.cpg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ISO-8859-1
Binary file added src/data/dno/dno.dbf
Binary file not shown.
1 change: 1 addition & 0 deletions src/data/dno/dno.prj
Original file line number Diff line number Diff line change
@@ -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]]
Binary file added src/data/dno/dno.shp
Binary file not shown.
Binary file added src/data/dno/dno.shx
Binary file not shown.
67 changes: 67 additions & 0 deletions src/data/gsp.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions src/data/gsp/gsp.cpg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ISO-8859-1
Binary file added src/data/gsp/gsp.dbf
Binary file not shown.
1 change: 1 addition & 0 deletions src/data/gsp/gsp.prj
Original file line number Diff line number Diff line change
@@ -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]]
Binary file added src/data/gsp/gsp.shp
Binary file not shown.
Binary file added src/data/gsp/gsp.shx
Binary file not shown.
Loading

0 comments on commit f46950c

Please sign in to comment.