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 64 : GDALVectorFilterAlgorithm::GDALVectorFilterAlgorithm(bool standaloneStep) 32 : : GDALVectorPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL, 33 64 : standaloneStep) 34 : { 35 64 : AddActiveLayerArg(&m_activeLayer); 36 64 : 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 128 : &m_where) 41 64 : .SetReadFromFileAtSyntaxAllowed() 42 128 : .SetMetaVar("<WHERE>|@<filename>") 43 64 : .SetRemoveSQLCommentsEnabled(); 44 64 : } 45 : 46 : /************************************************************************/ 47 : /* GDALVectorFilterAlgorithm::RunStep() */ 48 : /************************************************************************/ 49 : 50 12 : bool GDALVectorFilterAlgorithm::RunStep(GDALPipelineStepRunContext &) 51 : { 52 12 : auto poSrcDS = m_inputDataset[0].GetDatasetRef(); 53 12 : CPLAssert(poSrcDS); 54 : 55 12 : CPLAssert(m_outputDataset.GetName().empty()); 56 12 : CPLAssert(!m_outputDataset.GetDatasetRef()); 57 : 58 12 : const int nLayerCount = poSrcDS->GetLayerCount(); 59 : 60 12 : bool ret = true; 61 12 : if (m_bbox.size() == 4) 62 : { 63 4 : const double xmin = m_bbox[0]; 64 4 : const double ymin = m_bbox[1]; 65 4 : const double xmax = m_bbox[2]; 66 4 : const double ymax = m_bbox[3]; 67 8 : for (int i = 0; i < nLayerCount; ++i) 68 : { 69 4 : auto poSrcLayer = poSrcDS->GetLayer(i); 70 4 : ret = ret && (poSrcLayer != nullptr); 71 4 : if (poSrcLayer && (m_activeLayer.empty() || 72 0 : m_activeLayer == poSrcLayer->GetDescription())) 73 4 : poSrcLayer->SetSpatialFilterRect(xmin, ymin, xmax, ymax); 74 : } 75 : } 76 : 77 12 : if (ret && !m_where.empty()) 78 : { 79 14 : for (int i = 0; i < nLayerCount; ++i) 80 : { 81 8 : auto poSrcLayer = poSrcDS->GetLayer(i); 82 8 : ret = ret && (poSrcLayer != nullptr); 83 10 : if (ret && (m_activeLayer.empty() || 84 2 : m_activeLayer == poSrcLayer->GetDescription())) 85 : { 86 7 : ret = poSrcLayer->SetAttributeFilter(m_where.c_str()) == 87 : OGRERR_NONE; 88 : } 89 : } 90 : } 91 : 92 12 : if (ret) 93 : { 94 11 : m_outputDataset.Set(poSrcDS); 95 : } 96 : 97 12 : return ret; 98 : } 99 : 100 : GDALVectorFilterAlgorithmStandalone::~GDALVectorFilterAlgorithmStandalone() = 101 : default; 102 : 103 : //! @endcond