Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: GDAL 4 : * Purpose: "filter" 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_filter.h" 14 : 15 : #include "gdal_priv.h" 16 : #include "ogrsf_frmts.h" 17 : #include "ogr_p.h" 18 : 19 : #include <set> 20 : 21 : //! @cond Doxygen_Suppress 22 : 23 : #ifndef _ 24 : #define _(x) (x) 25 : #endif 26 : 27 : /************************************************************************/ 28 : /* GDALVectorFilterAlgorithm::GDALVectorFilterAlgorithm() */ 29 : /************************************************************************/ 30 : 31 31 : GDALVectorFilterAlgorithm::GDALVectorFilterAlgorithm(bool standaloneStep) 32 : : GDALVectorPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL, 33 31 : standaloneStep) 34 : { 35 31 : AddActiveLayerArg(&m_activeLayer); 36 31 : AddBBOXArg(&m_bbox); 37 : AddArg("where", 0, 38 : _("Attribute query in a restricted form of the queries used in the " 39 : "SQL WHERE statement"), 40 62 : &m_where) 41 31 : .SetReadFromFileAtSyntaxAllowed() 42 62 : .SetMetaVar("<WHERE>|@<filename>") 43 31 : .SetRemoveSQLCommentsEnabled(); 44 31 : } 45 : 46 : /************************************************************************/ 47 : /* GDALVectorFilterAlgorithm::RunStep() */ 48 : /************************************************************************/ 49 : 50 9 : bool GDALVectorFilterAlgorithm::RunStep(GDALPipelineStepRunContext &) 51 : { 52 9 : auto poSrcDS = m_inputDataset[0].GetDatasetRef(); 53 9 : CPLAssert(poSrcDS); 54 : 55 9 : CPLAssert(m_outputDataset.GetName().empty()); 56 9 : CPLAssert(!m_outputDataset.GetDatasetRef()); 57 : 58 9 : const int nLayerCount = poSrcDS->GetLayerCount(); 59 : 60 9 : bool ret = true; 61 9 : if (m_bbox.size() == 4) 62 : { 63 3 : const double xmin = m_bbox[0]; 64 3 : const double ymin = m_bbox[1]; 65 3 : const double xmax = m_bbox[2]; 66 3 : const double ymax = m_bbox[3]; 67 6 : for (int i = 0; i < nLayerCount; ++i) 68 : { 69 3 : auto poSrcLayer = poSrcDS->GetLayer(i); 70 3 : ret = ret && (poSrcLayer != nullptr); 71 3 : if (poSrcLayer && (m_activeLayer.empty() || 72 0 : m_activeLayer == poSrcLayer->GetDescription())) 73 3 : poSrcLayer->SetSpatialFilterRect(xmin, ymin, xmax, ymax); 74 : } 75 : } 76 : 77 9 : if (ret && !m_where.empty()) 78 : { 79 9 : for (int i = 0; i < nLayerCount; ++i) 80 : { 81 5 : auto poSrcLayer = poSrcDS->GetLayer(i); 82 5 : ret = ret && (poSrcLayer != nullptr); 83 7 : if (ret && (m_activeLayer.empty() || 84 2 : m_activeLayer == poSrcLayer->GetDescription())) 85 : { 86 4 : ret = poSrcLayer->SetAttributeFilter(m_where.c_str()) == 87 : OGRERR_NONE; 88 : } 89 : } 90 : } 91 : 92 9 : if (ret) 93 : { 94 8 : m_outputDataset.Set(poSrcDS); 95 : } 96 : 97 9 : return ret; 98 : } 99 : 100 : GDALVectorFilterAlgorithmStandalone::~GDALVectorFilterAlgorithmStandalone() = 101 : default; 102 : 103 : //! @endcond