File: gdalalg_vector_read.cpp

package info (click to toggle)
gdal 3.12.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 92,292 kB
  • sloc: cpp: 1,223,498; ansic: 206,434; python: 26,278; java: 6,001; xml: 4,769; sh: 3,855; cs: 2,513; yacc: 1,306; makefile: 214
file content (188 lines) | stat: -rw-r--r-- 6,539 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/******************************************************************************
 *
 * Project:  GDAL
 * Purpose:  "read" step of "vector pipeline"
 * Author:   Even Rouault <even dot rouault at spatialys.com>
 *
 ******************************************************************************
 * Copyright (c) 2024, Even Rouault <even dot rouault at spatialys.com>
 *
 * SPDX-License-Identifier: MIT
 ****************************************************************************/

#include "gdalalg_vector_read.h"

#include "gdal_priv.h"
#include "ogrsf_frmts.h"

//! @cond Doxygen_Suppress

#ifndef _
#define _(x) (x)
#endif

/************************************************************************/
/*          GDALVectorReadAlgorithm::GDALVectorReadAlgorithm()          */
/************************************************************************/

GDALVectorReadAlgorithm::GDALVectorReadAlgorithm()
    : GDALVectorPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL,
                                      /* standaloneStep =*/false)
{
    AddVectorInputArgs(/* hiddenForCLI = */ false);
}

/************************************************************************/
/*                 GDALVectorPipelineReadOutputDataset                  */
/************************************************************************/

/** Class used by vector pipeline steps to create an output on-the-fly
 * dataset where they can store on-the-fly layers.
 */
class GDALVectorPipelineReadOutputDataset final : public GDALDataset
{
    GDALDataset &m_srcDS;
    std::vector<OGRLayer *> m_layers{};

    CPL_DISALLOW_COPY_ASSIGN(GDALVectorPipelineReadOutputDataset)

  public:
    explicit GDALVectorPipelineReadOutputDataset(GDALDataset &oSrcDS);

    void AddLayer(OGRLayer &oSrcLayer);

    int GetLayerCount() const override;

    OGRLayer *GetLayer(int idx) const override;

    int TestCapability(const char *pszCap) const override;

    void ResetReading() override;

    OGRFeature *GetNextFeature(OGRLayer **ppoBelongingLayer,
                               double *pdfProgressPct,
                               GDALProgressFunc pfnProgress,
                               void *pProgressData) override;
};

/************************************************************************/
/*                 GDALVectorPipelineReadOutputDataset()                */
/************************************************************************/

GDALVectorPipelineReadOutputDataset::GDALVectorPipelineReadOutputDataset(
    GDALDataset &srcDS)
    : m_srcDS(srcDS)
{
    SetDescription(m_srcDS.GetDescription());
}

/************************************************************************/
/*            GDALVectorPipelineReadOutputDataset::AddLayer()           */
/************************************************************************/

void GDALVectorPipelineReadOutputDataset::AddLayer(OGRLayer &oSrcLayer)
{
    m_layers.push_back(&oSrcLayer);
}

/************************************************************************/
/*          GDALVectorPipelineReadOutputDataset::GetLayerCount()        */
/************************************************************************/

int GDALVectorPipelineReadOutputDataset::GetLayerCount() const
{
    return static_cast<int>(m_layers.size());
}

/************************************************************************/
/*           GDALVectorPipelineReadOutputDataset::GetLayer()            */
/************************************************************************/

OGRLayer *GDALVectorPipelineReadOutputDataset::GetLayer(int idx) const
{
    return idx >= 0 && idx < GetLayerCount() ? m_layers[idx] : nullptr;
}

/************************************************************************/
/*         GDALVectorPipelineReadOutputDataset::TestCapability()        */
/************************************************************************/

int GDALVectorPipelineReadOutputDataset::TestCapability(
    const char *pszCap) const
{
    if (EQUAL(pszCap, ODsCRandomLayerRead))
        return m_srcDS.TestCapability(pszCap);
    return false;
}

/************************************************************************/
/*           GDALVectorPipelineReadOutputDataset::ResetReading()        */
/************************************************************************/

void GDALVectorPipelineReadOutputDataset::ResetReading()
{
    m_srcDS.ResetReading();
}

/************************************************************************/
/*          GDALVectorPipelineReadOutputDataset::GetNextFeature()       */
/************************************************************************/

OGRFeature *GDALVectorPipelineReadOutputDataset::GetNextFeature(
    OGRLayer **ppoBelongingLayer, double *pdfProgressPct,
    GDALProgressFunc pfnProgress, void *pProgressData)
{
    while (true)
    {
        OGRLayer *poBelongingLayer = nullptr;
        auto poFeature = std::unique_ptr<OGRFeature>(m_srcDS.GetNextFeature(
            &poBelongingLayer, pdfProgressPct, pfnProgress, pProgressData));
        if (ppoBelongingLayer)
            *ppoBelongingLayer = poBelongingLayer;
        if (!poFeature)
            break;
        if (std::find(m_layers.begin(), m_layers.end(), poBelongingLayer) !=
            m_layers.end())
            return poFeature.release();
    }
    return nullptr;
}

/************************************************************************/
/*                  GDALVectorReadAlgorithm::RunStep()                  */
/************************************************************************/

bool GDALVectorReadAlgorithm::RunStep(GDALPipelineStepRunContext &)
{
    auto poSrcDS = m_inputDataset[0].GetDatasetRef();
    CPLAssert(poSrcDS);

    CPLAssert(m_outputDataset.GetName().empty());
    CPLAssert(!m_outputDataset.GetDatasetRef());

    if (m_inputLayerNames.empty())
    {
        m_outputDataset.Set(poSrcDS);
    }
    else
    {
        auto poOutDS =
            std::make_unique<GDALVectorPipelineReadOutputDataset>(*poSrcDS);
        for (const auto &srcLayerName : m_inputLayerNames)
        {
            auto poSrcLayer = poSrcDS->GetLayerByName(srcLayerName.c_str());
            if (!poSrcLayer)
            {
                ReportError(CE_Failure, CPLE_AppDefined,
                            "Cannot find source layer '%s'",
                            srcLayerName.c_str());
                return false;
            }
            poOutDS->AddLayer(*poSrcLayer);
        }
        m_outputDataset.Set(std::move(poOutDS));
    }
    return true;
}

//! @endcond