GDAL
gdalalg_vector_geom.h
1/******************************************************************************
2 *
3 * Project: GDAL
4 * Purpose: Base classes for some geometry-related vector algorithms
5 * Author: Even Rouault <even dot rouault at spatialys.com>
6 *
7 ******************************************************************************
8 * Copyright (c) 2025, Even Rouault <even dot rouault at spatialys.com>
9 *
10 * SPDX-License-Identifier: MIT
11 ****************************************************************************/
12
13#ifndef GDALALG_VECTOR_GEOM_INCLUDED
14#define GDALALG_VECTOR_GEOM_INCLUDED
15
16#include "gdalalg_vector_pipeline.h"
17#include "ogr_geos.h"
18
20
21/************************************************************************/
22/* GDALVectorGeomAbstractAlgorithm */
23/************************************************************************/
24
25class GDALVectorGeomAbstractAlgorithm /* non final */
26 : public GDALVectorPipelineStepAlgorithm
27{
28 protected:
29 struct OptionsBase
30 {
31 std::string m_activeLayer{};
32 std::string m_geomField{};
33 };
34
35 virtual std::unique_ptr<OGRLayerWithTranslateFeature>
36 CreateAlgLayer(OGRLayer &srcLayer) = 0;
37
38 GDALVectorGeomAbstractAlgorithm(const std::string &name,
39 const std::string &description,
40 const std::string &helpURL,
41 bool standaloneStep, OptionsBase &opts);
42
43 bool RunStep(GDALPipelineStepRunContext &ctxt) override;
44
45 private:
46 std::string &m_activeLayer;
47};
48
49/************************************************************************/
50/* GDALVectorGeomOneToOneAlgorithmLayer */
51/************************************************************************/
52
53template <class T>
54class GDALVectorGeomOneToOneAlgorithmLayer /* non final */
55 : public GDALVectorPipelineOutputLayer
56{
57 public:
58 const OGRFeatureDefn *GetLayerDefn() const override
59 {
60 return m_srcLayer.GetLayerDefn();
61 }
62
63 GIntBig GetFeatureCount(int bForce) override
64 {
65 if (!m_poAttrQuery && !m_poFilterGeom)
66 return m_srcLayer.GetFeatureCount(bForce);
67 return OGRLayer::GetFeatureCount(bForce);
68 }
69
70 OGRErr IGetExtent(int iGeomField, OGREnvelope *psExtent,
71 bool bForce) override
72 {
73 return m_srcLayer.GetExtent(iGeomField, psExtent, bForce);
74 }
75
76 OGRFeature *GetFeature(GIntBig nFID) override
77 {
78 auto poSrcFeature =
79 std::unique_ptr<OGRFeature>(m_srcLayer.GetFeature(nFID));
80 if (!poSrcFeature)
81 return nullptr;
82 return TranslateFeature(std::move(poSrcFeature)).release();
83 }
84
85 int TestCapability(const char *pszCap) const override
86 {
87 if (EQUAL(pszCap, OLCRandomRead) || EQUAL(pszCap, OLCCurveGeometries) ||
89 EQUAL(pszCap, OLCZGeometries) || EQUAL(pszCap, OLCFastGetExtent) ||
90 (EQUAL(pszCap, OLCFastFeatureCount) && !m_poAttrQuery &&
91 !m_poFilterGeom) ||
92 EQUAL(pszCap, OLCStringsAsUTF8))
93 {
94 return m_srcLayer.TestCapability(pszCap);
95 }
96 return false;
97 }
98
99 protected:
100 const typename T::Options m_opts;
101
102 GDALVectorGeomOneToOneAlgorithmLayer(OGRLayer &oSrcLayer,
103 const typename T::Options &opts)
104 : GDALVectorPipelineOutputLayer(oSrcLayer), m_opts(opts)
105 {
106 SetDescription(oSrcLayer.GetDescription());
107 SetMetadata(oSrcLayer.GetMetadata());
108 if (!m_opts.m_geomField.empty())
109 {
110 const int nIdx = oSrcLayer.GetLayerDefn()->GetGeomFieldIndex(
111 m_opts.m_geomField.c_str());
112 if (nIdx >= 0)
113 m_iGeomIdx = nIdx;
114 else
115 m_iGeomIdx = INT_MAX;
116 }
117 }
118
119 bool IsSelectedGeomField(int idx) const
120 {
121 return m_iGeomIdx < 0 || idx == m_iGeomIdx;
122 }
123
124 virtual std::unique_ptr<OGRFeature>
125 TranslateFeature(std::unique_ptr<OGRFeature> poSrcFeature) const = 0;
126
127 void TranslateFeature(
128 std::unique_ptr<OGRFeature> poSrcFeature,
129 std::vector<std::unique_ptr<OGRFeature>> &apoOutFeatures) override
130 {
131 auto poDstFeature = TranslateFeature(std::move(poSrcFeature));
132 if (poDstFeature)
133 apoOutFeatures.push_back(std::move(poDstFeature));
134 }
135
136 private:
137 int m_iGeomIdx = -1;
138};
139
140#ifdef HAVE_GEOS
141
142/************************************************************************/
143/* GDALGeosNonStreamingAlgorithmLayer */
144/************************************************************************/
145
152class GDALGeosNonStreamingAlgorithmLayer
153 : public GDALVectorNonStreamingAlgorithmLayer
154{
155 public:
156 GDALGeosNonStreamingAlgorithmLayer(OGRLayer &srcLayer, int geomFieldIndex);
157
158 ~GDALGeosNonStreamingAlgorithmLayer() override;
159
160 void ResetReading() override;
161
162 CPL_DISALLOW_COPY_ASSIGN(GDALGeosNonStreamingAlgorithmLayer)
163
164 bool Process(GDALProgressFunc pfnProgress, void *pProgressData) override;
165
166 std::unique_ptr<OGRFeature> GetNextProcessedFeature() override;
167
168 virtual bool ProcessGeos() = 0;
169
171 virtual bool PolygonsOnly() const = 0;
172
174 virtual bool SkipEmpty() const = 0;
175
176 protected:
177 GEOSContextHandle_t m_poGeosContext{nullptr};
178 std::vector<GEOSGeometry *> m_apoGeosInputs{};
179 GEOSGeometry *m_poGeosResultAsCollection{nullptr};
180 GEOSGeometry **m_papoGeosResults{nullptr};
181
182 private:
183 bool ConvertInputsToGeos(OGRLayer &srcLayer, int geomFieldIndex,
184 GDALProgressFunc pfnProgress, void *pProgressData);
185
186 void Cleanup();
187
188 std::vector<std::unique_ptr<OGRFeature>> m_apoFeatures{};
189 unsigned int m_nGeosResultSize{0};
190 unsigned int m_readPos{0};
191};
192
193#endif
194
196
197#endif /* GDALALG_VECTOR_GEOM_INCLUDED */
virtual CSLConstList GetMetadata(const char *pszDomain="")
Fetch metadata.
Definition gdalmajorobject.cpp:228
virtual const char * GetDescription() const
Fetch object description.
Definition gdalmajorobject.cpp:61
Simple container for a bounding region (rectangle)
Definition ogr_core.h:44
Definition of a feature class or feature layer.
Definition ogr_feature.h:521
virtual int GetGeomFieldIndex(const char *) const
Find geometry field by name.
Definition ogrfeaturedefn.cpp:957
A simple feature, including geometry and attributes.
Definition ogr_feature.h:1041
This class represents a layer of simple features, with access methods.
Definition ogrsf_frmts.h:61
virtual GIntBig GetFeatureCount(int bForce=TRUE)
Fetch the feature count in this layer.
Definition ogrlayer.cpp:204
virtual const OGRFeatureDefn * GetLayerDefn() const =0
Fetch the schema information for this layer.
#define EQUAL(a, b)
Alias for strcasecmp() == 0.
Definition cpl_port.h:532
#define CPL_DISALLOW_COPY_ASSIGN(ClassName)
Helper to remove the copy and assignment constructors so that the compiler will not generate the defa...
Definition cpl_port.h:1101
long long GIntBig
Large signed integer type (generally 64-bit integer type).
Definition cpl_port.h:195
#define OLCStringsAsUTF8
Layer capability for strings returned with UTF-8 \ encoding.
Definition ogr_core.h:1016
#define OLCFastFeatureCount
Layer capability for fast feature count retrieval .
Definition ogr_core.h:983
#define OLCZGeometries
Layer capability for geometry with Z dimension support.
Definition ogr_core.h:1028
#define OLCRandomRead
Layer capability for random read.
Definition ogr_core.h:977
#define OLCFastGetExtent
Layer capability for fast extent retrieval.
Definition ogr_core.h:986
#define OLCMeasuredGeometries
Layer capability for measured geometries support .
Definition ogr_core.h:1025
int OGRErr
Type for a OGR error.
Definition ogr_core.h:388
#define OLCCurveGeometries
Layer capability for curve geometries support.
Definition ogr_core.h:1023
struct GEOSContextHandle_HS * GEOSContextHandle_t
GEOS context handle type.
Definition ogr_geometry.h:110