Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Figure.savefig: Add the 'worldfile' parameter to write a companion wo…
…rld file
  • Loading branch information
seisman committed Oct 24, 2023
commit 797e5c6c76a64e1e65a5e0dafc8858307306e043
24 changes: 23 additions & 1 deletion 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 @@ -291,6 +298,14 @@ def savefig(
vector graphics.
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. 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 Down Expand Up @@ -340,6 +355,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"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 worldfile 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 worldfile 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