From 3718bfacf4f65fddef0285dd64d996cf6cefa50c Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 6 Nov 2024 11:39:42 +0800 Subject: [PATCH] Figure.savefig: Clarify that 'transparent' also works for the PNG file associated with the KML format (#3579) --- pygmt/figure.py | 32 +++++++++++++++----------------- pygmt/tests/test_figure.py | 11 ++++++++++- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/pygmt/figure.py b/pygmt/figure.py index 0b2faea00d0..4163ab52eb1 100644 --- a/pygmt/figure.py +++ b/pygmt/figure.py @@ -135,7 +135,7 @@ def region(self) -> np.ndarray: wesn = lib.extract_region() return wesn - def savefig( # noqa: PLR0912 + def savefig( self, fname: str | PurePath, transparent: bool = False, @@ -177,7 +177,8 @@ def savefig( # noqa: PLR0912 The desired figure file name, including the extension. See the list of supported formats and their extensions above. transparent - Use a transparent background for the figure. Only valid for PNG format. + Use a transparent background for the figure. Only valid for PNG format and + the PNG file asscoiated with KML format. crop Crop the figure canvas (page) to the plot area. anti_alias @@ -203,9 +204,9 @@ def savefig( # noqa: PLR0912 "bmp": "b", "eps": "e", "jpg": "j", - "kml": "g", + "kml": "G" if transparent is True else "g", "pdf": "f", - "png": "g", + "png": "G" if transparent is True else "g", "ppm": "m", "tif": "t", "tiff": None, # GeoTIFF doesn't need the -T option @@ -226,14 +227,12 @@ def savefig( # noqa: PLR0912 msg = "Extension '.ps' is not supported. Use '.eps' or '.pdf' instead." raise GMTInvalidInput(msg) case ext if ext not in fmts: - raise GMTInvalidInput(f"Unknown extension '.{ext}'.") - - fmt = fmts[ext] - if transparent: - if fmt != "g": - msg = f"Transparency unavailable for '{ext}', only for png." + msg = f"Unknown extension '.{ext}'." raise GMTInvalidInput(msg) - fmt = fmt.upper() + + if transparent and ext not in {"kml", "png"}: + msg = f"Transparency unavailable for '{ext}', only for png and kml." + raise GMTInvalidInput(msg) if anti_alias: kwargs["Qt"] = 2 kwargs["Qg"] = 2 @@ -244,18 +243,17 @@ def savefig( # noqa: PLR0912 raise GMTInvalidInput(msg) kwargs["W"] = True - # pytest-mpl v0.17.0 added the "metadata" parameter to Figure.savefig, which - # is not recognized. So remove it before calling Figure.psconvert. + # pytest-mpl v0.17.0 added the "metadata" parameter to Figure.savefig, which is + # not recognized. So remove it before calling Figure.psconvert. kwargs.pop("metadata", None) - self.psconvert(prefix=prefix, fmt=fmt, crop=crop, **kwargs) + self.psconvert(prefix=prefix, fmt=fmts[ext], crop=crop, **kwargs) - # Remove the .pgw world file if exists. - # Not necessary after GMT 6.5.0. + # Remove the .pgw world file if exists. Not necessary after GMT 6.5.0. # See upstream fix https://github.com/GenericMappingTools/gmt/pull/7865 if ext == "tiff": fname.with_suffix(".pgw").unlink(missing_ok=True) - # Rename if file extension doesn't match the input file suffix + # Rename if file extension doesn't match the input file suffix. if ext != suffix[1:]: fname.with_suffix("." + ext).rename(fname) diff --git a/pygmt/tests/test_figure.py b/pygmt/tests/test_figure.py index 10076b69437..1831961ced4 100644 --- a/pygmt/tests/test_figure.py +++ b/pygmt/tests/test_figure.py @@ -196,12 +196,21 @@ def test_figure_savefig_transparent(): fname = f"{prefix}.{fmt}" with pytest.raises(GMTInvalidInput): fig.savefig(fname, transparent=True) - # png should not raise an error + + # PNG should support transparency and should not raise an error. fname = Path(f"{prefix}.png") fig.savefig(fname, transparent=True) assert fname.exists() fname.unlink() + # The companion PNG file with KML format should also support transparency. + fname = Path(f"{prefix}.kml") + fig.savefig(fname, transparent=True) + assert fname.exists() + fname.unlink() + assert fname.with_suffix(".png").exists() + fname.with_suffix(".png").unlink() + def test_figure_savefig_filename_with_spaces(): """