Skip to content

Commit dc5ecb9

Browse files
committed
Implement the Pattern class for specifying pattern as fills
1 parent a337503 commit dc5ecb9

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

doc/api/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ Class-style Parameters
212212
:toctree: generated
213213

214214
Box
215+
Pattern
215216

216217
Enums
217218
-----

pygmt/params/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
"""
44

55
from pygmt.params.box import Box
6+
from pygmt.params.pattern import Pattern

pygmt/params/pattern.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
"""
2+
The Pattern class for specifying GMT filling patterns.
3+
"""
4+
5+
import dataclasses
6+
7+
from pygmt.alias import Alias
8+
from pygmt.exceptions import GMTValueError
9+
from pygmt.params.base import BaseParam
10+
11+
__doctest_skip__ = ["Pattern"]
12+
13+
14+
@dataclasses.dataclass(repr=False)
15+
class Pattern(BaseParam):
16+
"""
17+
Class for GMT filling patterns.
18+
19+
Parameters
20+
----------
21+
id
22+
The pattern identifier. It can be in two forms:
23+
24+
- An integer in the range 1-90, corresponding to one of
25+
:doc:`the 90 predefined 64x64 bit-patterns </techref/patterns>` provided with
26+
GMT.
27+
- The name of a 1-, 8-, or 24-bit image raster file, to create customized,
28+
repeating images using image raster files.
29+
dpi
30+
Resolution of the pattern in dots per inch (DPI) [Default is 1200].
31+
bgcolor/fgcolor
32+
The background/foreground color for predefined bit-patterns or 1-bit images.
33+
[Default is white for background and black for foreground]. Setting either to
34+
an empty string will yield a transparent background/foreground where only the
35+
foreground or background pixels will be painted.
36+
reversed
37+
If True, the pattern will be bit-reversed, i.e., white and black areas will be
38+
interchanged (only applies to 1-bit images or predefined bit-image patterns).
39+
40+
Examples
41+
--------
42+
Draw a global map with land areas filled with pattern 15 in a light red background
43+
and 300 dpi resolution:
44+
45+
>>> import pygmt
46+
>>> from pygmt.params import Pattern
47+
>>> fig = pygmt.Figure()
48+
>>> fig.coast(
49+
... region="g",
50+
... projection="H10c",
51+
... frame=True,
52+
... land=Pattern(15, bgcolor="lightred", dpi=300),
53+
... shorelines=True,
54+
... )
55+
>>> fig.show()
56+
"""
57+
58+
id: int | str
59+
dpi: int | None = None
60+
bgcolor: str | None = None
61+
fgcolor: str | None = None
62+
reversed: bool = False
63+
64+
def _validate(self):
65+
"""
66+
Validate the parameters.
67+
"""
68+
if isinstance(self.id, int) and not (1 <= self.id <= 90):
69+
raise GMTValueError(
70+
self.id,
71+
description="pattern id",
72+
reason=(
73+
"Pattern id must be an integer in the range 1-90 "
74+
"or the name of a 1-, 8-, or 24-bit image raster file."
75+
),
76+
)
77+
78+
@property
79+
def _aliases(self):
80+
"""
81+
Aliases for the Pattern class.
82+
"""
83+
return [
84+
Alias(self.id, name="id", prefix="P" if self.reversed else "p"),
85+
Alias(self.bgcolor, name="bgcolor", prefix="+b"),
86+
Alias(self.fgcolor, name="fgcolor", prefix="+f"),
87+
Alias(self.dpi, name="dpi", prefix="+r"),
88+
]

0 commit comments

Comments
 (0)