-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #11908
- Loading branch information
Showing
13 changed files
with
367 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/****************************************************************************** | ||
* | ||
* Project: GDAL | ||
* Purpose: "resize" step of "raster pipeline" | ||
* Author: Even Rouault <even dot rouault at spatialys.com> | ||
* | ||
****************************************************************************** | ||
* Copyright (c) 2025, Even Rouault <even dot rouault at spatialys.com> | ||
* | ||
* SPDX-License-Identifier: MIT | ||
****************************************************************************/ | ||
|
||
#include "gdalalg_raster_resize.h" | ||
|
||
#include "gdal_priv.h" | ||
#include "gdal_utils.h" | ||
|
||
//! @cond Doxygen_Suppress | ||
|
||
#ifndef _ | ||
#define _(x) (x) | ||
#endif | ||
|
||
/************************************************************************/ | ||
/* GDALRasterResizeAlgorithm::GDALRasterResizeAlgorithm() */ | ||
/************************************************************************/ | ||
|
||
GDALRasterResizeAlgorithm::GDALRasterResizeAlgorithm(bool standaloneStep) | ||
: GDALRasterPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL, | ||
standaloneStep) | ||
{ | ||
auto &sizeArg = AddArg("size", 0, _("Target size in pixels"), &m_size) | ||
.SetMinCount(2) | ||
.SetMaxCount(2) | ||
.SetRequired() | ||
.SetRepeatedArgAllowed(false) | ||
.SetDisplayHintAboutRepetition(false) | ||
.SetMetaVar("<width>,<height>") | ||
.SetMutualExclusionGroup("resolution-size"); | ||
sizeArg.AddValidationAction( | ||
[&sizeArg]() | ||
{ | ||
const auto &val = sizeArg.Get<std::vector<int>>(); | ||
CPLAssert(val.size() == 2); | ||
if (!(val[0] >= 0 && val[1] >= 0)) | ||
{ | ||
CPLError(CE_Failure, CPLE_AppDefined, | ||
"Target size should be positive or 0."); | ||
return false; | ||
} | ||
return true; | ||
}); | ||
|
||
AddArg("resampling", 'r', _("Resampling method"), &m_resampling) | ||
.SetChoices("nearest", "bilinear", "cubic", "cubicspline", "lanczos", | ||
"average", "mode") | ||
.SetDefault("nearest") | ||
.SetHiddenChoices("near"); | ||
} | ||
|
||
/************************************************************************/ | ||
/* GDALRasterResizeAlgorithm::RunStep() */ | ||
/************************************************************************/ | ||
|
||
bool GDALRasterResizeAlgorithm::RunStep(GDALProgressFunc, void *) | ||
{ | ||
CPLAssert(m_inputDataset.GetDatasetRef()); | ||
CPLAssert(m_outputDataset.GetName().empty()); | ||
CPLAssert(!m_outputDataset.GetDatasetRef()); | ||
|
||
CPLStringList aosOptions; | ||
aosOptions.AddString("-of"); | ||
aosOptions.AddString("VRT"); | ||
if (!m_size.empty()) | ||
{ | ||
aosOptions.AddString("-outsize"); | ||
aosOptions.AddString(CPLSPrintf("%d", m_size[0])); | ||
aosOptions.AddString(CPLSPrintf("%d", m_size[1])); | ||
} | ||
if (!m_resampling.empty()) | ||
{ | ||
aosOptions.AddString("-r"); | ||
aosOptions.AddString(m_resampling.c_str()); | ||
} | ||
|
||
GDALTranslateOptions *psOptions = | ||
GDALTranslateOptionsNew(aosOptions.List(), nullptr); | ||
|
||
auto poOutDS = std::unique_ptr<GDALDataset>(GDALDataset::FromHandle( | ||
GDALTranslate(m_outputDataset.GetName().c_str(), | ||
GDALDataset::ToHandle(m_inputDataset.GetDatasetRef()), | ||
psOptions, nullptr))); | ||
GDALTranslateOptionsFree(psOptions); | ||
const bool bRet = poOutDS != nullptr; | ||
if (poOutDS) | ||
{ | ||
m_outputDataset.Set(std::move(poOutDS)); | ||
} | ||
|
||
return bRet; | ||
} | ||
|
||
//! @endcond |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/****************************************************************************** | ||
* | ||
* Project: GDAL | ||
* Purpose: "resize" step of "raster pipeline" | ||
* Author: Even Rouault <even dot rouault at spatialys.com> | ||
* | ||
****************************************************************************** | ||
* Copyright (c) 2025, Even Rouault <even dot rouault at spatialys.com> | ||
* | ||
* SPDX-License-Identifier: MIT | ||
****************************************************************************/ | ||
|
||
#ifndef GDALALG_RASTER_RESIZE_INCLUDED | ||
#define GDALALG_RASTER_RESIZE_INCLUDED | ||
|
||
#include "gdalalg_raster_pipeline.h" | ||
|
||
//! @cond Doxygen_Suppress | ||
|
||
/************************************************************************/ | ||
/* GDALRasterResizeAlgorithm */ | ||
/************************************************************************/ | ||
|
||
class GDALRasterResizeAlgorithm /* non final */ | ||
: public GDALRasterPipelineStepAlgorithm | ||
{ | ||
public: | ||
static constexpr const char *NAME = "resize"; | ||
static constexpr const char *DESCRIPTION = | ||
"Resize a raster dataset without changing the georeferenced extents."; | ||
static constexpr const char *HELP_URL = "/programs/gdal_raster_resize.html"; | ||
|
||
static std::vector<std::string> GetAliases() | ||
{ | ||
return {}; | ||
} | ||
|
||
explicit GDALRasterResizeAlgorithm(bool standaloneStep = false); | ||
|
||
private: | ||
bool RunStep(GDALProgressFunc pfnProgress, void *pProgressData) override; | ||
|
||
std::vector<int> m_size{}; | ||
std::string m_resampling{}; | ||
}; | ||
|
||
/************************************************************************/ | ||
/* GDALRasterResizeAlgorithmStandalone */ | ||
/************************************************************************/ | ||
|
||
class GDALRasterResizeAlgorithmStandalone final | ||
: public GDALRasterResizeAlgorithm | ||
{ | ||
public: | ||
GDALRasterResizeAlgorithmStandalone() | ||
: GDALRasterResizeAlgorithm(/* standaloneStep = */ true) | ||
{ | ||
} | ||
}; | ||
|
||
//! @endcond | ||
|
||
#endif /* GDALALG_RASTER_RESIZE_INCLUDED */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#!/usr/bin/env pytest | ||
# -*- coding: utf-8 -*- | ||
############################################################################### | ||
# Project: GDAL/OGR Test Suite | ||
# Purpose: 'gdal raster resize' testing | ||
# Author: Even Rouault <even dot rouault @ spatialys.com> | ||
# | ||
############################################################################### | ||
# Copyright (c) 2025, Even Rouault <even dot rouault at spatialys.com> | ||
# | ||
# SPDX-License-Identifier: MIT | ||
############################################################################### | ||
|
||
from osgeo import gdal | ||
|
||
|
||
def get_resize_alg(): | ||
reg = gdal.GetGlobalAlgorithmRegistry() | ||
raster = reg.InstantiateAlg("raster") | ||
return raster.InstantiateSubAlgorithm("resize") | ||
|
||
|
||
def test_gdalalg_raster_resize(tmp_vsimem): | ||
|
||
out_filename = str(tmp_vsimem / "out.tif") | ||
|
||
last_pct = [0] | ||
|
||
def my_progress(pct, msg, user_data): | ||
last_pct[0] = pct | ||
return True | ||
|
||
pipeline = get_resize_alg() | ||
assert pipeline.ParseRunAndFinalize( | ||
[ | ||
"--size=10,0", | ||
"../gcore/data/byte.tif", | ||
out_filename, | ||
], | ||
my_progress, | ||
) | ||
assert last_pct[0] == 1.0 | ||
|
||
with gdal.OpenEx(out_filename) as ds: | ||
assert ds.RasterXSize == 10 | ||
assert ds.RasterYSize == 10 | ||
assert ds.GetRasterBand(1).Checksum() == 1192 | ||
|
||
|
||
def test_gdalalg_raster_resize_resampling(tmp_vsimem): | ||
|
||
out_filename = str(tmp_vsimem / "out.tif") | ||
|
||
last_pct = [0] | ||
|
||
def my_progress(pct, msg, user_data): | ||
last_pct[0] = pct | ||
return True | ||
|
||
pipeline = get_resize_alg() | ||
assert pipeline.ParseRunAndFinalize( | ||
[ | ||
"--size=0,10", | ||
"-r", | ||
"cubic", | ||
"../gcore/data/byte.tif", | ||
out_filename, | ||
], | ||
my_progress, | ||
) | ||
assert last_pct[0] == 1.0 | ||
|
||
with gdal.OpenEx(out_filename) as ds: | ||
assert ds.RasterXSize == 10 | ||
assert ds.RasterYSize == 10 | ||
assert ds.GetRasterBand(1).Checksum() == 1059 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.