GDAL
ogrunionlayer.h
1/******************************************************************************
2 *
3 * Project: OpenGIS Simple Features Reference Implementation
4 * Purpose: Defines OGRUnionLayer class
5 * Author: Even Rouault, even dot rouault at spatialys.com
6 *
7 ******************************************************************************
8 * Copyright (c) 2012-2014, Even Rouault <even dot rouault at spatialys.com>
9 *
10 * SPDX-License-Identifier: MIT
11 ****************************************************************************/
12
13#ifndef OGRUNIONLAYER_H_INCLUDED
14#define OGRUNIONLAYER_H_INCLUDED
15
16#ifndef DOXYGEN_SKIP
17
18#include "ogrsf_frmts.h"
19
20#include <algorithm>
21#include <mutex>
22#include <utility>
23
24/************************************************************************/
25/* OGRUnionLayerGeomFieldDefn */
26/************************************************************************/
27
28class CPL_DLL OGRUnionLayerGeomFieldDefn final : public OGRGeomFieldDefn
29{
30 public:
31 int bGeomTypeSet = false;
32 int bSRSSet = false;
33 OGREnvelope sStaticEnvelope{};
34
35 OGRUnionLayerGeomFieldDefn(const char *pszName, OGRwkbGeometryType eType);
36 explicit OGRUnionLayerGeomFieldDefn(const OGRGeomFieldDefn &oSrc);
37 OGRUnionLayerGeomFieldDefn(const OGRUnionLayerGeomFieldDefn &oSrc);
38 OGRUnionLayerGeomFieldDefn(OGRUnionLayerGeomFieldDefn &&) = default;
39 ~OGRUnionLayerGeomFieldDefn() override;
40
41 OGRUnionLayerGeomFieldDefn &
42 operator=(const OGRUnionLayerGeomFieldDefn &) = delete;
43 OGRUnionLayerGeomFieldDefn &
44 operator=(OGRUnionLayerGeomFieldDefn &&) = delete;
45};
46
47/************************************************************************/
48/* OGRUnionLayer */
49/************************************************************************/
50
51typedef enum
52{
53 FIELD_FROM_FIRST_LAYER,
54 FIELD_UNION_ALL_LAYERS,
55 FIELD_INTERSECTION_ALL_LAYERS,
56 FIELD_SPECIFIED,
57} FieldUnionStrategy;
58
59class CPL_DLL OGRUnionLayer final : public OGRLayer
60{
61 private:
62 CPL_DISALLOW_COPY_ASSIGN(OGRUnionLayer)
63
64 struct Layer
65 {
66 std::unique_ptr<OGRLayer> poLayerKeeper{};
67 OGRLayer *poLayer = nullptr;
68 bool bModified = false;
69 bool bCheckIfAutoWrap = false;
70
72
73 Layer(OGRLayer *poLayerIn, bool bOwnedIn)
74 : poLayerKeeper(bOwnedIn ? poLayerIn : nullptr),
75 poLayer(bOwnedIn ? poLayerKeeper.get() : poLayerIn)
76 {
77 }
78
79 Layer(Layer &&) = default;
80 Layer &operator=(Layer &&) = default;
81
82 OGRLayer *operator->()
83 {
84 return poLayer;
85 }
86
87 const OGRLayer *operator->() const
88 {
89 return poLayer;
90 }
91
92 std::pair<OGRLayer *, bool> release()
93 {
94 const bool bOwnedBackup = poLayerKeeper != nullptr;
95 OGRLayer *poLayerBackup =
96 poLayerKeeper ? poLayerKeeper.release() : poLayer;
97 poLayerKeeper.reset();
98 return std::make_pair(poLayerBackup, bOwnedBackup);
99 }
100
101 void reset(std::unique_ptr<OGRLayer> poLayerIn)
102 {
103 poLayerKeeper = std::move(poLayerIn);
104 poLayer = poLayerKeeper.get();
105 }
106 };
107
108 CPLString osName{};
109
110 std::vector<Layer> m_apoSrcLayers{};
111
112 mutable OGRFeatureDefn *poFeatureDefn = nullptr;
113 std::vector<std::unique_ptr<OGRFieldDefn>> apoFields{};
114 std::vector<std::unique_ptr<OGRUnionLayerGeomFieldDefn>> apoGeomFields{};
115 bool bUseGeomFields = true;
116 FieldUnionStrategy eFieldStrategy = FIELD_UNION_ALL_LAYERS;
117 CPLString osSourceLayerFieldName{};
118
119 int bPreserveSrcFID = false;
120
121 GIntBig nFeatureCount = -1;
122
123 int iCurLayer = -1;
124 bool m_bHasAlreadyIteratedOverFeatures = false;
125 char *pszAttributeFilter = nullptr;
126 int nNextFID = 0;
127 int *panMap = nullptr;
128 CPLStringList m_aosIgnoredFields{};
129 mutable int bAttrFilterPassThroughValue = -1;
130 mutable const OGRSpatialReference *poGlobalSRS = nullptr;
131
132 std::mutex m_oMutex{};
133
134 /* Map from target FID to (source layer, source FID) */
135 struct FIDRange
136 {
137 GIntBig nDstFIDStart = 0;
138 GIntBig nFIDCount = 0;
139 GIntBig nSrcFIDStart = 0;
140 int nLayerIdx = 0;
141 };
142
143 std::vector<FIDRange> m_fidRanges{};
144 bool m_fidRangesInvalid = false;
145 bool m_fidRangesComplete = false;
146
147 void AutoWarpLayerIfNecessary(int iSubLayer);
148 std::unique_ptr<OGRFeature> TranslateFromSrcLayer(OGRFeature *poSrcFeature,
149 GIntBig nFID);
150 void ApplyAttributeFilterToSrcLayer(int iSubLayer);
151 int GetAttrFilterPassThroughValue() const;
152 void ConfigureActiveLayer();
153 void SetSpatialFilterToSourceLayer(OGRLayer *poSrcLayer);
154
155 public:
156 OGRUnionLayer(
157 const char *pszName, int nSrcLayers, /* must be >= 1 */
158 OGRLayer *
159 *papoSrcLayers, /* array itself ownership always transferred, layer
160 ownership depending on bTakeLayerOwnership */
161 int bTakeLayerOwnership);
162
163 ~OGRUnionLayer() override;
164
165 /* All the following non virtual methods must be called just after the
166 * constructor */
167 /* and before any virtual method */
168 void SetFields(
169 FieldUnionStrategy eFieldStrategy, int nFields,
170 const OGRFieldDefn *paoFields, /* duplicated by the method */
171 int nGeomFields, /* maybe -1 to explicitly disable geometry fields */
172 const OGRUnionLayerGeomFieldDefn
173 *paoGeomFields /* duplicated by the method */);
174 void SetFields(FieldUnionStrategy eFieldStrategy,
175 const OGRFeatureDefn *poFeatureDefnIn);
176
177 void SetSourceLayerFieldName(const char *pszSourceLayerFieldName);
178 void SetPreserveSrcFID(int bPreserveSrcFID);
179 void SetFeatureCount(int nFeatureCount);
180
181 const char *GetName() const override
182 {
183 return osName.c_str();
184 }
185
186 OGRwkbGeometryType GetGeomType() const override;
187
188 void ResetReading() override;
189 OGRFeature *GetNextFeature() override;
190
191 OGRFeature *GetFeature(GIntBig nFeatureId) override;
192
193 OGRErr ICreateFeature(OGRFeature *poFeature) override;
194
195 OGRErr ISetFeature(OGRFeature *poFeature) override;
196
197 OGRErr IUpsertFeature(OGRFeature *poFeature) override;
198
199 OGRErr IUpdateFeature(OGRFeature *poFeature, int nUpdatedFieldsCount,
200 const int *panUpdatedFieldsIdx,
201 int nUpdatedGeomFieldsCount,
202 const int *panUpdatedGeomFieldsIdx,
203 bool bUpdateStyleString) override;
204
205 const OGRFeatureDefn *GetLayerDefn() const override;
206
207 const OGRSpatialReference *GetSpatialRef() const override;
208
209 GIntBig GetFeatureCount(int) override;
210
211 OGRErr SetAttributeFilter(const char *) override;
212
213 int TestCapability(const char *) const override;
214
215 OGRErr IGetExtent(int iGeomField, OGREnvelope *psExtent,
216 bool bForce) override;
217
218 virtual OGRErr ISetSpatialFilter(int iGeomField,
219 const OGRGeometry *) override;
220
221 OGRErr SetIgnoredFields(CSLConstList papszFields) override;
222
223 OGRErr SyncToDisk() override;
224};
225
226#endif /* #ifndef DOXYGEN_SKIP */
227
228#endif // OGRUNIONLAYER_H_INCLUDED
String list class designed around our use of C "char**" string lists.
Definition cpl_string.h:476
Convenient string class based on std::string.
Definition cpl_string.h:338
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
A simple feature, including geometry and attributes.
Definition ogr_feature.h:1041
Definition of an attribute of an OGRFeatureDefn.
Definition ogr_feature.h:72
Definition of a geometry field of an OGRFeatureDefn.
Definition ogr_feature.h:335
OGRGeomFieldDefn & operator=(const OGRGeomFieldDefn &oOther)
Copy assignment operator.
Definition ogrgeomfielddefn.cpp:156
virtual const OGRSpatialReference * GetSpatialRef() const
Fetch spatial reference system of this field.
Definition ogrgeomfielddefn.cpp:520
Abstract base class for all geometry classes.
Definition ogr_geometry.h:357
This class represents a layer of simple features, with access methods.
Definition ogrsf_frmts.h:61
This class represents an OpenGIS Spatial Reference System, and contains methods for converting betwee...
Definition ogr_spatialref.h:152
#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
char ** CSLConstList
Type of a constant null-terminated list of nul terminated strings.
Definition cpl_port.h:1252
long long GIntBig
Large signed integer type (generally 64-bit integer type).
Definition cpl_port.h:195
OGRwkbGeometryType
List of well known binary geometry types.
Definition ogr_core.h:423
int OGRErr
Type for a OGR error.
Definition ogr_core.h:388
Classes related to registration of format support, and opening datasets.