Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions pygmt/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,14 @@ def psconvert(self, **kwargs):
)

def savefig(
self, fname, transparent=False, crop=True, anti_alias=True, show=False, **kwargs
self,
fname,
transparent=False,
crop=True,
anti_alias=True,
show=False,
worldfile=False,
**kwargs,
):
"""
Save the figure to a file.
Expand Down Expand Up @@ -289,8 +296,16 @@ def savefig(
the arguments ``"t2"`` and ``"g2"`` to the ``anti_aliasing``
parameter of :meth:`pygmt.Figure.psconvert`. Ignored if creating
vector graphics.
show: bool
show : bool
If ``True``, will open the figure in an external viewer.
worldfile : bool
If ``True``, will create a companion
`world file <https://en.wikipedia.org/wiki/World_file>`__ for the
figure. The world file will have the same name as the figure file
but with different extension (e.g. tfw for tif). See
https://en.wikipedia.org/wiki/World_file#Filename_extension
for the convention of world file extensions.
This parameter does not work for KML and GeoTIFF formats.
dpi : int
Set raster resolution in dpi [Default is ``720`` for PDF, ``300``
for others].
Expand All @@ -299,6 +314,7 @@ def savefig(
:meth:`pygmt.Figure.psconvert`. Valid parameters are ``gs_path``,
``gs_option``, ``resize``, ``bb_style``, and ``verbose``.
"""
# pylint: disable=too-many-branches
# All supported formats
fmts = {
"png": "g",
Expand Down Expand Up @@ -340,6 +356,13 @@ def savefig(
kwargs["Qt"] = 2
kwargs["Qg"] = 2

if worldfile:
if ext in ["kml", "tiff"]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are worldfiles possible for other formats like pdf/bmp/eps?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes for PDF and BMP.

Aslo yes for EPS, but for unknown reasons, the current codes doesn't produce a .esw file correctly. Need some time to find out why.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think I've ever heard of people create world files for PDF (pfw files?!), but ok then if it works. Have added another suggestion below to test that bmp/bpw files are created.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are worldfiles possible for other formats like pdf/bmp/eps?

For EPS, see an upstream issue report GenericMappingTools/gmt#7976

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See GenericMappingTools/gmt#7976, a world file is useless for vector images like EPS and PDF.

raise GMTInvalidInput(
f"Saving a world file is not supported for '{ext}' format."
)
kwargs["W"] = True

self.psconvert(prefix=prefix, fmt=fmt, crop=crop, **kwargs)

# Remove the .pgw world file if exists
Expand Down
27 changes: 27 additions & 0 deletions pygmt/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,33 @@ def mock_psconvert(*args, **kwargs): # pylint: disable=unused-argument
}


@pytest.mark.parametrize("fmt", [".png", ".pdf", ".jpg", ".tif"])
def test_figure_savefig_worldfile(fmt):
"""
Check if a world file is created when requested.
"""
fig = Figure()
fig.basemap(region=[0, 1, 0, 1], projection="X1c/1c", frame=True)
with GMTTempFile(prefix="pygmt-worldfile", suffix=fmt) as imgfile:
fig.savefig(fname=imgfile.name, worldfile=True)
assert Path(imgfile.name).stat().st_size > 0
worldfile_suffix = "." + fmt[1] + fmt[3] + "w"
assert Path(imgfile.name).with_suffix(worldfile_suffix).stat().st_size > 0


@pytest.mark.parametrize("fmt", [".tiff", ".kml"])
def test_figure_savefig_worldfile_unsupported_format(fmt):
"""
Figure.savefig should raise an error when a world file is requested for
an unsupported format.
"""
fig = Figure()
fig.basemap(region=[0, 1, 0, 1], projection="X1c/1c", frame=True)
with GMTTempFile(prefix="pygmt-worldfile", suffix=fmt) as imgfile:
with pytest.raises(GMTInvalidInput):
fig.savefig(fname=imgfile.name, worldfile=True)


@pytest.mark.skipif(IPython is None, reason="run when IPython is installed")
def test_figure_show():
"""
Expand Down