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 : virtual ~OGRGMLLayer(); 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 7234 : OGRFeatureDefn *GetLayerDefn() override 77 : { 78 7234 : 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 *) override; 87 : }; 88 : 89 : /************************************************************************/ 90 : /* OGRGMLDataSource */ 91 : /************************************************************************/ 92 : 93 : class OGRGMLDataSource final : public GDALDataset 94 : { 95 : OGRLayer **papoLayers; 96 : int nLayers; 97 : 98 : OGRGMLLayer *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 : virtual ~OGRGMLDataSource(); 175 : 176 : bool Open(GDALOpenInfo *poOpenInfo); 177 : CPLErr Close() override; 178 : bool Create(const char *pszFile, char **papszOptions); 179 : 180 1447 : int GetLayerCount() override 181 : { 182 1447 : return nLayers; 183 : } 184 : 185 : OGRLayer *GetLayer(int) override; 186 : OGRLayer *ICreateLayer(const char *pszName, 187 : const OGRGeomFieldDefn *poGeomFieldDefn, 188 : CSLConstList papszOptions) override; 189 : int TestCapability(const char *) override; 190 : 191 266 : VSILFILE *GetOutputFP() const 192 : { 193 266 : return fpOutput; 194 : } 195 : 196 2843 : IGMLReader *GetReader() const 197 : { 198 2843 : return poReader; 199 : } 200 : 201 : void GrowExtents(OGREnvelope3D *psGeomBounds, int nCoordDimension); 202 : 203 705 : int ExposeId() const 204 : { 205 705 : return bExposeGMLId || bExposeFid; 206 : } 207 : 208 : void PrintLine(VSILFILE *fp, const char *fmt, ...) 209 : CPL_PRINT_FUNC_FORMAT(3, 4); 210 : 211 1778 : bool IsGML3Output() const 212 : { 213 1778 : return bIsOutputGML3; 214 : } 215 : 216 138 : bool IsGML3DeegreeOutput() const 217 : { 218 138 : return bIsOutputGML3Deegree; 219 : } 220 : 221 890 : bool IsGML32Output() const 222 : { 223 890 : return bIsOutputGML32; 224 : } 225 : 226 : /** Returns whether a writing error has occurred */ 227 530 : inline bool HasWriteError() const 228 : { 229 530 : return m_bWriteError; 230 : } 231 : 232 575 : OGRGMLSRSNameFormat GetSRSNameFormat() const 233 : { 234 575 : return eSRSNameFormat; 235 : } 236 : 237 266 : bool WriteSpaceIndentation() const 238 : { 239 266 : return bWriteSpaceIndentation; 240 : } 241 : 242 : const char *GetGlobalSRSName(); 243 : 244 692 : bool GetInvertAxisOrderIfLatLong() const 245 : { 246 692 : return m_bInvertAxisOrderIfLatLong; 247 : } 248 : 249 692 : bool GetConsiderEPSGAsURN() const 250 : { 251 692 : return m_bConsiderEPSGAsURN; 252 : } 253 : 254 692 : GMLSwapCoordinatesEnum GetSwapCoordinates() const 255 : { 256 692 : return m_eSwapCoordinates; 257 : } 258 : 259 692 : bool GetSecondaryGeometryOption() const 260 : { 261 692 : return m_bGetSecondaryGeometryOption; 262 : } 263 : 264 3260 : ReadMode GetReadMode() const 265 : { 266 3260 : return eReadMode; 267 : } 268 : 269 101 : void SetStoredGMLFeature(GMLFeature *poStoredGMLFeatureIn) 270 : { 271 101 : poStoredGMLFeature = poStoredGMLFeatureIn; 272 101 : } 273 : 274 1662 : GMLFeature *PeekStoredGMLFeature() const 275 : { 276 1662 : return poStoredGMLFeature; 277 : } 278 : 279 812 : OGRGMLLayer *GetLastReadLayer() const 280 : { 281 812 : return poLastReadLayer; 282 : } 283 : 284 381 : void SetLastReadLayer(OGRGMLLayer *poLayer) 285 : { 286 381 : poLastReadLayer = poLayer; 287 381 : } 288 : 289 : const char *GetAppPrefix() const; 290 : bool RemoveAppPrefix() const; 291 : bool WriteFeatureBoundedBy() const; 292 : const char *GetSRSDimensionLoc() const; 293 : bool GMLFeatureCollection() const; 294 : 295 : void DeclareNewWriteSRS(const OGRSpatialReference *poSRS); 296 : 297 215 : bool HasWriteGlobalSRS() const 298 : { 299 215 : return m_bWriteGlobalSRS; 300 : } 301 : 302 : virtual OGRLayer *ExecuteSQL(const char *pszSQLCommand, 303 : OGRGeometry *poSpatialFilter, 304 : const char *pszDialect) override; 305 : virtual void ReleaseResultSet(OGRLayer *poResultsSet) override; 306 : 307 : static bool CheckHeader(const char *pszStr); 308 : }; 309 : 310 : #endif /* OGR_GML_H_INCLUDED */