Skip to content

Commit

Permalink
gdalwarp: make sure to check that angular unit is degree for heuristi…
Browse files Browse the repository at this point in the history
…cs related to geographic CRS

Fixes OSGeo#10975
  • Loading branch information
rouault committed Oct 10, 2024
1 parent 7abb947 commit 75178bd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
25 changes: 16 additions & 9 deletions alg/gdaltransformer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ CPLErr CPL_STDCALL GDALSuggestedWarpOutput2(GDALDatasetH hSrcDS,
"transform.",
nFailedCount, nSamplePoints);

bool bIsGeographicCoords = false;
bool bIsGeographicCoordsDeg = false;
if (bIsGDALGenImgProjTransform)
{
const GDALGenImgProjTransformInfo *pGIPTI =
Expand Down Expand Up @@ -899,15 +899,17 @@ CPLErr CPL_STDCALL GDALSuggestedWarpOutput2(GDALDatasetH hSrcDS,
CSLFetchNameValue(pGLTI->papszGeolocationInfo, "SRS");
if (pszGLSRS == nullptr)
{
bIsGeographicCoords = true;
bIsGeographicCoordsDeg = true;
}
else
{
OGRSpatialReference oSRS;
if (oSRS.SetFromUserInput(pszGLSRS) == OGRERR_NONE &&
oSRS.IsGeographic())
oSRS.IsGeographic() &&
std::fabs(oSRS.GetAngularUnits() -
CPLAtof(SRS_UA_DEGREE_CONV)) < 1e-9)
{
bIsGeographicCoords = true;
bIsGeographicCoordsDeg = true;
}
}
}
Expand Down Expand Up @@ -967,7 +969,7 @@ CPLErr CPL_STDCALL GDALSuggestedWarpOutput2(GDALDatasetH hSrcDS,
CPLAtof(SRS_UA_DEGREE_CONV)) < 1e-9 &&
(!poSourceCRS || !poSourceCRS->IsGeographic()))
{
bIsGeographicCoords = true;
bIsGeographicCoordsDeg = true;

std::unique_ptr<CPLConfigOptionSetter> poSetter;
if (pGIPTI->bCheckWithInvertPROJ)
Expand Down Expand Up @@ -1194,7 +1196,7 @@ CPLErr CPL_STDCALL GDALSuggestedWarpOutput2(GDALDatasetH hSrcDS,
/* Recompute some bounds so that all return values are consistent */
/* -------------------------------------------------------------------- */
double dfMaxXOutNew = dfMinXOut + (*pnPixels) * dfPixelSizeX;
if (bIsGeographicCoords &&
if (bIsGeographicCoordsDeg &&
((dfMaxXOut <= 180 && dfMaxXOutNew > 180) || dfMaxXOut == 180))
{
dfMaxXOut = 180;
Expand All @@ -1206,7 +1208,7 @@ CPLErr CPL_STDCALL GDALSuggestedWarpOutput2(GDALDatasetH hSrcDS,
}

double dfMinYOutNew = dfMaxYOut - (*pnLines) * dfPixelSizeY;
if (bIsGeographicCoords && dfMinYOut >= -90 && dfMinYOutNew < -90)
if (bIsGeographicCoordsDeg && dfMinYOut >= -90 && dfMinYOutNew < -90)
{
dfMinYOut = -90;
dfPixelSizeY = (dfMaxYOut - dfMinYOut) / *pnLines;
Expand Down Expand Up @@ -1426,8 +1428,11 @@ static void InsertCenterLong(GDALDatasetH hDS, OGRSpatialReference *poSRS,
CPLStringList &aosOptions)

{
if (!poSRS->IsGeographic())
if (!poSRS->IsGeographic() || std::fabs(poSRS->GetAngularUnits() -
CPLAtof(SRS_UA_DEGREE_CONV)) > 1e-9)
{
return;
}

if (poSRS->GetExtension(nullptr, "CENTER_LONG"))
return;
Expand Down Expand Up @@ -2967,7 +2972,9 @@ int GDALTransformLonLatToDestGenImgProjTransformer(void *hTransformArg,
double z = 0;
int success = true;
auto poSourceCRS = psReprojInfo->poForwardTransform->GetSourceCS();
if (poSourceCRS->IsGeographic())
if (poSourceCRS->IsGeographic() &&
std::fabs(poSourceCRS->GetAngularUnits() -
CPLAtof(SRS_UA_DEGREE_CONV)) < 1e-9)
{
// Optimization to avoid creating a OGRCoordinateTransformation
OGRAxisOrientation eSourceFirstAxisOrient = OAO_Other;
Expand Down
Binary file added autotest/utilities/data/geog_arc_second.tif
Binary file not shown.
16 changes: 16 additions & 0 deletions autotest/utilities/test_gdalwarp_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -4314,3 +4314,19 @@ def test_gdalwarp_lib_average_ten_ten_to_one_one():
"", src_ds, width=1, height=1, resampleAlg=gdal.GRIORA_Average, format="MEM"
)
assert out_ds.GetRasterBand(1).ComputeRasterMinMax() == (1, 1)


###############################################################################
# Test bugfix for https://github.com/OSGeo/gdal/issues/10975


def test_gdalwarp_lib_src_is_geog_arc_second():

out_ds = gdal.Warp(
"",
"data/geog_arc_second.tif",
options="-f MEM -srcnodata 0 -te -81.5 28.5 -81.0 29.0 -t_srs EPSG:4326",
)
assert out_ds.RasterXSize == 5464
assert out_ds.RasterYSize == 5464
assert out_ds.GetRasterBand(1).Checksum() == 31856

0 comments on commit 75178bd

Please sign in to comment.