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