From b1ea2db0b6a5a90b3c1cd62e6bbbb287917b214b Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Tue, 27 Aug 2024 02:37:24 +0200 Subject: [PATCH] GRIB: adjust longitude range from [180, xxx] to [-180, xxx] Fixes #10655 --- autotest/gdrivers/data/grib/minx_180.grib2 | Bin 0 -> 23207 bytes autotest/gdrivers/grib.py | 7 +++++++ frmts/grib/gribdataset.cpp | 7 +++++++ 3 files changed, 14 insertions(+) create mode 100644 autotest/gdrivers/data/grib/minx_180.grib2 diff --git a/autotest/gdrivers/data/grib/minx_180.grib2 b/autotest/gdrivers/data/grib/minx_180.grib2 new file mode 100644 index 0000000000000000000000000000000000000000..dd785ceb34fdcfe94a1e53aa2995e2cb43d765da GIT binary patch literal 23207 zcmeIuF-k*W3;^K#&%RetCrcM65u9Ay92FOoyKzvuFK&~FY->epcyBObn|wI&D< zAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7 z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1pY5@k?{it CX(z@2 literal 0 HcmV?d00001 diff --git a/autotest/gdrivers/grib.py b/autotest/gdrivers/grib.py index fc20331cad99..66efaa6aef83 100755 --- a/autotest/gdrivers/grib.py +++ b/autotest/gdrivers/grib.py @@ -2419,3 +2419,10 @@ def test_grib_grib2_template_5_42_CCDS_aes_decompression(): assert ds.GetRasterBand(1).Checksum() == 41970 else: assert ds.GetRasterBand(1).Checksum() == -1 + + +# https://github.com/OSGeo/gdal/issues/10655 +def test_grib_grib2_minx_180(): + ds = gdal.Open("data/grib/minx_180.grib2") + gt = ds.GetGeoTransform() + assert gt == pytest.approx((-180.0625, 0.125, 0.0, 90.0625, 0.0, -0.125), rel=1e-6) diff --git a/frmts/grib/gribdataset.cpp b/frmts/grib/gribdataset.cpp index 7a9c4499beb6..49e1de0c8c2c 100644 --- a/frmts/grib/gribdataset.cpp +++ b/frmts/grib/gribdataset.cpp @@ -2681,6 +2681,13 @@ void GRIBDataset::SetGribMetaData(grib_MetaData *meta) if (rPixelSizeX * nRasterXSize > 360 + rPixelSizeX / 4) CPLDebug("GRIB", "Cannot properly handle GRIB2 files with " "overlaps and 0-360 longitudes"); + else if (rMinX == 180) + { + // Case of https://github.com/OSGeo/gdal/issues/10655 + CPLDebug("GRIB", "Shifting longitudes from %lf:%lf to %lf:%lf", + rMinX, rMaxX, -180.0, Lon360to180(rMaxX)); + rMinX = -180; + } else if (fabs(360 - rPixelSizeX * nRasterXSize) < rPixelSizeX / 4 && rMinX <= 180 && meta->gds.projType == GS3_LATLON) {