Skip to content

Commit 3e8774e

Browse files
authored
Merge pull request #64 from SheffieldSolar/63-add-support-for-using-latest-version-of-gsp-boundaries-20251204
add support for gsp boundaries version 20251204
2 parents 03338c3 + 99a9755 commit 3e8774e

2 files changed

Lines changed: 25 additions & 20 deletions

File tree

Tests/test_geocode.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_force_setup(self):
3737
geo.force_setup()
3838
cache_dir = geo.cache_manager.cache_dir
3939
assert cache_dir.is_dir()
40-
assert len([c for c in cache_dir.glob("*.p") if "gmaps" not in c.name]) == 14
40+
assert len([c for c in cache_dir.glob("*.p") if "gmaps" not in c.name]) == 15
4141

4242
def test_geocode_llsoa(self):
4343
"""

geocode/neso.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ def __init__(self, cache_manager, proxies=None, ssl_verify=True):
4040
"""The NESO data manager for the Geocode class."""
4141
self.cache_manager = cache_manager
4242
self.gsp_lookup_20181031_cache_file = "gsp_lookup_20181031"
43-
self.gsp_boundaries_20250109_cache_file = "gsp_boundaries_20250109"
4443
self.gsp_boundaries_20220314_cache_file = "gsp_boundaries_20220314"
4544
self.gsp_boundaries_20181031_cache_file = "gsp_boundaries_20181031"
4645
self.dno_boundaries_cache_file = "dno_boundaries"
@@ -55,8 +54,8 @@ def force_setup(self):
5554
"""
5655
Function to setup lookup files.
5756
"""
58-
self.load_gsp_boundaries("20220314")
59-
self.load_gsp_boundaries("20250109")
57+
for version in ["20220314", "20250109", "20251204"]:
58+
self.load_gsp_boundaries(version)
6059
self._load_dno_boundaries()
6160

6261
def _load_gsp_lookup_20181031(self):
@@ -95,36 +94,43 @@ def _load_gsp_lookup_20181031(self):
9594
)
9695
return gsp_lookup
9796

98-
def _load_gsp_boundaries_20250109(self):
97+
def _load_gsp_boundaries(self, version: str):
9998
"""
100-
Load the 20250109 GSP / GNode boundaries, either from local cache if available, else fetch
99+
Generic function to load latest GSP / GNode boundaries, either from local cache if available, else fetch
101100
from ESO Data Portal API.
102101
102+
Parameters
103+
----------
104+
`version` : str
105+
The version of the GSP boundaries to load.
106+
103107
Returns
104108
-------
105109
gsp_regions: GeoPandas.GeoDataFrame
106110
A geodataframe of MultiPolygons for the GSP boundaries.
107111
"""
112+
data_url = {
113+
"20250109": "https://api.neso.energy/dataset/2810092e-d4b2-472f-b955-d8bea01f9ec0/resource/d95e8c1b-9cd9-41dd-aacb-4b53b8c07c20/download/gsp_regions_20250109.zip",
114+
"20251204": "https://api.neso.energy/dataset/2810092e-d4b2-472f-b955-d8bea01f9ec0/resource/c5647312-afab-4a58-8158-2f1efed1d7fc/download/gsp_regions_20251204.zip",
115+
}
108116
gsp_boundaries_cache_contents = self.cache_manager.retrieve(
109-
self.gsp_boundaries_20250109_cache_file
117+
f"gsp_boundaries_{version}"
110118
)
111119
if gsp_boundaries_cache_contents is not None:
112120
logging.debug(
113-
"Loading 20250109 GSP boundaries from cache ('%s')",
114-
self.gsp_boundaries_20250109_cache_file,
121+
f"Loading {version} GSP boundaries from cache gsp_boundaries_{version}",
115122
)
116123
return gsp_boundaries_cache_contents
117124
logging.info(
118-
"Extracting the 20250109 GSP boundary data from NESO's Data Portal API (this "
125+
f"Extracting the {version} GSP boundary data from NESO's Data Portal API (this "
119126
"only needs to be done once)"
120127
)
121-
eso_url = "https://api.neso.energy/dataset/2810092e-d4b2-472f-b955-d8bea01f9ec0/resource/d95e8c1b-9cd9-41dd-aacb-4b53b8c07c20/download/gsp_regions_20250109.zip"
122128
success, api_response = utils.fetch_from_api(
123-
eso_url, proxies=self.proxies, ssl_verify=self.ssl_verify
129+
data_url[version], proxies=self.proxies, ssl_verify=self.ssl_verify
124130
)
125131
if success:
126132
zip_file = io.BytesIO(api_response.content)
127-
target_file = "Proj_27700/GSP_regions_27700_20250109.geojson"
133+
target_file = f"Proj_27700/GSP_regions_27700_{version}.geojson"
128134
with ZipFile(zip_file, "r") as zip_ref:
129135
if target_file in zip_ref.namelist():
130136
with zip_ref.open(target_file) as file:
@@ -135,12 +141,11 @@ def _load_gsp_boundaries_20250109(self):
135141
gsp_regions.geometry = gsp_regions.buffer(0)
136142
else:
137143
raise utils.GenericException(
138-
"Encountered an error while extracting GSP region data from ESO " "API."
144+
"Encountered an error while extracting GSP region data from ESO API."
139145
)
140-
self.cache_manager.write(self.gsp_boundaries_20250109_cache_file, gsp_regions)
146+
self.cache_manager.write(f"gsp_boundaries_{version}", gsp_regions)
141147
logging.info(
142-
"20250109 GSP boundaries extracted and pickled to '%s'",
143-
self.gsp_boundaries_20250109_cache_file,
148+
f"{version} GSP boundaries extracted and pickled to gsp_boundaries_{version}",
144149
)
145150
return gsp_regions
146151

@@ -202,10 +207,10 @@ def load_gsp_boundaries(self, version: str):
202207
gsp_regions: GeoPandas.GeoDataFrame
203208
A geodataframe of MultiPolygons for the GSP boundaries.
204209
"""
205-
if version == "20250109":
206-
return self._load_gsp_boundaries_20250109()
207-
elif version == "20220314":
210+
if version == "20220314":
208211
return self._load_gsp_boundaries_20220314()
212+
elif version in ["20250109", "20251204"]:
213+
return self._load_gsp_boundaries(version=version)
209214
else:
210215
raise ValueError(f"GSP boundaries version {version} is not supported.")
211216

0 commit comments

Comments
 (0)