Skip to content

Commit

Permalink
Issue/create site (#92)
Browse files Browse the repository at this point in the history
* add get all users
add get all site groups to read functions

* add functions get all users, get all site groups

* add test for get all users, get all site groups

* add new functions as imports in init file

* add delete functions for users, site, site group

* add passing tests for deleting site, user, site group

* add create user and create site functions

* add function for creating a user

* add test for create user

* add test for add site to site group

* add update site group

* test update site group

* lint

* remove make_user

* add create_site + tests

* add geopandas

* add tests to download dno and gsp

* lint

* move download dno and gsp to script

* simplify

---------

Co-authored-by: Rachel Tipton <[email protected]>
  • Loading branch information
peterdudfield and rachel-labri-tipton authored Jan 5, 2024
1 parent cbb5b78 commit 792fe28
Show file tree
Hide file tree
Showing 24 changed files with 1,036 additions and 127 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,16 @@ Currently available functions accessible via `from pvsite_datamodel.read import

Currently available write functions accessible via `from pvsite_datamodels.write import <func>`:
- insert_generation_values
- make_user
- make_site
- make_site_group
- create_site
- create_site_group
- create_user
- add_site_to_site_group
- change_user_site_group
- update_user_site_group
- delete_site
- delete_user
- delete_site_group
- make_fake_site


## Install the dependencies (requires [poetry][poetry])
Expand Down
237 changes: 236 additions & 1 deletion poetry.lock

Large diffs are not rendered by default.

62 changes: 62 additions & 0 deletions pvsite_datamodel/write/data/dno.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
""" The 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 logging
import os

import geopandas as gpd
from shapely.geometry import Point

from pvsite_datamodel.write.data.utils import lat_lon_to_osgb

logger = logging.getLogger(__name__)
dir_path = os.path.dirname(os.path.realpath(__file__))
dno_local_file = f"{dir_path}/dno"


def get_dno(latitude, longitude) -> dict:
"""
Convert 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}
logger.debug(dno_dict)
else:
dno_dict = {"dno_id": "999", "name": "unknown", "long_name": "unknown"}

return dno_dict


#
1 change: 1 addition & 0 deletions pvsite_datamodel/write/data/dno/dno.cpg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ISO-8859-1
Binary file added pvsite_datamodel/write/data/dno/dno.dbf
Binary file not shown.
1 change: 1 addition & 0 deletions pvsite_datamodel/write/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 pvsite_datamodel/write/data/dno/dno.shp
Binary file not shown.
Binary file added pvsite_datamodel/write/data/dno/dno.shx
Binary file not shown.
51 changes: 51 additions & 0 deletions pvsite_datamodel/write/data/gsp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
""" GSP functions for UK regions. """
import logging
import os

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

from pvsite_datamodel.write.data.utils import lat_lon_to_osgb

logger = logging.getLogger(__name__)

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 get_gsp(latitude, longitude) -> dict:
"""
Get a DNO from latitude and longitude.
: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}
else:
gsp_dict = {"gsp_id": "999", "name": "unknown"}

return gsp_dict
1 change: 1 addition & 0 deletions pvsite_datamodel/write/data/gsp/gsp.cpg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ISO-8859-1
Binary file added pvsite_datamodel/write/data/gsp/gsp.dbf
Binary file not shown.
1 change: 1 addition & 0 deletions pvsite_datamodel/write/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 pvsite_datamodel/write/data/gsp/gsp.shp
Binary file not shown.
Binary file added pvsite_datamodel/write/data/gsp/gsp.shx
Binary file not shown.
Loading

0 comments on commit 792fe28

Please sign in to comment.