Skip to content

Commit

Permalink
Revert "add timezone conversion"
Browse files Browse the repository at this point in the history
This reverts commit b1f470e.
  • Loading branch information
peterdudfield committed Sep 19, 2024
1 parent b1f470e commit 0c26c13
Show file tree
Hide file tree
Showing 16 changed files with 533 additions and 37 deletions.
Empty file added src/data/__init__.py
Empty file.
73 changes: 73 additions & 0 deletions src/data/dno.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
""" 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


dir_path = os.path.dirname(os.path.realpath(__file__))
dno_local_file = f"{dir_path}/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.
63 changes: 63 additions & 0 deletions src/data/gsp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import os

import geopandas as gpd
import pandas as pd
from shapely.geometry import Point

from data.utils import lat_lon_to_osgb


dir_path = os.path.dirname(os.path.realpath(__file__))
gsp_local_file = f"{dir_path}/gsp"
gsp_names = pd.read_csv(f"{dir_path}/gsp_new_ids_and_names-edited.csv")


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 0c26c13

Please sign in to comment.