|
| 1 | +/****************************************************************************** |
| 2 | + * |
| 3 | + * Project: GDAL |
| 4 | + * Purpose: "gdal vector dissolve" |
| 5 | + * Author: Dan Baston |
| 6 | + * |
| 7 | + ****************************************************************************** |
| 8 | + * Copyright (c) 2025, ISciences LLC |
| 9 | + * |
| 10 | + * SPDX-License-Identifier: MIT |
| 11 | + ****************************************************************************/ |
| 12 | + |
| 13 | +#include "gdalalg_vector_dissolve.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 | +/* GDALVectorDissolveAlgorithm() */ |
| 26 | +/************************************************************************/ |
| 27 | + |
| 28 | +GDALVectorDissolveAlgorithm::GDALVectorDissolveAlgorithm(bool standaloneStep) |
| 29 | + : GDALVectorGeomAbstractAlgorithm(NAME, DESCRIPTION, HELP_URL, |
| 30 | + standaloneStep, m_opts) |
| 31 | +{ |
| 32 | +} |
| 33 | + |
| 34 | +#ifdef HAVE_GEOS |
| 35 | + |
| 36 | +namespace |
| 37 | +{ |
| 38 | + |
| 39 | +/************************************************************************/ |
| 40 | +/* GDALVectorDissolveAlgorithmLayer */ |
| 41 | +/************************************************************************/ |
| 42 | + |
| 43 | +class GDALVectorDissolveAlgorithmLayer final |
| 44 | + : public GDALVectorGeomOneToOneAlgorithmLayer<GDALVectorDissolveAlgorithm> |
| 45 | +{ |
| 46 | + public: |
| 47 | + GDALVectorDissolveAlgorithmLayer( |
| 48 | + OGRLayer &oSrcLayer, const GDALVectorDissolveAlgorithm::Options &opts) |
| 49 | + : GDALVectorGeomOneToOneAlgorithmLayer<GDALVectorDissolveAlgorithm>( |
| 50 | + oSrcLayer, opts) |
| 51 | + { |
| 52 | + } |
| 53 | + |
| 54 | + protected: |
| 55 | + using GDALVectorGeomOneToOneAlgorithmLayer::TranslateFeature; |
| 56 | + |
| 57 | + std::unique_ptr<OGRFeature> |
| 58 | + TranslateFeature(std::unique_ptr<OGRFeature> poSrcFeature) const override; |
| 59 | + |
| 60 | + private: |
| 61 | +}; |
| 62 | + |
| 63 | +/************************************************************************/ |
| 64 | +/* TranslateFeature() */ |
| 65 | +/************************************************************************/ |
| 66 | + |
| 67 | +std::unique_ptr<OGRFeature> GDALVectorDissolveAlgorithmLayer::TranslateFeature( |
| 68 | + std::unique_ptr<OGRFeature> poSrcFeature) const |
| 69 | +{ |
| 70 | + const int nGeomFieldCount = poSrcFeature->GetGeomFieldCount(); |
| 71 | + for (int i = 0; i < nGeomFieldCount; ++i) |
| 72 | + { |
| 73 | + if (IsSelectedGeomField(i)) |
| 74 | + { |
| 75 | + if (auto poGeom = std::unique_ptr<OGRGeometry>( |
| 76 | + poSrcFeature->StealGeometry(i))) |
| 77 | + { |
| 78 | + poGeom.reset(poGeom->UnaryUnion()); |
| 79 | + |
| 80 | + if (poGeom) |
| 81 | + { |
| 82 | + poGeom->assignSpatialReference(m_srcLayer.GetLayerDefn() |
| 83 | + ->GetGeomFieldDefn(i) |
| 84 | + ->GetSpatialRef()); |
| 85 | + poSrcFeature->SetGeomField(i, std::move(poGeom)); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return poSrcFeature; |
| 92 | +} |
| 93 | + |
| 94 | +} // namespace |
| 95 | + |
| 96 | +#endif // HAVE_GEOS |
| 97 | + |
| 98 | +/************************************************************************/ |
| 99 | +/* GDALVectorDissolveAlgorithm::CreateAlgLayer() */ |
| 100 | +/************************************************************************/ |
| 101 | + |
| 102 | +std::unique_ptr<OGRLayerWithTranslateFeature> |
| 103 | +GDALVectorDissolveAlgorithm::CreateAlgLayer([[maybe_unused]] OGRLayer &srcLayer) |
| 104 | +{ |
| 105 | +#ifdef HAVE_GEOS |
| 106 | + return std::make_unique<GDALVectorDissolveAlgorithmLayer>(srcLayer, m_opts); |
| 107 | +#else |
| 108 | + CPLAssert(false); |
| 109 | + return nullptr; |
| 110 | +#endif |
| 111 | +} |
| 112 | + |
| 113 | +/************************************************************************/ |
| 114 | +/* GDALVectorDissolveAlgorithm::RunStep() */ |
| 115 | +/************************************************************************/ |
| 116 | + |
| 117 | +bool GDALVectorDissolveAlgorithm::RunStep(GDALPipelineStepRunContext &ctxt) |
| 118 | +{ |
| 119 | +#ifdef HAVE_GEOS |
| 120 | + return GDALVectorGeomAbstractAlgorithm::RunStep(ctxt); |
| 121 | +#else |
| 122 | + (void)ctxt; |
| 123 | + ReportError(CE_Failure, CPLE_NotSupported, |
| 124 | + "This algorithm is only supported for builds against GEOS"); |
| 125 | + return false; |
| 126 | +#endif |
| 127 | +} |
| 128 | + |
| 129 | +GDALVectorDissolveAlgorithmStandalone:: |
| 130 | + ~GDALVectorDissolveAlgorithmStandalone() = default; |
| 131 | + |
| 132 | +//! @endcond |
0 commit comments