perfect-postcode/pipeline/download/geosure.py

44 lines
1.2 KiB
Python

"""Download OS GeoSure ground stability data (5km hex grid).
Downloads the GB-Hex-5km-GeoSure dataset from Ordnance Survey as an ESRI
Shapefile and extracts it.
Source: https://osdatahub.os.uk/downloads/open/GeoSure
License: Open Government Licence v3.0
"""
import argparse
import tempfile
from pathlib import Path
from pipeline.utils import download, extract_zip
URL = "https://api.os.uk/downloads/v1/products/GB-Hex-5km-GeoSure/downloads?area=GB&format=ESRI%C2%AE+Shapefile&redirect"
def main() -> None:
parser = argparse.ArgumentParser(
description="Download OS GeoSure ground stability data"
)
parser.add_argument(
"--output",
type=Path,
required=True,
help="Output directory for extracted shapefile",
)
args = parser.parse_args()
with tempfile.TemporaryDirectory() as cache_dir:
zip_path = Path(cache_dir) / "geosure.zip"
download(URL, zip_path, timeout=300)
extract_zip(zip_path, args.output)
shp_files = list(args.output.rglob("*.shp"))
print(f"Extracted {len(shp_files)} shapefiles to {args.output}")
for f in shp_files:
print(f" {f.relative_to(args.output)}")
if __name__ == "__main__":
main()