Skip to content

Commit a9a02d5

Browse files
committed
Figure.coast: Improve parameters lakes/river_lakes for setting fill of lakes/river-lakes
1 parent 205739b commit a9a02d5

1 file changed

Lines changed: 69 additions & 16 deletions

File tree

pygmt/src/coast.py

Lines changed: 69 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,64 @@
88
from pygmt.alias import Alias, AliasSystem
99
from pygmt.clib import Session
1010
from pygmt.exceptions import GMTInvalidInput
11-
from pygmt.helpers import args_in_kwargs, build_arg_list, fmt_docstring, use_alias
11+
from pygmt.helpers import (
12+
args_in_kwargs,
13+
build_arg_list,
14+
fmt_docstring,
15+
is_nonstr_iter,
16+
use_alias,
17+
)
1218
from pygmt.params import Box
1319

1420
__doctest_skip__ = ["coast"]
1521

1622

23+
def _alias_option_C(lakes=None, river_lakes=None): # noqa: N802
24+
"""
25+
Helper function to create the alias list for the -C option.
26+
27+
Example
28+
-------
29+
>>> def parse(**kwargs):
30+
... return AliasSystem(C=_alias_option_C(**kwargs)).get("C")
31+
>>> parse()
32+
>>> parse(lakes="blue")
33+
'blue'
34+
>>> parse(river_lakes="cyan")
35+
'cyan+r'
36+
>>> parse(lakes="blue", river_lakes="cyan")
37+
['blue+l', 'cyan+r']
38+
39+
>>> # Check for backward compatibility
40+
>>> parse(lakes="blue+l")
41+
'blue+l'
42+
>>> parse(lakes="cyan+r")
43+
'cyan+r'
44+
>>> parse(lakes=["blue+l", "cyan+r"])
45+
['blue+l', 'cyan+r']
46+
47+
>>> # Check for mixed usage error
48+
>>> parse(lakes=["blue+l", "cyan+r"], river_lakes="cyan")
49+
Traceback (most recent call last):
50+
...
51+
pygmt.exceptions.GMTInvalidInput: Parameter 'lakes' is given with a list; ...
52+
"""
53+
# Check for backward compatibility.
54+
if is_nonstr_iter(lakes): # Old syntax: lakes is a list of strings.
55+
if river_lakes is not None:
56+
msg = "Parameter 'lakes' is given with a list; 'river_lakes' must be None."
57+
raise GMTInvalidInput(msg)
58+
return Alias(lakes, name="lakes") # Return as is.
59+
60+
# If only 'lakes' is specified, no suffix is needed.
61+
return [
62+
Alias(lakes, name="lakes", suffix="+l" if river_lakes is not None else ""),
63+
Alias(river_lakes, name="river_lakes", suffix="+r"),
64+
]
65+
66+
1767
@fmt_docstring
18-
@use_alias(A="area_thresh", C="lakes", E="dcw")
68+
@use_alias(A="area_thresh", E="dcw")
1969
def coast( # noqa: PLR0913
2070
self,
2171
resolution: Literal[
@@ -26,6 +76,8 @@ def coast( # noqa: PLR0913
2676
rivers: int | str | Sequence[int | str] | None = None,
2777
borders: int | str | Sequence[int | str] | None = None,
2878
shorelines: bool | str | Sequence[int | str] = False,
79+
lakes: str | None = None,
80+
river_lakes: str | None = None,
2981
map_scale: str | None = None,
3082
box: Box | bool = False,
3183
projection: str | None = None,
@@ -59,6 +111,7 @@ def coast( # noqa: PLR0913
59111
60112
$aliases
61113
- B = frame
114+
- C = lakes, river_lakes
62115
- D = resolution
63116
- F = box
64117
- G = land
@@ -74,18 +127,7 @@ def coast( # noqa: PLR0913
74127
75128
Parameters
76129
----------
77-
$projection
78-
$region
79-
*Required if this is the first plot command.*
80130
$area_thresh
81-
$frame
82-
lakes : str or list
83-
*fill*\ [**+l**\|\ **+r**].
84-
Set the shade, color, or pattern for lakes and river-lakes. The
85-
default is the fill chosen for "wet" areas set by the ``water``
86-
parameter. Optionally, specify separate fills by appending
87-
**+l** for lakes or **+r** for river-lakes, and passing multiple
88-
strings in a list.
89131
resolution
90132
Select the resolution of the coastline dataset to use. The available resolutions
91133
from highest to lowest are: ``"full"``, ``"high"``, ``"intermediate"``,
@@ -96,6 +138,11 @@ def coast( # noqa: PLR0913
96138
Select filling of "dry" areas.
97139
water
98140
Select filling of "wet" areas.
141+
lakes
142+
river_lakes
143+
Select filling of lakes and river-lakes. If not specified, will use the fill for
144+
"wet" areas set by the ``water`` parameter. If ``lakes`` is specified but
145+
``river_lakes`` isn't, ``river_lakes`` will use the same fill as ``lakes``.
99146
rivers
100147
Draw rivers. Specify the type of rivers to draw, and optionally append a pen
101148
attribute, in the format *river*\ /*pen* [Default pen is
@@ -203,10 +250,14 @@ def coast( # noqa: PLR0913
203250
to any of the continent codes (e.g. ``"=EU"`` for Europe). Append
204251
**+p**\ *pen* to draw polygon outlines [Default is no outline] and
205252
**+g**\ *fill* to fill them [Default is no fill].
253+
$projection
254+
$region
255+
*Required if this is the first plot command.*
256+
$frame
257+
$verbose
206258
$panel
207259
$perspective
208260
$transparency
209-
$verbose
210261
211262
Example
212263
-------
@@ -239,15 +290,17 @@ def coast( # noqa: PLR0913
239290
and kwargs.get("I", rivers) is None
240291
and kwargs.get("N", borders) is None
241292
and kwargs.get("W", shorelines) is False
242-
and not args_in_kwargs(args=["C", "E", "Q"], kwargs=kwargs)
293+
and kwargs.get("C", lakes or river_lakes) is None
294+
and not args_in_kwargs(args=["E", "Q"], kwargs=kwargs)
243295
):
244296
msg = (
245297
"At least one of the following parameters must be specified: "
246-
"land, water, rivers, borders, shorelines, lakes, dcw, or Q."
298+
"land, water, rivers, borders, shorelines, lakes, river_lakes, dcw, or Q."
247299
)
248300
raise GMTInvalidInput(msg)
249301

250302
aliasdict = AliasSystem(
303+
C=_alias_option_C(lakes=lakes, river_lakes=river_lakes),
251304
D=Alias(
252305
resolution,
253306
name="resolution",

0 commit comments

Comments
 (0)