Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clip parameter to pygmt.Figure.coast #1706

Draft
wants to merge 35 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
a2a2bc5
Add missing Q alias to pygmt.Figure.coast
michaelgrund Jan 17, 2022
5fb8590
format
michaelgrund Jan 17, 2022
ff3deb0
Merge branch 'main' into coast-add-q-alias
michaelgrund Jan 19, 2022
79e7a82
Merge branch 'main' into coast-add-q-alias
michaelgrund Feb 8, 2022
5288421
Merge branch 'main' into coast-add-q-alias
michaelgrund Feb 13, 2022
7d1342d
add clip parameter and different selection choices
michaelgrund Feb 13, 2022
63da1ae
remove old end_path parameter
michaelgrund Feb 13, 2022
0951160
Merge branch 'main' into coast-add-q-alias
michaelgrund Feb 14, 2022
e57c02b
Merge branch 'main' into coast-add-q-alias
michaelgrund Feb 15, 2022
cd800d3
updates based on code review
michaelgrund Feb 15, 2022
f44aa2e
Add plot into DVC
michaelgrund Feb 15, 2022
8fd9b59
format
michaelgrund Feb 15, 2022
4bc69a5
Merge branch 'main' into coast-add-q-alias
michaelgrund Feb 15, 2022
bbec7e7
Merge branch 'main' into coast-add-q-alias
michaelgrund Feb 16, 2022
1cd776f
Apply suggestions from code review
michaelgrund Feb 16, 2022
9a158e3
updates based on code review
michaelgrund Feb 16, 2022
082dadb
Apply suggestions from code review
michaelgrund Feb 16, 2022
2246ab5
Apply suggestions from code review
michaelgrund Feb 17, 2022
f750d10
change cmap in tests to relief
michaelgrund Feb 17, 2022
5c21707
use earth_age dataset for tests
michaelgrund Feb 17, 2022
e30d1e0
update baseline images in dvc
michaelgrund Feb 17, 2022
d7a34dc
format
michaelgrund Feb 17, 2022
bec668b
add test
michaelgrund Feb 17, 2022
c55e4dc
update
michaelgrund Feb 17, 2022
03625a8
remove unused data argument in test
michaelgrund Feb 17, 2022
05c384a
add inline example for clip
michaelgrund Feb 18, 2022
4827513
update tests and dvc figures
michaelgrund Feb 19, 2022
0c8fe0c
update docstring and format
michaelgrund Feb 19, 2022
cb955a0
Apply suggestions from code review
michaelgrund Feb 19, 2022
29076e7
Merge branch 'main' into coast-add-q-alias
michaelgrund Feb 19, 2022
448be5c
update inline example
michaelgrund Feb 19, 2022
053a478
update inline example
michaelgrund Feb 19, 2022
7dba666
Merge branch 'main' into coast-add-q-alias
michaelgrund Feb 20, 2022
00940db
Merge branch 'main' into coast-add-q-alias
michaelgrund Feb 21, 2022
98dde53
Merge branch 'main' into coast-add-q-alias
michaelgrund Mar 1, 2022
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
50 changes: 46 additions & 4 deletions pygmt/src/coast.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
t="transparency",
)
@kwargs_to_strings(R="sequence", c="sequence_comma", p="sequence")
def coast(self, **kwargs):
def coast(self, clip=None, **kwargs):
r"""
Plot continents, shorelines, rivers, and borders on maps

Expand Down Expand Up @@ -81,7 +81,7 @@ def coast(self, **kwargs):
(**h**\ )igh, (**i**\ )ntermediate, (**l**\ )ow,
and (**c**\ )rude.
land : str
Select filling or clipping of "dry" areas.
Select filling of "dry" areas.
rivers : int or str or list
*river*\ [/*pen*].
Draw rivers. Specify the type of rivers and [optionally] append
Expand Down Expand Up @@ -147,7 +147,7 @@ def coast(self, **kwargs):

a = All boundaries (1-3)
water : str
Select filling or clipping of "wet" areas.
Select filling of "wet" areas.
{U}
shorelines : int or str or list
[*level*\ /]\ *pen*.
Expand All @@ -159,6 +159,13 @@ def coast(self, **kwargs):
lake-in-island-in-lake shore. Pass a list of *level*\ /*pen*
strings to ``shorelines`` to set multiple levels. When specific
level pens are set, those not listed will not be drawn.
clip : str
To clip land do ``clip="land"``, ``clip="water"`` clips water. Use
``clip="end"`` to mark end of existing clip path. The clip path applies
to all plotting calls between the start of the clip path and the end of
the clip path. No projection information is needed. Also supply
``xshift`` and ``yshift`` settings if you have moved since the clip
started.
dcw : str or list
*code1,code2,…*\ [**+l**\|\ **L**\ ][**+g**\ *fill*\ ]
[**+p**\ *pen*\ ][**+z**].
Expand All @@ -181,12 +188,47 @@ def coast(self, **kwargs):
{p}
{t}
{V}

Example
-------
>>> # Initiate a clip path for Africa so that the subsequent colorimage of
>>> # gridded topography is only seen over land
>>> import pygmt # doctest: +SKIP
>>> # Load a grid of earth_relief_05m data, with an x-range of -30 to 30,
>>> # and a y-range of -40 to 40
>>> grid = pygmt.datasets.load_earth_relief(
... resolution="05m", region=[-30, 30, -40, 40]
... ) # doctest: +SKIP
>>> # Create an instance of the Figure class
>>> fig = pygmt.Figure() # doctest: +SKIP
>>> # Initiate clip path for land areas based on low-resolution coastlines
>>> fig.coast(
... projection="M12c", resolution="l", clip="land", frame=True
... ) # doctest: +SKIP
>>> # Plot the clipped grid
>>> fig.grdimage(grid=grid, cmap="relief") # doctest: +SKIP
>>> # End clip path
>>> fig.coast(clip="end") # doctest: +SKIP
"""
kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access

if clip:
if clip == "land":
kwargs["G"] = True
elif clip == "water":
kwargs["S"] = True
elif clip == "end":
kwargs["Q"] = True
else:
raise GMTInvalidInput(
"Invalid clip parameter. Must be one of 'land', 'water', and 'end'."
)
michaelgrund marked this conversation as resolved.
Show resolved Hide resolved

if not args_in_kwargs(args=["C", "G", "S", "I", "N", "E", "Q", "W"], kwargs=kwargs):
raise GMTInvalidInput(
"""At least one of the following parameters must be specified:
lakes, land, water, rivers, borders, dcw, Q, or shorelines"""
lakes, land, water, rivers, borders, dcw, clip, or shorelines"""
)

with Session() as lib:
lib.call_module("coast", build_arg_string(kwargs))
4 changes: 4 additions & 0 deletions pygmt/tests/baseline/test_coast_clip_land.png.dvc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
outs:
- md5: 12f5586967f870a7e70e012a5dadf76c
size: 14597
path: test_coast_clip_land.png
4 changes: 4 additions & 0 deletions pygmt/tests/baseline/test_coast_clip_water.png.dvc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
outs:
- md5: dcae0e21783cd153f6daaae927e3a771
size: 23423
path: test_coast_clip_water.png
54 changes: 54 additions & 0 deletions pygmt/tests/test_coast.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,57 @@ def test_coast_dcw_list():
dcw=["ES+gbisque+pgreen", "IT+gcyan+pblue"],
)
return fig


@pytest.mark.mpl_image_compare
def test_coast_clip_land():
"""
Test to clip dry areas.
"""
region = [-28, -10, 62, 68]

fig = Figure()
fig.basemap(region=region, projection="M8c", frame=True)
fig.coast(resolution="l", clip="land")
fig.plot(
x=[-22.5, -22.5, -15, -15],
y=[66, 64, 66, 64],
style="c4c",
color="red",
pen="1.5p,black",
)
fig.coast(clip="end")
return fig


@pytest.mark.mpl_image_compare
def test_coast_clip_water():
"""
Test to clip wet areas.
"""
region = [-28, -10, 62, 68]

fig = Figure()
fig.basemap(region=region, projection="M8c", frame=True)
fig.coast(resolution="l", clip="water")
fig.plot(
x=[-22.5, -22.5, -15, -15],
y=[66, 64, 66, 64],
style="c4c",
color="red",
pen="1.5p,black",
)
fig.coast(clip="end")
return fig


def test_coast_fail_invalid_parameter():
"""
Coast should raise an exception if an invalid parameter is given as input.
"""
region = [-28, -10, 62, 68]

fig = Figure()

with pytest.raises(GMTInvalidInput):
fig.coast(region=region, resolution="l", clip="invalid")