/src/gdal/apps/gdalalg_vector_reproject.cpp
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: GDAL |
4 | | * Purpose: "reproject" 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_reproject.h" |
14 | | |
15 | | #include "gdal_priv.h" |
16 | | #include "ogr_spatialref.h" |
17 | | #include "ogrsf_frmts.h" |
18 | | #include "ogrwarpedlayer.h" |
19 | | |
20 | | //! @cond Doxygen_Suppress |
21 | | |
22 | | #ifndef _ |
23 | 0 | #define _(x) (x) |
24 | | #endif |
25 | | |
26 | | /************************************************************************/ |
27 | | /* GDALVectorReprojectAlgorithm::GDALVectorReprojectAlgorithm() */ |
28 | | /************************************************************************/ |
29 | | |
30 | | GDALVectorReprojectAlgorithm::GDALVectorReprojectAlgorithm(bool standaloneStep) |
31 | 0 | : GDALVectorPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL, |
32 | 0 | standaloneStep) |
33 | 0 | { |
34 | 0 | AddActiveLayerArg(&m_activeLayer); |
35 | 0 | AddArg(GDAL_ARG_NAME_INPUT_CRS, 's', _("Input CRS"), &m_srcCrs) |
36 | 0 | .SetIsCRSArg() |
37 | 0 | .AddHiddenAlias("src-crs") |
38 | 0 | .AddHiddenAlias("s_srs"); |
39 | 0 | AddArg(GDAL_ARG_NAME_OUTPUT_CRS, 'd', _("Output CRS"), &m_dstCrs) |
40 | 0 | .SetIsCRSArg() |
41 | 0 | .SetRequired() |
42 | 0 | .AddHiddenAlias("dst-crs") |
43 | 0 | .AddHiddenAlias("t_srs"); |
44 | 0 | } |
45 | | |
46 | | /************************************************************************/ |
47 | | /* GDALVectorReprojectAlgorithm::RunStep() */ |
48 | | /************************************************************************/ |
49 | | |
50 | | bool GDALVectorReprojectAlgorithm::RunStep(GDALPipelineStepRunContext &) |
51 | 0 | { |
52 | 0 | auto poSrcDS = m_inputDataset[0].GetDatasetRef(); |
53 | 0 | CPLAssert(poSrcDS); |
54 | | |
55 | 0 | CPLAssert(m_outputDataset.GetName().empty()); |
56 | 0 | CPLAssert(!m_outputDataset.GetDatasetRef()); |
57 | | |
58 | 0 | std::unique_ptr<OGRSpatialReference> poSrcCRS; |
59 | 0 | if (!m_srcCrs.empty()) |
60 | 0 | { |
61 | 0 | poSrcCRS = std::make_unique<OGRSpatialReference>(); |
62 | 0 | poSrcCRS->SetFromUserInput(m_srcCrs.c_str()); |
63 | 0 | poSrcCRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER); |
64 | 0 | } |
65 | |
|
66 | 0 | OGRSpatialReference oDstCRS; |
67 | 0 | oDstCRS.SetFromUserInput(m_dstCrs.c_str()); |
68 | 0 | oDstCRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER); |
69 | |
|
70 | 0 | auto reprojectedDataset = |
71 | 0 | std::make_unique<GDALVectorPipelineOutputDataset>(*poSrcDS); |
72 | |
|
73 | 0 | const int nLayerCount = poSrcDS->GetLayerCount(); |
74 | 0 | bool ret = true; |
75 | 0 | for (int i = 0; ret && i < nLayerCount; ++i) |
76 | 0 | { |
77 | 0 | auto poSrcLayer = poSrcDS->GetLayer(i); |
78 | 0 | ret = (poSrcLayer != nullptr); |
79 | 0 | if (ret) |
80 | 0 | { |
81 | 0 | if ((m_activeLayer.empty() && |
82 | 0 | poSrcLayer->GetGeomType() != wkbNone) || |
83 | 0 | m_activeLayer == poSrcLayer->GetDescription()) |
84 | 0 | { |
85 | 0 | const OGRSpatialReference *poSrcLayerCRS; |
86 | 0 | if (poSrcCRS) |
87 | 0 | poSrcLayerCRS = poSrcCRS.get(); |
88 | 0 | else |
89 | 0 | poSrcLayerCRS = poSrcLayer->GetSpatialRef(); |
90 | 0 | if (!poSrcLayerCRS) |
91 | 0 | { |
92 | 0 | ReportError(CE_Failure, CPLE_AppDefined, |
93 | 0 | "Layer '%s' has no spatial reference system", |
94 | 0 | poSrcLayer->GetName()); |
95 | 0 | return false; |
96 | 0 | } |
97 | 0 | auto poCT = std::unique_ptr<OGRCoordinateTransformation>( |
98 | 0 | OGRCreateCoordinateTransformation(poSrcLayerCRS, &oDstCRS)); |
99 | 0 | auto poReversedCT = |
100 | 0 | std::unique_ptr<OGRCoordinateTransformation>( |
101 | 0 | OGRCreateCoordinateTransformation(&oDstCRS, |
102 | 0 | poSrcLayerCRS)); |
103 | 0 | ret = (poCT != nullptr) && (poReversedCT != nullptr); |
104 | 0 | if (ret) |
105 | 0 | { |
106 | 0 | reprojectedDataset->AddLayer( |
107 | 0 | *poSrcLayer, |
108 | 0 | std::make_unique<OGRWarpedLayer>( |
109 | 0 | poSrcLayer, /* iGeomField = */ 0, |
110 | 0 | /*bTakeOwnership = */ false, std::move(poCT), |
111 | 0 | std::move(poReversedCT))); |
112 | 0 | } |
113 | 0 | } |
114 | 0 | else |
115 | 0 | { |
116 | 0 | reprojectedDataset->AddLayer( |
117 | 0 | *poSrcLayer, |
118 | 0 | std::make_unique<GDALVectorPipelinePassthroughLayer>( |
119 | 0 | *poSrcLayer)); |
120 | 0 | } |
121 | 0 | } |
122 | 0 | } |
123 | | |
124 | 0 | if (ret) |
125 | 0 | m_outputDataset.Set(std::move(reprojectedDataset)); |
126 | |
|
127 | 0 | return ret; |
128 | 0 | } |
129 | | |
130 | | GDALVectorReprojectAlgorithmStandalone:: |
131 | 0 | ~GDALVectorReprojectAlgorithmStandalone() = default; |
132 | | |
133 | | //! @endcond |