Line data Source code
1 : /******************************************************************************
2 : * $Id$
3 : *
4 : * Project: OpenGIS Simple Features Reference Implementation
5 : * Purpose: Private definitions for OGR/VRT driver.
6 : * Author: Frank Warmerdam, warmerdam@pobox.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2003, Frank Warmerdam <warmerdam@pobox.com>
10 : * Copyright (c) 2009-2014, Even Rouault <even dot rouault at spatialys.com>
11 : *
12 : * Permission is hereby granted, free of charge, to any person obtaining a
13 : * copy of this software and associated documentation files (the "Software"),
14 : * to deal in the Software without restriction, including without limitation
15 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 : * and/or sell copies of the Software, and to permit persons to whom the
17 : * Software is furnished to do so, subject to the following conditions:
18 : *
19 : * The above copyright notice and this permission notice shall be included
20 : * in all copies or substantial portions of the Software.
21 : *
22 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 : * DEALINGS IN THE SOFTWARE.
29 : ****************************************************************************/
30 :
31 : #ifndef OGR_VRT_H_INCLUDED
32 : #define OGR_VRT_H_INCLUDED
33 :
34 : #include "cpl_error.h"
35 : #include "cpl_minixml.h"
36 : #include "cpl_string.h"
37 : #include "ogrlayerpool.h"
38 : #include "ogrsf_frmts.h"
39 :
40 : #include <set>
41 : #include <string>
42 : #include <vector>
43 :
44 : typedef enum
45 : {
46 : VGS_None,
47 : VGS_Direct,
48 : VGS_PointFromColumns,
49 : VGS_WKT,
50 : VGS_WKB,
51 : VGS_Shape
52 : } OGRVRTGeometryStyle;
53 :
54 : /************************************************************************/
55 : /* OGRVRTGeomFieldProps */
56 : /************************************************************************/
57 :
58 : class OGRVRTGeomFieldProps
59 : {
60 : public:
61 : CPLString osName; // Name of the VRT geometry field */
62 : OGRwkbGeometryType eGeomType;
63 : const OGRSpatialReference *poSRS;
64 :
65 : bool bSrcClip;
66 : OGRGeometry *poSrcRegion;
67 :
68 : // Geometry interpretation related.
69 : OGRVRTGeometryStyle eGeometryStyle;
70 :
71 : // Points to a OGRField for VGS_WKT, VGS_WKB, VGS_Shape and OGRGeomField
72 : // for VGS_Direct.
73 : int iGeomField;
74 :
75 : // VGS_PointFromColumn
76 : int iGeomXField;
77 : int iGeomYField;
78 : int iGeomZField;
79 : int iGeomMField;
80 : bool bReportSrcColumn;
81 : bool bUseSpatialSubquery;
82 : bool bNullable;
83 :
84 : OGREnvelope sStaticEnvelope;
85 :
86 : OGRGeomCoordinatePrecision sCoordinatePrecision{};
87 :
88 : OGRVRTGeomFieldProps();
89 : ~OGRVRTGeomFieldProps();
90 : };
91 :
92 : /************************************************************************/
93 : /* OGRVRTLayer */
94 : /************************************************************************/
95 :
96 : class OGRVRTDataSource;
97 :
98 : class OGRVRTLayer final : public OGRLayer
99 : {
100 : protected:
101 : OGRVRTDataSource *poDS;
102 : std::vector<OGRVRTGeomFieldProps *> apoGeomFieldProps;
103 :
104 : bool bHasFullInitialized;
105 : CPLString osName;
106 : CPLXMLNode *psLTree;
107 : CPLString osVRTDirectory;
108 :
109 : OGRFeatureDefn *poFeatureDefn;
110 :
111 : GDALDataset *poSrcDS;
112 : OGRLayer *poSrcLayer;
113 : OGRFeatureDefn *poSrcFeatureDefn;
114 : bool bNeedReset;
115 : bool bSrcLayerFromSQL;
116 : bool bSrcDSShared;
117 : bool bAttrFilterPassThrough;
118 :
119 : char *pszAttrFilter;
120 :
121 : int iFIDField; // -1 means pass through.
122 : CPLString osFIDFieldName;
123 : int iStyleField; // -1 means pass through.
124 :
125 : // Attribute mapping.
126 : std::vector<int> anSrcField;
127 : std::vector<int> abDirectCopy;
128 :
129 : bool bUpdate;
130 :
131 : OGRFeature *TranslateFeature(OGRFeature *&, int bUseSrcRegion);
132 : OGRFeature *TranslateVRTFeatureToSrcFeature(OGRFeature *poVRTFeature);
133 :
134 : bool ResetSourceReading();
135 :
136 : bool FullInitialize();
137 :
138 : OGRFeatureDefn *GetSrcLayerDefn();
139 : void ClipAndAssignSRS(OGRFeature *poFeature);
140 :
141 : GIntBig nFeatureCount;
142 :
143 : bool bError;
144 :
145 : bool ParseGeometryField(CPLXMLNode *psNode, CPLXMLNode *psNodeParent,
146 : OGRVRTGeomFieldProps *poProps);
147 :
148 : public:
149 : explicit OGRVRTLayer(OGRVRTDataSource *poDSIn);
150 : virtual ~OGRVRTLayer();
151 :
152 : bool FastInitialize(CPLXMLNode *psLTree, const char *pszVRTDirectory,
153 : int bUpdate);
154 :
155 647 : virtual const char *GetName() override
156 : {
157 647 : return osName.c_str();
158 : }
159 :
160 : virtual OGRwkbGeometryType GetGeomType() override;
161 :
162 : /* -------------------------------------------------------------------- */
163 : /* Caution : all the below methods should care of calling */
164 : /* FullInitialize() if not already done */
165 : /* -------------------------------------------------------------------- */
166 :
167 : virtual void ResetReading() override;
168 : virtual OGRFeature *GetNextFeature() override;
169 :
170 : virtual OGRFeature *GetFeature(GIntBig nFeatureId) override;
171 :
172 : virtual OGRErr SetNextByIndex(GIntBig nIndex) override;
173 :
174 : virtual OGRFeatureDefn *GetLayerDefn() override;
175 :
176 : virtual GIntBig GetFeatureCount(int) override;
177 :
178 : virtual OGRErr SetAttributeFilter(const char *) override;
179 :
180 : virtual int TestCapability(const char *) override;
181 :
182 : virtual OGRErr GetExtent(OGREnvelope *psExtent, int bForce = TRUE) override;
183 : virtual OGRErr GetExtent(int iGeomField, OGREnvelope *psExtent,
184 : int bForce = TRUE) override;
185 :
186 : virtual void SetSpatialFilter(OGRGeometry *poGeomIn) override;
187 : virtual void SetSpatialFilter(int iGeomField,
188 : OGRGeometry *poGeomIn) override;
189 :
190 : virtual OGRErr ICreateFeature(OGRFeature *poFeature) override;
191 :
192 : virtual OGRErr ISetFeature(OGRFeature *poFeature) override;
193 :
194 : virtual OGRErr DeleteFeature(GIntBig nFID) override;
195 :
196 : virtual OGRErr SyncToDisk() override;
197 :
198 : virtual const char *GetFIDColumn() override;
199 :
200 : virtual OGRErr StartTransaction() override;
201 : virtual OGRErr CommitTransaction() override;
202 : virtual OGRErr RollbackTransaction() override;
203 :
204 : virtual OGRErr SetIgnoredFields(CSLConstList papszFields) override;
205 :
206 : GDALDataset *GetSrcDataset();
207 : };
208 :
209 : /************************************************************************/
210 : /* OGRVRTDataSource */
211 : /************************************************************************/
212 :
213 : typedef enum
214 : {
215 : OGR_VRT_PROXIED_LAYER,
216 : OGR_VRT_LAYER,
217 : OGR_VRT_OTHER_LAYER,
218 : } OGRLayerType;
219 :
220 : class OGRVRTDataSource final : public OGRDataSource
221 : {
222 : OGRLayer **papoLayers;
223 : OGRLayerType *paeLayerType;
224 : int nLayers;
225 :
226 : char *pszName;
227 :
228 : CPLXMLNode *psTree;
229 :
230 : int nCallLevel;
231 :
232 : std::set<std::string> aosOtherDSNameSet;
233 :
234 : OGRLayer *InstantiateWarpedLayer(CPLXMLNode *psLTree,
235 : const char *pszVRTDirectory, int bUpdate,
236 : int nRecLevel);
237 : OGRLayer *InstantiateUnionLayer(CPLXMLNode *psLTree,
238 : const char *pszVRTDirectory, int bUpdate,
239 : int nRecLevel);
240 :
241 : OGRLayerPool *poLayerPool;
242 :
243 : OGRVRTDataSource *poParentDS;
244 : bool bRecursionDetected;
245 :
246 : public:
247 : explicit OGRVRTDataSource(GDALDriver *poDriver);
248 : virtual ~OGRVRTDataSource();
249 :
250 : virtual int CloseDependentDatasets() override;
251 :
252 : OGRLayer *InstantiateLayer(CPLXMLNode *psLTree, const char *pszVRTDirectory,
253 : int bUpdate, int nRecLevel = 0);
254 :
255 : OGRLayer *InstantiateLayerInternal(CPLXMLNode *psLTree,
256 : const char *pszVRTDirectory, int bUpdate,
257 : int nRecLevel);
258 :
259 : bool Initialize(CPLXMLNode *psXML, const char *pszName, int bUpdate);
260 :
261 19 : const char *GetName() override
262 : {
263 19 : return pszName;
264 : }
265 :
266 833 : int GetLayerCount() override
267 : {
268 833 : return nLayers;
269 : }
270 :
271 : OGRLayer *GetLayer(int) override;
272 :
273 : int TestCapability(const char *) override;
274 :
275 : virtual char **GetFileList() override;
276 :
277 : // Anti-recursion mechanism for standard Open.
278 66 : void SetCallLevel(int nCallLevelIn)
279 : {
280 66 : nCallLevel = nCallLevelIn;
281 66 : }
282 :
283 430 : int GetCallLevel()
284 : {
285 430 : return nCallLevel;
286 : }
287 :
288 66 : void SetParentDS(OGRVRTDataSource *poParentDSIn)
289 : {
290 66 : poParentDS = poParentDSIn;
291 66 : }
292 :
293 66 : OGRVRTDataSource *GetParentDS()
294 : {
295 66 : return poParentDS;
296 : }
297 :
298 70 : void SetRecursionDetected()
299 : {
300 70 : bRecursionDetected = true;
301 70 : }
302 :
303 33992 : bool GetRecursionDetected() const
304 : {
305 33992 : return bRecursionDetected;
306 : }
307 :
308 : // Anti-recursion mechanism for shared Open.
309 : void AddForbiddenNames(const char *pszOtherDSName);
310 : bool IsInForbiddenNames(const char *pszOtherDSName) const;
311 : };
312 :
313 : OGRwkbGeometryType OGRVRTGetGeometryType(const char *pszGType, int *pbError);
314 : CPLString CPL_DLL OGRVRTGetSerializedGeometryType(OGRwkbGeometryType eGeomType);
315 :
316 : #endif // ndef OGR_VRT_H_INCLUDED
|