Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: OpenGIS Simple Features Reference Implementation 4 : * Purpose: Private definitions for OGR/VRT driver. 5 : * Author: Frank Warmerdam, warmerdam@pobox.com 6 : * 7 : ****************************************************************************** 8 : * Copyright (c) 2003, Frank Warmerdam <warmerdam@pobox.com> 9 : * Copyright (c) 2009-2014, Even Rouault <even dot rouault at spatialys.com> 10 : * 11 : * SPDX-License-Identifier: MIT 12 : ****************************************************************************/ 13 : 14 : #ifndef OGR_VRT_H_INCLUDED 15 : #define OGR_VRT_H_INCLUDED 16 : 17 : #include "cpl_error.h" 18 : #include "cpl_minixml.h" 19 : #include "cpl_string.h" 20 : #include "ogrlayerpool.h" 21 : #include "ogrsf_frmts.h" 22 : 23 : #include <set> 24 : #include <string> 25 : #include <vector> 26 : 27 : typedef enum 28 : { 29 : VGS_None, 30 : VGS_Direct, 31 : VGS_PointFromColumns, 32 : VGS_WKT, 33 : VGS_WKB, 34 : VGS_Shape 35 : } OGRVRTGeometryStyle; 36 : 37 : /************************************************************************/ 38 : /* OGRVRTGeomFieldProps */ 39 : /************************************************************************/ 40 : 41 1602 : class OGRVRTGeomFieldProps 42 : { 43 : public: 44 : CPLString osName{}; // Name of the VRT geometry field */ 45 : OGRwkbGeometryType eGeomType = wkbUnknown; 46 : OGRSpatialReferenceRefCountedPtr poSRS{}; 47 : 48 : bool bSrcClip = false; 49 : std::unique_ptr<OGRGeometry> poSrcRegion{}; 50 : 51 : // Geometry interpretation related. 52 : OGRVRTGeometryStyle eGeometryStyle = VGS_Direct; 53 : 54 : // Points to a OGRField for VGS_WKT, VGS_WKB, VGS_Shape and OGRGeomField 55 : // for VGS_Direct. 56 : int iGeomField = -1; 57 : 58 : // VGS_PointFromColumn 59 : int iGeomXField = -1; 60 : int iGeomYField = -1; 61 : int iGeomZField = -1; 62 : int iGeomMField = -1; 63 : bool bReportSrcColumn = true; 64 : bool bUseSpatialSubquery = false; 65 : bool bNullable = true; 66 : 67 : OGREnvelope sStaticEnvelope{}; 68 : 69 : OGRGeomCoordinatePrecision sCoordinatePrecision{}; 70 : 71 : OGRVRTGeomFieldProps(); 72 : ~OGRVRTGeomFieldProps(); 73 : 74 : private: 75 : CPL_DISALLOW_COPY_ASSIGN(OGRVRTGeomFieldProps) 76 : }; 77 : 78 : /************************************************************************/ 79 : /* OGRVRTLayer */ 80 : /************************************************************************/ 81 : 82 : class OGRVRTDataSource; 83 : 84 : class OGRVRTLayer final : public OGRLayer 85 : { 86 : protected: 87 : OGRVRTDataSource *poDS = nullptr; 88 : std::vector<std::unique_ptr<OGRVRTGeomFieldProps>> apoGeomFieldProps{}; 89 : 90 : bool bHasFullInitialized = false; 91 : CPLString osName{}; 92 : CPLXMLNode *psLTree = nullptr; 93 : CPLString osVRTDirectory{}; 94 : 95 : OGRFeatureDefnRefCountedPtr poFeatureDefn{}; 96 : 97 : std::unique_ptr<GDALDataset, GDALDatasetUniquePtrReleaser> poSrcDS{}; 98 : OGRLayer *poSrcLayer = nullptr; 99 : OGRFeatureDefn *poSrcFeatureDefn = nullptr; 100 : bool bNeedReset = true; 101 : bool bSrcLayerFromSQL = false; 102 : bool bSrcDSShared = false; 103 : bool bAttrFilterPassThrough = false; 104 : 105 : char *pszAttrFilter = nullptr; 106 : 107 : int iFIDField = -1; // -1 means pass through. 108 : CPLString osFIDFieldName{}; 109 : int iStyleField = -1; // -1 means pass through. 110 : 111 : // Attribute mapping. 112 : std::vector<int> anSrcField{}; 113 : std::vector<int> abDirectCopy{}; 114 : 115 : bool bUpdate = false; 116 : 117 : GIntBig nFeatureCount = -1; 118 : 119 : bool bError = false; 120 : 121 : bool m_bEmptyResultSet = false; 122 : 123 : std::unique_ptr<OGRFeature> TranslateFeature(std::unique_ptr<OGRFeature> &, 124 : int bUseSrcRegion); 125 : std::unique_ptr<OGRFeature> 126 : TranslateVRTFeatureToSrcFeature(const OGRFeature *poVRTFeature); 127 : 128 : bool ResetSourceReading(); 129 : 130 : bool FullInitialize(); 131 : 132 : OGRFeatureDefn *GetSrcLayerDefn(); 133 : void ClipAndAssignSRS(OGRFeature *poFeature); 134 : 135 : bool ParseGeometryField(CPLXMLNode *psNode, CPLXMLNode *psNodeParent, 136 : OGRVRTGeomFieldProps *poProps); 137 : 138 : CPL_DISALLOW_COPY_ASSIGN(OGRVRTLayer) 139 : 140 : public: 141 : explicit OGRVRTLayer(OGRVRTDataSource *poDSIn); 142 : ~OGRVRTLayer() override; 143 : 144 : bool FastInitialize(CPLXMLNode *psLTree, const char *pszVRTDirectory, 145 : int bUpdate); 146 : 147 649 : const char *GetName() const override 148 : { 149 649 : return osName.c_str(); 150 : } 151 : 152 : OGRwkbGeometryType GetGeomType() const override; 153 : 154 : /* -------------------------------------------------------------------- */ 155 : /* Caution : all the below methods should care of calling */ 156 : /* FullInitialize() if not already done */ 157 : /* -------------------------------------------------------------------- */ 158 : 159 : void ResetReading() override; 160 : OGRFeature *GetNextFeature() override; 161 : 162 : OGRFeature *GetFeature(GIntBig nFeatureId) override; 163 : 164 : OGRErr SetNextByIndex(GIntBig nIndex) override; 165 : 166 : const OGRFeatureDefn *GetLayerDefn() const override; 167 : 168 : GIntBig GetFeatureCount(int) override; 169 : 170 : OGRErr SetAttributeFilter(const char *) override; 171 : 172 : int TestCapability(const char *) const override; 173 : 174 : OGRErr IGetExtent(int iGeomField, OGREnvelope *psExtent, 175 : bool bForce = TRUE) override; 176 : 177 : virtual OGRErr ISetSpatialFilter(int iGeomField, 178 : const OGRGeometry *poGeomIn) override; 179 : 180 : OGRErr ICreateFeature(OGRFeature *poFeature) override; 181 : 182 : OGRErr ISetFeature(OGRFeature *poFeature) override; 183 : 184 : OGRErr DeleteFeature(GIntBig nFID) override; 185 : 186 : OGRErr SyncToDisk() override; 187 : 188 : const char *GetFIDColumn() const override; 189 : 190 : OGRErr StartTransaction() override; 191 : OGRErr CommitTransaction() override; 192 : OGRErr RollbackTransaction() override; 193 : 194 : OGRErr SetIgnoredFields(CSLConstList papszFields) override; 195 : 196 : GDALDataset *GetSrcDataset(); 197 : }; 198 : 199 : /************************************************************************/ 200 : /* OGRVRTDataSource */ 201 : /************************************************************************/ 202 : 203 : typedef enum 204 : { 205 : OGR_VRT_PROXIED_LAYER, 206 : OGR_VRT_LAYER, 207 : OGR_VRT_OTHER_LAYER, 208 : } OGRLayerType; 209 : 210 : class OGRVRTDataSource final : public GDALDataset 211 : { 212 : std::unique_ptr<OGRLayerPool> poLayerPool{}; 213 : 214 : OGRLayer **papoLayers = nullptr; 215 : OGRLayerType *paeLayerType = nullptr; 216 : int nLayers = 0; 217 : 218 : CPLXMLNode *psTree = nullptr; 219 : 220 : int nCallLevel = 0; 221 : 222 : std::set<std::string> aosOtherDSNameSet{}; 223 : 224 : OGRVRTDataSource *poParentDS = nullptr; 225 : bool bRecursionDetected = false; 226 : 227 : OGRLayer *InstantiateWarpedLayer(CPLXMLNode *psLTree, 228 : const char *pszVRTDirectory, int bUpdate, 229 : int nRecLevel); 230 : OGRLayer *InstantiateUnionLayer(CPLXMLNode *psLTree, 231 : const char *pszVRTDirectory, int bUpdate, 232 : int nRecLevel); 233 : 234 : CPL_DISALLOW_COPY_ASSIGN(OGRVRTDataSource) 235 : 236 : public: 237 : explicit OGRVRTDataSource(GDALDriver *poDriver); 238 : ~OGRVRTDataSource() override; 239 : 240 : int CloseDependentDatasets() override; 241 : 242 : OGRLayer *InstantiateLayer(CPLXMLNode *psLTree, const char *pszVRTDirectory, 243 : int bUpdate, int nRecLevel = 0); 244 : 245 : OGRLayer *InstantiateLayerInternal(CPLXMLNode *psLTree, 246 : const char *pszVRTDirectory, int bUpdate, 247 : int nRecLevel); 248 : 249 : bool Initialize(CPLXMLNode *psXML, const char *pszName, int bUpdate); 250 : 251 1270 : int GetLayerCount() const override 252 : { 253 1270 : return nLayers; 254 : } 255 : 256 : const OGRLayer *GetLayer(int) const override; 257 : 258 : int TestCapability(const char *) const override; 259 : 260 : char **GetFileList() override; 261 : 262 : // Anti-recursion mechanism for standard Open. 263 66 : void SetCallLevel(int nCallLevelIn) 264 : { 265 66 : nCallLevel = nCallLevelIn; 266 66 : } 267 : 268 441 : int GetCallLevel() const 269 : { 270 441 : return nCallLevel; 271 : } 272 : 273 66 : void SetParentDS(OGRVRTDataSource *poParentDSIn) 274 : { 275 66 : poParentDS = poParentDSIn; 276 66 : } 277 : 278 66 : OGRVRTDataSource *GetParentDS() 279 : { 280 66 : return poParentDS; 281 : } 282 : 283 70 : void SetRecursionDetected() 284 : { 285 70 : bRecursionDetected = true; 286 70 : } 287 : 288 32000 : bool GetRecursionDetected() const 289 : { 290 32000 : return bRecursionDetected; 291 : } 292 : 293 : // Anti-recursion mechanism for shared Open. 294 : void AddForbiddenNames(const char *pszOtherDSName); 295 : bool IsInForbiddenNames(const char *pszOtherDSName) const; 296 : }; 297 : 298 : #endif // ndef OGR_VRT_H_INCLUDED