Skip to content

POC: Figure.clip: Clip paths by polygons, land or water #3878

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

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions doc/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Plotting map elements
Figure.text
Figure.timestamp
Figure.vlines
Figure.clip

Plotting tabular data
~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -220,6 +221,7 @@ Miscellaneous

which
show_versions
pygmt.src.clip.ClipAccessor

Datasets
--------
Expand Down
10 changes: 10 additions & 0 deletions pygmt/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import Literal, overload

from pygmt._typing import PathLike
from pygmt.src.clip import ClipAccessor

try:
import IPython
Expand Down Expand Up @@ -137,6 +138,15 @@
wesn = lib.extract_region()
return wesn

@property
def clip(self) -> ClipAccessor:
"""
Set up a clipping path.

:class:`pygmt.src.clip.ClipAccessor`
"""
return ClipAccessor()

Check warning on line 148 in pygmt/figure.py

View check run for this annotation

Codecov / codecov/patch

pygmt/figure.py#L148

Added line #L148 was not covered by tests

def savefig(
self,
fname: PathLike,
Expand Down
107 changes: 107 additions & 0 deletions pygmt/src/clip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
"""
Clip.
"""

from pygmt.clib import Session
from pygmt.helpers import build_arg_list


def _clip_method(clip_type):
"""
Return the clip method for the given clip type.
"""
return {

Check warning on line 13 in pygmt/src/clip.py

View check run for this annotation

Codecov / codecov/patch

pygmt/src/clip.py#L13

Added line #L13 was not covered by tests
"polygon": "clip",
"solar": "solar",
"mask": "mask",
"land": "coast",
"water": "coast",
"dcw": "coast",
}[clip_type]


class ClipAccessor:
"""
Clip.
"""

def __init__(self):
self.type = None
self.data = None
self.args_enter = ""
self.args_exit = ""

Check warning on line 32 in pygmt/src/clip.py

View check run for this annotation

Codecov / codecov/patch

pygmt/src/clip.py#L29-L32

Added lines #L29 - L32 were not covered by tests

def land(self):
"""
Clip the data to the land.
"""
self.type = "land"
self.data = None
self.args_enter = build_arg_list({"G": True})
self.args_exit = build_arg_list({"Q": True})
return self

Check warning on line 42 in pygmt/src/clip.py

View check run for this annotation

Codecov / codecov/patch

pygmt/src/clip.py#L38-L42

Added lines #L38 - L42 were not covered by tests

def water(self):
"""
Clip the data to the water.
"""
self.type = "water"
self.data = None
self.args_enter = build_arg_list({"S": True})
self.args_exit = build_arg_list({"Q": True})
return self

Check warning on line 52 in pygmt/src/clip.py

View check run for this annotation

Codecov / codecov/patch

pygmt/src/clip.py#L48-L52

Added lines #L48 - L52 were not covered by tests

def dcw(self):
"""
Clip based on the Digital Chart of the World.
"""
raise NotImplementedError

Check warning on line 58 in pygmt/src/clip.py

View check run for this annotation

Codecov / codecov/patch

pygmt/src/clip.py#L58

Added line #L58 was not covered by tests

def polygon(self, x, y):
"""
Clip the data to a polygon.

Parameters
----------
x/y
Coordinates of polygon.
"""
self.type = "polygon"
self.data = (x, y)
self.args_enter = []
self.args_exit = ["-C"]
return self

Check warning on line 73 in pygmt/src/clip.py

View check run for this annotation

Codecov / codecov/patch

pygmt/src/clip.py#L69-L73

Added lines #L69 - L73 were not covered by tests

def solar(self):
"""
Clip the data to the solar terminator.
"""
raise NotImplementedError

Check warning on line 79 in pygmt/src/clip.py

View check run for this annotation

Codecov / codecov/patch

pygmt/src/clip.py#L79

Added line #L79 was not covered by tests

def mask(self):
"""
Clip the data to a mask.
"""
raise NotImplementedError

Check warning on line 85 in pygmt/src/clip.py

View check run for this annotation

Codecov / codecov/patch

pygmt/src/clip.py#L85

Added line #L85 was not covered by tests

def __enter__(self):
"""
Enter the context manager.
"""
module = _clip_method(self.type)

Check warning on line 91 in pygmt/src/clip.py

View check run for this annotation

Codecov / codecov/patch

pygmt/src/clip.py#L91

Added line #L91 was not covered by tests

with Session() as lib:
if module == "clip":
with lib.virtualfile_in(x=self.data[0], y=self.data[1]) as vintbl:
lib.call_module(module, args=[vintbl])

Check warning on line 96 in pygmt/src/clip.py

View check run for this annotation

Codecov / codecov/patch

pygmt/src/clip.py#L93-L96

Added lines #L93 - L96 were not covered by tests
else:
lib.call_module(module, args=self.args_enter)
return self

Check warning on line 99 in pygmt/src/clip.py

View check run for this annotation

Codecov / codecov/patch

pygmt/src/clip.py#L98-L99

Added lines #L98 - L99 were not covered by tests

def __exit__(self, exc_type, exc_value, traceback):
"""
Exit the context manager.
"""
module = _clip_method(self.type)
with Session() as lib:
lib.call_module(module, args=self.args_exit)

Check warning on line 107 in pygmt/src/clip.py

View check run for this annotation

Codecov / codecov/patch

pygmt/src/clip.py#L105-L107

Added lines #L105 - L107 were not covered by tests