Commit 2d9f54c
committed
geotiff: reject writes whose crs and crs_wkt attrs disagree (#1987 PR 1)
First fail-closed slice for issue #1987. The framework PR (#2006)
landed the error hierarchy and the validator-hook registry; this PR
wires the first check.
Behaviour today
---------------
``to_geotiff`` / ``_write_vrt_tiled`` / ``write_geotiff_gpu`` all
consult ``data.attrs.get('crs')`` first and fall back to
``data.attrs.get('crs_wkt')`` only when the EPSG attr is missing.
When the two attrs encode different CRSes the writer silently emits
the EPSG and drops the WKT, producing a TIFF whose on-disk CRS does
not match what the DataArray advertised.
Change
------
* ``_validation.py`` gains ``_check_write_conflicting_crs``: when
both attrs are populated and neither is suppressed by an explicit
``crs=`` kwarg, the check canonicalises each via pyproj and raises
``ConflictingCRSError`` if they disagree. ``ConflictingCRSError``
is exported from ``xrspatial.geotiff`` already (the framework PR
added it to ``_errors.py``).
* The check is registered at module load via
``register_write_metadata_check``. Tests that need to scope around
it can call ``unregister_write_metadata_check`` in a fixture.
* The three write entry points (``to_geotiff``,
``_write_vrt_tiled``, ``write_geotiff_gpu``) now call
``validate_write_metadata`` with a context dict carrying
``crs_kwarg``, ``attrs_crs``, and ``attrs_crs_wkt``.
Soft preconditions kept lenient
-------------------------------
* No pyproj installed -> no-op (the writer's own WKT-fallback path
decides what happens; pyproj is otherwise listed in setup.cfg).
* Either attr unparseable -> no-op (sibling
``UnparseableCRSError`` PR will catch those).
* Caller passes ``crs=`` explicitly -> short-circuit, since the
kwarg overrides both attrs for this write.
Round-trip safety
-----------------
The reader emits ``attrs['crs']`` and ``attrs['crs_wkt']`` whenever
the file has a CRS, so a typical ``open_geotiff -> to_geotiff`` pair
hits the check with both attrs set. They derive from the same on-disk
CRS, so pyproj equality passes and the write proceeds. The
end-to-end ``test_read_back_roundtrip_does_not_raise`` pins this.
Tests
-----
14 new tests in ``test_conflicting_crs_write_1987.py`` cover:
* registration in the write-side hook registry
* exception-class hierarchy (ValueError + GeoTIFFAmbiguousMetadataError)
* disagreement raises
* agreement passes (EPSG int + WKT, EPSG string + WKT)
* only-one-attr-set cases pass
* explicit ``crs=`` kwarg bypasses the check
* ``crs=0`` corner does not mask the existing kwarg validation
* unparseable WKT defers to the (future) ``UnparseableCRSError``
* end-to-end read-back round-trip
Full geotiff test suite passes (2993 tests, no regressions).1 parent d1bd260 commit 2d9f54c
5 files changed
Lines changed: 381 additions & 0 deletions
File tree
- xrspatial/geotiff
- _writers
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
| |||
433 | 434 | | |
434 | 435 | | |
435 | 436 | | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| 47 | + | |
47 | 48 | | |
48 | 49 | | |
49 | 50 | | |
| |||
270 | 271 | | |
271 | 272 | | |
272 | 273 | | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
273 | 286 | | |
274 | 287 | | |
275 | 288 | | |
| |||
785 | 798 | | |
786 | 799 | | |
787 | 800 | | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
788 | 812 | | |
789 | 813 | | |
790 | 814 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
| |||
261 | 262 | | |
262 | 263 | | |
263 | 264 | | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
264 | 274 | | |
265 | 275 | | |
266 | 276 | | |
| |||
0 commit comments