Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: GDAL 4 : * Purpose: "gdal vector geom 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_geom_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 : /* GDALVectorGeomSwapXYAlgorithm() */ 26 : /************************************************************************/ 27 : 28 6 : GDALVectorGeomSwapXYAlgorithm::GDALVectorGeomSwapXYAlgorithm( 29 6 : bool standaloneStep) 30 : : GDALVectorGeomAbstractAlgorithm(NAME, DESCRIPTION, HELP_URL, 31 6 : standaloneStep, m_opts) 32 : { 33 6 : } 34 : 35 : namespace 36 : { 37 : 38 : /************************************************************************/ 39 : /* GDALVectorGeomSwapXYAlgorithmLayer */ 40 : /************************************************************************/ 41 : 42 : class GDALVectorGeomSwapXYAlgorithmLayer final 43 : : public GDALVectorGeomOneToOneAlgorithmLayer<GDALVectorGeomSwapXYAlgorithm> 44 : { 45 : public: 46 1 : GDALVectorGeomSwapXYAlgorithmLayer( 47 : OGRLayer &oSrcLayer, const GDALVectorGeomSwapXYAlgorithm::Options &opts) 48 1 : : GDALVectorGeomOneToOneAlgorithmLayer<GDALVectorGeomSwapXYAlgorithm>( 49 1 : oSrcLayer, opts) 50 : { 51 1 : } 52 : 53 1 : OGRErr IGetExtent(int iGeomField, OGREnvelope *psExtent, 54 : bool bForce) override 55 : { 56 1 : OGRErr eErr = m_srcLayer.GetExtent(iGeomField, psExtent, bForce); 57 1 : if (eErr == CE_None) 58 : { 59 1 : std::swap(psExtent->MinX, psExtent->MinY); 60 1 : std::swap(psExtent->MaxX, psExtent->MaxY); 61 : } 62 1 : return eErr; 63 : } 64 : 65 : protected: 66 : using GDALVectorGeomOneToOneAlgorithmLayer::TranslateFeature; 67 : 68 : std::unique_ptr<OGRFeature> 69 : TranslateFeature(std::unique_ptr<OGRFeature> poSrcFeature) const override; 70 : }; 71 : 72 : /************************************************************************/ 73 : /* TranslateFeature() */ 74 : /************************************************************************/ 75 : 76 : std::unique_ptr<OGRFeature> 77 5 : GDALVectorGeomSwapXYAlgorithmLayer::TranslateFeature( 78 : std::unique_ptr<OGRFeature> poSrcFeature) const 79 : { 80 5 : const int nGeomFieldCount = poSrcFeature->GetGeomFieldCount(); 81 10 : for (int i = 0; i < nGeomFieldCount; ++i) 82 : { 83 5 : if (IsSelectedGeomField(i)) 84 : { 85 5 : if (auto poGeom = poSrcFeature->GetGeomFieldRef(i)) 86 : { 87 3 : poGeom->swapXY(); 88 : } 89 : } 90 : } 91 : 92 5 : return poSrcFeature; 93 : } 94 : 95 : } // namespace 96 : 97 : /************************************************************************/ 98 : /* GDALVectorGeomSwapXYAlgorithm::CreateAlgLayer() */ 99 : /************************************************************************/ 100 : 101 : std::unique_ptr<OGRLayerWithTranslateFeature> 102 1 : GDALVectorGeomSwapXYAlgorithm::CreateAlgLayer(OGRLayer &srcLayer) 103 : { 104 1 : return std::make_unique<GDALVectorGeomSwapXYAlgorithmLayer>(srcLayer, 105 1 : m_opts); 106 : } 107 : 108 : //! @endcond