1 /******************************************************************************
2  * $Id: ogr_vrt.h 28522 2015-02-18 14:16:40Z rouault $
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 mines-paris dot org>
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 "ogrsf_frmts.h"
35 #include "cpl_error.h"
36 #include "cpl_minixml.h"
37 #include "ogrlayerpool.h"
38 
39 #include <vector>
40 #include <string>
41 #include <set>
42 
43 typedef enum {
44     VGS_None,
45     VGS_Direct,
46     VGS_PointFromColumns,
47     VGS_WKT,
48     VGS_WKB,
49     VGS_Shape
50 } OGRVRTGeometryStyle;
51 
52 /************************************************************************/
53 /*                         OGRVRTGeomFieldProps                         */
54 /************************************************************************/
55 
56 class OGRVRTGeomFieldProps
57 {
58     public:
59         CPLString           osName;         /* Name of the VRT geometry field */
60         OGRwkbGeometryType  eGeomType;
61         OGRSpatialReference *poSRS;
62 
63         int                 bSrcClip;
64         OGRGeometry         *poSrcRegion;
65 
66         // Geometry interpretation related.
67         OGRVRTGeometryStyle eGeometryStyle;
68 
69         /* points to a OGRField for VGS_WKT, VGS_WKB, VGS_Shape and OGRGeomField for VGS_Direct */
70         int                 iGeomField;
71 
72                             // VGS_PointFromColumn
73         int                 iGeomXField, iGeomYField, iGeomZField;
74         int                 bReportSrcColumn;
75         int                 bUseSpatialSubquery;
76 
77         OGREnvelope         sStaticEnvelope;
78 
79         int                 bNullable;
80 
81                         OGRVRTGeomFieldProps();
82                        ~OGRVRTGeomFieldProps();
83 };
84 
85 /************************************************************************/
86 /*                            OGRVRTLayer                                */
87 /************************************************************************/
88 
89 class OGRVRTDataSource;
90 
91 class OGRVRTLayer : public OGRLayer
92 {
93   protected:
94     OGRVRTDataSource*   poDS;
95     std::vector<OGRVRTGeomFieldProps*> apoGeomFieldProps;
96 
97     int                 bHasFullInitialized;
98     CPLString           osName;
99     CPLXMLNode         *psLTree;
100     CPLString           osVRTDirectory;
101 
102     OGRFeatureDefn      *poFeatureDefn;
103 
104     GDALDataset         *poSrcDS;
105     OGRLayer            *poSrcLayer;
106     OGRFeatureDefn      *poSrcFeatureDefn;
107     int                 bNeedReset;
108     int                 bSrcLayerFromSQL;
109     int                 bSrcDSShared;
110     int                 bAttrFilterPassThrough;
111 
112     char                *pszAttrFilter;
113 
114     int                 iFIDField; // -1 means pass through.
115     CPLString           osFIDFieldName;
116     int                 iStyleField; // -1 means pass through.
117 
118     // Attribute Mapping
119     std::vector<int>    anSrcField;
120     std::vector<int>    abDirectCopy;
121 
122     int                 bUpdate;
123 
124     OGRFeature         *TranslateFeature( OGRFeature*& , int bUseSrcRegion );
125     OGRFeature         *TranslateVRTFeatureToSrcFeature( OGRFeature* poVRTFeature);
126 
127     int                 ResetSourceReading();
128 
129     int                 FullInitialize();
130 
131     OGRFeatureDefn     *GetSrcLayerDefn();
132     void                ClipAndAssignSRS(OGRFeature* poFeature);
133 
134     GIntBig             nFeatureCount;
135 
136     int                 bError;
137 
138     int                 ParseGeometryField(CPLXMLNode* psNode,
139                                            CPLXMLNode* psNodeParent,
140                                            OGRVRTGeomFieldProps* poProps);
141 
142   public:
143                         OGRVRTLayer(OGRVRTDataSource* poDSIn);
144     virtual             ~OGRVRTLayer();
145 
146     int                FastInitialize( CPLXMLNode *psLTree,
147                                     const char *pszVRTDirectory,
148                                     int bUpdate);
149 
GetName()150     virtual const char  *GetName() { return osName.c_str(); }
151     virtual OGRwkbGeometryType GetGeomType();
152 
153 /* -------------------------------------------------------------------- */
154 /*      Caution : all the below methods should care of calling          */
155 /*      FullInitialize() if not already done                            */
156 /* -------------------------------------------------------------------- */
157 
158     virtual void        ResetReading();
159     virtual OGRFeature *GetNextFeature();
160 
161     virtual OGRFeature *GetFeature( GIntBig nFeatureId );
162 
163     virtual OGRErr      SetNextByIndex( GIntBig nIndex );
164 
165     virtual OGRFeatureDefn *GetLayerDefn();
166 
167     virtual OGRSpatialReference *GetSpatialRef();
168 
169     virtual GIntBig     GetFeatureCount( int );
170 
171     virtual OGRErr      SetAttributeFilter( const char * );
172 
173     virtual int         TestCapability( const char * );
174 
175     virtual OGRErr      GetExtent(OGREnvelope *psExtent, int bForce = TRUE);
176     virtual OGRErr      GetExtent(int iGeomField, OGREnvelope *psExtent,
177                                   int bForce = TRUE);
178 
179     virtual void        SetSpatialFilter( OGRGeometry * poGeomIn );
180     virtual void        SetSpatialFilter( int iGeomField, OGRGeometry * poGeomIn );
181 
182     virtual OGRErr      ICreateFeature( OGRFeature* poFeature );
183 
184     virtual OGRErr      ISetFeature( OGRFeature* poFeature );
185 
186     virtual OGRErr      DeleteFeature( GIntBig nFID );
187 
188     virtual OGRErr      SyncToDisk();
189 
190     virtual const char *GetFIDColumn();
191 
192     virtual OGRErr      StartTransaction();
193     virtual OGRErr      CommitTransaction();
194     virtual OGRErr      RollbackTransaction();
195 
196     virtual OGRErr      SetIgnoredFields( const char **papszFields );
197 
198     GDALDataset*        GetSrcDataset();
199 };
200 
201 /************************************************************************/
202 /*                           OGRVRTDataSource                            */
203 /************************************************************************/
204 
205 typedef enum
206 {
207     OGR_VRT_PROXIED_LAYER,
208     OGR_VRT_LAYER,
209     OGR_VRT_OTHER_LAYER,
210 } OGRLayerType;
211 
212 class OGRVRTDataSource : public OGRDataSource
213 {
214     OGRLayer          **papoLayers;
215     OGRLayerType       *paeLayerType;
216     int                 nLayers;
217 
218     char               *pszName;
219 
220     CPLXMLNode         *psTree;
221 
222     int                 nCallLevel;
223 
224     std::set<std::string> aosOtherDSNameSet;
225 
226     OGRLayer*           InstanciateWarpedLayer(CPLXMLNode *psLTree,
227                                                const char *pszVRTDirectory,
228                                                int bUpdate,
229                                                int nRecLevel);
230     OGRLayer*           InstanciateUnionLayer(CPLXMLNode *psLTree,
231                                                const char *pszVRTDirectory,
232                                                int bUpdate,
233                                                int nRecLevel);
234 
235     OGRLayerPool*       poLayerPool;
236 
237     OGRVRTDataSource   *poParentDS;
238     int                 bRecursionDetected;
239 
240   public:
241                         OGRVRTDataSource(GDALDriver* poDriver);
242                         ~OGRVRTDataSource();
243 
244     OGRLayer*           InstanciateLayer(CPLXMLNode *psLTree,
245                                     const char *pszVRTDirectory,
246                                     int bUpdate,
247                                     int nRecLevel = 0);
248 
249     OGRLayer*           InstanciateLayerInternal(CPLXMLNode *psLTree,
250                                             const char *pszVRTDirectory,
251                                             int bUpdate,
252                                             int nRecLevel);
253 
254     int                 Initialize( CPLXMLNode *psXML, const char *pszName,
255                                     int bUpdate );
256 
GetName()257     const char          *GetName() { return pszName; }
GetLayerCount()258     int                 GetLayerCount() { return nLayers; }
259     OGRLayer            *GetLayer( int );
260 
261     int                 TestCapability( const char * );
262 
263     virtual char      **GetFileList();
264 
265     /* Anti-recursion mechanism for standard Open */
SetCallLevel(int nCallLevelIn)266     void                SetCallLevel(int nCallLevelIn) { nCallLevel = nCallLevelIn; }
GetCallLevel()267     int                 GetCallLevel() { return nCallLevel; }
268 
SetParentDS(OGRVRTDataSource * poParentDSIn)269     void                SetParentDS(OGRVRTDataSource* poParentDSIn) { poParentDS = poParentDSIn; }
GetParentDS()270     OGRVRTDataSource*   GetParentDS() { return poParentDS; }
271 
SetRecursionDetected()272     void                SetRecursionDetected() { bRecursionDetected = TRUE; }
GetRecursionDetected()273     int                 GetRecursionDetected() { return bRecursionDetected; }
274 
275     /* Anti-recursion mechanism for shared Open */
276     void                AddForbiddenNames(const char* pszOtherDSName);
277     int                 IsInForbiddenNames(const char* pszOtherDSName);
278 };
279 
280 OGRwkbGeometryType OGRVRTGetGeometryType(const char* pszGType, int* pbError);
281 
282 #endif /* ndef _OGR_VRT_H_INCLUDED */
283 
284 
285