Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Tests/test_geocode.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_force_setup(self):
geo.force_setup()
cache_dir = geo.cache_manager.cache_dir
assert cache_dir.is_dir()
assert len([c for c in cache_dir.glob("*.p") if "gmaps" not in c.name]) == 14
assert len([c for c in cache_dir.glob("*.p") if "gmaps" not in c.name]) == 15

def test_geocode_llsoa(self):
"""
Expand Down
43 changes: 24 additions & 19 deletions geocode/neso.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def __init__(self, cache_manager, proxies=None, ssl_verify=True):
"""The NESO data manager for the Geocode class."""
self.cache_manager = cache_manager
self.gsp_lookup_20181031_cache_file = "gsp_lookup_20181031"
self.gsp_boundaries_20250109_cache_file = "gsp_boundaries_20250109"
self.gsp_boundaries_20220314_cache_file = "gsp_boundaries_20220314"
self.gsp_boundaries_20181031_cache_file = "gsp_boundaries_20181031"
self.dno_boundaries_cache_file = "dno_boundaries"
Expand All @@ -55,8 +54,8 @@ def force_setup(self):
"""
Function to setup lookup files.
"""
self.load_gsp_boundaries("20220314")
self.load_gsp_boundaries("20250109")
for version in ["20220314", "20250109", "20251204"]:
self.load_gsp_boundaries(version)
self._load_dno_boundaries()

def _load_gsp_lookup_20181031(self):
Expand Down Expand Up @@ -95,36 +94,43 @@ def _load_gsp_lookup_20181031(self):
)
return gsp_lookup

def _load_gsp_boundaries_20250109(self):
def _load_gsp_boundaries(self, version: str):
"""
Load the 20250109 GSP / GNode boundaries, either from local cache if available, else fetch
Generic function to load latest GSP / GNode boundaries, either from local cache if available, else fetch
from ESO Data Portal API.

Parameters
----------
`version` : str
The version of the GSP boundaries to load.

Returns
-------
gsp_regions: GeoPandas.GeoDataFrame
A geodataframe of MultiPolygons for the GSP boundaries.
"""
data_url = {
"20250109": "https://api.neso.energy/dataset/2810092e-d4b2-472f-b955-d8bea01f9ec0/resource/d95e8c1b-9cd9-41dd-aacb-4b53b8c07c20/download/gsp_regions_20250109.zip",
"20251204": "https://api.neso.energy/dataset/2810092e-d4b2-472f-b955-d8bea01f9ec0/resource/c5647312-afab-4a58-8158-2f1efed1d7fc/download/gsp_regions_20251204.zip",
}
gsp_boundaries_cache_contents = self.cache_manager.retrieve(
self.gsp_boundaries_20250109_cache_file
f"gsp_boundaries_{version}"
)
if gsp_boundaries_cache_contents is not None:
logging.debug(
"Loading 20250109 GSP boundaries from cache ('%s')",
self.gsp_boundaries_20250109_cache_file,
f"Loading {version} GSP boundaries from cache gsp_boundaries_{version}",
)
return gsp_boundaries_cache_contents
logging.info(
"Extracting the 20250109 GSP boundary data from NESO's Data Portal API (this "
f"Extracting the {version} GSP boundary data from NESO's Data Portal API (this "
"only needs to be done once)"
)
eso_url = "https://api.neso.energy/dataset/2810092e-d4b2-472f-b955-d8bea01f9ec0/resource/d95e8c1b-9cd9-41dd-aacb-4b53b8c07c20/download/gsp_regions_20250109.zip"
success, api_response = utils.fetch_from_api(
eso_url, proxies=self.proxies, ssl_verify=self.ssl_verify
data_url[version], proxies=self.proxies, ssl_verify=self.ssl_verify
)
if success:
zip_file = io.BytesIO(api_response.content)
target_file = "Proj_27700/GSP_regions_27700_20250109.geojson"
target_file = f"Proj_27700/GSP_regions_27700_{version}.geojson"
with ZipFile(zip_file, "r") as zip_ref:
if target_file in zip_ref.namelist():
with zip_ref.open(target_file) as file:
Expand All @@ -135,12 +141,11 @@ def _load_gsp_boundaries_20250109(self):
gsp_regions.geometry = gsp_regions.buffer(0)
else:
raise utils.GenericException(
"Encountered an error while extracting GSP region data from ESO " "API."
"Encountered an error while extracting GSP region data from ESO API."
)
self.cache_manager.write(self.gsp_boundaries_20250109_cache_file, gsp_regions)
self.cache_manager.write(f"gsp_boundaries_{version}", gsp_regions)
logging.info(
"20250109 GSP boundaries extracted and pickled to '%s'",
self.gsp_boundaries_20250109_cache_file,
f"{version} GSP boundaries extracted and pickled to gsp_boundaries_{version}",
)
return gsp_regions

Expand Down Expand Up @@ -202,10 +207,10 @@ def load_gsp_boundaries(self, version: str):
gsp_regions: GeoPandas.GeoDataFrame
A geodataframe of MultiPolygons for the GSP boundaries.
"""
if version == "20250109":
return self._load_gsp_boundaries_20250109()
elif version == "20220314":
if version == "20220314":
return self._load_gsp_boundaries_20220314()
elif version in ["20250109", "20251204"]:
return self._load_gsp_boundaries(version=version)
else:
raise ValueError(f"GSP boundaries version {version} is not supported.")

Expand Down