Line data Source code
1 : /******************************************************************************
2 : *
3 : * Name: gdal_dataset.h
4 : * Project: GDAL Core
5 : * Purpose: Declaration of GDALDataset class
6 : * Author: Frank Warmerdam, warmerdam@pobox.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 1998, Frank Warmerdam
10 : * Copyright (c) 2007-2014, Even Rouault <even dot rouault at spatialys.com>
11 : *
12 : * SPDX-License-Identifier: MIT
13 : ****************************************************************************/
14 :
15 : #ifndef GDALDATASET_H_INCLUDED
16 : #define GDALDATASET_H_INCLUDED
17 :
18 : #include "cpl_port.h"
19 : #include "gdal_fwd.h"
20 : #include "gdal.h"
21 : #include "gdal_geotransform.h"
22 : #include "gdal_majorobject.h"
23 : #include "gdal_defaultoverviews.h"
24 : #include "ogr_core.h" // for OGRErr
25 : #include "ogr_feature.h" // for OGRFeatureUniquePtr
26 :
27 : #include <cstddef>
28 : #include <iterator>
29 : #include <memory>
30 : #include <vector>
31 :
32 : /* ******************************************************************** */
33 : /* GDALDataset */
34 : /* ******************************************************************** */
35 :
36 : class OGRGeometry;
37 : class OGRLayer;
38 : class OGRSpatialReference;
39 : class OGRStyleTable;
40 : class swq_select;
41 : class swq_select_parse_options;
42 : class GDALAsyncReader;
43 : class GDALDriver;
44 : class GDALGroup;
45 : class GDALMDArray;
46 : class GDALRasterBand;
47 : class GDALRelationship;
48 :
49 : //! @cond Doxygen_Suppress
50 : typedef struct GDALSQLParseInfo GDALSQLParseInfo;
51 : //! @endcond
52 :
53 : //! @cond Doxygen_Suppress
54 : #if !defined(OPTIONAL_OUTSIDE_GDAL)
55 : #if defined(GDAL_COMPILATION)
56 : #define OPTIONAL_OUTSIDE_GDAL(val)
57 : #else
58 : #define OPTIONAL_OUTSIDE_GDAL(val) = val
59 : #endif
60 : #endif
61 : //! @endcond
62 :
63 : //! @cond Doxygen_Suppress
64 : // This macro can be defined to check that GDALDataset::IRasterIO()
65 : // implementations do not alter the passed panBandList. It is not defined
66 : // by default (and should not!), hence int* is used.
67 : #if defined(GDAL_BANDMAP_TYPE_CONST_SAFE)
68 : #define BANDMAP_TYPE const int *
69 : #else
70 : #define BANDMAP_TYPE int *
71 : #endif
72 : //! @endcond
73 :
74 : /** A set of associated raster bands, usually from one file. */
75 : class CPL_DLL GDALDataset : public GDALMajorObject
76 : {
77 : friend GDALDatasetH CPL_STDCALL
78 : GDALOpenEx(const char *pszFilename, unsigned int nOpenFlags,
79 : const char *const *papszAllowedDrivers,
80 : const char *const *papszOpenOptions,
81 : const char *const *papszSiblingFiles);
82 : friend CPLErr CPL_STDCALL GDALClose(GDALDatasetH hDS);
83 :
84 : friend class GDALDriver;
85 : friend class GDALDefaultOverviews;
86 : friend class GDALProxyDataset;
87 : friend class GDALDriverManager;
88 :
89 : CPL_INTERNAL void AddToDatasetOpenList();
90 :
91 : CPL_INTERNAL void UnregisterFromSharedDataset();
92 :
93 : CPL_INTERNAL static void ReportErrorV(const char *pszDSName,
94 : CPLErr eErrClass, CPLErrorNum err_no,
95 : const char *fmt, va_list args);
96 :
97 : protected:
98 : //! @cond Doxygen_Suppress
99 : GDALDriver *poDriver = nullptr;
100 : GDALAccess eAccess = GA_ReadOnly;
101 :
102 : // Stored raster information.
103 : int nRasterXSize = 512;
104 : int nRasterYSize = 512;
105 : int nBands = 0;
106 : GDALRasterBand **papoBands = nullptr;
107 :
108 : static constexpr int OPEN_FLAGS_CLOSED = -1;
109 : int nOpenFlags =
110 : 0; // set to OPEN_FLAGS_CLOSED after Close() has been called
111 :
112 : int nRefCount = 1;
113 : bool bForceCachedIO = false;
114 : bool bShared = false;
115 : bool bIsInternal = true;
116 : bool bSuppressOnClose = false;
117 :
118 : mutable std::map<std::string, std::unique_ptr<OGRFieldDomain>>
119 : m_oMapFieldDomains{};
120 :
121 : GDALDataset(void);
122 : explicit GDALDataset(int bForceCachedIO);
123 :
124 : void RasterInitialize(int, int);
125 : void SetBand(int nNewBand, GDALRasterBand *poBand);
126 : void SetBand(int nNewBand, std::unique_ptr<GDALRasterBand> poBand);
127 :
128 : GDALDefaultOverviews oOvManager{};
129 :
130 : virtual CPLErr IBuildOverviews(const char *pszResampling, int nOverviews,
131 : const int *panOverviewList, int nListBands,
132 : const int *panBandList,
133 : GDALProgressFunc pfnProgress,
134 : void *pProgressData,
135 : CSLConstList papszOptions);
136 :
137 : virtual CPLErr
138 : IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize, int nYSize,
139 : void *pData, int nBufXSize, int nBufYSize, GDALDataType eBufType,
140 : int nBandCount, BANDMAP_TYPE panBandMap, GSpacing nPixelSpace,
141 : GSpacing nLineSpace, GSpacing nBandSpace,
142 : GDALRasterIOExtraArg *psExtraArg) CPL_WARN_UNUSED_RESULT;
143 :
144 : /* This method should only be be overloaded by GDALProxyDataset */
145 : virtual CPLErr
146 : BlockBasedRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize,
147 : int nYSize, void *pData, int nBufXSize, int nBufYSize,
148 : GDALDataType eBufType, int nBandCount,
149 : const int *panBandMap, GSpacing nPixelSpace,
150 : GSpacing nLineSpace, GSpacing nBandSpace,
151 : GDALRasterIOExtraArg *psExtraArg) CPL_WARN_UNUSED_RESULT;
152 : CPLErr BlockBasedFlushCache(bool bAtClosing);
153 :
154 : CPLErr
155 : BandBasedRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize,
156 : int nYSize, void *pData, int nBufXSize, int nBufYSize,
157 : GDALDataType eBufType, int nBandCount,
158 : const int *panBandMap, GSpacing nPixelSpace,
159 : GSpacing nLineSpace, GSpacing nBandSpace,
160 : GDALRasterIOExtraArg *psExtraArg) CPL_WARN_UNUSED_RESULT;
161 :
162 : CPLErr
163 : RasterIOResampled(GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize,
164 : int nYSize, void *pData, int nBufXSize, int nBufYSize,
165 : GDALDataType eBufType, int nBandCount,
166 : const int *panBandMap, GSpacing nPixelSpace,
167 : GSpacing nLineSpace, GSpacing nBandSpace,
168 : GDALRasterIOExtraArg *psExtraArg) CPL_WARN_UNUSED_RESULT;
169 :
170 : CPLErr ValidateRasterIOOrAdviseReadParameters(
171 : const char *pszCallingFunc, int *pbStopProcessingOnCENone, int nXOff,
172 : int nYOff, int nXSize, int nYSize, int nBufXSize, int nBufYSize,
173 : int nBandCount, const int *panBandMap);
174 :
175 : CPLErr TryOverviewRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff,
176 : int nXSize, int nYSize, void *pData,
177 : int nBufXSize, int nBufYSize,
178 : GDALDataType eBufType, int nBandCount,
179 : const int *panBandMap, GSpacing nPixelSpace,
180 : GSpacing nLineSpace, GSpacing nBandSpace,
181 : GDALRasterIOExtraArg *psExtraArg, int *pbTried);
182 :
183 : void ShareLockWithParentDataset(GDALDataset *poParentDataset);
184 :
185 : bool m_bCanBeReopened = false;
186 :
187 : virtual bool CanBeCloned(int nScopeFlags, bool bCanShareState) const;
188 :
189 : friend class GDALThreadSafeDataset;
190 : friend class MEMDataset;
191 : virtual std::unique_ptr<GDALDataset> Clone(int nScopeFlags,
192 : bool bCanShareState) const;
193 :
194 : //! @endcond
195 :
196 : void CleanupPostFileClosing();
197 :
198 : virtual int CloseDependentDatasets();
199 : //! @cond Doxygen_Suppress
200 : int ValidateLayerCreationOptions(const char *const *papszLCO);
201 :
202 : char **papszOpenOptions = nullptr;
203 :
204 : friend class GDALRasterBand;
205 :
206 : // The below methods related to read write mutex are fragile logic, and
207 : // should not be used by out-of-tree code if possible.
208 : int EnterReadWrite(GDALRWFlag eRWFlag);
209 : void LeaveReadWrite();
210 : void InitRWLock();
211 :
212 : void TemporarilyDropReadWriteLock();
213 : void ReacquireReadWriteLock();
214 :
215 : void DisableReadWriteMutex();
216 :
217 : int AcquireMutex();
218 : void ReleaseMutex();
219 :
220 : bool IsAllBands(int nBandCount, const int *panBandList) const;
221 : //! @endcond
222 :
223 : public:
224 : ~GDALDataset() override;
225 :
226 : virtual CPLErr Close();
227 :
228 : int GetRasterXSize() const;
229 : int GetRasterYSize() const;
230 : int GetRasterCount() const;
231 : GDALRasterBand *GetRasterBand(int);
232 : const GDALRasterBand *GetRasterBand(int) const;
233 :
234 : /**
235 : * @brief SetQueryLoggerFunc
236 : * @param pfnQueryLoggerFuncIn query logger function callback
237 : * @param poQueryLoggerArgIn arguments passed to the query logger function
238 : * @return true on success
239 : */
240 : virtual bool SetQueryLoggerFunc(GDALQueryLoggerFunc pfnQueryLoggerFuncIn,
241 : void *poQueryLoggerArgIn);
242 :
243 : /** Class returned by GetBands() that act as a container for raster bands.
244 : */
245 : class CPL_DLL Bands
246 : {
247 : private:
248 : friend class GDALDataset;
249 : GDALDataset *m_poSelf;
250 :
251 7 : CPL_INTERNAL explicit Bands(GDALDataset *poSelf) : m_poSelf(poSelf)
252 : {
253 7 : }
254 :
255 6 : class CPL_DLL Iterator
256 : {
257 : struct Private;
258 : std::unique_ptr<Private> m_poPrivate;
259 :
260 : public:
261 : Iterator(GDALDataset *poDS, bool bStart);
262 : Iterator(const Iterator &oOther); // declared but not defined.
263 : // Needed for gcc 5.4 at least
264 : Iterator(Iterator &&oOther) noexcept; // declared but not defined.
265 : // Needed for gcc 5.4 at least
266 : ~Iterator();
267 : GDALRasterBand *operator*();
268 : Iterator &operator++();
269 : bool operator!=(const Iterator &it) const;
270 : };
271 :
272 : public:
273 : const Iterator begin() const;
274 :
275 : const Iterator end() const;
276 :
277 : size_t size() const;
278 :
279 : GDALRasterBand *operator[](int iBand);
280 : GDALRasterBand *operator[](size_t iBand);
281 : };
282 :
283 : Bands GetBands();
284 :
285 : /** Class returned by GetBands() that act as a container for raster bands.
286 : */
287 : class CPL_DLL ConstBands
288 : {
289 : private:
290 : friend class GDALDataset;
291 : const GDALDataset *const m_poSelf;
292 :
293 4 : CPL_INTERNAL explicit ConstBands(const GDALDataset *poSelf)
294 4 : : m_poSelf(poSelf)
295 : {
296 4 : }
297 :
298 2 : class CPL_DLL Iterator
299 : {
300 : struct Private;
301 : std::unique_ptr<Private> m_poPrivate;
302 :
303 : public:
304 : Iterator(const GDALDataset *poDS, bool bStart);
305 : ~Iterator();
306 : const GDALRasterBand *operator*() const;
307 : Iterator &operator++();
308 : bool operator!=(const Iterator &it) const;
309 : };
310 :
311 : public:
312 : const Iterator begin() const;
313 :
314 : const Iterator end() const;
315 :
316 : size_t size() const;
317 :
318 : const GDALRasterBand *operator[](int iBand) const;
319 : const GDALRasterBand *operator[](size_t iBand) const;
320 : };
321 :
322 : ConstBands GetBands() const;
323 :
324 : virtual CPLErr FlushCache(bool bAtClosing = false);
325 : virtual CPLErr DropCache();
326 :
327 : virtual GIntBig GetEstimatedRAMUsage();
328 :
329 : virtual const OGRSpatialReference *GetSpatialRef() const;
330 : virtual CPLErr SetSpatialRef(const OGRSpatialReference *poSRS);
331 :
332 : virtual const OGRSpatialReference *GetSpatialRefRasterOnly() const;
333 : virtual const OGRSpatialReference *GetSpatialRefVectorOnly() const;
334 :
335 : // Compatibility layer
336 : const char *GetProjectionRef(void) const;
337 : CPLErr SetProjection(const char *pszProjection);
338 :
339 : virtual CPLErr GetGeoTransform(GDALGeoTransform >) const;
340 : virtual CPLErr SetGeoTransform(const GDALGeoTransform >);
341 :
342 : CPLErr GetGeoTransform(double *padfGeoTransform) const
343 : #if defined(GDAL_COMPILATION) && !defined(DOXYGEN_XML)
344 : CPL_WARN_DEPRECATED("Use GetGeoTransform(GDALGeoTransform&) instead")
345 : #endif
346 : ;
347 :
348 : CPLErr SetGeoTransform(const double *padfGeoTransform)
349 : #if defined(GDAL_COMPILATION) && !defined(DOXYGEN_XML)
350 : CPL_WARN_DEPRECATED(
351 : "Use SetGeoTransform(const GDALGeoTransform&) instead")
352 : #endif
353 : ;
354 :
355 : virtual CPLErr GetExtent(OGREnvelope *psExtent,
356 : const OGRSpatialReference *poCRS = nullptr) const;
357 : virtual CPLErr GetExtentWGS84LongLat(OGREnvelope *psExtent) const;
358 :
359 : CPLErr GeolocationToPixelLine(
360 : double dfGeolocX, double dfGeolocY, const OGRSpatialReference *poSRS,
361 : double *pdfPixel, double *pdfLine,
362 : CSLConstList papszTransformerOptions = nullptr) const;
363 :
364 : virtual CPLErr AddBand(GDALDataType eType, char **papszOptions = nullptr);
365 :
366 : virtual void *GetInternalHandle(const char *pszHandleName);
367 : virtual GDALDriver *GetDriver(void);
368 : virtual char **GetFileList(void);
369 :
370 : const char *GetDriverName() const;
371 :
372 : virtual const OGRSpatialReference *GetGCPSpatialRef() const;
373 : virtual int GetGCPCount();
374 : virtual const GDAL_GCP *GetGCPs();
375 : virtual CPLErr SetGCPs(int nGCPCount, const GDAL_GCP *pasGCPList,
376 : const OGRSpatialReference *poGCP_SRS);
377 :
378 : // Compatibility layer
379 : const char *GetGCPProjection() const;
380 : CPLErr SetGCPs(int nGCPCount, const GDAL_GCP *pasGCPList,
381 : const char *pszGCPProjection);
382 :
383 : virtual CPLErr AdviseRead(int nXOff, int nYOff, int nXSize, int nYSize,
384 : int nBufXSize, int nBufYSize, GDALDataType eDT,
385 : int nBandCount, int *panBandList,
386 : char **papszOptions);
387 :
388 : virtual CPLErr CreateMaskBand(int nFlagsIn);
389 :
390 : virtual GDALAsyncReader *
391 : BeginAsyncReader(int nXOff, int nYOff, int nXSize, int nYSize, void *pBuf,
392 : int nBufXSize, int nBufYSize, GDALDataType eBufType,
393 : int nBandCount, int *panBandMap, int nPixelSpace,
394 : int nLineSpace, int nBandSpace, char **papszOptions);
395 : virtual void EndAsyncReader(GDALAsyncReader *poARIO);
396 :
397 : //! @cond Doxygen_Suppress
398 : struct RawBinaryLayout
399 : {
400 : enum class Interleaving
401 : {
402 : UNKNOWN,
403 : BIP,
404 : BIL,
405 : BSQ
406 : };
407 : std::string osRawFilename{};
408 : Interleaving eInterleaving = Interleaving::UNKNOWN;
409 : GDALDataType eDataType = GDT_Unknown;
410 : bool bLittleEndianOrder = false;
411 :
412 : vsi_l_offset nImageOffset = 0;
413 : GIntBig nPixelOffset = 0;
414 : GIntBig nLineOffset = 0;
415 : GIntBig nBandOffset = 0;
416 : };
417 :
418 : virtual bool GetRawBinaryLayout(RawBinaryLayout &);
419 : //! @endcond
420 :
421 : #ifndef DOXYGEN_SKIP
422 : CPLErr RasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize,
423 : int nYSize, void *pData, int nBufXSize, int nBufYSize,
424 : GDALDataType eBufType, int nBandCount,
425 : const int *panBandMap, GSpacing nPixelSpace,
426 : GSpacing nLineSpace, GSpacing nBandSpace,
427 : GDALRasterIOExtraArg *psExtraArg
428 : OPTIONAL_OUTSIDE_GDAL(nullptr)) CPL_WARN_UNUSED_RESULT;
429 : #else
430 : CPLErr RasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize,
431 : int nYSize, void *pData, int nBufXSize, int nBufYSize,
432 : GDALDataType eBufType, int nBandCount,
433 : const int *panBandMap, GSpacing nPixelSpace,
434 : GSpacing nLineSpace, GSpacing nBandSpace,
435 : GDALRasterIOExtraArg *psExtraArg) CPL_WARN_UNUSED_RESULT;
436 : #endif
437 :
438 : virtual CPLStringList GetCompressionFormats(int nXOff, int nYOff,
439 : int nXSize, int nYSize,
440 : int nBandCount,
441 : const int *panBandList);
442 : virtual CPLErr ReadCompressedData(const char *pszFormat, int nXOff,
443 : int nYOff, int nXSize, int nYSize,
444 : int nBands, const int *panBandList,
445 : void **ppBuffer, size_t *pnBufferSize,
446 : char **ppszDetailedFormat);
447 :
448 : int Reference();
449 : int Dereference();
450 : int ReleaseRef();
451 :
452 : /** Return access mode.
453 : * @return access mode.
454 : */
455 195072 : GDALAccess GetAccess() const
456 : {
457 195072 : return eAccess;
458 : }
459 :
460 : int GetShared() const;
461 : void MarkAsShared();
462 :
463 : void MarkSuppressOnClose();
464 : void UnMarkSuppressOnClose();
465 :
466 : /** Return MarkSuppressOnClose flag.
467 : * @return MarkSuppressOnClose flag.
468 : */
469 5730334 : bool IsMarkedSuppressOnClose() const
470 : {
471 5730334 : return bSuppressOnClose;
472 : }
473 :
474 : /** Return open options.
475 : * @return open options.
476 : */
477 144202 : char **GetOpenOptions()
478 : {
479 144202 : return papszOpenOptions;
480 : }
481 :
482 : bool IsThreadSafe(int nScopeFlags) const;
483 :
484 : #ifndef DOXYGEN_SKIP
485 : /** Return open options.
486 : * @return open options.
487 : */
488 8 : CSLConstList GetOpenOptions() const
489 : {
490 8 : return papszOpenOptions;
491 : }
492 : #endif
493 :
494 : static GDALDataset **GetOpenDatasets(int *pnDatasetCount);
495 :
496 : #ifndef DOXYGEN_SKIP
497 : CPLErr
498 : BuildOverviews(const char *pszResampling, int nOverviews,
499 : const int *panOverviewList, int nListBands,
500 : const int *panBandList, GDALProgressFunc pfnProgress,
501 : void *pProgressData,
502 : CSLConstList papszOptions OPTIONAL_OUTSIDE_GDAL(nullptr));
503 : #else
504 : CPLErr BuildOverviews(const char *pszResampling, int nOverviews,
505 : const int *panOverviewList, int nListBands,
506 : const int *panBandList, GDALProgressFunc pfnProgress,
507 : void *pProgressData, CSLConstList papszOptions);
508 : #endif
509 :
510 : virtual CPLErr AddOverviews(const std::vector<GDALDataset *> &apoSrcOvrDS,
511 : GDALProgressFunc pfnProgress,
512 : void *pProgressData, CSLConstList papszOptions);
513 :
514 : #ifndef DOXYGEN_XML
515 : void ReportError(CPLErr eErrClass, CPLErrorNum err_no, const char *fmt,
516 : ...) const CPL_PRINT_FUNC_FORMAT(4, 5);
517 :
518 : static void ReportError(const char *pszDSName, CPLErr eErrClass,
519 : CPLErrorNum err_no, const char *fmt, ...)
520 : CPL_PRINT_FUNC_FORMAT(4, 5);
521 : #endif
522 :
523 : char **GetMetadata(const char *pszDomain = "") override;
524 :
525 : // Only defined when Doxygen enabled
526 : #ifdef DOXYGEN_SKIP
527 : CPLErr SetMetadata(char **papszMetadata, const char *pszDomain) override;
528 : CPLErr SetMetadataItem(const char *pszName, const char *pszValue,
529 : const char *pszDomain) override;
530 : #endif
531 :
532 : char **GetMetadataDomainList() override;
533 :
534 : virtual void ClearStatistics();
535 :
536 : std::shared_ptr<GDALMDArray> AsMDArray(CSLConstList papszOptions = nullptr);
537 :
538 : /** Convert a GDALDataset* to a GDALDatasetH.
539 : */
540 31988 : static inline GDALDatasetH ToHandle(GDALDataset *poDS)
541 : {
542 31988 : return static_cast<GDALDatasetH>(poDS);
543 : }
544 :
545 : /** Convert a GDALDatasetH to a GDALDataset*.
546 : */
547 1527629 : static inline GDALDataset *FromHandle(GDALDatasetH hDS)
548 : {
549 1527629 : return static_cast<GDALDataset *>(hDS);
550 : }
551 :
552 : /** @see GDALOpenEx().
553 : */
554 31271 : static GDALDataset *Open(const char *pszFilename,
555 : unsigned int nOpenFlags = 0,
556 : const char *const *papszAllowedDrivers = nullptr,
557 : const char *const *papszOpenOptions = nullptr,
558 : const char *const *papszSiblingFiles = nullptr)
559 : {
560 31271 : return FromHandle(GDALOpenEx(pszFilename, nOpenFlags,
561 : papszAllowedDrivers, papszOpenOptions,
562 31270 : papszSiblingFiles));
563 : }
564 :
565 : /** Object returned by GetFeatures() iterators */
566 : struct FeatureLayerPair
567 : {
568 : /** Unique pointer to a OGRFeature. */
569 : OGRFeatureUniquePtr feature{};
570 :
571 : /** Layer to which the feature belongs to. */
572 : OGRLayer *layer = nullptr;
573 : };
574 :
575 : //! @cond Doxygen_Suppress
576 : // SetEnableOverviews() only to be used by GDALOverviewDataset
577 : void SetEnableOverviews(bool bEnable);
578 :
579 : // Only to be used by driver's GetOverviewCount() method.
580 : bool AreOverviewsEnabled() const;
581 :
582 : static void ReportUpdateNotSupportedByDriver(const char *pszDriverName);
583 : //! @endcond
584 :
585 : private:
586 : class Private;
587 : Private *m_poPrivate;
588 :
589 : CPL_INTERNAL OGRLayer *BuildLayerFromSelectInfo(
590 : swq_select *psSelectInfo, OGRGeometry *poSpatialFilter,
591 : const char *pszDialect, swq_select_parse_options *poSelectParseOptions);
592 : CPLStringList oDerivedMetadataList{};
593 :
594 : public:
595 : virtual int GetLayerCount() const;
596 : virtual const OGRLayer *GetLayer(int iLayer) const;
597 :
598 1865858 : OGRLayer *GetLayer(int iLayer)
599 : {
600 : return const_cast<OGRLayer *>(
601 1865858 : const_cast<const GDALDataset *>(this)->GetLayer(iLayer));
602 : }
603 :
604 : virtual bool IsLayerPrivate(int iLayer) const;
605 :
606 : /** Class returned by GetLayers() that acts as a range of layers.
607 : */
608 : class CPL_DLL Layers
609 : {
610 : private:
611 : friend class GDALDataset;
612 : GDALDataset *m_poSelf;
613 :
614 305 : CPL_INTERNAL explicit Layers(GDALDataset *poSelf) : m_poSelf(poSelf)
615 : {
616 305 : }
617 :
618 : public:
619 : /** Layer iterator.
620 : */
621 620 : class CPL_DLL Iterator
622 : {
623 : struct Private;
624 : std::unique_ptr<Private> m_poPrivate;
625 :
626 : public:
627 : using value_type = OGRLayer *; /**< value_type */
628 : using reference = OGRLayer *; /**< reference */
629 : using difference_type = void; /**< difference_type */
630 : using pointer = void; /**< pointer */
631 : using iterator_category =
632 : std::input_iterator_tag; /**< iterator_category */
633 :
634 : Iterator(); /**< Default constructor */
635 : Iterator(GDALDataset *poDS, bool bStart); /**< Constructor */
636 : Iterator(const Iterator &oOther); /**< Copy constructor */
637 : Iterator(Iterator &&oOther) noexcept; /**< Move constructor */
638 : ~Iterator(); /**< Destructor */
639 :
640 : Iterator &
641 : operator=(const Iterator &oOther); /**< Assignment operator */
642 : Iterator &operator=(
643 : Iterator &&oOther) noexcept; /**< Move assignment operator */
644 :
645 : value_type operator*() const; /**< Dereference operator */
646 : Iterator &operator++(); /**< Pre-increment operator */
647 : Iterator operator++(int); /**< Post-increment operator */
648 : bool operator!=(const Iterator &it)
649 : const; /**< Difference comparison operator */
650 : };
651 :
652 : Iterator begin() const;
653 : Iterator end() const;
654 :
655 : size_t size() const;
656 :
657 : OGRLayer *operator[](int iLayer);
658 : OGRLayer *operator[](size_t iLayer);
659 : OGRLayer *operator[](const char *pszLayername);
660 : };
661 :
662 : Layers GetLayers();
663 :
664 : /** Class returned by GetLayers() that acts as a range of layers.
665 : * @since GDAL 3.12
666 : */
667 : class CPL_DLL ConstLayers
668 : {
669 : private:
670 : friend class GDALDataset;
671 : const GDALDataset *m_poSelf;
672 :
673 17287 : CPL_INTERNAL explicit ConstLayers(const GDALDataset *poSelf)
674 17287 : : m_poSelf(poSelf)
675 : {
676 17287 : }
677 :
678 : public:
679 : /** Layer iterator.
680 : * @since GDAL 3.12
681 : */
682 34585 : class CPL_DLL Iterator
683 : {
684 : struct Private;
685 : std::unique_ptr<Private> m_poPrivate;
686 :
687 : public:
688 : using value_type = const OGRLayer *; /**< value_type */
689 : using reference = const OGRLayer *; /**< reference */
690 : using difference_type = void; /**< difference_type */
691 : using pointer = void; /**< pointer */
692 : using iterator_category =
693 : std::input_iterator_tag; /**< iterator_category */
694 :
695 : Iterator(); /**< Default constructor */
696 : Iterator(const GDALDataset *poDS, bool bStart); /**< Constructor */
697 : Iterator(const Iterator &oOther); /**< Copy constructor */
698 : Iterator(Iterator &&oOther) noexcept; /**< Move constructor */
699 : ~Iterator(); /**< Destructor */
700 :
701 : Iterator &
702 : operator=(const Iterator &oOther); /**< Assignment operator */
703 : Iterator &operator=(
704 : Iterator &&oOther) noexcept; /**< Move assignment operator */
705 :
706 : value_type operator*() const; /**< Dereference operator */
707 : Iterator &operator++(); /**< Pre-increment operator */
708 : Iterator operator++(int); /**< Post-increment operator */
709 : bool operator!=(const Iterator &it)
710 : const; /**< Difference comparison operator */
711 : };
712 :
713 : Iterator begin() const;
714 : Iterator end() const;
715 :
716 : size_t size() const;
717 :
718 : const OGRLayer *operator[](int iLayer);
719 : const OGRLayer *operator[](size_t iLayer);
720 : const OGRLayer *operator[](const char *pszLayername);
721 : };
722 :
723 : ConstLayers GetLayers() const;
724 :
725 : virtual OGRLayer *GetLayerByName(const char *);
726 :
727 : int GetLayerIndex(const char *pszName) const;
728 :
729 : virtual OGRErr DeleteLayer(int iLayer);
730 :
731 : virtual void ResetReading();
732 : virtual OGRFeature *GetNextFeature(OGRLayer **ppoBelongingLayer,
733 : double *pdfProgressPct,
734 : GDALProgressFunc pfnProgress,
735 : void *pProgressData);
736 :
737 : /** Class returned by GetFeatures() that act as a container for vector
738 : * features. */
739 : class CPL_DLL Features
740 : {
741 : private:
742 : friend class GDALDataset;
743 : GDALDataset *m_poSelf;
744 :
745 2 : CPL_INTERNAL explicit Features(GDALDataset *poSelf) : m_poSelf(poSelf)
746 : {
747 2 : }
748 :
749 4 : class CPL_DLL Iterator
750 : {
751 : struct Private;
752 : std::unique_ptr<Private> m_poPrivate;
753 :
754 : public:
755 : Iterator(GDALDataset *poDS, bool bStart);
756 : Iterator(const Iterator &oOther); // declared but not defined.
757 : // Needed for gcc 5.4 at least
758 : Iterator(Iterator &&oOther) noexcept; // declared but not defined.
759 : // Needed for gcc 5.4 at least
760 : ~Iterator();
761 : const FeatureLayerPair &operator*() const;
762 : Iterator &operator++();
763 : bool operator!=(const Iterator &it) const;
764 : };
765 :
766 : public:
767 : const Iterator begin() const;
768 :
769 : const Iterator end() const;
770 : };
771 :
772 : Features GetFeatures();
773 :
774 : virtual int TestCapability(const char *) const;
775 :
776 : virtual std::vector<std::string>
777 : GetFieldDomainNames(CSLConstList papszOptions = nullptr) const;
778 :
779 : virtual const OGRFieldDomain *GetFieldDomain(const std::string &name) const;
780 :
781 : virtual bool AddFieldDomain(std::unique_ptr<OGRFieldDomain> &&domain,
782 : std::string &failureReason);
783 :
784 : virtual bool DeleteFieldDomain(const std::string &name,
785 : std::string &failureReason);
786 :
787 : virtual bool UpdateFieldDomain(std::unique_ptr<OGRFieldDomain> &&domain,
788 : std::string &failureReason);
789 :
790 : virtual std::vector<std::string>
791 : GetRelationshipNames(CSLConstList papszOptions = nullptr) const;
792 :
793 : virtual const GDALRelationship *
794 : GetRelationship(const std::string &name) const;
795 :
796 : virtual bool
797 : AddRelationship(std::unique_ptr<GDALRelationship> &&relationship,
798 : std::string &failureReason);
799 :
800 : virtual bool DeleteRelationship(const std::string &name,
801 : std::string &failureReason);
802 :
803 : virtual bool
804 : UpdateRelationship(std::unique_ptr<GDALRelationship> &&relationship,
805 : std::string &failureReason);
806 :
807 : //! @cond Doxygen_Suppress
808 : OGRLayer *CreateLayer(const char *pszName);
809 :
810 : OGRLayer *CreateLayer(const char *pszName, std::nullptr_t);
811 : //! @endcond
812 :
813 : OGRLayer *CreateLayer(const char *pszName,
814 : const OGRSpatialReference *poSpatialRef,
815 : OGRwkbGeometryType eGType = wkbUnknown,
816 : CSLConstList papszOptions = nullptr);
817 :
818 : OGRLayer *CreateLayer(const char *pszName,
819 : const OGRGeomFieldDefn *poGeomFieldDefn,
820 : CSLConstList papszOptions = nullptr);
821 :
822 : virtual OGRLayer *CopyLayer(OGRLayer *poSrcLayer, const char *pszNewName,
823 : char **papszOptions = nullptr);
824 :
825 : virtual OGRStyleTable *GetStyleTable();
826 : virtual void SetStyleTableDirectly(OGRStyleTable *poStyleTable);
827 :
828 : virtual void SetStyleTable(OGRStyleTable *poStyleTable);
829 :
830 : virtual OGRLayer *ExecuteSQL(const char *pszStatement,
831 : OGRGeometry *poSpatialFilter,
832 : const char *pszDialect);
833 : virtual void ReleaseResultSet(OGRLayer *poResultsSet);
834 : virtual OGRErr AbortSQL();
835 :
836 : int GetRefCount() const;
837 : int GetSummaryRefCount() const;
838 : OGRErr Release();
839 :
840 : virtual OGRErr StartTransaction(int bForce = FALSE);
841 : virtual OGRErr CommitTransaction();
842 : virtual OGRErr RollbackTransaction();
843 :
844 : virtual std::shared_ptr<GDALGroup> GetRootGroup() const;
845 :
846 : static std::string BuildFilename(const char *pszFilename,
847 : const char *pszReferencePath,
848 : bool bRelativeToReferencePath);
849 :
850 : //! @cond Doxygen_Suppress
851 : static int IsGenericSQLDialect(const char *pszDialect);
852 :
853 : // Semi-public methods. Only to be used by in-tree drivers.
854 : GDALSQLParseInfo *
855 : BuildParseInfo(swq_select *psSelectInfo,
856 : swq_select_parse_options *poSelectParseOptions);
857 : static void DestroyParseInfo(GDALSQLParseInfo *psParseInfo);
858 : OGRLayer *ExecuteSQL(const char *pszStatement, OGRGeometry *poSpatialFilter,
859 : const char *pszDialect,
860 : swq_select_parse_options *poSelectParseOptions);
861 :
862 : static constexpr const char *const apszSpecialSubDatasetSyntax[] = {
863 : "NITF_IM:{ANY}:{FILENAME}", "PDF:{ANY}:{FILENAME}",
864 : "RASTERLITE:{FILENAME},{ANY}", "TILEDB:\"{FILENAME}\":{ANY}",
865 : "TILEDB:{FILENAME}:{ANY}"};
866 :
867 : //! @endcond
868 :
869 : protected:
870 : virtual OGRLayer *ICreateLayer(const char *pszName,
871 : const OGRGeomFieldDefn *poGeomFieldDefn,
872 : CSLConstList papszOptions);
873 :
874 : //! @cond Doxygen_Suppress
875 : OGRErr ProcessSQLCreateIndex(const char *);
876 : OGRErr ProcessSQLDropIndex(const char *);
877 : OGRErr ProcessSQLDropTable(const char *);
878 : OGRErr ProcessSQLAlterTableAddColumn(const char *);
879 : OGRErr ProcessSQLAlterTableDropColumn(const char *);
880 : OGRErr ProcessSQLAlterTableAlterColumn(const char *);
881 : OGRErr ProcessSQLAlterTableRenameColumn(const char *);
882 :
883 : OGRStyleTable *m_poStyleTable = nullptr;
884 :
885 : friend class GDALProxyPoolDataset;
886 : //! @endcond
887 :
888 : private:
889 : CPL_DISALLOW_COPY_ASSIGN(GDALDataset)
890 : };
891 :
892 : //! @cond Doxygen_Suppress
893 : struct CPL_DLL GDALDatasetUniquePtrDeleter
894 : {
895 101 : void operator()(GDALDataset *poDataset) const
896 : {
897 101 : GDALClose(poDataset);
898 101 : }
899 : };
900 :
901 : //! @endcond
902 :
903 : //! @cond Doxygen_Suppress
904 : struct CPL_DLL GDALDatasetUniquePtrReleaser
905 : {
906 2297 : void operator()(GDALDataset *poDataset) const
907 : {
908 2297 : if (poDataset)
909 2293 : poDataset->Release();
910 2297 : }
911 : };
912 :
913 : //! @endcond
914 :
915 : /** Unique pointer type for GDALDataset.
916 : * Appropriate for use on datasets open in non-shared mode and onto which
917 : * reference counter has not been manually modified.
918 : */
919 : using GDALDatasetUniquePtr =
920 : std::unique_ptr<GDALDataset, GDALDatasetUniquePtrDeleter>;
921 :
922 : #endif
|