77"""
88
99import os
10- import sys
11- import zipfile
12- import json
13- import csv
10+ import tempfile
1411import logging
1512from pathlib import Path
16- from typing import Literal , Optional , Iterable , Tuple , Union , List , Dict
13+ from typing import Literal , Iterable , Tuple , Union , List , Dict
1714
1815import pandas as pd
1916import geopandas as gpd
20- import shapefile
2117
2218try :
23- from shapely .geometry import shape , Point
24- from shapely .ops import unary_union
19+ from shapely .geometry import shape
2520except ImportError :
2621 logging .warning (
2722 "Failed to import Shapely library - you will not be able to reverse-geocode! "
@@ -45,13 +40,12 @@ def __init__(self, cache_manager, proxies=None, ssl_verify=True):
4540 """
4641 self .cache_manager = cache_manager
4742 self .data_dir = SCRIPT_DIR .joinpath ("ons" )
48- self .nrs_zipfile = self .data_dir .joinpath ("nrs_2011.zip" )
4943 self .constituency_lookup_file = self .data_dir .joinpath (
5044 "constituency_centroids_Dec2020.psv"
5145 )
5246 self .lad_lookup_file = self .data_dir .joinpath ("lad_centroids_May2021.psv" )
53- self .pc_llsoa_zipfile = self .data_dir .joinpath (
54- "PCD_OA_LSOA_MSOA_LAD_MAY22_UK_LU.zip "
47+ self .pc_llsoa_sevenzipfile = self .data_dir .joinpath (
48+ "PCD_OA_LSOA_MSOA_LAD_MAY22_UK_LU.7z "
5549 )
5650 self .llsoa_lookup = None
5751 self .llsoa_regions = None
@@ -117,36 +111,40 @@ def _load_llsoa_lookup(self):
117111 ]
118112 engwales_lookup .reset_index (names = "code" , inplace = True )
119113
120- zip_path_2011 = self .data_dir .joinpath ("nrs_2011.zip " )
121- zip_path_2021 = self .data_dir .joinpath ("nrs_2021.zip " )
114+ sevenzip_path_2011 = self .data_dir .joinpath ("nrs_2011.7z " )
115+ sevenzip_path_2021 = self .data_dir .joinpath ("nrs_2021.7z " )
122116
123- OA_2011_centroids = gpd .read_file (
124- f"zip://{ zip_path_2011 } !OutputArea2011_PWC_WGS84.csv" ,
125- columns = ["code" , "easting" , "northing" ],
117+ OA_2011_centroids = utils .read_csv_from_7z (
118+ sevenzip_path_2011 ,
119+ "OutputArea2011_PWC_WGS84.csv" ,
120+ usecols = ["code" , "easting" , "northing" ],
126121 )
127122 OA_2011_centroids = utils .add_latlon (OA_2011_centroids , "easting" , "northing" )
128123 scots_lookup_2011 = OA_2011_centroids [["code" , "latitude" , "longitude" ]]
129- OA_2021_centroids = gpd .read_file (
130- f"zip://{ zip_path_2021 } !OutputArea2022_PWC_WGS84.csv" ,
131- columns = ["code" , "easting" , "northing" ],
124+ OA_2021_centroids = utils .read_csv_from_7z (
125+ sevenzip_path_2021 ,
126+ "OutputArea2022_PWC_WGS84.csv" ,
127+ usecols = ["code" , "easting" , "northing" ],
132128 )
133129 OA_2021_centroids = utils .add_latlon (OA_2021_centroids , "easting" , "northing" )
134130 scots_lookup_2021 = OA_2021_centroids [["code" , "latitude" , "longitude" ]]
135131 scots_lookup = pd .concat ([scots_lookup_2011 , scots_lookup_2021 ]).reset_index (
136132 drop = True
137133 )
138134
139- DZ_2011_centroids = gpd .read_file (
140- f"zip://{ zip_path_2011 } !SG_DataZone_Cent_2011.csv" ,
141- columns = ["DataZone" , "Easting" , "Northing" ],
135+ DZ_2011_centroids = utils .read_csv_from_7z (
136+ sevenzip_path_2011 ,
137+ "SG_DataZone_Cent_2011.csv" ,
138+ usecols = ["DataZone" , "Easting" , "Northing" ],
142139 )
143140 DZ_2011_centroids = utils .add_latlon (DZ_2011_centroids , "Easting" , "Northing" )
144141 scots_dz_lookup_2011 = DZ_2011_centroids [
145142 ["DataZone" , "latitude" , "longitude" ]
146143 ].rename (columns = {"DataZone" : "code" })
147- DZ_2021_centroids = gpd .read_file (
148- f"zip://{ zip_path_2021 } !SG_DataZone_Cent_2022.csv" ,
149- columns = ["DataZone" , "Easting" , "Northing" ],
144+ DZ_2021_centroids = utils .read_csv_from_7z (
145+ sevenzip_path_2021 ,
146+ "SG_DataZone_Cent_2022.csv" ,
147+ usecols = ["DataZone" , "Easting" , "Northing" ],
150148 )
151149 DZ_2021_centroids = utils .add_latlon (DZ_2021_centroids , "Easting" , "Northing" )
152150 scots_dz_lookup_2021 = DZ_2021_centroids [
@@ -204,19 +202,24 @@ def _load_llsoa_boundaries_engwales_regions(self, version: Literal["2011", "2021
204202
205203 def _load_llsoa_boundaries_scots_regions (self , version : Literal ["2011" , "2021" ]):
206204 """
207- Load the LLSOA boundaries for Scotland from the NRS zipfile .
205+ Load the LLSOA boundaries for Scotland from the NRS 7z file .
208206
209207 Parameters
210208 ----------
211209 `version` : Literal["2011", "2021"]
212210 The version of the LLSOA boundaries to load.
213211 """
214- zip_path = self .data_dir .joinpath (f"nrs_{ version } .zip " )
212+ sevenzip_path = self .data_dir .joinpath (f"nrs_{ version } .7z " )
215213 llsoa_filename = {
216- "2011" : "OutputArea2011_EoR_WGS84.shp " ,
217- "2021" : "OutputArea2022_EoR.shp " ,
214+ "2011" : "OutputArea2011_EoR_WGS84.geojson " ,
215+ "2021" : "OutputArea2022_EoR.geojson " ,
218216 }
219- gdf = gpd .read_file (f"zip://{ zip_path } !{ llsoa_filename [version ]} " )
217+ target_file = llsoa_filename [version ]
218+
219+ with tempfile .TemporaryDirectory () as tmpdir :
220+ utils .extract_from_7z (sevenzip_path , target_file , tmpdir )
221+ extracted_file = Path (tmpdir ) / target_file
222+ gdf = gpd .read_file (extracted_file )
220223 if version == "2021" :
221224 gdf .set_crs ("EPSG:27700" , inplace = True )
222225 gdf .to_crs ("EPSG:4326" , inplace = True )
@@ -273,11 +276,9 @@ def _load_datazone_lookup(self, version: Literal["2011", "2021"]):
273276 f"Loading { version } LLSOA<->Datazone lookup from cache { cache_label } "
274277 )
275278 return datazone_lookup_cache_contents
276- zip_path = self .data_dir .joinpath (f"nrs_{ version } .zip " )
279+ sevenzip_path = self .data_dir .joinpath (f"nrs_{ version } .7z " )
277280 dz_lookup_filename = {"2011" : "OA_DZ_IZ_2011.csv" , "2021" : "OA22_DZ22_IZ22.csv" }
278- with zipfile .ZipFile (zip_path , "r" ) as nrs_zip :
279- with nrs_zip .open (dz_lookup_filename [version ], "r" ) as fid :
280- dz_lookup = pd .read_csv (fid )
281+ dz_lookup = utils .read_csv_from_7z (sevenzip_path , dz_lookup_filename [version ])
281282 if version == "2011" :
282283 dz_lookup .set_index ("OutputArea2011Code" , inplace = True )
283284 dz_lookup .drop (columns = ["IntermediateZone2011Code" ], inplace = True )
@@ -522,7 +523,11 @@ def _load_postcode_llsoa_lookup(self):
522523 "Loading postcode<->LLSOA lookup from cache ('%s')" , "pc_llsoa_lookup"
523524 )
524525 return postcode_llsoa_lookup_cache_contents
525- pc_llsoa_lookup = pd .read_csv (self .pc_llsoa_zipfile , dtype = str )
526+ pc_llsoa_lookup = utils .read_csv_from_7z (
527+ self .pc_llsoa_sevenzipfile ,
528+ "PCD_OA_LSOA_MSOA_LAD_MAY22_UK_LU.csv" ,
529+ dtype = str ,
530+ )
526531 pc_llsoa_lookup ["postcode" ] = (
527532 pc_llsoa_lookup .pcds .str .strip ().str .upper ().str .replace (" " , "" )
528533 )
0 commit comments