Skip to content

Commit 4061ad3

Browse files
authored
gdal_translate: transform entire bounds of -projwin, not just two corners (#12127)
References #12100
1 parent c4a2e0b commit 4061ad3

File tree

4 files changed

+51
-9
lines changed

4 files changed

+51
-9
lines changed

apps/gdal_translate_lib.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -956,10 +956,11 @@ GDALDatasetH GDALTranslate(const char *pszDest, GDALDatasetH hSrcDataset,
956956
OGRCoordinateTransformation *poCT =
957957
OGRCreateCoordinateTransformation(&oSRSIn, &oSRSDS);
958958
if (!(poCT &&
959-
poCT->Transform(1, &psOptions->dfULX,
960-
&psOptions->dfULY) &&
961-
poCT->Transform(1, &psOptions->dfLRX,
962-
&psOptions->dfLRY)))
959+
poCT->TransformBounds(
960+
psOptions->dfULX, psOptions->dfLRY,
961+
psOptions->dfLRX, psOptions->dfULY,
962+
&psOptions->dfULX, &psOptions->dfLRY,
963+
&psOptions->dfLRX, &psOptions->dfULY, 21)))
963964
{
964965
OGRCoordinateTransformation::DestroyCT(poCT);
965966

autotest/gcore/vrt_read.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1626,9 +1626,9 @@ def test_vrt_protocol():
16261626
"vrt://data/float32.tif?projwin_srs=OGC:CRS84&projwin=-117.6407,33.90027,-117.6292,33.89181"
16271627
)
16281628

1629-
assert ds.GetGeoTransform()[0] == 440840.0
1629+
assert ds.GetGeoTransform()[0] == 440780.0
16301630
assert ds.GetGeoTransform()[3] == 3751140.0
1631-
assert ds.GetRasterBand(1).XSize == 18
1631+
assert ds.GetRasterBand(1).XSize == 19
16321632
assert ds.GetRasterBand(1).YSize == 17
16331633

16341634
with pytest.raises(Exception):

autotest/utilities/test_gdal_translate.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ def test_gdal_translate_31(gdal_translate_path, tmp_path):
847847
dst_tif = str(tmp_path / "test_gdal_translate_30.tif")
848848

849849
gdaltest.runexternal(
850-
f"{gdal_translate_path} -projwin_srs EPSG:4267 -projwin -117.641168620797 33.9023526904262 -117.628110837847 33.8915970129613 ../gcore/data/byte.tif {dst_tif}"
850+
f"{gdal_translate_path} -projwin_srs EPSG:4267 -projwin -117.6408 33.9023 -117.6282 33.8920 ../gcore/data/byte.tif {dst_tif}"
851851
)
852852

853853
ds = gdal.Open(dst_tif)
@@ -861,8 +861,6 @@ def test_gdal_translate_31(gdal_translate_path, tmp_path):
861861
1e-6,
862862
)
863863

864-
ds = None
865-
866864

867865
###############################################################################
868866
# Test subsetting a file with a RPC

autotest/utilities/test_gdal_translate_lib.py

+43
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,49 @@ def test_gdal_translate_lib_projwin_expand():
584584
assert ds.RasterYSize == 20
585585

586586

587+
###############################################################################
588+
# Test -projwin_srs option
589+
590+
591+
def test_gdal_translate_lib_31():
592+
593+
ds = gdal.Translate(
594+
"",
595+
"../gcore/data/byte.tif",
596+
projWin=(-117.6408, 33.9023, -117.6282, 33.8920),
597+
projWinSRS="EPSG:4267",
598+
format="MEM",
599+
)
600+
601+
assert ds.GetRasterBand(1).Checksum() == 4672, "Bad checksum"
602+
603+
gdaltest.check_geotransform(
604+
gdal.Open("../gcore/data/byte.tif").GetGeoTransform(),
605+
ds.GetGeoTransform(),
606+
1e-6,
607+
)
608+
609+
610+
def test_gdal_translate_lib_projwin_polar(tmp_vsimem):
611+
612+
src = gdal.GetDriverByName("GTiff").Create(
613+
tmp_vsimem / "in.tif", 16620, 30000, options={"SPARSE_OK": True}
614+
)
615+
src.SetGeoTransform((-640000.0, 90.0, 0.0, -655550.0, 0.0, -90.0))
616+
src.SetProjection("EPSG:3413")
617+
618+
dst = gdal.Translate(
619+
"", src, projWin=(-20, 80, -19, 79), projWinSRS="EPSG:4326", format="VRT"
620+
)
621+
622+
dst_gt = dst.GetGeoTransform()
623+
assert dst_gt[0] == 458900
624+
assert dst_gt[3] == -975950
625+
626+
assert dst.RasterXSize == 723
627+
assert dst.RasterYSize == 1192
628+
629+
587630
###############################################################################
588631
# Test translate with a MEM source to a anonymous VRT
589632

0 commit comments

Comments
 (0)