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