Figure.clip_xxx: A consistent API for clipping by polygon, land, water, solar, and mask

Open
#4,751 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Quiet
Tech stack
python

Research direction

Start by reviewing the proof of concept in #3878 and POC PR #4703, then compare the four API designs against the listed GMT clipping modes. Done means the project settles on an API and defines coverage for polygon, land, water, mask, DCW, and solar clipping.

Written by the indexing model from the issue text.

Description

feature request

GMT supports several related clipping operations:

Description Activate Deactivate User input
Clip land areas coast -G coast -Q No
Clip water areas coast -S coast -Q No
Clip by polygon clip clip -C (x, y) coordinates
Clip by mask mask mask -C (x, y) coordinates
Clip by a country or region code coast -E+c coast -Q No
Clip by solar terminators solar -G clip -C No

For every mode, subsequent plotting commands are clipped until the path is explicitly ended. This stateful behavior makes clipping a natural fit for context managers.

Possible API designs:

  1. Low-level begin/end methods

    fig.begin_clip(data=polygon)
    fig.plot(...)
    fig.end_clip()
    

    This closely mirrors GMT, but requires users to end clipping explicitly and can leave a clip path active after an exception.

  2. A single context-manager method with a mode

    with fig.clip(mode="polygon", data=polygon):
        ...
    with fig.clip(mode="land"):
        ...
    with fig.clip(mode="dcw", code="US"):
        ...
    

    This has a compact API, but each mode wraps a different GMT module and accepts different parameters. Documenting and validating this mixed argument set could become complicated.

  3. A clip namespace

    with fig.clip.polygon(data=polygon):
        ...
    
    with fig.clip.land():
        ...
    

    This groups related functionality neatly, but may be challenging to expose and document correctly. See the proof of concept in #3878.

  4. Explicit context-manager methods

    with fig.clip_polygon(data=polygon):    
        ...
    with fig.clip_land():
       ...
    with fig.clip_water():
       ...
    

    This one is much easier to document that option 4. A POC PR is at #4703.

I feel option 4 is the best API design.

Dominant language
Python
Stars
882
Forks
261
Avg merge
1d 3h
Merged PRs (30d)
29

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from GenericMappingTools/pygmt

All issues in GenericMappingTools/pygmt

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.