Skip to content

Keep the pixel registration of grids written through GDAL (#8633) - #9147

Merged
joa-quim merged 1 commit into
masterfrom
issue-8633
Aug 26, 2026
Merged

Keep the pixel registration of grids written through GDAL (#8633)#9147
joa-quim merged 1 commit into
masterfrom
issue-8633

Conversation

@joa-quim

Copy link
Copy Markdown
Member

Let grdsample and grdmath save pixel registered grids.

Fixes #8633 (grdsample) and #8988 (grdmath): a pixel registered grid saved with =gd came back gridline registered, its region pulled in by half a cell, so in practice only gridline registered grids could be written that way.

A new test gdal_registration.sh tests this issue.

Assisted by: GPT Terra and revised/corrected by Claude Opus 5.0

Fixes #8633 (grdsample) and #8988 (grdmath): a pixel registered grid saved with
=gd came back gridline registered, its region pulled in by half a cell, so in
practice only gridline registered grids could be written that way.

The raster itself was written correctly; what was missing was the tag that says
whether the values are cell centres or whole cells:

* gmt_gdalwrite set AREA_OR_POINT for GTiff output only, and then only when the
  grid was geographic or carried a WKT, and only when the user had passed no
  +c<options> - the call sat inside the "add default compression" block.  Plain
  Cartesian grids, which is what both reports used, got no tag at all.
* Even unconditionally, AREA_OR_POINT is not enough: GDAL never records it when
  it is "Area", that being the implicit default, and the GeoTIFF driver writes
  no keys whatsoever for a raster with no referencing.  So the registration of a
  Cartesian grid cannot be recovered from AREA_OR_POINT alone.

AREA_OR_POINT is now set for every driver except the picture-only ones (PNG,
JPEG, GIF, BMP, WEBP), which have nowhere to keep it and would only gain a
.aux.xml sidecar for it, and a GMT_REGISTRATION item is stored beside it stating
the registration outright.  GeoTIFF and netCDF keep both in the file; the drivers
that need PAM for this already write a .aux.xml for their band statistics, so no
new sidecar appears anywhere.  gmt_gdalread consults GMT_REGISTRATION and, when
present, prefers it to its own data-type guess; files without it - everything
written before this change, and everything from other software - are read exactly
as before.

Every GMT grid format now preserves pixel registration on output except the
three whose headers hold node coordinates only: Surfer 6, Surfer 7 and AGC, plus
GDAL's GSBG driver.  AGC already said so; Surfer now warns as well instead of
silently shifting the region.

Also add the missing break at the end of grdsample's -Q case, which fell through
into -T and set T.active for anyone using the deprecated option.

test/gdal/gdal_registration.sh covers the round trip both ways, through the
GeoTIFF and AAIGrid drivers, referenced and not, with and without +c<options>.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@joa-quim
joa-quim requested a review from a team August 25, 2026 21:32
@Esteban82 Esteban82 added the AI-assisted All (or most) of the code was written by Artificial Intelligence. label Aug 25, 2026

@Esteban82 Esteban82 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I ran the test and it worked fine. No messages appeared on the terminal.

@seisman

seisman commented Aug 26, 2026

Copy link
Copy Markdown
Member

The CI reports a few new failures due to this PR.

@anbj

anbj commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Excellent work. Hopefully the next time I need to make grids, I won't be so confused by this grid vs pixel registration thing.

@joa-quim

Copy link
Copy Markdown
Member Author

The 3 failures are

568 - test/grdimage/transp_mix.sh (Failed)
578 - test/grdinterpolate/slices.sh (Failed)
579 - test/grdinterpolate/slices_file.sh (Failed)

The transp_mix must be one that Claude committed without authorization from my disk and fails because the basemap PS is missing. The other two are strange. They also failed for me locally but pass when I run them individually.
I'll merge it anyway.

@joa-quim
joa-quim merged commit 8303866 into master Aug 26, 2026
11 of 14 checks passed
@joa-quim
joa-quim deleted the issue-8633 branch August 26, 2026 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI-assisted All (or most) of the code was written by Artificial Intelligence.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Let grdsample save pixel registered grids.

4 participants