Skip to content

Commit 08ed217

Browse files
committed
Figure.clip: Initial implementation
1 parent 280595e commit 08ed217

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

pygmt/figure.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from tempfile import TemporaryDirectory
99
from typing import Literal, overload
1010

11+
from pygmt.src import clip
12+
1113
try:
1214
import IPython
1315

@@ -100,6 +102,8 @@ def __init__(self) -> None:
100102
self._preview_dir = TemporaryDirectory(prefix=f"{self._name}-preview-")
101103
self._activate_figure()
102104

105+
self.clip = clip()
106+
103107
def __del__(self) -> None:
104108
"""
105109
Clean up the temporary directory that stores the previews.
@@ -411,6 +415,7 @@ def _repr_html_(self) -> str:
411415

412416
from pygmt.src import ( # type: ignore[misc]
413417
basemap,
418+
clip,
414419
coast,
415420
colorbar,
416421
contour,

pygmt/src/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pygmt.src.basemap import basemap
66
from pygmt.src.binstats import binstats
77
from pygmt.src.blockm import blockmean, blockmedian, blockmode
8+
from pygmt.src.clip import clip
89
from pygmt.src.coast import coast
910
from pygmt.src.colorbar import colorbar
1011
from pygmt.src.config import config

pygmt/src/clip.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"""
2+
Clip.
3+
"""
4+
5+
from pygmt.clib import Session
6+
from pygmt.helpers import build_arg_list
7+
8+
9+
def _clip_method(clip_type):
10+
"""
11+
Return the clip method for the given clip type.
12+
"""
13+
match clip_type:
14+
case "polygon":
15+
return "clip"
16+
case "land":
17+
return "coast"
18+
case "water":
19+
return "coast"
20+
21+
22+
class clip: # noqa: N801
23+
"""
24+
Clip.
25+
"""
26+
27+
def __init__(self):
28+
self.type = None
29+
self.data = None
30+
self.args_enter = ""
31+
self.args_exit = ""
32+
33+
def polygon(self, x, y):
34+
"""
35+
Clip the data to a polygon.
36+
"""
37+
self.type = "polygon"
38+
self.data = (x, y)
39+
self.args_enter = []
40+
self.args_exit = ["-C"]
41+
return self
42+
43+
def land(self):
44+
"""
45+
Clip the data to the land.
46+
"""
47+
self.type = "land"
48+
self.data = None
49+
self.args_enter = build_arg_list({"G": True})
50+
self.args_exit = build_arg_list({"Q": True})
51+
return self
52+
53+
def water(self):
54+
"""
55+
Clip the data to the water.
56+
"""
57+
self.type = "water"
58+
self.data = None
59+
self.args_enter = build_arg_list({"S": True})
60+
self.args_exit = build_arg_list({"Q": True})
61+
return self
62+
63+
def __enter__(self):
64+
"""
65+
Enter the context manager.
66+
"""
67+
module = _clip_method(self.type)
68+
69+
with Session() as lib:
70+
if module == "clip":
71+
with lib.virtualfile_in(x=self.data[0], y=self.data[1]) as vintbl:
72+
lib.call_module(module, args=[vintbl])
73+
else:
74+
lib.call_module(module, args=self.args_enter)
75+
return self
76+
77+
def __exit__(self, exc_type, exc_value, traceback):
78+
"""
79+
Exit the context manager.
80+
"""
81+
module = _clip_method(self.type)
82+
with Session() as lib:
83+
lib.call_module(module, args=self.args_exit)

0 commit comments

Comments
 (0)