From 8f1ab5518a1940f2d0f6c82ec2d1602cefd669f4 Mon Sep 17 00:00:00 2001 From: Mieszko Makuch Date: Wed, 7 May 2025 12:35:14 +0200 Subject: [PATCH] ISEPOS-2373 Add saving geographical coordinates to SVG files as metadata --- src/seismic_hazard_forecasting.py | 41 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/seismic_hazard_forecasting.py b/src/seismic_hazard_forecasting.py index b1efe81..c711f4f 100644 --- a/src/seismic_hazard_forecasting.py +++ b/src/seismic_hazard_forecasting.py @@ -468,6 +468,16 @@ verbose: {verbose}") else: iml_grid_prep = iml_grid_raw + # TODO Remove. Saving coordinates to + # north, south = lat.max(), lat.min() + # east, west = lon.max(), lon.min() + # bbox_json = {"south": float(south), "west": float(west), + # "north": float(north), "east": float(east)} + # with open("overlay_bounds.json", "w", encoding="utf‑8") as fh: + # json.dump(bbox_json, fh, indent=2) + # + # logger.info(f"Saved bbox to overlay_bounds.json → {bbox_json}") + for j in range(0, len(products)): vmin = min(x for x in iml_grid_prep[j] if x is not np.nan) vmax = max(x for x in iml_grid_prep[j] if x is not np.nan) @@ -498,7 +508,38 @@ verbose: {verbose}") plt.savefig("overlay_" + str(j) + ".svg", bbox_inches="tight", pad_inches=0, transparent=True) plt.close(fig) + + import xml.etree.ElementTree as ET # <-- TODO place this with other imports + + # ----------------------------------------- + # Inject bounding box (BBOX) metadata into the SVG + # ----------------------------------------- + north, south = lat.max(), lat.min() + east, west = lon.max(), lon.min() + bbox_dict = { + "south": float(south), + "west": float(west), + "north": float(north), + "east": float(east) + } + tree = ET.parse(svg_path) + + root = tree.getroot() + + # Remove any existing tags + for old_meta in root.findall("{http://www.w3.org/2000/svg}metadata"): + root.remove(old_meta) + + # Add new element with the bounding box JSON + meta_elem = ET.SubElement(root, "metadata") + meta_elem.text = json.dumps(bbox_dict) + + # ----------------------------------------- + # END Inject bounding box (BBOX) metadata into the SVG + # ----------------------------------------- + # Make the color bar + cmap_name = 'viridis' width = 50 height = 500