/src/gdal/apps/gdalalg_raster_mosaic.cpp
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: GDAL |
4 | | * Purpose: gdal "raster mosaic" subcommand |
5 | | * Author: Even Rouault <even dot rouault at spatialys.com> |
6 | | * |
7 | | ****************************************************************************** |
8 | | * Copyright (c) 2024, Even Rouault <even dot rouault at spatialys.com> |
9 | | * |
10 | | * SPDX-License-Identifier: MIT |
11 | | ****************************************************************************/ |
12 | | |
13 | | #include "gdalalg_raster_mosaic.h" |
14 | | |
15 | | #include "cpl_conv.h" |
16 | | |
17 | | #include "gdal_priv.h" |
18 | | #include "gdal_utils.h" |
19 | | |
20 | | //! @cond Doxygen_Suppress |
21 | | |
22 | | #ifndef _ |
23 | 0 | #define _(x) (x) |
24 | | #endif |
25 | | |
26 | | /************************************************************************/ |
27 | | /* GDALRasterMosaicAlgorithm::GDALRasterMosaicAlgorithm() */ |
28 | | /************************************************************************/ |
29 | | |
30 | | GDALRasterMosaicAlgorithm::GDALRasterMosaicAlgorithm(bool bStandalone) |
31 | 0 | : GDALRasterMosaicStackCommonAlgorithm(NAME, DESCRIPTION, HELP_URL, |
32 | 0 | bStandalone) |
33 | 0 | { |
34 | 0 | AddArg("add-alpha", 0, |
35 | 0 | _("Adds an alpha mask band to the destination when the source " |
36 | 0 | "raster have " |
37 | 0 | "none."), |
38 | 0 | &m_addAlpha); |
39 | 0 | AddPixelFunctionNameArg(&m_pixelFunction); |
40 | 0 | AddPixelFunctionArgsArg(&m_pixelFunctionArgs); |
41 | 0 | } |
42 | | |
43 | | /************************************************************************/ |
44 | | /* GDALRasterMosaicAlgorithm::RunStep() */ |
45 | | /************************************************************************/ |
46 | | |
47 | | bool GDALRasterMosaicAlgorithm::RunStep(GDALPipelineStepRunContext &ctxt) |
48 | 0 | { |
49 | 0 | CPLAssert(!m_outputDataset.GetDatasetRef()); |
50 | | |
51 | 0 | std::vector<GDALDatasetH> ahInputDatasets; |
52 | 0 | CPLStringList aosInputDatasetNames; |
53 | 0 | bool foundByName = false; |
54 | 0 | if (!GetInputDatasetNames(ctxt, ahInputDatasets, aosInputDatasetNames, |
55 | 0 | foundByName)) |
56 | 0 | { |
57 | | // Error message emitted by GetInputDatasetNames() |
58 | 0 | return false; |
59 | 0 | } |
60 | | |
61 | 0 | CPLStringList aosOptions; |
62 | 0 | aosOptions.push_back("-strict"); |
63 | |
|
64 | 0 | aosOptions.push_back("-program_name"); |
65 | 0 | aosOptions.push_back("gdal raster mosaic"); |
66 | |
|
67 | 0 | SetBuildVRTOptions(aosOptions); |
68 | |
|
69 | 0 | if (m_addAlpha) |
70 | 0 | { |
71 | 0 | aosOptions.push_back("-addalpha"); |
72 | 0 | } |
73 | 0 | if (!m_pixelFunction.empty()) |
74 | 0 | { |
75 | 0 | aosOptions.push_back("-pixel-function"); |
76 | 0 | aosOptions.push_back(m_pixelFunction); |
77 | 0 | } |
78 | |
|
79 | 0 | for (const auto &arg : m_pixelFunctionArgs) |
80 | 0 | { |
81 | 0 | aosOptions.push_back("-pixel-function-arg"); |
82 | 0 | aosOptions.push_back(arg); |
83 | 0 | } |
84 | |
|
85 | 0 | bool bOK = false; |
86 | 0 | GDALBuildVRTOptions *psOptions = |
87 | 0 | GDALBuildVRTOptionsNew(aosOptions.List(), nullptr); |
88 | 0 | if (psOptions) |
89 | 0 | { |
90 | 0 | auto poOutDS = |
91 | 0 | std::unique_ptr<GDALDataset>(GDALDataset::FromHandle(GDALBuildVRT( |
92 | 0 | "", |
93 | 0 | foundByName ? aosInputDatasetNames.size() |
94 | 0 | : static_cast<int>(m_inputDataset.size()), |
95 | 0 | ahInputDatasets.empty() ? nullptr : ahInputDatasets.data(), |
96 | 0 | aosInputDatasetNames.List(), psOptions, nullptr))); |
97 | 0 | GDALBuildVRTOptionsFree(psOptions); |
98 | 0 | bOK = poOutDS != nullptr; |
99 | 0 | if (bOK) |
100 | 0 | { |
101 | 0 | m_outputDataset.Set(std::move(poOutDS)); |
102 | 0 | } |
103 | 0 | } |
104 | 0 | return bOK; |
105 | 0 | } |
106 | | |
107 | 0 | GDALRasterMosaicAlgorithmStandalone::~GDALRasterMosaicAlgorithmStandalone() = |
108 | | default; |
109 | | |
110 | | //! @endcond |