Skip to content

Commit

Permalink
Rename 'gdal raster buildvrt' to 'gdal raster mosaic', and allow both…
Browse files Browse the repository at this point in the history
… virtual and materialized output
  • Loading branch information
rouault committed Jan 24, 2025
1 parent a9854d1 commit bc83681
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 151 deletions.
2 changes: 1 addition & 1 deletion apps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ add_library(
gdalalg_main.cpp
gdalalg_pipeline.cpp
gdalalg_raster.cpp
gdalalg_raster_buildvrt.cpp
gdalalg_raster_info.cpp
gdalalg_raster_clip.cpp
gdalalg_raster_convert.cpp
gdalalg_raster_edit.cpp
gdalalg_raster_mosaic.cpp
gdalalg_raster_pipeline.cpp
gdalalg_raster_overview_add.cpp
gdalalg_raster_overview_delete.cpp
Expand Down
4 changes: 2 additions & 2 deletions apps/gdalalg_raster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@

#include "gdalalgorithm.h"

#include "gdalalg_raster_buildvrt.h"
#include "gdalalg_raster_info.h"
#include "gdalalg_raster_clip.h"
#include "gdalalg_raster_convert.h"
#include "gdalalg_raster_edit.h"
#include "gdalalg_raster_mosaic.h"
#include "gdalalg_raster_overview.h"
#include "gdalalg_raster_pipeline.h"
#include "gdalalg_raster_reproject.h"
Expand Down Expand Up @@ -46,7 +46,7 @@ class GDALRasterAlgorithm final : public GDALAlgorithm
RegisterSubAlgorithm<GDALRasterOverviewAlgorithm>();
RegisterSubAlgorithm<GDALRasterPipelineAlgorithm>();
RegisterSubAlgorithm<GDALRasterReprojectAlgorithmStandalone>();
RegisterSubAlgorithm<GDALRasterBuildVRTAlgorithm>();
RegisterSubAlgorithm<GDALRasterMosaicAlgorithm>();
}

private:
Expand Down
97 changes: 70 additions & 27 deletions apps/gdalalg_raster_buildvrt.cpp → apps/gdalalg_raster_mosaic.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/******************************************************************************
*
* Project: GDAL
* Purpose: gdal "raster buildvrt" subcommand
* Purpose: gdal "raster mosaic" subcommand
* Author: Even Rouault <even dot rouault at spatialys.com>
*
******************************************************************************
Expand All @@ -10,7 +10,7 @@
* SPDX-License-Identifier: MIT
****************************************************************************/

#include "gdalalg_raster_buildvrt.h"
#include "gdalalg_raster_mosaic.h"

#include "cpl_conv.h"
#include "cpl_vsi_virtual.h"
Expand All @@ -25,13 +25,14 @@
#endif

/************************************************************************/
/* GDALRasterBuildVRTAlgorithm::GDALRasterBuildVRTAlgorithm() */
/* GDALRasterMosaicAlgorithm::GDALRasterMosaicAlgorithm() */
/************************************************************************/

GDALRasterBuildVRTAlgorithm::GDALRasterBuildVRTAlgorithm()
GDALRasterMosaicAlgorithm::GDALRasterMosaicAlgorithm()
: GDALAlgorithm(NAME, DESCRIPTION, HELP_URL)
{
AddProgressArg();
AddOutputFormatArg(&m_format);
AddArg(GDAL_ARG_NAME_INPUT, 'i',
_("Input raster datasets (or specify a @<filename> to point to a "
"file containing filenames)"),
Expand All @@ -43,8 +44,6 @@ GDALRasterBuildVRTAlgorithm::GDALRasterBuildVRTAlgorithm()
AddOutputDatasetArg(&m_outputDataset, GDAL_OF_RASTER);
AddCreationOptionsArg(&m_creationOptions);
AddArg("band", 'b', _("Specify input band(s) number."), &m_bands);
AddArg("separate", 0, _("Place each input file into a separate band."),
&m_separate);
AddOverwriteArg(&m_overwrite);
{
auto &arg = AddArg("resolution", 0,
Expand Down Expand Up @@ -85,29 +84,31 @@ GDALRasterBuildVRTAlgorithm::GDALRasterBuildVRTAlgorithm()
&m_srcNoData)
.SetMinCount(1)
.SetRepeatedArgAllowed(false);
AddArg("vrtnodata", 0, _("Set nodata values at the VRT band level."),
&m_vrtNoData)
AddArg("dstnodata", 0,
_("Set nodata values at the destination band level."), &m_dstNoData)
.SetMinCount(1)
.SetRepeatedArgAllowed(false);
AddArg("hidenodata", 0, _("Makes the VRT band not report the NoData."),
AddArg("hidenodata", 0,
_("Makes the destination band not report the NoData."),
&m_hideNoData);
AddArg("addalpha", 0,
_("Adds an alpha mask band to the VRT when the source raster have "
_("Adds an alpha mask band to the destination when the source "
"raster have "
"none."),
&m_addAlpha);
}

/************************************************************************/
/* GDALRasterBuildVRTAlgorithm::RunImpl() */
/* GDALRasterMosaicAlgorithm::RunImpl() */
/************************************************************************/

bool GDALRasterBuildVRTAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
void *pProgressData)
bool GDALRasterMosaicAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
void *pProgressData)
{
if (m_outputDataset.GetDatasetRef())
{
ReportError(CE_Failure, CPLE_NotSupported,
"gdal raster buildvrt does not support outputting to an "
"gdal raster mosaic does not support outputting to an "
"already opened output dataset");
return false;
}
Expand Down Expand Up @@ -178,6 +179,11 @@ bool GDALRasterBuildVRTAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
return false;
}

const bool bVRTOutput =
m_outputDataset.GetName().empty() || EQUAL(m_format.c_str(), "VRT") ||
EQUAL(CPLGetExtensionSafe(m_outputDataset.GetName().c_str()).c_str(),
"VRT");

CPLStringList aosOptions;
if (!m_resolution.empty())
{
Expand Down Expand Up @@ -219,26 +225,25 @@ bool GDALRasterBuildVRTAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
}
aosOptions.push_back(s);
}
if (!m_vrtNoData.empty())
if (!m_dstNoData.empty())
{
aosOptions.push_back("-vrtnodata");
std::string s;
for (double v : m_vrtNoData)
for (double v : m_dstNoData)
{
if (!s.empty())
s += " ";
s += CPLSPrintf("%.17g", v);
}
aosOptions.push_back(s);
}
if (m_separate)
{
aosOptions.push_back("-separate");
}
for (const auto &co : m_creationOptions)
if (bVRTOutput)
{
aosOptions.push_back("-co");
aosOptions.push_back(co);
for (const auto &co : m_creationOptions)
{
aosOptions.push_back("-co");
aosOptions.push_back(co);
}
}
for (const int b : m_bands)
{
Expand All @@ -256,19 +261,57 @@ bool GDALRasterBuildVRTAlgorithm::RunImpl(GDALProgressFunc pfnProgress,

GDALBuildVRTOptions *psOptions =
GDALBuildVRTOptionsNew(aosOptions.List(), nullptr);
GDALBuildVRTOptionsSetProgress(psOptions, pfnProgress, pProgressData);
if (bVRTOutput)
{
GDALBuildVRTOptionsSetProgress(psOptions, pfnProgress, pProgressData);
}

auto poOutDS = std::unique_ptr<GDALDataset>(GDALDataset::FromHandle(
GDALBuildVRT(m_outputDataset.GetName().c_str(),
GDALBuildVRT(bVRTOutput ? m_outputDataset.GetName().c_str() : "",
foundByName ? aosInputDatasetNames.size()
: static_cast<int>(m_inputDatasets.size()),
ahInputDatasets.empty() ? nullptr : ahInputDatasets.data(),
aosInputDatasetNames.List(), psOptions, nullptr)));
GDALBuildVRTOptionsFree(psOptions);
const bool bOK = poOutDS != nullptr;
bool bOK = poOutDS != nullptr;
if (bOK)
{
m_outputDataset.Set(std::move(poOutDS));
if (bVRTOutput)
{
m_outputDataset.Set(std::move(poOutDS));
}
else
{
CPLStringList aosTranslateOptions;
if (!m_format.empty())
{
aosTranslateOptions.AddString("-of");
aosTranslateOptions.AddString(m_format.c_str());
}
for (const auto &co : m_creationOptions)
{
aosTranslateOptions.AddString("-co");
aosTranslateOptions.AddString(co.c_str());
}

GDALTranslateOptions *psTranslateOptions =
GDALTranslateOptionsNew(aosTranslateOptions.List(), nullptr);
GDALTranslateOptionsSetProgress(psTranslateOptions, pfnProgress,
pProgressData);

auto poFinalDS =
std::unique_ptr<GDALDataset>(GDALDataset::FromHandle(
GDALTranslate(m_outputDataset.GetName().c_str(),
GDALDataset::ToHandle(poOutDS.get()),
psTranslateOptions, nullptr)));
GDALTranslateOptionsFree(psTranslateOptions);

bOK = poFinalDS != nullptr;
if (bOK)
{
m_outputDataset.Set(std::move(poFinalDS));
}
}
}

return bOK;
Expand Down
24 changes: 12 additions & 12 deletions apps/gdalalg_raster_buildvrt.h → apps/gdalalg_raster_mosaic.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/******************************************************************************
*
* Project: GDAL
* Purpose: gdal "raster buildvrt" subcommand
* Purpose: gdal "raster mosaic" subcommand
* Author: Even Rouault <even dot rouault at spatialys.com>
*
******************************************************************************
Expand All @@ -10,45 +10,45 @@
* SPDX-License-Identifier: MIT
****************************************************************************/

#ifndef GDALALG_RASTER_BUILDVRT_INCLUDED
#define GDALALG_RASTER_BUILDVRT_INCLUDED
#ifndef GDALALG_RASTER_MOSAIC_INCLUDED
#define GDALALG_RASTER_MOSAIC_INCLUDED

#include "gdalalgorithm.h"

//! @cond Doxygen_Suppress

/************************************************************************/
/* GDALRasterBuildVRTAlgorithm */
/* GDALRasterMosaicAlgorithm */
/************************************************************************/

class GDALRasterBuildVRTAlgorithm final : public GDALAlgorithm
class GDALRasterMosaicAlgorithm final : public GDALAlgorithm
{
public:
static constexpr const char *NAME = "buildvrt";
static constexpr const char *DESCRIPTION = "Build a virtual dataset (VRT).";
static constexpr const char *HELP_URL =
"/programs/gdal_raster_buildvrt.html";
static constexpr const char *NAME = "mosaic";
static constexpr const char *DESCRIPTION =
"Build a mosaic, either virtual (VRT) or materialized.";
static constexpr const char *HELP_URL = "/programs/gdal_raster_mosaic.html";

static std::vector<std::string> GetAliases()
{
return {};
}

explicit GDALRasterBuildVRTAlgorithm();
explicit GDALRasterMosaicAlgorithm();

private:
bool RunImpl(GDALProgressFunc pfnProgress, void *pProgressData) override;

std::vector<GDALArgDatasetValue> m_inputDatasets{};
std::string m_format{};
GDALArgDatasetValue m_outputDataset{};
std::vector<std::string> m_creationOptions{};
bool m_overwrite = false;
bool m_separate = false;
std::string m_resolution{};
std::vector<double> m_bbox{};
bool m_targetAlignedPixels = false;
std::vector<double> m_srcNoData{};
std::vector<double> m_vrtNoData{};
std::vector<double> m_dstNoData{};
std::vector<int> m_bands{};
bool m_hideNoData = false;
bool m_addAlpha = false;
Expand Down
Loading

0 comments on commit bc83681

Please sign in to comment.