Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: GDAL 4 : * Purpose: "gdal vector swap-xy" 5 : * Author: Even Rouault <even dot rouault at spatialys.com> 6 : * 7 : ****************************************************************************** 8 : * Copyright (c) 2025, Even Rouault <even dot rouault at spatialys.com> 9 : * 10 : * SPDX-License-Identifier: MIT 11 : ****************************************************************************/ 12 : 13 : #include "gdalalg_vector_swap_xy.h" 14 : 15 : #include "gdal_priv.h" 16 : #include "ogrsf_frmts.h" 17 : 18 : //! @cond Doxygen_Suppress 19 : 20 : #ifndef _ 21 : #define _(x) (x) 22 : #endif 23 : 24 : /************************************************************************/ 25 : /* GDALVectorSwapXYAlgorithm() */ 26 : /************************************************************************/ 27 : 28 21 : GDALVectorSwapXYAlgorithm::GDALVectorSwapXYAlgorithm(bool standaloneStep) 29 : : GDALVectorGeomAbstractAlgorithm(NAME, DESCRIPTION, HELP_URL, 30 21 : standaloneStep, m_opts) 31 : { 32 21 : } 33 : 34 : namespace 35 : { 36 : 37 : /************************************************************************/ 38 : /* GDALVectorSwapXYAlgorithmLayer */ 39 : /************************************************************************/ 40 : 41 : class GDALVectorSwapXYAlgorithmLayer final 42 : : public GDALVectorGeomOneToOneAlgorithmLayer<GDALVectorSwapXYAlgorithm> 43 : { 44 : public: 45 1 : GDALVectorSwapXYAlgorithmLayer( 46 : OGRLayer &oSrcLayer, const GDALVectorSwapXYAlgorithm::Options &opts) 47 1 : : GDALVectorGeomOneToOneAlgorithmLayer<GDALVectorSwapXYAlgorithm>( 48 1 : oSrcLayer, opts) 49 : { 50 1 : } 51 : 52 1 : OGRErr IGetExtent(int iGeomField, OGREnvelope *psExtent, 53 : bool bForce) override 54 : { 55 1 : OGRErr eErr = m_srcLayer.GetExtent(iGeomField, psExtent, bForce); 56 1 : if (eErr == CE_None) 57 : { 58 1 : std::swap(psExtent->MinX, psExtent->MinY); 59 1 : std::swap(psExtent->MaxX, psExtent->MaxY); 60 : } 61 1 : return eErr; 62 : } 63 : 64 : protected: 65 : using GDALVectorGeomOneToOneAlgorithmLayer::TranslateFeature; 66 : 67 : std::unique_ptr<OGRFeature> 68 : TranslateFeature(std::unique_ptr<OGRFeature> poSrcFeature) const override; 69 : }; 70 : 71 : /************************************************************************/ 72 : /* TranslateFeature() */ 73 : /************************************************************************/ 74 : 75 5 : std::unique_ptr<OGRFeature> GDALVectorSwapXYAlgorithmLayer::TranslateFeature( 76 : std::unique_ptr<OGRFeature> poSrcFeature) const 77 : { 78 5 : const int nGeomFieldCount = poSrcFeature->GetGeomFieldCount(); 79 10 : for (int i = 0; i < nGeomFieldCount; ++i) 80 : { 81 5 : if (IsSelectedGeomField(i)) 82 : { 83 5 : if (auto poGeom = poSrcFeature->GetGeomFieldRef(i)) 84 : { 85 3 : poGeom->swapXY(); 86 : } 87 : } 88 : } 89 : 90 5 : return poSrcFeature; 91 : } 92 : 93 : } // namespace 94 : 95 : /************************************************************************/ 96 : /* GDALVectorSwapXYAlgorithm::CreateAlgLayer() */ 97 : /************************************************************************/ 98 : 99 : std::unique_ptr<OGRLayerWithTranslateFeature> 100 1 : GDALVectorSwapXYAlgorithm::CreateAlgLayer(OGRLayer &srcLayer) 101 : { 102 1 : return std::make_unique<GDALVectorSwapXYAlgorithmLayer>(srcLayer, m_opts); 103 : } 104 : 105 : GDALVectorSwapXYAlgorithmStandalone::~GDALVectorSwapXYAlgorithmStandalone() = 106 : default; 107 : 108 : //! @endcond