Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: GML Reader 4 : * Purpose: Declarations for OGR wrapper classes for GML, and GML<->OGR 5 : * translation of geometry. 6 : * Author: Frank Warmerdam, warmerdam@pobox.com 7 : * 8 : ****************************************************************************** 9 : * Copyright (c) 2002, Frank Warmerdam 10 : * Copyright (c) 2010-2013, Even Rouault <even dot rouault at spatialys.com> 11 : * 12 : * SPDX-License-Identifier: MIT 13 : ****************************************************************************/ 14 : 15 : #ifndef OGR_GML_H_INCLUDED 16 : #define OGR_GML_H_INCLUDED 17 : 18 : #include "ogrsf_frmts.h" 19 : #include "gmlreader.h" 20 : #include "gmlutils.h" 21 : 22 : #include <memory> 23 : #include <set> 24 : #include <vector> 25 : 26 : class OGRGMLDataSource; 27 : 28 : typedef enum 29 : { 30 : STANDARD, 31 : SEQUENTIAL_LAYERS, 32 : INTERLEAVED_LAYERS 33 : } ReadMode; 34 : 35 : /************************************************************************/ 36 : /* OGRGMLLayer */ 37 : /************************************************************************/ 38 : 39 : class OGRGMLLayer final : public OGRLayer 40 : { 41 : OGRFeatureDefn *poFeatureDefn; 42 : 43 : GIntBig m_iNextGMLId = 0; 44 : bool m_bInvalidFIDFound = false; 45 : char *m_pszFIDPrefix = nullptr; 46 : std::set<GIntBig> m_oSetFIDs{}; 47 : 48 : bool bWriter; 49 : 50 : OGRGMLDataSource *poDS; 51 : 52 : GMLFeatureClass *poFClass; 53 : 54 : std::unique_ptr<OGRGML_SRSCache, decltype(&OGRGML_SRSCache_Destroy)> 55 715 : m_srsCache{OGRGML_SRSCache_Create(), OGRGML_SRSCache_Destroy}; 56 : 57 : bool bUseOldFIDFormat; 58 : 59 : bool bFaceHoleNegative; 60 : 61 : CPL_DISALLOW_COPY_ASSIGN(OGRGMLLayer) 62 : 63 : public: 64 : OGRGMLLayer(const char *pszName, bool bWriter, OGRGMLDataSource *poDS); 65 : 66 : ~OGRGMLLayer() override; 67 : 68 : GDALDataset *GetDataset() override; 69 : 70 : void ResetReading() override; 71 : OGRFeature *GetNextFeature() override; 72 : 73 : GIntBig GetFeatureCount(int bForce = TRUE) override; 74 : OGRErr IGetExtent(int iGeomField, OGREnvelope *psExtent, 75 : bool bForce) override; 76 : 77 : OGRErr ICreateFeature(OGRFeature *poFeature) override; 78 : 79 7222 : const OGRFeatureDefn *GetLayerDefn() const override 80 : { 81 7222 : return poFeatureDefn; 82 : } 83 : 84 : virtual OGRErr CreateField(const OGRFieldDefn *poField, 85 : int bApproxOK = TRUE) override; 86 : virtual OGRErr CreateGeomField(const OGRGeomFieldDefn *poField, 87 : int bApproxOK = TRUE) override; 88 : 89 : int TestCapability(const char *) const override; 90 : }; 91 : 92 : /************************************************************************/ 93 : /* OGRGMLDataSource */ 94 : /************************************************************************/ 95 : 96 : class OGRGMLDataSource final : public GDALDataset 97 : { 98 : OGRLayer **papoLayers; 99 : int nLayers; 100 : 101 : OGRLayer *TranslateGMLSchema(GMLFeatureClass *); 102 : 103 : char **papszCreateOptions; 104 : 105 : // output related parameters 106 : VSILFILE *fpOutput; 107 : bool bFpOutputIsNonSeekable; 108 : bool bFpOutputSingleFile; 109 : bool m_bWriteError = false; 110 : OGREnvelope3D sBoundingRect{}; 111 : bool bBBOX3D; 112 : int nBoundedByLocation; 113 : 114 : int nSchemaInsertLocation; 115 : bool bIsOutputGML3; 116 : bool bIsOutputGML3Deegree; /* if TRUE, then bIsOutputGML3 is also TRUE */ 117 : bool bIsOutputGML32; /* if TRUE, then bIsOutputGML3 is also TRUE */ 118 : OGRGMLSRSNameFormat eSRSNameFormat; 119 : bool bWriteSpaceIndentation; 120 : 121 : //! Whether all geometry fields of all layers have the same SRS (or no SRS at all) 122 : bool m_bWriteGlobalSRS = true; 123 : 124 : //! The global SRS (may be null), that is valid only if m_bWriteGlobalSRS == true 125 : std::unique_ptr<OGRSpatialReference> m_poWriteGlobalSRS{}; 126 : 127 : //! Whether at least one geometry field has been created 128 : bool m_bWriteGlobalSRSInit = false; 129 : 130 : // input related parameters. 131 : CPLString osFilename{}; 132 : CPLString osXSDFilename{}; 133 : bool m_bUnlinkXSDFilename = false; 134 : 135 : IGMLReader *poReader; 136 : bool bOutIsTempFile; 137 : 138 : void InsertHeader(); 139 : 140 : bool bExposeGMLId; 141 : bool bExposeFid; 142 : bool bIsWFS; 143 : 144 : bool bUseGlobalSRSName; 145 : 146 : bool m_bInvertAxisOrderIfLatLong; 147 : bool m_bConsiderEPSGAsURN; 148 : GMLSwapCoordinatesEnum m_eSwapCoordinates; 149 : bool m_bGetSecondaryGeometryOption; 150 : 151 : ReadMode eReadMode; 152 : GMLFeature *poStoredGMLFeature; 153 : OGRGMLLayer *poLastReadLayer; 154 : 155 : bool bEmptyAsNull; 156 : 157 : OGRSpatialReference m_oStandaloneGeomSRS{}; 158 : std::unique_ptr<OGRGeometry> m_poStandaloneGeom{}; 159 : 160 : std::vector<std::string> m_aosGMLExtraElements{}; 161 : 162 : void FindAndParseTopElements(VSILFILE *fp); 163 : void SetExtents(double dfMinX, double dfMinY, double dfMaxX, double dfMaxY); 164 : 165 : void BuildJointClassFromXSD(); 166 : void BuildJointClassFromScannedSchema(); 167 : 168 : void WriteTopElements(); 169 : 170 : // Analyze the OGR_SCHEMA open options and apply changes to the GML reader, return false in case of a critical error 171 : bool DealWithOgrSchemaOpenOption(const GDALOpenInfo *poOpenInfo); 172 : 173 : CPL_DISALLOW_COPY_ASSIGN(OGRGMLDataSource) 174 : 175 : public: 176 : OGRGMLDataSource(); 177 : ~OGRGMLDataSource() override; 178 : 179 : bool Open(GDALOpenInfo *poOpenInfo); 180 : CPLErr Close() override; 181 : bool Create(const char *pszFile, char **papszOptions); 182 : 183 1505 : int GetLayerCount() const override 184 : { 185 1505 : return nLayers; 186 : } 187 : 188 : using GDALDataset::GetLayer; 189 : const OGRLayer *GetLayer(int) const override; 190 : OGRLayer *ICreateLayer(const char *pszName, 191 : const OGRGeomFieldDefn *poGeomFieldDefn, 192 : CSLConstList papszOptions) override; 193 : int TestCapability(const char *) const override; 194 : 195 266 : VSILFILE *GetOutputFP() const 196 : { 197 266 : return fpOutput; 198 : } 199 : 200 3173 : IGMLReader *GetReader() const 201 : { 202 3173 : return poReader; 203 : } 204 : 205 : void GrowExtents(OGREnvelope3D *psGeomBounds, int nCoordDimension); 206 : 207 745 : int ExposeId() const 208 : { 209 745 : return bExposeGMLId || bExposeFid; 210 : } 211 : 212 : void PrintLine(VSILFILE *fp, const char *fmt, ...) 213 : CPL_PRINT_FUNC_FORMAT(3, 4); 214 : 215 1778 : bool IsGML3Output() const 216 : { 217 1778 : return bIsOutputGML3; 218 : } 219 : 220 138 : bool IsGML3DeegreeOutput() const 221 : { 222 138 : return bIsOutputGML3Deegree; 223 : } 224 : 225 890 : bool IsGML32Output() const 226 : { 227 890 : return bIsOutputGML32; 228 : } 229 : 230 : /** Returns whether a writing error has occurred */ 231 530 : inline bool HasWriteError() const 232 : { 233 530 : return m_bWriteError; 234 : } 235 : 236 575 : OGRGMLSRSNameFormat GetSRSNameFormat() const 237 : { 238 575 : return eSRSNameFormat; 239 : } 240 : 241 266 : bool WriteSpaceIndentation() const 242 : { 243 266 : return bWriteSpaceIndentation; 244 : } 245 : 246 : const char *GetGlobalSRSName(); 247 : 248 718 : bool GetInvertAxisOrderIfLatLong() const 249 : { 250 718 : return m_bInvertAxisOrderIfLatLong; 251 : } 252 : 253 718 : bool GetConsiderEPSGAsURN() const 254 : { 255 718 : return m_bConsiderEPSGAsURN; 256 : } 257 : 258 718 : GMLSwapCoordinatesEnum GetSwapCoordinates() const 259 : { 260 718 : return m_eSwapCoordinates; 261 : } 262 : 263 718 : bool GetSecondaryGeometryOption() const 264 : { 265 718 : return m_bGetSecondaryGeometryOption; 266 : } 267 : 268 3780 : ReadMode GetReadMode() const 269 : { 270 3780 : return eReadMode; 271 : } 272 : 273 107 : void SetStoredGMLFeature(GMLFeature *poStoredGMLFeatureIn) 274 : { 275 107 : poStoredGMLFeature = poStoredGMLFeatureIn; 276 107 : } 277 : 278 1941 : GMLFeature *PeekStoredGMLFeature() const 279 : { 280 1941 : return poStoredGMLFeature; 281 : } 282 : 283 863 : OGRGMLLayer *GetLastReadLayer() const 284 : { 285 863 : return poLastReadLayer; 286 : } 287 : 288 405 : void SetLastReadLayer(OGRGMLLayer *poLayer) 289 : { 290 405 : poLastReadLayer = poLayer; 291 405 : } 292 : 293 : const char *GetAppPrefix() const; 294 : bool RemoveAppPrefix() const; 295 : bool WriteFeatureBoundedBy() const; 296 : const char *GetSRSDimensionLoc() const; 297 : bool GMLFeatureCollection() const; 298 : 299 : void DeclareNewWriteSRS(const OGRSpatialReference *poSRS); 300 : 301 215 : bool HasWriteGlobalSRS() const 302 : { 303 215 : return m_bWriteGlobalSRS; 304 : } 305 : 306 : OGRLayer *ExecuteSQL(const char *pszSQLCommand, 307 : OGRGeometry *poSpatialFilter, 308 : const char *pszDialect) override; 309 : void ReleaseResultSet(OGRLayer *poResultsSet) override; 310 : 311 : static bool CheckHeader(const char *pszStr); 312 : }; 313 : 314 : #endif /* OGR_GML_H_INCLUDED */