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