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 4 commits into
base: main
Choose a base branch
from

Conversation

seisman
Copy link
Member

@seisman seisman commented Mar 26, 2025

Description of proposed changes

This is an initial implementation of wrapping the clip method, based on the idea proposed in #1706 (comment), i.e., integrate all clipping functionalities (clip for clipping a polygon path, coast for clipping the land, water or dcw, and maybe more).

This PR almost works as expected, but I'm having difficulties adding Figure.clip into the documentation.

Syntax

import pygmt
fig = pygmt.Figure()
with fig.clip.land():
    # Plot data with land clipped
with fig.clip.water():
    # Plot data with water clipped
with fig.clip.polygon(...):
    # Plot data with polygons clipped
with fig.clip.dcw(...):
    # Plot data with clipping based on dcw

Example

import pygmt
from pygmt.datasets import load_earth_relief

grid = load_earth_relief()
fig = pygmt.Figure()
fig.basemap(region="g", projection="H15c", frame=["a", "+tClipping land"])
with fig.clip.land():
    fig.grdimage(grid, cmap="geo")

fig.shift_origin(yshift=10)
fig.basemap(region="g", projection="H15c", frame=["a", "+tClipping water"])
with fig.clip.water():
    fig.grdimage(grid, cmap="geo")

fig.shift_origin(yshift=-10, xshift=16)
fig.basemap(region="g", projection="H15c", frame=["a", "+tClipping polygon"])
with fig.clip.polygon(x=[60, 60, 150, 150], y=[-30, 60, 60, -30]):
    fig.grdimage(grid, cmap="geo")

fig.shift_origin(yshift=10)
fig.basemap(region="g", projection="H15c", frame=["a", "+tMixed clipping"])
with fig.clip.polygon(x=[60, 60, 150, 150], y=[-30, 60, 60, -30]):
    with fig.clip.land():
        fig.grdimage(grid, cmap="geo")
with fig.clip.polygon(x=[200, 200, 250, 250], y=[-30, 60, 60, -30]):
    with fig.clip.water():
        fig.grdimage(grid, cmap="geo")

fig.show()

map

Supersedes PR #1779, #1706

Preview:

Reminders

  • Run make format and make check to make sure the code follows the style guide.
  • Add tests for new features or tests that would have caught the bug that you're fixing.
  • Add new public functions/methods/classes to doc/api/index.rst.
  • Write detailed docstrings for all functions/methods.
  • If wrapping a new module, open a 'Wrap new GMT module' issue and submit reasonably-sized PRs.
  • If adding new functionality, add an example to docstrings or tutorials.

Slash Commands

You can write slash commands (/command) in the first line of a comment to perform
specific operations. Supported slash command is:

  • /format: automatically format and lint the code

@seisman seisman added the feature Brand new feature label Mar 26, 2025
@yvonnefroehlich
Copy link
Member

This PR almost works as expected, but I'm having difficulties adding Figure.clip into the documentation.

Hm. Maybe adding the method to doc/api/index.rst (https://github.com/GenericMappingTools/pygmt/blob/main/doc/api/index.rst?plain=1) is just missing?

@seisman
Copy link
Member Author

seisman commented Mar 27, 2025

This PR almost works as expected, but I'm having difficulties adding Figure.clip into the documentation.

Hm. Maybe adding the method to doc/api/index.rst (https://github.com/GenericMappingTools/pygmt/blob/main/doc/api/index.rst?plain=1) is just missing?

No, it won't work, because Figure.clip is not a class method, but an attribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Brand new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants