File: gdalalg_vector_reproject.cpp

package info (click to toggle)
gdal 3.12.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 92,396 kB
  • sloc: cpp: 1,224,305; ansic: 206,456; python: 26,284; java: 6,001; xml: 4,769; sh: 3,869; cs: 2,513; yacc: 1,306; makefile: 214
file content (130 lines) | stat: -rw-r--r-- 4,642 bytes parent folder | download | duplicates (3)
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
/******************************************************************************
 *
 * Project:  GDAL
 * Purpose:  "reproject" 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_reproject.h"

#include "gdal_priv.h"
#include "ogr_spatialref.h"
#include "ogrsf_frmts.h"
#include "ogrwarpedlayer.h"

//! @cond Doxygen_Suppress

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

/************************************************************************/
/*      GDALVectorReprojectAlgorithm::GDALVectorReprojectAlgorithm()    */
/************************************************************************/

GDALVectorReprojectAlgorithm::GDALVectorReprojectAlgorithm(bool standaloneStep)
    : GDALVectorPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL,
                                      standaloneStep)
{
    AddActiveLayerArg(&m_activeLayer);
    AddArg("src-crs", 's', _("Source CRS"), &m_srsCrs)
        .SetIsCRSArg()
        .AddHiddenAlias("s_srs");
    AddArg("dst-crs", 'd', _("Destination CRS"), &m_dstCrs)
        .SetIsCRSArg()
        .SetRequired()
        .AddHiddenAlias("t_srs");
}

/************************************************************************/
/*            GDALVectorReprojectAlgorithm::RunStep()                   */
/************************************************************************/

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

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

    std::unique_ptr<OGRSpatialReference> poSrcCRS;
    if (!m_srsCrs.empty())
    {
        poSrcCRS = std::make_unique<OGRSpatialReference>();
        poSrcCRS->SetFromUserInput(m_srsCrs.c_str());
        poSrcCRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    }

    OGRSpatialReference oDstCRS;
    oDstCRS.SetFromUserInput(m_dstCrs.c_str());
    oDstCRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);

    auto reprojectedDataset =
        std::make_unique<GDALVectorPipelineOutputDataset>(*poSrcDS);

    const int nLayerCount = poSrcDS->GetLayerCount();
    bool ret = true;
    for (int i = 0; ret && i < nLayerCount; ++i)
    {
        auto poSrcLayer = poSrcDS->GetLayer(i);
        ret = (poSrcLayer != nullptr);
        if (ret)
        {
            if (m_activeLayer.empty() ||
                m_activeLayer == poSrcLayer->GetDescription())
            {
                const OGRSpatialReference *poSrcLayerCRS;
                if (poSrcCRS)
                    poSrcLayerCRS = poSrcCRS.get();
                else
                    poSrcLayerCRS = poSrcLayer->GetSpatialRef();
                if (!poSrcLayerCRS)
                {
                    ReportError(CE_Failure, CPLE_AppDefined,
                                "Layer '%s' has no spatial reference system",
                                poSrcLayer->GetName());
                    return false;
                }
                auto poCT = std::unique_ptr<OGRCoordinateTransformation>(
                    OGRCreateCoordinateTransformation(poSrcLayerCRS, &oDstCRS));
                auto poReversedCT =
                    std::unique_ptr<OGRCoordinateTransformation>(
                        OGRCreateCoordinateTransformation(&oDstCRS,
                                                          poSrcLayerCRS));
                ret = (poCT != nullptr) && (poReversedCT != nullptr);
                if (ret)
                {
                    reprojectedDataset->AddLayer(
                        *poSrcLayer,
                        std::make_unique<OGRWarpedLayer>(
                            poSrcLayer, /* iGeomField = */ 0,
                            /*bTakeOwnership = */ false, poCT.release(),
                            poReversedCT.release()));
                }
            }
            else
            {
                reprojectedDataset->AddLayer(
                    *poSrcLayer,
                    std::make_unique<GDALVectorPipelinePassthroughLayer>(
                        *poSrcLayer));
            }
        }
    }

    if (ret)
        m_outputDataset.Set(std::move(reprojectedDataset));

    return ret;
}

GDALVectorReprojectAlgorithmStandalone::
    ~GDALVectorReprojectAlgorithmStandalone() = default;

//! @endcond