|
| 1 | +/****************************************************************************** |
| 2 | + * |
| 3 | + * Project: GDAL |
| 4 | + * Purpose: "change-field-type" step of "vector pipeline" |
| 5 | + * Author: Alessandro Pasotti <elpaso at itopen dot it> |
| 6 | + * |
| 7 | + ****************************************************************************** |
| 8 | + * Copyright (c) 2025, Alessandro Pasotti <elpaso at itopen dot it> |
| 9 | + * |
| 10 | + * SPDX-License-Identifier: MIT |
| 11 | + ****************************************************************************/ |
| 12 | + |
| 13 | +#include "gdalalg_vector_change_field_type.h" |
| 14 | +#include "gdal_priv.h" |
| 15 | + |
| 16 | +//! @cond Doxygen_Suppress |
| 17 | + |
| 18 | +#ifndef _ |
| 19 | +#define _(x) (x) |
| 20 | +#endif |
| 21 | + |
| 22 | +/************************************************************************/ |
| 23 | +/* GDALVectorChangeFieldTypeAlgorithm() */ |
| 24 | +/************************************************************************/ |
| 25 | + |
| 26 | +GDALVectorChangeFieldTypeAlgorithm::GDALVectorChangeFieldTypeAlgorithm( |
| 27 | + bool standaloneStep) |
| 28 | + : GDALVectorPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL, |
| 29 | + standaloneStep) |
| 30 | +{ |
| 31 | + auto &layerArg = AddActiveLayerArg(&m_activeLayer); |
| 32 | + auto &fieldNameArg = AddFieldNameArg(&m_fieldName) |
| 33 | + .SetRequired() |
| 34 | + .SetMutualExclusionGroup("name-or-type"); |
| 35 | + SetAutoCompleteFunctionForFieldName(fieldNameArg, layerArg, m_inputDataset); |
| 36 | + AddFieldTypeSubtypeArg(&m_srcFieldType, &m_srcFieldSubType, |
| 37 | + &m_srcFieldTypeSubTypeStr, "src-field-type", |
| 38 | + _("Source field type or subtype")) |
| 39 | + .SetRequired() |
| 40 | + .SetMutualExclusionGroup("name-or-type"); |
| 41 | + AddFieldTypeSubtypeArg(&m_newFieldType, &m_newFieldSubType, |
| 42 | + &m_newFieldTypeSubTypeStr, std::string(), |
| 43 | + _("Target field type or subtype")) |
| 44 | + .AddAlias("dst-field-type") |
| 45 | + .SetRequired(); |
| 46 | + AddValidationAction( |
| 47 | + [this] |
| 48 | + { |
| 49 | + if (!m_inputDataset.empty()) |
| 50 | + { |
| 51 | + auto mInDS = m_inputDataset[0].GetDatasetRef(); |
| 52 | + auto layer = m_activeLayer.empty() |
| 53 | + ? mInDS->GetLayer(0) |
| 54 | + : mInDS->GetLayerByName(m_activeLayer.c_str()); |
| 55 | + if (!layer) |
| 56 | + { |
| 57 | + CPLError(CE_Failure, CPLE_AppDefined, |
| 58 | + "Cannot find layer '%s'", m_activeLayer.c_str()); |
| 59 | + return false; |
| 60 | + } |
| 61 | + if (!m_fieldName.empty() && |
| 62 | + layer->GetLayerDefn()->GetFieldIndex(m_fieldName.c_str()) < |
| 63 | + 0) |
| 64 | + { |
| 65 | + CPLError(CE_Failure, CPLE_AppDefined, |
| 66 | + "Cannot find field '%s' in layer '%s'", |
| 67 | + m_fieldName.c_str(), layer->GetName()); |
| 68 | + return false; |
| 69 | + } |
| 70 | + return true; |
| 71 | + } |
| 72 | + return false; |
| 73 | + }); |
| 74 | +} |
| 75 | + |
| 76 | +/************************************************************************/ |
| 77 | +/* GDALVectorChangeFieldTypeAlgorithmLayer */ |
| 78 | +/************************************************************************/ |
| 79 | + |
| 80 | +namespace |
| 81 | +{ |
| 82 | +class GDALVectorChangeFieldTypeAlgorithmLayer final |
| 83 | + : public GDALVectorPipelineOutputLayer |
| 84 | +{ |
| 85 | + public: |
| 86 | + GDALVectorChangeFieldTypeAlgorithmLayer( |
| 87 | + OGRLayer &oSrcLayer, const std::string &activeLayer, |
| 88 | + const std::string &fieldName, const OGRFieldType srcFieldType, |
| 89 | + const OGRFieldSubType srcFieldSubType, const OGRFieldType newFieldType, |
| 90 | + const OGRFieldSubType newFieldSubType) |
| 91 | + : GDALVectorPipelineOutputLayer(oSrcLayer) |
| 92 | + { |
| 93 | + |
| 94 | + m_poFeatureDefn = oSrcLayer.GetLayerDefn()->Clone(); |
| 95 | + m_poFeatureDefn->Reference(); |
| 96 | + |
| 97 | + if (activeLayer.empty() || activeLayer == GetDescription()) |
| 98 | + { |
| 99 | + if (!fieldName.empty()) |
| 100 | + { |
| 101 | + m_fieldIndex = |
| 102 | + m_poFeatureDefn->GetFieldIndex(fieldName.c_str()); |
| 103 | + if (m_fieldIndex >= 0) |
| 104 | + { |
| 105 | + auto poFieldDefn = |
| 106 | + m_poFeatureDefn->GetFieldDefn(m_fieldIndex); |
| 107 | + if (poFieldDefn->GetType() != newFieldType) |
| 108 | + { |
| 109 | + m_passThrough = false; |
| 110 | + } |
| 111 | + |
| 112 | + // Set to OFSTNone to bypass the check that prevents changing the type |
| 113 | + poFieldDefn->SetSubType(OFSTNone); |
| 114 | + poFieldDefn->SetType(newFieldType); |
| 115 | + poFieldDefn->SetSubType(newFieldSubType); |
| 116 | + } |
| 117 | + } |
| 118 | + else |
| 119 | + { |
| 120 | + for (int i = 0; i < m_poFeatureDefn->GetFieldCount(); ++i) |
| 121 | + { |
| 122 | + auto poFieldDefn = m_poFeatureDefn->GetFieldDefn(i); |
| 123 | + if (poFieldDefn->GetType() == srcFieldType && |
| 124 | + poFieldDefn->GetSubType() == srcFieldSubType) |
| 125 | + { |
| 126 | + m_passThrough = false; |
| 127 | + |
| 128 | + // Set to OFSTNone to bypass the check that prevents changing the type |
| 129 | + poFieldDefn->SetSubType(OFSTNone); |
| 130 | + poFieldDefn->SetType(newFieldType); |
| 131 | + poFieldDefn->SetSubType(newFieldSubType); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + for (int i = 0; i < m_poFeatureDefn->GetFieldCount(); i++) |
| 137 | + { |
| 138 | + m_identityMap.push_back(i); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + ~GDALVectorChangeFieldTypeAlgorithmLayer() override |
| 144 | + { |
| 145 | + m_poFeatureDefn->Release(); |
| 146 | + } |
| 147 | + |
| 148 | + const OGRFeatureDefn *GetLayerDefn() const override |
| 149 | + { |
| 150 | + return m_poFeatureDefn; |
| 151 | + } |
| 152 | + |
| 153 | + void TranslateFeature( |
| 154 | + std::unique_ptr<OGRFeature> poSrcFeature, |
| 155 | + std::vector<std::unique_ptr<OGRFeature>> &apoOutFeatures) override |
| 156 | + { |
| 157 | + if (m_passThrough) |
| 158 | + { |
| 159 | + apoOutFeatures.push_back(std::move(poSrcFeature)); |
| 160 | + } |
| 161 | + else |
| 162 | + { |
| 163 | + auto poDstFeature = std::make_unique<OGRFeature>(m_poFeatureDefn); |
| 164 | + const auto result{poDstFeature->SetFrom( |
| 165 | + poSrcFeature.get(), m_identityMap.data(), false, true)}; |
| 166 | + if (result != OGRERR_NONE) |
| 167 | + { |
| 168 | + if (m_fieldIndex >= 0) |
| 169 | + { |
| 170 | + CPLError(CE_Warning, CPLE_AppDefined, |
| 171 | + "Cannot convert field '%s' to new type, setting " |
| 172 | + "it to NULL", |
| 173 | + m_poFeatureDefn->GetFieldDefn(m_fieldIndex) |
| 174 | + ->GetNameRef()); |
| 175 | + } |
| 176 | + } |
| 177 | + else |
| 178 | + { |
| 179 | + poDstFeature->SetFID(poSrcFeature->GetFID()); |
| 180 | + apoOutFeatures.push_back(std::move(poDstFeature)); |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + int TestCapability(const char *pszCap) const override |
| 186 | + { |
| 187 | + if (EQUAL(pszCap, OLCStringsAsUTF8) || |
| 188 | + EQUAL(pszCap, OLCCurveGeometries) || |
| 189 | + EQUAL(pszCap, OLCZGeometries) || |
| 190 | + EQUAL(pszCap, OLCMeasuredGeometries)) |
| 191 | + return m_srcLayer.TestCapability(pszCap); |
| 192 | + return false; |
| 193 | + } |
| 194 | + |
| 195 | + private: |
| 196 | + OGRFeatureDefn *m_poFeatureDefn = nullptr; |
| 197 | + int m_fieldIndex{-1}; |
| 198 | + bool m_passThrough = true; |
| 199 | + std::vector<int> m_identityMap{}; |
| 200 | + |
| 201 | + CPL_DISALLOW_COPY_ASSIGN(GDALVectorChangeFieldTypeAlgorithmLayer) |
| 202 | +}; |
| 203 | + |
| 204 | +} // namespace |
| 205 | + |
| 206 | +/************************************************************************/ |
| 207 | +/* GDALVectorChangeFieldTypeAlgorithm::RunStep() */ |
| 208 | +/************************************************************************/ |
| 209 | + |
| 210 | +bool GDALVectorChangeFieldTypeAlgorithm::RunStep(GDALPipelineStepRunContext &) |
| 211 | +{ |
| 212 | + auto poSrcDS = m_inputDataset[0].GetDatasetRef(); |
| 213 | + CPLAssert(poSrcDS); |
| 214 | + |
| 215 | + CPLAssert(m_outputDataset.GetName().empty()); |
| 216 | + CPLAssert(!m_outputDataset.GetDatasetRef()); |
| 217 | + |
| 218 | + const int nLayerCount = poSrcDS->GetLayerCount(); |
| 219 | + |
| 220 | + auto outDS = std::make_unique<GDALVectorPipelineOutputDataset>(*poSrcDS); |
| 221 | + |
| 222 | + bool ret = true; |
| 223 | + for (int i = 0; ret && i < nLayerCount; ++i) |
| 224 | + { |
| 225 | + auto poSrcLayer = poSrcDS->GetLayer(i); |
| 226 | + ret = (poSrcLayer != nullptr); |
| 227 | + if (ret) |
| 228 | + { |
| 229 | + outDS->AddLayer( |
| 230 | + *poSrcLayer, |
| 231 | + std::make_unique<GDALVectorChangeFieldTypeAlgorithmLayer>( |
| 232 | + *poSrcLayer, m_activeLayer, m_fieldName, m_srcFieldType, |
| 233 | + m_srcFieldSubType, m_newFieldType, m_newFieldSubType)); |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + if (ret) |
| 238 | + m_outputDataset.Set(std::move(outDS)); |
| 239 | + |
| 240 | + printf("ret= %d\n", ret); |
| 241 | + return ret; |
| 242 | +} |
| 243 | + |
| 244 | +GDALVectorChangeFieldTypeAlgorithmStandalone:: |
| 245 | + ~GDALVectorChangeFieldTypeAlgorithmStandalone() = default; |
| 246 | + |
| 247 | +//! @endcond |
0 commit comments