From 27f70b8456aab547e1cd9f9467d90b07f21e5534 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Brunner?= Date: Thu, 12 Dec 2024 17:32:07 +0100 Subject: [PATCH] Replace epsg.io by pyproj --- .../c2cgeoportal_geoportal/scripts/pcreate.py | 30 +++++-------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/geoportal/c2cgeoportal_geoportal/scripts/pcreate.py b/geoportal/c2cgeoportal_geoportal/scripts/pcreate.py index 978475a448..41a666f453 100644 --- a/geoportal/c2cgeoportal_geoportal/scripts/pcreate.py +++ b/geoportal/c2cgeoportal_geoportal/scripts/pcreate.py @@ -26,7 +26,6 @@ # either expressed or implied, of the FreeBSD Project. -import json import os import re import subprocess # nosec @@ -35,7 +34,7 @@ from typing import Any, cast import pkg_resources -import requests +import pyproj import yaml from cookiecutter.log import configure_logger from cookiecutter.main import cookiecutter @@ -274,27 +273,14 @@ def get_var( @staticmethod def epsg2bbox(srid: int) -> list[str] | None: try: - r = requests.get(f"https://epsg.io/?format=json&q={srid}", timeout=60) - bbox = r.json()["results"][0]["bbox"] - r = requests.get( - f"https://epsg.io/trans?s_srs=4326&t_srs={srid}&data={bbox[1]},{bbox[0]}", - timeout=60, - ) - r1 = r.json()[0] - r = requests.get( - f"https://epsg.io/trans?s_srs=4326&t_srs={srid}&data={bbox[3]},{bbox[2]}", - timeout=60, - ) - r2 = r.json()[0] - return [r1["x"], r2["y"], r2["x"], r1["y"]] - except requests.RequestException: - print("Failed to establish a connection to epsg.io.") - except json.JSONDecodeError: - print("epsg.io doesn't return a correct json.") - except IndexError: - print("Unable to get the bbox") + crs = pyproj.CRS.from_epsg(srid) + assert crs.area_of_use is not None + transformer = pyproj.Transformer.from_crs(4326, crs) + c1 = transformer.transform(crs.area_of_use.bounds[1], crs.area_of_use.bounds[0]) + c2 = transformer.transform(crs.area_of_use.bounds[3], crs.area_of_use.bounds[2]) + return [c1[0], c1[1], c2[0], c2[1]] except Exception as exception: # pylint: disable=broad-exception-caught - print(f"unexpected error: {str(exception)}") + print(f"Unexpected error: {str(exception)}") return None