Skip to content

pygmt.grdmask: Create mask grid from polygons or point coverage(required arguments)#4428

Open
Chuan1937 wants to merge 10 commits intoGenericMappingTools:mainfrom
Chuan1937:grdmask1
Open

pygmt.grdmask: Create mask grid from polygons or point coverage(required arguments)#4428
Chuan1937 wants to merge 10 commits intoGenericMappingTools:mainfrom
Chuan1937:grdmask1

Conversation

@Chuan1937
Copy link
Member

@Chuan1937 Chuan1937 commented Feb 25, 2026

pygmt.grdmask: Create mask grid from polygons or point coverage(required arguments)

Preview: https://pygmt-dev--4428.org.readthedocs.build/en/4428/api/generated/pygmt.grdmask.html#pygmt.grdmask

@Chuan1937 Chuan1937 changed the title pygmt.grdmask: Create mask grid from polygons or point coverage pygmt.grdmask: Create mask grid from polygons or point coverage(required arguments) Feb 25, 2026
@Chuan1937 Chuan1937 self-assigned this Feb 25, 2026
Chuan1937 and others added 3 commits February 25, 2026 22:01
Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
- **Point coverage mode**: Data points (used with ``search_radius`` parameter)
$outgrid
$spacing
mask_values
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both GMT and GMT.jl use out_edge_in, but I feel mask_values is readable and more consistent with modules like grdlandmask and select.

What's your plan about syntax like -Nz/-NZ/-Np/-NP?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The plan is to make the mask_values parameter support both lists and strings:
List format: mask_values=[0, 0, 1] (current usage),
String format: mask_values="Nz" / “NZ” / ‘Np’ / “NP” (corresponding to GMT's -Nz/-NZ/-Np/-NP),
What are your thoughts?

Copy link
Member

@seisman seisman Feb 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the GMT documentation, it seems the syntax is more complicated than that. The following are also valid: -Nz/1, -Np5, but syntax like mask_values="z/1", or mask_values="p5" are still not Pythonic.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Three new parameters—id_mode, id_start, and out_value—can be added to correspond to -Nz/Z/p/P and their variants, while being mutually exclusive with mask_values.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking about outside, edge, inside rather than mask_values.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that you've already implemented the outside/edge/inside options. Could you please elaborate on the other related parameters? Just describe what you're planning to do, and there's no need to implement it yet. That way, we can review whether the proposed approach makes sense before moving forward.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use -Nz to set polygon insides to the z-value obtained from the data (either segment header -Zzval, -Lheader or via -aZ=name); use -NZ to consider the polygon boundary as part of the inside. Alternatively, use -Np to use a running number as polygon ID; optionally append start of the sequence [0].

Actually, I don't fully understand the GMT documentation. As I understand it, -Nz and -Np control the value for polygon inside. -Nz sets the inside to be the z-value from the segment header, and -Np sets it to a polygon ID (starting from 0). Then -NZ and -NP have the same meaning but also force edge to be the same as inside. So, maybe we should allow syntax like inside="z" and inside="id".

  • outside=1, edge=2, inside=3 -> 1/2/3
  • inside="z" -> -Nz
  • inside="id" -> -Np
  • outside=1, inside="z" -> -Nz/1 [Can we set edge in this case?]
  • outside=1, inside="id" -> -Np/1 [Can we set edge in this case?]
  • outside=1, inside="z", edge="z" -> -NZ/1
  • outside=1, inside="id", edge="id" -> -NP/1
  • outside=1, inside="z", edge="id": invalid

Copy link
Member Author

@Chuan1937 Chuan1937 Feb 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

search_radius (-S):

search_radius="50k" → -S50k (point coverage mode, 50km search radius)
search_radius="z" → -Sz (read radius from data column 3)
search_radius="1c" → -S1c (1 grid cell)

clobber (-C):

clobber="first" → -Cf (use the first overlapping polygon)
clobber="last" → -Co (use the last overlapping polygon)
clobber="low" → -Cl (use the polygon with the lowest z-value)
clobber="upper" → -Cu (use the polygon with the highest z-value)

straight_line (-A):

straight_line=True → -A (Use straight lines instead of great circles for geographic coordinates)
straight_line="m" → -Am (Meridians first, parallels second)
straight_line="p" → -Ap (Parallels first, meridians second)

Chuan1937 and others added 2 commits February 25, 2026 22:21
Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
@Chuan1937 Chuan1937 requested a review from seisman February 26, 2026 05:40
if spacing is None or region is None:
raise GMTParameterError(required=["region", "spacing"])

# Build the -N parameter string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put the related codes into a private function (e.g., _build_N_option).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants