Coverage Report

Created: 2026-02-14 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/apps/gdalalg_vector_write.cpp
Line
Count
Source
1
/******************************************************************************
2
 *
3
 * Project:  GDAL
4
 * Purpose:  "write" step of "vector pipeline"
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_vector_write.h"
14
#include "cpl_string.h"
15
#include "gdal_utils.h"
16
#include "gdal_priv.h"
17
18
#ifndef _
19
#define _(x) (x)
20
#endif
21
22
//! @cond Doxygen_Suppress
23
24
/************************************************************************/
25
/*         GDALVectorWriteAlgorithm::GDALVectorWriteAlgorithm()         */
26
/************************************************************************/
27
28
GDALVectorWriteAlgorithm::GDALVectorWriteAlgorithm()
29
0
    : GDALVectorPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL,
30
0
                                      /* standaloneStep =*/false)
31
0
{
32
0
    AddVectorOutputArgs(/* hiddenForCLI = */ false,
33
0
                        /* shortNameOutputLayerAllowed=*/true);
34
0
}
35
36
/************************************************************************/
37
/*                 GDALVectorWriteAlgorithm::RunStep()                  */
38
/************************************************************************/
39
40
bool GDALVectorWriteAlgorithm::RunStep(GDALPipelineStepRunContext &ctxt)
41
0
{
42
0
    auto pfnProgress = ctxt.m_pfnProgress;
43
0
    auto pProgressData = ctxt.m_pProgressData;
44
0
    auto poSrcDS = m_inputDataset[0].GetDatasetRef();
45
0
    CPLAssert(poSrcDS);
46
47
0
    if (m_format == "stream")
48
0
    {
49
0
        m_outputDataset.Set(poSrcDS);
50
0
        return true;
51
0
    }
52
53
0
    CPLStringList aosOptions;
54
0
    aosOptions.AddString("--invoked-from-gdal-algorithm");
55
0
    if (!m_overwrite)
56
0
    {
57
0
        aosOptions.AddString("--no-overwrite");
58
0
    }
59
0
    if (m_overwriteLayer)
60
0
    {
61
0
        aosOptions.AddString("-overwrite");
62
0
    }
63
0
    if (m_appendLayer)
64
0
    {
65
0
        aosOptions.AddString("-append");
66
0
    }
67
0
    if (m_upsert)
68
0
    {
69
0
        aosOptions.AddString("-upsert");
70
0
    }
71
0
    if (!m_format.empty())
72
0
    {
73
0
        aosOptions.AddString("-of");
74
0
        aosOptions.AddString(m_format.c_str());
75
0
    }
76
0
    for (const auto &co : m_creationOptions)
77
0
    {
78
0
        aosOptions.AddString("-dsco");
79
0
        aosOptions.AddString(co.c_str());
80
0
    }
81
0
    for (const auto &co : m_layerCreationOptions)
82
0
    {
83
0
        aosOptions.AddString("-lco");
84
0
        aosOptions.AddString(co.c_str());
85
0
    }
86
0
    if (!m_outputLayerName.empty())
87
0
    {
88
0
        aosOptions.AddString("-nln");
89
0
        aosOptions.AddString(m_outputLayerName.c_str());
90
0
    }
91
0
    if (pfnProgress && pfnProgress != GDALDummyProgress)
92
0
    {
93
0
        aosOptions.AddString("-progress");
94
0
    }
95
0
    if (m_skipErrors)
96
0
    {
97
0
        aosOptions.AddString("-skipfailures");
98
0
    }
99
100
0
    GDALDataset *poRetDS = nullptr;
101
0
    GDALDatasetH hOutDS =
102
0
        GDALDataset::ToHandle(m_outputDataset.GetDatasetRef());
103
0
    GDALVectorTranslateOptions *psOptions =
104
0
        GDALVectorTranslateOptionsNew(aosOptions.List(), nullptr);
105
0
    if (psOptions)
106
0
    {
107
0
        GDALVectorTranslateOptionsSetProgress(psOptions, pfnProgress,
108
0
                                              pProgressData);
109
110
0
        GDALDatasetH hSrcDS = GDALDataset::ToHandle(poSrcDS);
111
0
        poRetDS = GDALDataset::FromHandle(
112
0
            GDALVectorTranslate(m_outputDataset.GetName().c_str(), hOutDS, 1,
113
0
                                &hSrcDS, psOptions, nullptr));
114
0
        GDALVectorTranslateOptionsFree(psOptions);
115
0
    }
116
117
0
    if (!poRetDS)
118
0
    {
119
0
        return false;
120
0
    }
121
122
0
    if (!hOutDS)
123
0
    {
124
0
        m_outputDataset.Set(std::unique_ptr<GDALDataset>(poRetDS));
125
0
    }
126
127
0
    return true;
128
0
}
129
130
//! @endcond