Coverage Report

Created: 2026-04-01 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/frmts/mem/memdataset.cpp
Line
Count
Source
1
/******************************************************************************
2
 *
3
 * Project:  Memory Array Translator
4
 * Purpose:  Complete implementation.
5
 * Author:   Frank Warmerdam, warmerdam@pobox.com
6
 *
7
 ******************************************************************************
8
 * Copyright (c) 2000, Frank Warmerdam
9
 * Copyright (c) 2008-2013, Even Rouault <even dot rouault at spatialys.com>
10
 *
11
 * SPDX-License-Identifier: MIT
12
 ****************************************************************************/
13
14
#include "cpl_port.h"
15
#include "memdataset.h"
16
#include "memmultidim.h"
17
18
#include <algorithm>
19
#include <climits>
20
#include <cstdlib>
21
#include <cstring>
22
#include <limits>
23
#include <vector>
24
25
#include "cpl_config.h"
26
#include "cpl_conv.h"
27
#include "cpl_error.h"
28
#include "cpl_minixml.h"
29
#include "cpl_progress.h"
30
#include "cpl_string.h"
31
#include "cpl_vsi.h"
32
#include "gdal.h"
33
#include "gdal_frmts.h"
34
#include "gdal_mem.h"
35
36
struct MEMDataset::Private
37
{
38
    std::shared_ptr<GDALGroup> m_poRootGroup{};
39
    std::map<std::string, std::unique_ptr<GDALRelationship>>
40
        m_oMapRelationships{};
41
};
42
43
/************************************************************************/
44
/*                             MEMCreate()                              */
45
/************************************************************************/
46
47
/**
48
 * Create a new in-memory raster dataset.
49
 *
50
 * @param nXSize Width of created raster in pixels.
51
 * @param nYSize Height of created raster in pixels.
52
 * @param nBands Number of bands.
53
 * @param eType Type of raster bands.
54
 * @param papszOptions MEM driver creation options.
55
 *
56
 * @return NULL on failure, or a new MEM dataset handle on success.
57
 */
58
59
GDALDatasetH MEMCreate(int nXSize, int nYSize, int nBands, GDALDataType eType,
60
                       CSLConstList papszOptions)
61
62
0
{
63
0
    return GDALDataset::ToHandle(
64
0
        MEMDataset::Create("", nXSize, nYSize, nBands, eType, papszOptions));
65
0
}
66
67
/************************************************************************/
68
/*                        MEMCreateRasterBand()                         */
69
/************************************************************************/
70
71
GDALRasterBandH MEMCreateRasterBand(GDALDataset *poDS, int nBand,
72
                                    GByte *pabyData, GDALDataType eType,
73
                                    int nPixelOffset, int nLineOffset,
74
                                    int bAssumeOwnership)
75
76
0
{
77
0
    return GDALRasterBand::ToHandle(
78
0
        new MEMRasterBand(poDS, nBand, pabyData, eType, nPixelOffset,
79
0
                          nLineOffset, bAssumeOwnership));
80
0
}
81
82
/************************************************************************/
83
/*                       MEMCreateRasterBandEx()                        */
84
/************************************************************************/
85
86
GDALRasterBandH MEMCreateRasterBandEx(GDALDataset *poDS, int nBand,
87
                                      GByte *pabyData, GDALDataType eType,
88
                                      GSpacing nPixelOffset,
89
                                      GSpacing nLineOffset,
90
                                      int bAssumeOwnership)
91
92
0
{
93
0
    return GDALRasterBand::ToHandle(
94
0
        new MEMRasterBand(poDS, nBand, pabyData, eType, nPixelOffset,
95
0
                          nLineOffset, bAssumeOwnership));
96
0
}
97
98
/************************************************************************/
99
/*                           MEMRasterBand()                            */
100
/************************************************************************/
101
102
MEMRasterBand::MEMRasterBand(GByte *pabyDataIn, GDALDataType eTypeIn,
103
                             int nXSizeIn, int nYSizeIn, bool bOwnDataIn)
104
0
    : GDALPamRasterBand(FALSE), pabyData(pabyDataIn),
105
0
      nPixelOffset(GDALGetDataTypeSizeBytes(eTypeIn)), nLineOffset(0),
106
0
      bOwnData(bOwnDataIn)
107
0
{
108
0
    eAccess = GA_Update;
109
0
    eDataType = eTypeIn;
110
0
    nRasterXSize = nXSizeIn;
111
0
    nRasterYSize = nYSizeIn;
112
0
    nBlockXSize = nXSizeIn;
113
0
    nBlockYSize = 1;
114
0
    nLineOffset = nPixelOffset * static_cast<size_t>(nBlockXSize);
115
116
0
    PamInitializeNoParent();
117
0
}
118
119
/************************************************************************/
120
/*                           MEMRasterBand()                            */
121
/************************************************************************/
122
123
MEMRasterBand::MEMRasterBand(GDALDataset *poDSIn, int nBandIn,
124
                             GByte *pabyDataIn, GDALDataType eTypeIn,
125
                             GSpacing nPixelOffsetIn, GSpacing nLineOffsetIn,
126
                             int bAssumeOwnership, const char *pszPixelType)
127
0
    : GDALPamRasterBand(FALSE), pabyData(pabyDataIn),
128
0
      nPixelOffset(nPixelOffsetIn), nLineOffset(nLineOffsetIn),
129
0
      bOwnData(bAssumeOwnership)
130
0
{
131
0
    poDS = poDSIn;
132
0
    nBand = nBandIn;
133
134
0
    eAccess = poDS->GetAccess();
135
136
0
    eDataType = eTypeIn;
137
138
0
    nBlockXSize = poDS->GetRasterXSize();
139
0
    nBlockYSize = 1;
140
141
0
    if (nPixelOffsetIn == 0)
142
0
        nPixelOffset = GDALGetDataTypeSizeBytes(eTypeIn);
143
144
0
    if (nLineOffsetIn == 0)
145
0
        nLineOffset = nPixelOffset * static_cast<size_t>(nBlockXSize);
146
147
0
    if (pszPixelType && EQUAL(pszPixelType, "SIGNEDBYTE"))
148
0
        SetMetadataItem("PIXELTYPE", "SIGNEDBYTE", "IMAGE_STRUCTURE");
149
150
0
    PamInitializeNoParent();
151
0
}
152
153
/************************************************************************/
154
/*                           ~MEMRasterBand()                           */
155
/************************************************************************/
156
157
MEMRasterBand::~MEMRasterBand()
158
159
0
{
160
0
    if (bOwnData)
161
0
    {
162
0
        VSIFree(pabyData);
163
0
    }
164
0
}
165
166
/************************************************************************/
167
/*                             IReadBlock()                             */
168
/************************************************************************/
169
170
CPLErr MEMRasterBand::IReadBlock(CPL_UNUSED int nBlockXOff, int nBlockYOff,
171
                                 void *pImage)
172
0
{
173
0
    CPLAssert(nBlockXOff == 0);
174
175
0
    const int nWordSize = GDALGetDataTypeSizeBytes(eDataType);
176
177
0
    if (nPixelOffset == nWordSize)
178
0
    {
179
0
        memcpy(pImage, pabyData + nLineOffset * static_cast<size_t>(nBlockYOff),
180
0
               static_cast<size_t>(nPixelOffset) * nBlockXSize);
181
0
    }
182
0
    else
183
0
    {
184
0
        GByte *const pabyCur =
185
0
            pabyData + nLineOffset * static_cast<size_t>(nBlockYOff);
186
187
0
        for (int iPixel = 0; iPixel < nBlockXSize; iPixel++)
188
0
        {
189
0
            memcpy(static_cast<GByte *>(pImage) + iPixel * nWordSize,
190
0
                   pabyCur + iPixel * nPixelOffset, nWordSize);
191
0
        }
192
0
    }
193
194
0
    return CE_None;
195
0
}
196
197
/************************************************************************/
198
/*                            IWriteBlock()                             */
199
/************************************************************************/
200
201
CPLErr MEMRasterBand::IWriteBlock(CPL_UNUSED int nBlockXOff, int nBlockYOff,
202
                                  void *pImage)
203
0
{
204
0
    CPLAssert(nBlockXOff == 0);
205
0
    const int nWordSize = GDALGetDataTypeSizeBytes(eDataType);
206
207
0
    if (nPixelOffset == nWordSize)
208
0
    {
209
0
        memcpy(pabyData + nLineOffset * static_cast<size_t>(nBlockYOff), pImage,
210
0
               static_cast<size_t>(nPixelOffset) * nBlockXSize);
211
0
    }
212
0
    else
213
0
    {
214
0
        GByte *pabyCur =
215
0
            pabyData + nLineOffset * static_cast<size_t>(nBlockYOff);
216
217
0
        for (int iPixel = 0; iPixel < nBlockXSize; iPixel++)
218
0
        {
219
0
            memcpy(pabyCur + iPixel * nPixelOffset,
220
0
                   static_cast<GByte *>(pImage) + iPixel * nWordSize,
221
0
                   nWordSize);
222
0
        }
223
0
    }
224
225
0
    return CE_None;
226
0
}
227
228
/************************************************************************/
229
/*                             IRasterIO()                              */
230
/************************************************************************/
231
232
CPLErr MEMRasterBand::IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff,
233
                                int nXSize, int nYSize, void *pData,
234
                                int nBufXSize, int nBufYSize,
235
                                GDALDataType eBufType, GSpacing nPixelSpaceBuf,
236
                                GSpacing nLineSpaceBuf,
237
                                GDALRasterIOExtraArg *psExtraArg)
238
0
{
239
0
    if (nXSize != nBufXSize || nYSize != nBufYSize)
240
0
    {
241
0
        return GDALRasterBand::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize,
242
0
                                         pData, nBufXSize, nBufYSize, eBufType,
243
0
                                         static_cast<int>(nPixelSpaceBuf),
244
0
                                         nLineSpaceBuf, psExtraArg);
245
0
    }
246
247
    // In case block based I/O has been done before.
248
0
    FlushCache(false);
249
250
0
    if (eRWFlag == GF_Read)
251
0
    {
252
0
        for (int iLine = 0; iLine < nYSize; iLine++)
253
0
        {
254
0
            GDALCopyWords64(pabyData +
255
0
                                nLineOffset *
256
0
                                    static_cast<GPtrDiff_t>(iLine + nYOff) +
257
0
                                nXOff * nPixelOffset,
258
0
                            eDataType, static_cast<int>(nPixelOffset),
259
0
                            static_cast<GByte *>(pData) +
260
0
                                nLineSpaceBuf * static_cast<GPtrDiff_t>(iLine),
261
0
                            eBufType, static_cast<int>(nPixelSpaceBuf), nXSize);
262
0
        }
263
0
    }
264
0
    else
265
0
    {
266
0
        if (nXSize == nRasterXSize && nPixelSpaceBuf == nPixelOffset &&
267
0
            nLineSpaceBuf == nLineOffset)
268
0
        {
269
0
            GDALCopyWords64(pData, eBufType, static_cast<int>(nPixelSpaceBuf),
270
0
                            pabyData +
271
0
                                nLineOffset * static_cast<GPtrDiff_t>(nYOff),
272
0
                            eDataType, static_cast<int>(nPixelOffset),
273
0
                            static_cast<GPtrDiff_t>(nXSize) * nYSize);
274
0
        }
275
0
        else
276
0
        {
277
0
            for (int iLine = 0; iLine < nYSize; iLine++)
278
0
            {
279
0
                GDALCopyWords64(
280
0
                    static_cast<GByte *>(pData) +
281
0
                        nLineSpaceBuf * static_cast<GPtrDiff_t>(iLine),
282
0
                    eBufType, static_cast<int>(nPixelSpaceBuf),
283
0
                    pabyData +
284
0
                        nLineOffset * static_cast<GPtrDiff_t>(iLine + nYOff) +
285
0
                        nXOff * nPixelOffset,
286
0
                    eDataType, static_cast<int>(nPixelOffset), nXSize);
287
0
            }
288
0
        }
289
0
    }
290
0
    return CE_None;
291
0
}
292
293
/************************************************************************/
294
/*                             IRasterIO()                              */
295
/************************************************************************/
296
297
CPLErr MEMDataset::IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff,
298
                             int nXSize, int nYSize, void *pData, int nBufXSize,
299
                             int nBufYSize, GDALDataType eBufType,
300
                             int nBandCount, BANDMAP_TYPE panBandMap,
301
                             GSpacing nPixelSpaceBuf, GSpacing nLineSpaceBuf,
302
                             GSpacing nBandSpaceBuf,
303
                             GDALRasterIOExtraArg *psExtraArg)
304
0
{
305
0
    const int eBufTypeSize = GDALGetDataTypeSizeBytes(eBufType);
306
307
0
    const auto IsPixelInterleaveDataset = [this, nBandCount, panBandMap]()
308
0
    {
309
0
        GDALDataType eDT = GDT_Unknown;
310
0
        GByte *pabyData = nullptr;
311
0
        GSpacing nPixelOffset = 0;
312
0
        GSpacing nLineOffset = 0;
313
0
        int eDTSize = 0;
314
0
        for (int iBandIndex = 0; iBandIndex < nBandCount; iBandIndex++)
315
0
        {
316
0
            if (panBandMap[iBandIndex] != iBandIndex + 1)
317
0
                return false;
318
319
0
            MEMRasterBand *poBand =
320
0
                cpl::down_cast<MEMRasterBand *>(GetRasterBand(iBandIndex + 1));
321
0
            if (iBandIndex == 0)
322
0
            {
323
0
                eDT = poBand->GetRasterDataType();
324
0
                pabyData = poBand->pabyData;
325
0
                nPixelOffset = poBand->nPixelOffset;
326
0
                nLineOffset = poBand->nLineOffset;
327
0
                eDTSize = GDALGetDataTypeSizeBytes(eDT);
328
0
                if (nPixelOffset != static_cast<GSpacing>(nBands) * eDTSize)
329
0
                    return false;
330
0
            }
331
0
            else if (poBand->GetRasterDataType() != eDT ||
332
0
                     nPixelOffset != poBand->nPixelOffset ||
333
0
                     nLineOffset != poBand->nLineOffset ||
334
0
                     poBand->pabyData != pabyData + iBandIndex * eDTSize)
335
0
            {
336
0
                return false;
337
0
            }
338
0
        }
339
0
        return true;
340
0
    };
341
342
    // Detect if we have a pixel-interleaved buffer
343
0
    if (nXSize == nBufXSize && nYSize == nBufYSize && nBandCount == nBands &&
344
0
        nBands > 1 && nBandSpaceBuf == eBufTypeSize &&
345
0
        nPixelSpaceBuf == nBandSpaceBuf * nBands)
346
0
    {
347
0
        const auto IsBandSeparatedDataset = [this, nBandCount, panBandMap]()
348
0
        {
349
0
            GDALDataType eDT = GDT_Unknown;
350
0
            GSpacing nPixelOffset = 0;
351
0
            GSpacing nLineOffset = 0;
352
0
            int eDTSize = 0;
353
0
            for (int iBandIndex = 0; iBandIndex < nBandCount; iBandIndex++)
354
0
            {
355
0
                if (panBandMap[iBandIndex] != iBandIndex + 1)
356
0
                    return false;
357
358
0
                MEMRasterBand *poBand = cpl::down_cast<MEMRasterBand *>(
359
0
                    GetRasterBand(iBandIndex + 1));
360
0
                if (iBandIndex == 0)
361
0
                {
362
0
                    eDT = poBand->GetRasterDataType();
363
0
                    nPixelOffset = poBand->nPixelOffset;
364
0
                    nLineOffset = poBand->nLineOffset;
365
0
                    eDTSize = GDALGetDataTypeSizeBytes(eDT);
366
0
                    if (nPixelOffset != eDTSize)
367
0
                        return false;
368
0
                }
369
0
                else if (poBand->GetRasterDataType() != eDT ||
370
0
                         nPixelOffset != poBand->nPixelOffset ||
371
0
                         nLineOffset != poBand->nLineOffset)
372
0
                {
373
0
                    return false;
374
0
                }
375
0
            }
376
0
            return true;
377
0
        };
378
379
0
        if (IsPixelInterleaveDataset())
380
0
        {
381
0
            FlushCache(false);
382
0
            const auto poFirstBand =
383
0
                cpl::down_cast<MEMRasterBand *>(papoBands[0]);
384
0
            const GDALDataType eDT = poFirstBand->GetRasterDataType();
385
0
            GByte *pabyData = poFirstBand->pabyData;
386
0
            const GSpacing nPixelOffset = poFirstBand->nPixelOffset;
387
0
            const GSpacing nLineOffset = poFirstBand->nLineOffset;
388
0
            const int eDTSize = GDALGetDataTypeSizeBytes(eDT);
389
0
            if (eRWFlag == GF_Read)
390
0
            {
391
0
                for (int iLine = 0; iLine < nYSize; iLine++)
392
0
                {
393
0
                    GDALCopyWords(
394
0
                        pabyData +
395
0
                            nLineOffset * static_cast<size_t>(iLine + nYOff) +
396
0
                            nXOff * nPixelOffset,
397
0
                        eDT, eDTSize,
398
0
                        static_cast<GByte *>(pData) +
399
0
                            nLineSpaceBuf * static_cast<size_t>(iLine),
400
0
                        eBufType, eBufTypeSize, nXSize * nBands);
401
0
                }
402
0
            }
403
0
            else
404
0
            {
405
0
                for (int iLine = 0; iLine < nYSize; iLine++)
406
0
                {
407
0
                    GDALCopyWords(
408
0
                        static_cast<GByte *>(pData) +
409
0
                            nLineSpaceBuf * static_cast<size_t>(iLine),
410
0
                        eBufType, eBufTypeSize,
411
0
                        pabyData +
412
0
                            nLineOffset * static_cast<size_t>(iLine + nYOff) +
413
0
                            nXOff * nPixelOffset,
414
0
                        eDT, eDTSize, nXSize * nBands);
415
0
                }
416
0
            }
417
0
            return CE_None;
418
0
        }
419
0
        else if (eRWFlag == GF_Write && nBandCount <= 4 &&
420
0
                 IsBandSeparatedDataset())
421
0
        {
422
            // TODO: once we have a GDALInterleave() function, implement the
423
            // GF_Read case
424
0
            FlushCache(false);
425
0
            const auto poFirstBand =
426
0
                cpl::down_cast<MEMRasterBand *>(papoBands[0]);
427
0
            const GDALDataType eDT = poFirstBand->GetRasterDataType();
428
0
            void *ppDestBuffer[4] = {nullptr, nullptr, nullptr, nullptr};
429
0
            if (nXOff == 0 && nXSize == nRasterXSize &&
430
0
                poFirstBand->nLineOffset ==
431
0
                    poFirstBand->nPixelOffset * nXSize &&
432
0
                nLineSpaceBuf == nPixelSpaceBuf * nXSize)
433
0
            {
434
                // Optimization of the general case in the below else() clause:
435
                // writing whole strips from a fully packed buffer
436
0
                for (int i = 0; i < nBandCount; ++i)
437
0
                {
438
0
                    const auto poBand =
439
0
                        cpl::down_cast<MEMRasterBand *>(papoBands[i]);
440
0
                    ppDestBuffer[i] =
441
0
                        poBand->pabyData + poBand->nLineOffset * nYOff;
442
0
                }
443
0
                GDALDeinterleave(pData, eBufType, nBandCount, ppDestBuffer, eDT,
444
0
                                 static_cast<size_t>(nXSize) * nYSize);
445
0
            }
446
0
            else
447
0
            {
448
0
                for (int iLine = 0; iLine < nYSize; iLine++)
449
0
                {
450
0
                    for (int i = 0; i < nBandCount; ++i)
451
0
                    {
452
0
                        const auto poBand =
453
0
                            cpl::down_cast<MEMRasterBand *>(papoBands[i]);
454
0
                        ppDestBuffer[i] = poBand->pabyData +
455
0
                                          poBand->nPixelOffset * nXOff +
456
0
                                          poBand->nLineOffset * (iLine + nYOff);
457
0
                    }
458
0
                    GDALDeinterleave(
459
0
                        static_cast<GByte *>(pData) +
460
0
                            nLineSpaceBuf * static_cast<size_t>(iLine),
461
0
                        eBufType, nBandCount, ppDestBuffer, eDT, nXSize);
462
0
                }
463
0
            }
464
0
            return CE_None;
465
0
        }
466
0
    }
467
    // From a band-interleaved buffer to a pixel-interleaved dataset
468
0
    else if (eRWFlag == GF_Write && nXSize == nBufXSize &&
469
0
             nYSize == nBufYSize && nXSize == nRasterXSize &&
470
0
             nBandCount == nBands && nBands > 1 &&
471
0
             nPixelSpaceBuf == eBufTypeSize &&
472
0
             nLineSpaceBuf == nPixelSpaceBuf * nBufXSize &&
473
0
             nBandSpaceBuf == nLineSpaceBuf * nBufYSize &&
474
0
             IsPixelInterleaveDataset())
475
0
    {
476
0
        FlushCache(false);
477
478
0
        auto poDstBand = cpl::down_cast<MEMRasterBand *>(papoBands[0]);
479
0
        GDALTranspose2D(pData, eBufType,
480
0
                        poDstBand->pabyData + nYOff * poDstBand->nLineOffset,
481
0
                        poDstBand->GetRasterDataType(),
482
0
                        static_cast<size_t>(nXSize) * nYSize, nBands);
483
0
        return CE_None;
484
0
    }
485
486
0
    if (nBufXSize != nXSize || nBufYSize != nYSize)
487
0
        return GDALDataset::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize,
488
0
                                      pData, nBufXSize, nBufYSize, eBufType,
489
0
                                      nBandCount, panBandMap, nPixelSpaceBuf,
490
0
                                      nLineSpaceBuf, nBandSpaceBuf, psExtraArg);
491
492
0
    return GDALDataset::BandBasedRasterIO(
493
0
        eRWFlag, nXOff, nYOff, nXSize, nYSize, pData, nBufXSize, nBufYSize,
494
0
        eBufType, nBandCount, panBandMap, nPixelSpaceBuf, nLineSpaceBuf,
495
0
        nBandSpaceBuf, psExtraArg);
496
0
}
497
498
/************************************************************************/
499
/*                          GetOverviewCount()                          */
500
/************************************************************************/
501
502
int MEMRasterBand::GetOverviewCount()
503
0
{
504
0
    MEMDataset *poMemDS = dynamic_cast<MEMDataset *>(poDS);
505
0
    if (poMemDS == nullptr)
506
0
        return 0;
507
0
    return static_cast<int>(poMemDS->m_apoOverviewDS.size());
508
0
}
509
510
/************************************************************************/
511
/*                            GetOverview()                             */
512
/************************************************************************/
513
514
GDALRasterBand *MEMRasterBand::GetOverview(int i)
515
516
0
{
517
0
    MEMDataset *poMemDS = dynamic_cast<MEMDataset *>(poDS);
518
0
    if (poMemDS == nullptr)
519
0
        return nullptr;
520
0
    if (i < 0 || i >= static_cast<int>(poMemDS->m_apoOverviewDS.size()))
521
0
        return nullptr;
522
0
    return poMemDS->m_apoOverviewDS[i]->GetRasterBand(nBand);
523
0
}
524
525
/************************************************************************/
526
/*                           CreateMaskBand()                           */
527
/************************************************************************/
528
529
CPLErr MEMRasterBand::CreateMaskBand(int nFlagsIn)
530
0
{
531
0
    InvalidateMaskBand();
532
533
0
    MEMDataset *poMemDS = dynamic_cast<MEMDataset *>(poDS);
534
0
    if ((nFlagsIn & GMF_PER_DATASET) != 0 && nBand != 1 && poMemDS != nullptr)
535
0
    {
536
0
        MEMRasterBand *poFirstBand =
537
0
            dynamic_cast<MEMRasterBand *>(poMemDS->GetRasterBand(1));
538
0
        if (poFirstBand != nullptr)
539
0
            return poFirstBand->CreateMaskBand(nFlagsIn);
540
0
    }
541
542
0
    GByte *pabyMaskData =
543
0
        static_cast<GByte *>(VSI_CALLOC_VERBOSE(nRasterXSize, nRasterYSize));
544
0
    if (pabyMaskData == nullptr)
545
0
        return CE_Failure;
546
547
0
    nMaskFlags = nFlagsIn;
548
0
    auto poMemMaskBand = std::unique_ptr<MEMRasterBand>(
549
0
        new MEMRasterBand(pabyMaskData, GDT_UInt8, nRasterXSize, nRasterYSize,
550
0
                          /* bOwnData= */ true));
551
0
    poMemMaskBand->m_bIsMask = true;
552
0
    poMask.reset(std::move(poMemMaskBand));
553
0
    if ((nFlagsIn & GMF_PER_DATASET) != 0 && nBand == 1 && poMemDS != nullptr)
554
0
    {
555
0
        for (int i = 2; i <= poMemDS->GetRasterCount(); ++i)
556
0
        {
557
0
            MEMRasterBand *poOtherBand =
558
0
                cpl::down_cast<MEMRasterBand *>(poMemDS->GetRasterBand(i));
559
0
            poOtherBand->InvalidateMaskBand();
560
0
            poOtherBand->nMaskFlags = nFlagsIn;
561
0
            poOtherBand->poMask.resetNotOwned(poMask.get());
562
0
        }
563
0
    }
564
0
    return CE_None;
565
0
}
566
567
/************************************************************************/
568
/*                             IsMaskBand()                             */
569
/************************************************************************/
570
571
bool MEMRasterBand::IsMaskBand() const
572
0
{
573
0
    return m_bIsMask || GDALPamRasterBand::IsMaskBand();
574
0
}
575
576
/************************************************************************/
577
/* ==================================================================== */
578
/*      MEMDataset                                                     */
579
/* ==================================================================== */
580
/************************************************************************/
581
582
/************************************************************************/
583
/*                             MEMDataset()                             */
584
/************************************************************************/
585
586
MEMDataset::MEMDataset()
587
0
    : GDALDataset(FALSE), bGeoTransformSet(FALSE), m_poPrivate(new Private())
588
0
{
589
0
    m_gt.yscale = -1;
590
0
    DisableReadWriteMutex();
591
0
}
592
593
/************************************************************************/
594
/*                            ~MEMDataset()                             */
595
/************************************************************************/
596
597
MEMDataset::~MEMDataset()
598
599
0
{
600
0
    MEMDataset::Close();
601
0
}
602
603
/************************************************************************/
604
/*                               Close()                                */
605
/************************************************************************/
606
607
CPLErr MEMDataset::Close(GDALProgressFunc, void *)
608
0
{
609
0
    CPLErr eErr = CE_None;
610
0
    if (nOpenFlags != OPEN_FLAGS_CLOSED)
611
0
    {
612
0
        const bool bSuppressOnCloseBackup = bSuppressOnClose;
613
0
        bSuppressOnClose = true;
614
0
        FlushCache(true);
615
0
        for (int i = 0; i < nBands; ++i)
616
0
        {
617
0
            auto poMEMBand = dynamic_cast<MEMRasterBand *>(papoBands[i]);
618
0
            if (poMEMBand && poMEMBand->poMask)
619
0
                poMEMBand->poMask.get()->FlushCache(true);
620
0
        }
621
0
        bSuppressOnClose = bSuppressOnCloseBackup;
622
0
        m_apoOverviewDS.clear();
623
0
        eErr = GDALDataset::Close();
624
0
    }
625
626
0
    return eErr;
627
0
}
628
629
#if 0
630
/************************************************************************/
631
/*                           EnterReadWrite()                           */
632
/************************************************************************/
633
634
int MEMDataset::EnterReadWrite(CPL_UNUSED GDALRWFlag eRWFlag)
635
{
636
    return TRUE;
637
}
638
639
/************************************************************************/
640
/*                           LeaveReadWrite()                           */
641
/************************************************************************/
642
643
void MEMDataset::LeaveReadWrite()
644
{
645
}
646
#endif  // if 0
647
648
/************************************************************************/
649
/*                           GetSpatialRef()                            */
650
/************************************************************************/
651
652
const OGRSpatialReference *MEMDataset::GetSpatialRef() const
653
654
0
{
655
0
    if (GetLayerCount())
656
0
        return GDALDataset::GetSpatialRef();
657
0
    return GetSpatialRefRasterOnly();
658
0
}
659
660
/************************************************************************/
661
/*                      GetSpatialRefRasterOnly()                       */
662
/************************************************************************/
663
664
const OGRSpatialReference *MEMDataset::GetSpatialRefRasterOnly() const
665
666
0
{
667
0
    return m_oSRS.IsEmpty() ? nullptr : &m_oSRS;
668
0
}
669
670
/************************************************************************/
671
/*                           SetSpatialRef()                            */
672
/************************************************************************/
673
674
CPLErr MEMDataset::SetSpatialRef(const OGRSpatialReference *poSRS)
675
676
0
{
677
0
    m_oSRS.Clear();
678
0
    if (poSRS)
679
0
        m_oSRS = *poSRS;
680
681
0
    return CE_None;
682
0
}
683
684
/************************************************************************/
685
/*                          GetGeoTransform()                           */
686
/************************************************************************/
687
688
CPLErr MEMDataset::GetGeoTransform(GDALGeoTransform &gt) const
689
690
0
{
691
0
    gt = m_gt;
692
0
    if (bGeoTransformSet)
693
0
        return CE_None;
694
695
0
    return CE_Failure;
696
0
}
697
698
/************************************************************************/
699
/*                          SetGeoTransform()                           */
700
/************************************************************************/
701
702
CPLErr MEMDataset::SetGeoTransform(const GDALGeoTransform &gt)
703
704
0
{
705
0
    m_gt = gt;
706
0
    bGeoTransformSet = TRUE;
707
708
0
    return CE_None;
709
0
}
710
711
/************************************************************************/
712
/*                         GetInternalHandle()                          */
713
/************************************************************************/
714
715
void *MEMDataset::GetInternalHandle(const char *pszRequest)
716
717
0
{
718
    // check for MEMORYnnn string in pszRequest (nnnn can be up to 10
719
    // digits, or even omitted)
720
0
    if (STARTS_WITH_CI(pszRequest, "MEMORY"))
721
0
    {
722
0
        if (int BandNumber = static_cast<int>(CPLScanLong(&pszRequest[6], 10)))
723
0
        {
724
0
            MEMRasterBand *RequestedRasterBand =
725
0
                cpl::down_cast<MEMRasterBand *>(GetRasterBand(BandNumber));
726
727
            // we're within a MEMDataset so the only thing a RasterBand
728
            // could be is a MEMRasterBand
729
730
0
            if (RequestedRasterBand != nullptr)
731
0
            {
732
                // return the internal band data pointer
733
0
                return RequestedRasterBand->GetData();
734
0
            }
735
0
        }
736
0
    }
737
738
0
    return nullptr;
739
0
}
740
741
/************************************************************************/
742
/*                            GetGCPCount()                             */
743
/************************************************************************/
744
745
int MEMDataset::GetGCPCount()
746
747
0
{
748
0
    return static_cast<int>(m_aoGCPs.size());
749
0
}
750
751
/************************************************************************/
752
/*                          GetGCPSpatialRef()                          */
753
/************************************************************************/
754
755
const OGRSpatialReference *MEMDataset::GetGCPSpatialRef() const
756
757
0
{
758
0
    return m_oGCPSRS.IsEmpty() ? nullptr : &m_oGCPSRS;
759
0
}
760
761
/************************************************************************/
762
/*                              GetGCPs()                               */
763
/************************************************************************/
764
765
const GDAL_GCP *MEMDataset::GetGCPs()
766
767
0
{
768
0
    return gdal::GCP::c_ptr(m_aoGCPs);
769
0
}
770
771
/************************************************************************/
772
/*                              SetGCPs()                               */
773
/************************************************************************/
774
775
CPLErr MEMDataset::SetGCPs(int nNewCount, const GDAL_GCP *pasNewGCPList,
776
                           const OGRSpatialReference *poSRS)
777
778
0
{
779
0
    m_oGCPSRS.Clear();
780
0
    if (poSRS)
781
0
        m_oGCPSRS = *poSRS;
782
783
0
    m_aoGCPs = gdal::GCP::fromC(pasNewGCPList, nNewCount);
784
785
0
    return CE_None;
786
0
}
787
788
/************************************************************************/
789
/*                              AddBand()                               */
790
/*                                                                      */
791
/*      Add a new band to the dataset, allowing creation options to     */
792
/*      specify the existing memory to use, otherwise create new        */
793
/*      memory.                                                         */
794
/************************************************************************/
795
796
CPLErr MEMDataset::AddBand(GDALDataType eType, CSLConstList papszOptions)
797
798
0
{
799
0
    const int nBandId = GetRasterCount() + 1;
800
0
    const GSpacing nPixelSize = GDALGetDataTypeSizeBytes(eType);
801
0
    if (nPixelSize == 0)
802
0
    {
803
0
        ReportError(CE_Failure, CPLE_IllegalArg,
804
0
                    "Illegal GDT_Unknown/GDT_TypeCount argument");
805
0
        return CE_Failure;
806
0
    }
807
808
    /* -------------------------------------------------------------------- */
809
    /*      Do we need to allocate the memory ourselves?  This is the       */
810
    /*      simple case.                                                    */
811
    /* -------------------------------------------------------------------- */
812
0
    const CPLStringList aosOptions(papszOptions);
813
0
    if (aosOptions.FetchNameValue("DATAPOINTER") == nullptr)
814
0
    {
815
0
        const GSpacing nTmp = nPixelSize * GetRasterXSize();
816
0
        GByte *pData =
817
#if SIZEOF_VOIDP == 4
818
            (nTmp > INT_MAX) ? nullptr :
819
#endif
820
0
                             static_cast<GByte *>(VSI_CALLOC_VERBOSE(
821
0
                                 static_cast<size_t>(nTmp), GetRasterYSize()));
822
823
0
        if (pData == nullptr)
824
0
        {
825
0
            return CE_Failure;
826
0
        }
827
828
0
        SetBand(nBandId,
829
0
                new MEMRasterBand(this, nBandId, pData, eType, nPixelSize,
830
0
                                  nPixelSize * GetRasterXSize(), TRUE));
831
832
0
        return CE_None;
833
0
    }
834
835
    /* -------------------------------------------------------------------- */
836
    /*      Get layout of memory and other flags.                           */
837
    /* -------------------------------------------------------------------- */
838
0
    const char *pszDataPointer = aosOptions.FetchNameValue("DATAPOINTER");
839
0
    GByte *pData = static_cast<GByte *>(CPLScanPointer(
840
0
        pszDataPointer, static_cast<int>(strlen(pszDataPointer))));
841
842
0
    const char *pszOption = aosOptions.FetchNameValue("PIXELOFFSET");
843
0
    GSpacing nPixelOffset;
844
0
    if (pszOption == nullptr)
845
0
        nPixelOffset = nPixelSize;
846
0
    else
847
0
        nPixelOffset = CPLAtoGIntBig(pszOption);
848
849
0
    pszOption = aosOptions.FetchNameValue("LINEOFFSET");
850
0
    GSpacing nLineOffset;
851
0
    if (pszOption == nullptr)
852
0
        nLineOffset = GetRasterXSize() * static_cast<size_t>(nPixelOffset);
853
0
    else
854
0
        nLineOffset = CPLAtoGIntBig(pszOption);
855
856
0
    SetBand(nBandId, new MEMRasterBand(this, nBandId, pData, eType,
857
0
                                       nPixelOffset, nLineOffset, FALSE));
858
859
0
    return CE_None;
860
0
}
861
862
/************************************************************************/
863
/*                             AddMEMBand()                             */
864
/************************************************************************/
865
866
void MEMDataset::AddMEMBand(GDALRasterBandH hMEMBand)
867
0
{
868
0
    auto poBand = GDALRasterBand::FromHandle(hMEMBand);
869
0
    CPLAssert(dynamic_cast<MEMRasterBand *>(poBand) != nullptr);
870
0
    SetBand(1 + nBands, poBand);
871
0
}
872
873
/************************************************************************/
874
/*                          IBuildOverviews()                           */
875
/************************************************************************/
876
877
CPLErr MEMDataset::IBuildOverviews(const char *pszResampling, int nOverviews,
878
                                   const int *panOverviewList, int nListBands,
879
                                   const int *panBandList,
880
                                   GDALProgressFunc pfnProgress,
881
                                   void *pProgressData,
882
                                   CSLConstList papszOptions)
883
0
{
884
0
    if (nBands == 0)
885
0
    {
886
0
        CPLError(CE_Failure, CPLE_NotSupported, "Dataset has zero bands.");
887
0
        return CE_Failure;
888
0
    }
889
890
0
    if (nListBands != nBands)
891
0
    {
892
0
        CPLError(CE_Failure, CPLE_NotSupported,
893
0
                 "Generation of overviews in MEM only"
894
0
                 "supported when operating on all bands.");
895
0
        return CE_Failure;
896
0
    }
897
898
0
    if (nOverviews == 0)
899
0
    {
900
        // Cleanup existing overviews
901
0
        m_apoOverviewDS.clear();
902
0
        return CE_None;
903
0
    }
904
905
    /* -------------------------------------------------------------------- */
906
    /*      Force cascading. Help to get accurate results when masks are    */
907
    /*      involved.                                                       */
908
    /* -------------------------------------------------------------------- */
909
0
    if (nOverviews > 1 &&
910
0
        (STARTS_WITH_CI(pszResampling, "AVER") ||
911
0
         STARTS_WITH_CI(pszResampling, "GAUSS") ||
912
0
         EQUAL(pszResampling, "CUBIC") || EQUAL(pszResampling, "CUBICSPLINE") ||
913
0
         EQUAL(pszResampling, "LANCZOS") || EQUAL(pszResampling, "BILINEAR")))
914
0
    {
915
0
        double dfTotalPixels = 0;
916
0
        for (int i = 0; i < nOverviews; i++)
917
0
        {
918
0
            dfTotalPixels += static_cast<double>(nRasterXSize) * nRasterYSize /
919
0
                             (panOverviewList[i] * panOverviewList[i]);
920
0
        }
921
922
0
        double dfAccPixels = 0;
923
0
        for (int i = 0; i < nOverviews; i++)
924
0
        {
925
0
            double dfPixels = static_cast<double>(nRasterXSize) * nRasterYSize /
926
0
                              (panOverviewList[i] * panOverviewList[i]);
927
0
            void *pScaledProgress = GDALCreateScaledProgress(
928
0
                dfAccPixels / dfTotalPixels,
929
0
                (dfAccPixels + dfPixels) / dfTotalPixels, pfnProgress,
930
0
                pProgressData);
931
0
            CPLErr eErr = IBuildOverviews(
932
0
                pszResampling, 1, &panOverviewList[i], nListBands, panBandList,
933
0
                GDALScaledProgress, pScaledProgress, papszOptions);
934
0
            GDALDestroyScaledProgress(pScaledProgress);
935
0
            dfAccPixels += dfPixels;
936
0
            if (eErr == CE_Failure)
937
0
                return eErr;
938
0
        }
939
0
        return CE_None;
940
0
    }
941
942
    /* -------------------------------------------------------------------- */
943
    /*      Establish which of the overview levels we already have, and     */
944
    /*      which are new.                                                  */
945
    /* -------------------------------------------------------------------- */
946
0
    GDALRasterBand *poBand = GetRasterBand(1);
947
948
0
    for (int i = 0; i < nOverviews; i++)
949
0
    {
950
0
        bool bExisting = false;
951
0
        for (int j = 0; j < poBand->GetOverviewCount(); j++)
952
0
        {
953
0
            GDALRasterBand *poOverview = poBand->GetOverview(j);
954
0
            if (poOverview == nullptr)
955
0
                continue;
956
957
0
            int nOvFactor =
958
0
                GDALComputeOvFactor(poOverview->GetXSize(), poBand->GetXSize(),
959
0
                                    poOverview->GetYSize(), poBand->GetYSize());
960
961
0
            if (nOvFactor == panOverviewList[i] ||
962
0
                nOvFactor == GDALOvLevelAdjust2(panOverviewList[i],
963
0
                                                poBand->GetXSize(),
964
0
                                                poBand->GetYSize()))
965
0
            {
966
0
                bExisting = true;
967
0
                break;
968
0
            }
969
0
        }
970
971
        // Create new overview dataset if needed.
972
0
        if (!bExisting)
973
0
        {
974
0
            auto poOvrDS = std::make_unique<MEMDataset>();
975
0
            poOvrDS->eAccess = GA_Update;
976
0
            poOvrDS->nRasterXSize =
977
0
                DIV_ROUND_UP(nRasterXSize, panOverviewList[i]);
978
0
            poOvrDS->nRasterYSize =
979
0
                DIV_ROUND_UP(nRasterYSize, panOverviewList[i]);
980
0
            poOvrDS->bGeoTransformSet = bGeoTransformSet;
981
0
            poOvrDS->m_gt = m_gt;
982
0
            const double dfOvrXRatio =
983
0
                static_cast<double>(nRasterXSize) / poOvrDS->nRasterXSize;
984
0
            const double dfOvrYRatio =
985
0
                static_cast<double>(nRasterYSize) / poOvrDS->nRasterYSize;
986
0
            poOvrDS->m_gt.Rescale(dfOvrXRatio, dfOvrYRatio);
987
0
            poOvrDS->m_oSRS = m_oSRS;
988
0
            for (int iBand = 0; iBand < nBands; iBand++)
989
0
            {
990
0
                const GDALDataType eDT =
991
0
                    GetRasterBand(iBand + 1)->GetRasterDataType();
992
0
                if (poOvrDS->AddBand(eDT, nullptr) != CE_None)
993
0
                {
994
0
                    return CE_Failure;
995
0
                }
996
0
            }
997
0
            m_apoOverviewDS.emplace_back(poOvrDS.release());
998
0
        }
999
0
    }
1000
1001
    /* -------------------------------------------------------------------- */
1002
    /*      Build band list.                                                */
1003
    /* -------------------------------------------------------------------- */
1004
0
    GDALRasterBand **pahBands = static_cast<GDALRasterBand **>(
1005
0
        CPLCalloc(sizeof(GDALRasterBand *), nBands));
1006
0
    for (int i = 0; i < nBands; i++)
1007
0
        pahBands[i] = GetRasterBand(panBandList[i]);
1008
1009
    /* -------------------------------------------------------------------- */
1010
    /*      Refresh overviews that were listed.                             */
1011
    /* -------------------------------------------------------------------- */
1012
0
    GDALRasterBand **papoOverviewBands =
1013
0
        static_cast<GDALRasterBand **>(CPLCalloc(sizeof(void *), nOverviews));
1014
0
    GDALRasterBand **papoMaskOverviewBands =
1015
0
        static_cast<GDALRasterBand **>(CPLCalloc(sizeof(void *), nOverviews));
1016
1017
0
    CPLErr eErr = CE_None;
1018
0
    for (int iBand = 0; iBand < nBands && eErr == CE_None; iBand++)
1019
0
    {
1020
0
        poBand = GetRasterBand(panBandList[iBand]);
1021
1022
0
        int nNewOverviews = 0;
1023
0
        for (int i = 0; i < nOverviews; i++)
1024
0
        {
1025
0
            for (int j = 0; j < poBand->GetOverviewCount(); j++)
1026
0
            {
1027
0
                GDALRasterBand *poOverview = poBand->GetOverview(j);
1028
1029
0
                int bHasNoData = FALSE;
1030
0
                double noDataValue = poBand->GetNoDataValue(&bHasNoData);
1031
1032
0
                if (bHasNoData)
1033
0
                    poOverview->SetNoDataValue(noDataValue);
1034
1035
0
                const int nOvFactor = GDALComputeOvFactor(
1036
0
                    poOverview->GetXSize(), poBand->GetXSize(),
1037
0
                    poOverview->GetYSize(), poBand->GetYSize());
1038
1039
0
                if (nOvFactor == panOverviewList[i] ||
1040
0
                    nOvFactor == GDALOvLevelAdjust2(panOverviewList[i],
1041
0
                                                    poBand->GetXSize(),
1042
0
                                                    poBand->GetYSize()))
1043
0
                {
1044
0
                    papoOverviewBands[nNewOverviews++] = poOverview;
1045
0
                    break;
1046
0
                }
1047
0
            }
1048
0
        }
1049
1050
        // If the band has an explicit mask, we need to create overviews
1051
        // for it
1052
0
        MEMRasterBand *poMEMBand = cpl::down_cast<MEMRasterBand *>(poBand);
1053
0
        const bool bMustGenerateMaskOvr =
1054
0
            ((poMEMBand->poMask != nullptr && poMEMBand->poMask.IsOwned()) ||
1055
             // Or if it is a per-dataset mask, in which case just do it for the
1056
             // first band
1057
0
             ((poMEMBand->nMaskFlags & GMF_PER_DATASET) != 0 && iBand == 0)) &&
1058
0
            dynamic_cast<MEMRasterBand *>(poBand->GetMaskBand()) != nullptr;
1059
1060
0
        if (nNewOverviews > 0 && bMustGenerateMaskOvr)
1061
0
        {
1062
0
            for (int i = 0; i < nNewOverviews; i++)
1063
0
            {
1064
0
                MEMRasterBand *poMEMOvrBand =
1065
0
                    cpl::down_cast<MEMRasterBand *>(papoOverviewBands[i]);
1066
0
                if (!(poMEMOvrBand->poMask != nullptr &&
1067
0
                      poMEMOvrBand->poMask.IsOwned()) &&
1068
0
                    (poMEMOvrBand->nMaskFlags & GMF_PER_DATASET) == 0)
1069
0
                {
1070
0
                    poMEMOvrBand->CreateMaskBand(poMEMBand->nMaskFlags);
1071
0
                }
1072
0
                papoMaskOverviewBands[i] = poMEMOvrBand->GetMaskBand();
1073
0
            }
1074
1075
0
            void *pScaledProgress = GDALCreateScaledProgress(
1076
0
                1.0 * iBand / nBands, 1.0 * (iBand + 0.5) / nBands, pfnProgress,
1077
0
                pProgressData);
1078
1079
0
            MEMRasterBand *poMaskBand =
1080
0
                cpl::down_cast<MEMRasterBand *>(poBand->GetMaskBand());
1081
            // Make the mask band to be its own mask, similarly to what is
1082
            // done for alpha bands in GDALRegenerateOverviews() (#5640)
1083
0
            poMaskBand->InvalidateMaskBand();
1084
0
            poMaskBand->poMask.resetNotOwned(poMaskBand);
1085
0
            poMaskBand->nMaskFlags = 0;
1086
0
            eErr = GDALRegenerateOverviewsEx(
1087
0
                GDALRasterBand::ToHandle(poMaskBand), nNewOverviews,
1088
0
                reinterpret_cast<GDALRasterBandH *>(papoMaskOverviewBands),
1089
0
                pszResampling, GDALScaledProgress, pScaledProgress,
1090
0
                papszOptions);
1091
0
            poMaskBand->InvalidateMaskBand();
1092
0
            GDALDestroyScaledProgress(pScaledProgress);
1093
0
        }
1094
1095
        // Generate overview of bands *AFTER* mask overviews
1096
0
        if (nNewOverviews > 0 && eErr == CE_None)
1097
0
        {
1098
0
            void *pScaledProgress = GDALCreateScaledProgress(
1099
0
                1.0 * (iBand + (bMustGenerateMaskOvr ? 0.5 : 1)) / nBands,
1100
0
                1.0 * (iBand + 1) / nBands, pfnProgress, pProgressData);
1101
0
            eErr = GDALRegenerateOverviewsEx(
1102
0
                GDALRasterBand::ToHandle(poBand), nNewOverviews,
1103
0
                reinterpret_cast<GDALRasterBandH *>(papoOverviewBands),
1104
0
                pszResampling, GDALScaledProgress, pScaledProgress,
1105
0
                papszOptions);
1106
0
            GDALDestroyScaledProgress(pScaledProgress);
1107
0
        }
1108
0
    }
1109
1110
    /* -------------------------------------------------------------------- */
1111
    /*      Cleanup                                                         */
1112
    /* -------------------------------------------------------------------- */
1113
0
    CPLFree(papoOverviewBands);
1114
0
    CPLFree(papoMaskOverviewBands);
1115
0
    CPLFree(pahBands);
1116
1117
0
    return eErr;
1118
0
}
1119
1120
/************************************************************************/
1121
/*                           CreateMaskBand()                           */
1122
/************************************************************************/
1123
1124
CPLErr MEMDataset::CreateMaskBand(int nFlagsIn)
1125
0
{
1126
0
    GDALRasterBand *poFirstBand = GetRasterBand(1);
1127
0
    if (poFirstBand == nullptr)
1128
0
        return CE_Failure;
1129
0
    return poFirstBand->CreateMaskBand(nFlagsIn | GMF_PER_DATASET);
1130
0
}
1131
1132
/************************************************************************/
1133
/*                            CanBeCloned()                             */
1134
/************************************************************************/
1135
1136
/** Implements GDALDataset::CanBeCloned()
1137
 *
1138
 * This method is called by GDALThreadSafeDataset::Create() to determine if
1139
 * it is possible to create a thread-safe wrapper for a dataset, which involves
1140
 * the ability to Clone() it.
1141
 *
1142
 * The implementation of this method must be thread-safe.
1143
 */
1144
bool MEMDataset::CanBeCloned(int nScopeFlags, bool bCanShareState) const
1145
0
{
1146
0
    return nScopeFlags == GDAL_OF_RASTER && bCanShareState &&
1147
0
           typeid(this) == typeid(const MEMDataset *);
1148
0
}
1149
1150
/************************************************************************/
1151
/*                               Clone()                                */
1152
/************************************************************************/
1153
1154
/** Implements GDALDataset::Clone()
1155
 *
1156
 * This method returns a new instance, identical to "this", but which shares the
1157
 * same memory buffer as "this".
1158
 *
1159
 * The implementation of this method must be thread-safe.
1160
 */
1161
std::unique_ptr<GDALDataset> MEMDataset::Clone(int nScopeFlags,
1162
                                               bool bCanShareState) const
1163
0
{
1164
0
    if (MEMDataset::CanBeCloned(nScopeFlags, bCanShareState))
1165
0
    {
1166
0
        auto poNewDS = std::make_unique<MEMDataset>();
1167
0
        poNewDS->poDriver = poDriver;
1168
0
        poNewDS->nRasterXSize = nRasterXSize;
1169
0
        poNewDS->nRasterYSize = nRasterYSize;
1170
0
        poNewDS->bGeoTransformSet = bGeoTransformSet;
1171
0
        poNewDS->m_gt = m_gt;
1172
0
        poNewDS->m_oSRS = m_oSRS;
1173
0
        poNewDS->m_aoGCPs = m_aoGCPs;
1174
0
        poNewDS->m_oGCPSRS = m_oGCPSRS;
1175
0
        for (const auto &poOvrDS : m_apoOverviewDS)
1176
0
        {
1177
0
            poNewDS->m_apoOverviewDS.emplace_back(
1178
0
                poOvrDS->Clone(nScopeFlags, bCanShareState).release());
1179
0
        }
1180
1181
0
        poNewDS->SetDescription(GetDescription());
1182
0
        poNewDS->oMDMD = oMDMD;
1183
1184
        // Clone bands
1185
0
        for (int i = 1; i <= nBands; ++i)
1186
0
        {
1187
0
            auto poSrcMEMBand =
1188
0
                dynamic_cast<const MEMRasterBand *>(papoBands[i - 1]);
1189
0
            CPLAssert(poSrcMEMBand);
1190
0
            auto poNewBand = std::make_unique<MEMRasterBand>(
1191
0
                poNewDS.get(), i, poSrcMEMBand->pabyData,
1192
0
                poSrcMEMBand->GetRasterDataType(), poSrcMEMBand->nPixelOffset,
1193
0
                poSrcMEMBand->nLineOffset,
1194
0
                /* bAssumeOwnership = */ false);
1195
1196
0
            poNewBand->SetDescription(poSrcMEMBand->GetDescription());
1197
0
            poNewBand->oMDMD = poSrcMEMBand->oMDMD;
1198
1199
0
            if (poSrcMEMBand->psPam)
1200
0
            {
1201
0
                poNewBand->PamInitialize();
1202
0
                CPLAssert(poNewBand->psPam);
1203
0
                poNewBand->psPam->CopyFrom(*(poSrcMEMBand->psPam));
1204
0
            }
1205
1206
            // Instantiates a mask band when needed.
1207
0
            if ((poSrcMEMBand->nMaskFlags &
1208
0
                 (GMF_ALL_VALID | GMF_ALPHA | GMF_NODATA)) == 0)
1209
0
            {
1210
0
                auto poSrcMaskBand = dynamic_cast<const MEMRasterBand *>(
1211
0
                    poSrcMEMBand->poMask.get());
1212
0
                if (poSrcMaskBand)
1213
0
                {
1214
0
                    auto poMaskBand =
1215
0
                        std::unique_ptr<MEMRasterBand>(new MEMRasterBand(
1216
0
                            poSrcMaskBand->pabyData, GDT_UInt8, nRasterXSize,
1217
0
                            nRasterYSize, /* bOwnData = */ false));
1218
0
                    poMaskBand->m_bIsMask = true;
1219
0
                    poNewBand->poMask.reset(std::move(poMaskBand));
1220
0
                    poNewBand->nMaskFlags = poSrcMaskBand->nMaskFlags;
1221
0
                }
1222
0
            }
1223
1224
0
            poNewDS->SetBand(i, std::move(poNewBand));
1225
0
        }
1226
1227
0
        return poNewDS;
1228
0
    }
1229
0
    return GDALDataset::Clone(nScopeFlags, bCanShareState);
1230
0
}
1231
1232
/************************************************************************/
1233
/*                                Open()                                */
1234
/************************************************************************/
1235
1236
GDALDataset *MEMDataset::Open(GDALOpenInfo *poOpenInfo)
1237
1238
0
{
1239
    /* -------------------------------------------------------------------- */
1240
    /*      Do we have the special filename signature for MEM format        */
1241
    /*      description strings?                                            */
1242
    /* -------------------------------------------------------------------- */
1243
0
    if (!STARTS_WITH_CI(poOpenInfo->pszFilename, "MEM:::") ||
1244
0
        poOpenInfo->fpL != nullptr)
1245
0
        return nullptr;
1246
1247
0
#ifndef GDAL_MEM_ENABLE_OPEN
1248
0
    if (!CPLTestBool(CPLGetConfigOption("GDAL_MEM_ENABLE_OPEN", "NO")))
1249
0
    {
1250
0
        CPLError(CE_Failure, CPLE_AppDefined,
1251
0
                 "Opening a MEM dataset with the MEM:::DATAPOINTER= syntax "
1252
0
                 "is no longer supported by default for security reasons. "
1253
0
                 "If you want to allow it, define the "
1254
0
                 "GDAL_MEM_ENABLE_OPEN "
1255
0
                 "configuration option to YES, or build GDAL with the "
1256
0
                 "GDAL_MEM_ENABLE_OPEN compilation definition");
1257
0
        return nullptr;
1258
0
    }
1259
0
#endif
1260
1261
0
    const CPLStringList aosOptions(CSLTokenizeStringComplex(
1262
0
        poOpenInfo->pszFilename + 6, ",", TRUE, FALSE));
1263
1264
    /* -------------------------------------------------------------------- */
1265
    /*      Verify we have all required fields                              */
1266
    /* -------------------------------------------------------------------- */
1267
0
    if (aosOptions.FetchNameValue("PIXELS") == nullptr ||
1268
0
        aosOptions.FetchNameValue("LINES") == nullptr ||
1269
0
        aosOptions.FetchNameValue("DATAPOINTER") == nullptr)
1270
0
    {
1271
0
        CPLError(
1272
0
            CE_Failure, CPLE_AppDefined,
1273
0
            "Missing required field (one of PIXELS, LINES or DATAPOINTER).  "
1274
0
            "Unable to access in-memory array.");
1275
1276
0
        return nullptr;
1277
0
    }
1278
1279
    /* -------------------------------------------------------------------- */
1280
    /*      Create the new MEMDataset object.                               */
1281
    /* -------------------------------------------------------------------- */
1282
0
    auto poDS = std::make_unique<MEMDataset>();
1283
1284
0
    poDS->nRasterXSize = atoi(aosOptions.FetchNameValue("PIXELS"));
1285
0
    poDS->nRasterYSize = atoi(aosOptions.FetchNameValue("LINES"));
1286
0
    poDS->eAccess = poOpenInfo->eAccess;
1287
1288
    /* -------------------------------------------------------------------- */
1289
    /*      Extract other information.                                      */
1290
    /* -------------------------------------------------------------------- */
1291
0
    const char *pszOption = aosOptions.FetchNameValue("BANDS");
1292
0
    int nBands = 1;
1293
0
    if (pszOption != nullptr)
1294
0
    {
1295
0
        nBands = atoi(pszOption);
1296
0
    }
1297
1298
0
    if (!GDALCheckDatasetDimensions(poDS->nRasterXSize, poDS->nRasterYSize) ||
1299
0
        !GDALCheckBandCount(nBands, TRUE))
1300
0
    {
1301
0
        return nullptr;
1302
0
    }
1303
1304
0
    pszOption = aosOptions.FetchNameValue("DATATYPE");
1305
0
    GDALDataType eType = GDT_UInt8;
1306
0
    if (pszOption != nullptr)
1307
0
    {
1308
0
        if (atoi(pszOption) > 0 && atoi(pszOption) < GDT_TypeCount)
1309
0
            eType = static_cast<GDALDataType>(atoi(pszOption));
1310
0
        else
1311
0
        {
1312
0
            eType = GDALGetDataTypeByName(pszOption);
1313
0
            if (eType == GDT_Unknown)
1314
0
            {
1315
0
                CPLError(CE_Failure, CPLE_AppDefined,
1316
0
                         "DATATYPE=%s not recognised.", pszOption);
1317
0
                return nullptr;
1318
0
            }
1319
0
        }
1320
0
    }
1321
1322
0
    pszOption = aosOptions.FetchNameValue("PIXELOFFSET");
1323
0
    GSpacing nPixelOffset;
1324
0
    if (pszOption == nullptr)
1325
0
        nPixelOffset = GDALGetDataTypeSizeBytes(eType);
1326
0
    else
1327
0
        nPixelOffset =
1328
0
            CPLScanUIntBig(pszOption, static_cast<int>(strlen(pszOption)));
1329
1330
0
    pszOption = aosOptions.FetchNameValue("LINEOFFSET");
1331
0
    GSpacing nLineOffset = 0;
1332
0
    if (pszOption == nullptr)
1333
0
        nLineOffset = poDS->nRasterXSize * static_cast<size_t>(nPixelOffset);
1334
0
    else
1335
0
        nLineOffset =
1336
0
            CPLScanUIntBig(pszOption, static_cast<int>(strlen(pszOption)));
1337
1338
0
    pszOption = aosOptions.FetchNameValue("BANDOFFSET");
1339
0
    GSpacing nBandOffset = 0;
1340
0
    if (pszOption == nullptr)
1341
0
        nBandOffset = nLineOffset * static_cast<size_t>(poDS->nRasterYSize);
1342
0
    else
1343
0
        nBandOffset =
1344
0
            CPLScanUIntBig(pszOption, static_cast<int>(strlen(pszOption)));
1345
1346
0
    const char *pszDataPointer = aosOptions.FetchNameValue("DATAPOINTER");
1347
0
    GByte *pabyData = static_cast<GByte *>(CPLScanPointer(
1348
0
        pszDataPointer, static_cast<int>(strlen(pszDataPointer))));
1349
1350
    /* -------------------------------------------------------------------- */
1351
    /*      Create band information objects.                                */
1352
    /* -------------------------------------------------------------------- */
1353
0
    for (int iBand = 0; iBand < nBands; iBand++)
1354
0
    {
1355
0
        poDS->SetBand(iBand + 1,
1356
0
                      std::make_unique<MEMRasterBand>(
1357
0
                          poDS.get(), iBand + 1, pabyData + iBand * nBandOffset,
1358
0
                          eType, nPixelOffset, nLineOffset, FALSE));
1359
0
    }
1360
1361
    /* -------------------------------------------------------------------- */
1362
    /*      Set GeoTransform information.                                   */
1363
    /* -------------------------------------------------------------------- */
1364
1365
0
    pszOption = aosOptions.FetchNameValue("GEOTRANSFORM");
1366
0
    if (pszOption != nullptr)
1367
0
    {
1368
0
        const CPLStringList values(
1369
0
            CSLTokenizeStringComplex(pszOption, "/", TRUE, FALSE));
1370
0
        if (values.size() == 6)
1371
0
        {
1372
0
            GDALGeoTransform gt;
1373
0
            for (size_t i = 0; i < 6; ++i)
1374
0
            {
1375
0
                gt[i] = CPLScanDouble(values[i],
1376
0
                                      static_cast<int>(strlen(values[i])));
1377
0
            }
1378
0
            poDS->SetGeoTransform(gt);
1379
0
        }
1380
0
    }
1381
1382
    /* -------------------------------------------------------------------- */
1383
    /*      Set Projection Information                                      */
1384
    /* -------------------------------------------------------------------- */
1385
1386
0
    pszOption = aosOptions.FetchNameValue("SPATIALREFERENCE");
1387
0
    if (pszOption != nullptr)
1388
0
    {
1389
0
        poDS->m_oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
1390
0
        if (poDS->m_oSRS.SetFromUserInput(pszOption) != OGRERR_NONE)
1391
0
        {
1392
0
            CPLError(CE_Warning, CPLE_AppDefined, "Unrecognized crs: %s",
1393
0
                     pszOption);
1394
0
        }
1395
0
    }
1396
    /* -------------------------------------------------------------------- */
1397
    /*      Try to return a regular handle on the file.                     */
1398
    /* -------------------------------------------------------------------- */
1399
0
    return poDS.release();
1400
0
}
1401
1402
/************************************************************************/
1403
/*                               Create()                               */
1404
/************************************************************************/
1405
1406
MEMDataset *MEMDataset::Create(const char * /* pszFilename */, int nXSize,
1407
                               int nYSize, int nBandsIn, GDALDataType eType,
1408
                               CSLConstList papszOptions)
1409
0
{
1410
1411
    /* -------------------------------------------------------------------- */
1412
    /*      Do we want a pixel interleaved buffer?  I mostly care about     */
1413
    /*      this to test pixel interleaved IO in other contexts, but it     */
1414
    /*      could be useful to create a directly accessible buffer for      */
1415
    /*      some apps.                                                      */
1416
    /* -------------------------------------------------------------------- */
1417
0
    bool bPixelInterleaved = false;
1418
0
    const char *pszOption = CSLFetchNameValue(papszOptions, "INTERLEAVE");
1419
0
    if (pszOption && EQUAL(pszOption, "PIXEL"))
1420
0
        bPixelInterleaved = true;
1421
1422
    /* -------------------------------------------------------------------- */
1423
    /*      First allocate band data, verifying that we can get enough      */
1424
    /*      memory.                                                         */
1425
    /* -------------------------------------------------------------------- */
1426
0
    const int nWordSize = GDALGetDataTypeSizeBytes(eType);
1427
0
    if (nBandsIn > 0 && nWordSize > 0 &&
1428
0
        (nBandsIn > INT_MAX / nWordSize ||
1429
0
         static_cast<GIntBig>(nXSize) * nYSize >
1430
0
             GINTBIG_MAX / (nWordSize * nBandsIn)))
1431
0
    {
1432
0
        CPLError(CE_Failure, CPLE_OutOfMemory, "Multiplication overflow");
1433
0
        return nullptr;
1434
0
    }
1435
1436
0
    const GUIntBig nGlobalBigSize =
1437
0
        static_cast<GUIntBig>(nWordSize) * nBandsIn * nXSize * nYSize;
1438
0
    const size_t nGlobalSize = static_cast<size_t>(nGlobalBigSize);
1439
#if SIZEOF_VOIDP == 4
1440
    if (static_cast<GUIntBig>(nGlobalSize) != nGlobalBigSize)
1441
    {
1442
        CPLError(CE_Failure, CPLE_OutOfMemory,
1443
                 "Cannot allocate " CPL_FRMT_GUIB " bytes on this platform.",
1444
                 nGlobalBigSize);
1445
        return nullptr;
1446
    }
1447
#endif
1448
1449
0
    std::vector<GByte *> apbyBandData;
1450
0
    if (nBandsIn > 0)
1451
0
    {
1452
0
        GByte *pabyData =
1453
0
            static_cast<GByte *>(VSI_CALLOC_VERBOSE(1, nGlobalSize));
1454
0
        if (!pabyData)
1455
0
        {
1456
0
            return nullptr;
1457
0
        }
1458
1459
0
        if (bPixelInterleaved)
1460
0
        {
1461
0
            for (int iBand = 0; iBand < nBandsIn; iBand++)
1462
0
            {
1463
0
                apbyBandData.push_back(pabyData + iBand * nWordSize);
1464
0
            }
1465
0
        }
1466
0
        else
1467
0
        {
1468
0
            for (int iBand = 0; iBand < nBandsIn; iBand++)
1469
0
            {
1470
0
                apbyBandData.push_back(
1471
0
                    pabyData +
1472
0
                    (static_cast<size_t>(nWordSize) * nXSize * nYSize) * iBand);
1473
0
            }
1474
0
        }
1475
0
    }
1476
1477
    /* -------------------------------------------------------------------- */
1478
    /*      Create the new GTiffDataset object.                             */
1479
    /* -------------------------------------------------------------------- */
1480
0
    MEMDataset *poDS = new MEMDataset();
1481
1482
0
    poDS->nRasterXSize = nXSize;
1483
0
    poDS->nRasterYSize = nYSize;
1484
0
    poDS->eAccess = GA_Update;
1485
1486
0
    const char *pszPixelType = CSLFetchNameValue(papszOptions, "PIXELTYPE");
1487
0
    if (pszPixelType && EQUAL(pszPixelType, "SIGNEDBYTE"))
1488
0
        poDS->SetMetadataItem("PIXELTYPE", "SIGNEDBYTE", "IMAGE_STRUCTURE");
1489
1490
0
    if (nXSize != 0 && nYSize != 0)
1491
0
    {
1492
0
        if (bPixelInterleaved)
1493
0
            poDS->SetMetadataItem("INTERLEAVE", "PIXEL", "IMAGE_STRUCTURE");
1494
0
        else
1495
0
            poDS->SetMetadataItem("INTERLEAVE", "BAND", "IMAGE_STRUCTURE");
1496
0
    }
1497
1498
    /* -------------------------------------------------------------------- */
1499
    /*      Create band information objects.                                */
1500
    /* -------------------------------------------------------------------- */
1501
0
    for (int iBand = 0; iBand < nBandsIn; iBand++)
1502
0
    {
1503
0
        MEMRasterBand *poNewBand = nullptr;
1504
1505
0
        if (bPixelInterleaved)
1506
0
            poNewBand = new MEMRasterBand(
1507
0
                poDS, iBand + 1, apbyBandData[iBand], eType,
1508
0
                cpl::fits_on<int>(nWordSize * nBandsIn), 0, iBand == 0);
1509
0
        else
1510
0
            poNewBand = new MEMRasterBand(poDS, iBand + 1, apbyBandData[iBand],
1511
0
                                          eType, 0, 0, iBand == 0);
1512
1513
0
        if (const char *pszNBITS = CSLFetchNameValue(papszOptions, "NBITS"))
1514
0
        {
1515
0
            poNewBand->SetMetadataItem("NBITS", pszNBITS, "IMAGE_STRUCTURE");
1516
0
        }
1517
1518
0
        poDS->SetBand(iBand + 1, poNewBand);
1519
0
    }
1520
1521
    /* -------------------------------------------------------------------- */
1522
    /*      Try to return a regular handle on the file.                     */
1523
    /* -------------------------------------------------------------------- */
1524
0
    return poDS;
1525
0
}
1526
1527
GDALDataset *MEMDataset::CreateBase(const char *pszFilename, int nXSize,
1528
                                    int nYSize, int nBandsIn,
1529
                                    GDALDataType eType,
1530
                                    CSLConstList papszOptions)
1531
0
{
1532
0
    return Create(pszFilename, nXSize, nYSize, nBandsIn, eType, papszOptions);
1533
0
}
1534
1535
/************************************************************************/
1536
/*                        ~MEMAttributeHolder()                         */
1537
/************************************************************************/
1538
1539
0
MEMAttributeHolder::~MEMAttributeHolder() = default;
1540
1541
/************************************************************************/
1542
/*                          RenameAttribute()                           */
1543
/************************************************************************/
1544
1545
bool MEMAttributeHolder::RenameAttribute(const std::string &osOldName,
1546
                                         const std::string &osNewName)
1547
0
{
1548
0
    if (m_oMapAttributes.find(osNewName) != m_oMapAttributes.end())
1549
0
    {
1550
0
        CPLError(CE_Failure, CPLE_AppDefined,
1551
0
                 "An attribute with same name already exists");
1552
0
        return false;
1553
0
    }
1554
0
    auto oIter = m_oMapAttributes.find(osOldName);
1555
0
    if (oIter == m_oMapAttributes.end())
1556
0
    {
1557
0
        CPLAssert(false);
1558
0
        return false;
1559
0
    }
1560
0
    auto poAttr = std::move(oIter->second);
1561
0
    m_oMapAttributes.erase(oIter);
1562
0
    m_oMapAttributes[osNewName] = std::move(poAttr);
1563
0
    return true;
1564
0
}
1565
1566
/************************************************************************/
1567
/*                          GetMDArrayNames()                           */
1568
/************************************************************************/
1569
1570
std::vector<std::string> MEMGroup::GetMDArrayNames(CSLConstList) const
1571
0
{
1572
0
    if (!CheckValidAndErrorOutIfNot())
1573
0
        return {};
1574
0
    std::vector<std::string> names;
1575
0
    for (const auto &iter : m_oMapMDArrays)
1576
0
        names.push_back(iter.first);
1577
0
    return names;
1578
0
}
1579
1580
/************************************************************************/
1581
/*                            OpenMDArray()                             */
1582
/************************************************************************/
1583
1584
std::shared_ptr<GDALMDArray> MEMGroup::OpenMDArray(const std::string &osName,
1585
                                                   CSLConstList) const
1586
0
{
1587
0
    if (!CheckValidAndErrorOutIfNot())
1588
0
        return nullptr;
1589
0
    auto oIter = m_oMapMDArrays.find(osName);
1590
0
    if (oIter != m_oMapMDArrays.end())
1591
0
        return oIter->second;
1592
0
    return nullptr;
1593
0
}
1594
1595
/************************************************************************/
1596
/*                           GetGroupNames()                            */
1597
/************************************************************************/
1598
1599
std::vector<std::string> MEMGroup::GetGroupNames(CSLConstList) const
1600
0
{
1601
0
    if (!CheckValidAndErrorOutIfNot())
1602
0
        return {};
1603
0
    std::vector<std::string> names;
1604
0
    for (const auto &iter : m_oMapGroups)
1605
0
        names.push_back(iter.first);
1606
0
    return names;
1607
0
}
1608
1609
/************************************************************************/
1610
/*                             OpenGroup()                              */
1611
/************************************************************************/
1612
1613
std::shared_ptr<GDALGroup> MEMGroup::OpenGroup(const std::string &osName,
1614
                                               CSLConstList) const
1615
0
{
1616
0
    if (!CheckValidAndErrorOutIfNot())
1617
0
        return nullptr;
1618
0
    auto oIter = m_oMapGroups.find(osName);
1619
0
    if (oIter != m_oMapGroups.end())
1620
0
        return oIter->second;
1621
0
    return nullptr;
1622
0
}
1623
1624
/************************************************************************/
1625
/*                               Create()                               */
1626
/************************************************************************/
1627
1628
/*static*/
1629
std::shared_ptr<MEMGroup> MEMGroup::Create(const std::string &osParentName,
1630
                                           const char *pszName)
1631
0
{
1632
0
    auto newGroup(
1633
0
        std::shared_ptr<MEMGroup>(new MEMGroup(osParentName, pszName)));
1634
0
    newGroup->SetSelf(newGroup);
1635
0
    if (osParentName.empty())
1636
0
        newGroup->m_poRootGroupWeak = newGroup;
1637
0
    return newGroup;
1638
0
}
1639
1640
/************************************************************************/
1641
/*                            CreateGroup()                             */
1642
/************************************************************************/
1643
1644
std::shared_ptr<GDALGroup> MEMGroup::CreateGroup(const std::string &osName,
1645
                                                 CSLConstList /*papszOptions*/)
1646
0
{
1647
0
    if (!CheckValidAndErrorOutIfNot())
1648
0
        return nullptr;
1649
0
    if (osName.empty())
1650
0
    {
1651
0
        CPLError(CE_Failure, CPLE_NotSupported,
1652
0
                 "Empty group name not supported");
1653
0
        return nullptr;
1654
0
    }
1655
0
    if (m_oMapGroups.find(osName) != m_oMapGroups.end())
1656
0
    {
1657
0
        CPLError(CE_Failure, CPLE_AppDefined,
1658
0
                 "A group with same name already exists");
1659
0
        return nullptr;
1660
0
    }
1661
0
    auto newGroup = MEMGroup::Create(GetFullName(), osName.c_str());
1662
0
    newGroup->m_pParent = std::dynamic_pointer_cast<MEMGroup>(m_pSelf.lock());
1663
0
    newGroup->m_poRootGroupWeak = m_poRootGroupWeak;
1664
0
    m_oMapGroups[osName] = newGroup;
1665
0
    return newGroup;
1666
0
}
1667
1668
/************************************************************************/
1669
/*                            DeleteGroup()                             */
1670
/************************************************************************/
1671
1672
bool MEMGroup::DeleteGroup(const std::string &osName,
1673
                           CSLConstList /*papszOptions*/)
1674
0
{
1675
0
    if (!CheckValidAndErrorOutIfNot())
1676
0
        return false;
1677
0
    auto oIter = m_oMapGroups.find(osName);
1678
0
    if (oIter == m_oMapGroups.end())
1679
0
    {
1680
0
        CPLError(CE_Failure, CPLE_AppDefined,
1681
0
                 "Group %s is not a sub-group of this group", osName.c_str());
1682
0
        return false;
1683
0
    }
1684
1685
0
    oIter->second->Deleted();
1686
0
    m_oMapGroups.erase(oIter);
1687
0
    return true;
1688
0
}
1689
1690
/************************************************************************/
1691
/*                      NotifyChildrenOfDeletion()                      */
1692
/************************************************************************/
1693
1694
void MEMGroup::NotifyChildrenOfDeletion()
1695
0
{
1696
0
    for (const auto &oIter : m_oMapGroups)
1697
0
        oIter.second->ParentDeleted();
1698
0
    for (const auto &oIter : m_oMapMDArrays)
1699
0
        oIter.second->ParentDeleted();
1700
0
    for (const auto &oIter : m_oMapAttributes)
1701
0
        oIter.second->ParentDeleted();
1702
0
    for (const auto &oIter : m_oMapDimensions)
1703
0
        oIter.second->ParentDeleted();
1704
0
}
1705
1706
/************************************************************************/
1707
/*                           CreateMDArray()                            */
1708
/************************************************************************/
1709
1710
std::shared_ptr<GDALMDArray> MEMGroup::CreateMDArray(
1711
    const std::string &osName,
1712
    const std::vector<std::shared_ptr<GDALDimension>> &aoDimensions,
1713
    const GDALExtendedDataType &oType, void *pData, CSLConstList papszOptions)
1714
0
{
1715
0
    if (!CheckValidAndErrorOutIfNot())
1716
0
        return nullptr;
1717
0
    if (osName.empty())
1718
0
    {
1719
0
        CPLError(CE_Failure, CPLE_NotSupported,
1720
0
                 "Empty array name not supported");
1721
0
        return nullptr;
1722
0
    }
1723
0
    if (m_oMapMDArrays.find(osName) != m_oMapMDArrays.end())
1724
0
    {
1725
0
        CPLError(CE_Failure, CPLE_AppDefined,
1726
0
                 "An array with same name already exists");
1727
0
        return nullptr;
1728
0
    }
1729
0
    auto newArray(
1730
0
        MEMMDArray::Create(GetFullName(), osName, aoDimensions, oType));
1731
1732
0
    GByte *pabyData = nullptr;
1733
0
    std::vector<GPtrDiff_t> anStrides;
1734
0
    if (pData)
1735
0
    {
1736
0
        pabyData = static_cast<GByte *>(pData);
1737
0
        const char *pszStrides = CSLFetchNameValue(papszOptions, "STRIDES");
1738
0
        if (pszStrides)
1739
0
        {
1740
0
            CPLStringList aosStrides(CSLTokenizeString2(pszStrides, ",", 0));
1741
0
            if (static_cast<size_t>(aosStrides.size()) != aoDimensions.size())
1742
0
            {
1743
0
                CPLError(CE_Failure, CPLE_AppDefined,
1744
0
                         "Invalid number of strides");
1745
0
                return nullptr;
1746
0
            }
1747
0
            for (int i = 0; i < aosStrides.size(); i++)
1748
0
            {
1749
0
                const auto nStride = CPLAtoGIntBig(aosStrides[i]);
1750
0
                anStrides.push_back(static_cast<GPtrDiff_t>(nStride));
1751
0
            }
1752
0
        }
1753
0
    }
1754
0
    if (!newArray->Init(pabyData, anStrides))
1755
0
        return nullptr;
1756
1757
0
    for (auto &poDim : newArray->GetDimensions())
1758
0
    {
1759
0
        const auto dim = std::dynamic_pointer_cast<MEMDimension>(poDim);
1760
0
        if (dim)
1761
0
            dim->RegisterUsingArray(newArray.get());
1762
0
    }
1763
1764
0
    newArray->RegisterGroup(m_pSelf);
1765
0
    m_oMapMDArrays[osName] = newArray;
1766
0
    return newArray;
1767
0
}
1768
1769
std::shared_ptr<GDALMDArray> MEMGroup::CreateMDArray(
1770
    const std::string &osName,
1771
    const std::vector<std::shared_ptr<GDALDimension>> &aoDimensions,
1772
    const GDALExtendedDataType &oType, CSLConstList papszOptions)
1773
0
{
1774
0
    void *pData = nullptr;
1775
0
    const char *pszDataPointer = CSLFetchNameValue(papszOptions, "DATAPOINTER");
1776
0
    if (pszDataPointer)
1777
0
    {
1778
        // Will not work on architectures with "capability pointers"
1779
0
        pData = CPLScanPointer(pszDataPointer,
1780
0
                               static_cast<int>(strlen(pszDataPointer)));
1781
0
    }
1782
0
    return CreateMDArray(osName, aoDimensions, oType, pData, papszOptions);
1783
0
}
1784
1785
/************************************************************************/
1786
/*                           DeleteMDArray()                            */
1787
/************************************************************************/
1788
1789
bool MEMGroup::DeleteMDArray(const std::string &osName,
1790
                             CSLConstList /*papszOptions*/)
1791
0
{
1792
0
    if (!CheckValidAndErrorOutIfNot())
1793
0
        return false;
1794
0
    auto oIter = m_oMapMDArrays.find(osName);
1795
0
    if (oIter == m_oMapMDArrays.end())
1796
0
    {
1797
0
        CPLError(CE_Failure, CPLE_AppDefined,
1798
0
                 "Array %s is not an array of this group", osName.c_str());
1799
0
        return false;
1800
0
    }
1801
1802
0
    oIter->second->Deleted();
1803
0
    m_oMapMDArrays.erase(oIter);
1804
0
    return true;
1805
0
}
1806
1807
/************************************************************************/
1808
/*                       MEMGroupCreateMDArray()                        */
1809
/************************************************************************/
1810
1811
// Used by NUMPYMultiDimensionalDataset
1812
std::shared_ptr<GDALMDArray> MEMGroupCreateMDArray(
1813
    GDALGroup *poGroup, const std::string &osName,
1814
    const std::vector<std::shared_ptr<GDALDimension>> &aoDimensions,
1815
    const GDALExtendedDataType &oDataType, void *pData,
1816
    CSLConstList papszOptions)
1817
0
{
1818
0
    auto poMemGroup = dynamic_cast<MEMGroup *>(poGroup);
1819
0
    if (!poMemGroup)
1820
0
    {
1821
0
        CPLError(CE_Failure, CPLE_AppDefined,
1822
0
                 "MEMGroupCreateMDArray(): poGroup not of type MEMGroup");
1823
0
        return nullptr;
1824
0
    }
1825
0
    return poMemGroup->CreateMDArray(osName, aoDimensions, oDataType, pData,
1826
0
                                     papszOptions);
1827
0
}
1828
1829
/************************************************************************/
1830
/*                            GetAttribute()                            */
1831
/************************************************************************/
1832
1833
std::shared_ptr<GDALAttribute>
1834
MEMGroup::GetAttribute(const std::string &osName) const
1835
0
{
1836
0
    if (!CheckValidAndErrorOutIfNot())
1837
0
        return nullptr;
1838
0
    auto oIter = m_oMapAttributes.find(osName);
1839
0
    if (oIter != m_oMapAttributes.end())
1840
0
        return oIter->second;
1841
0
    return nullptr;
1842
0
}
1843
1844
/************************************************************************/
1845
/*                           GetAttributes()                            */
1846
/************************************************************************/
1847
1848
std::vector<std::shared_ptr<GDALAttribute>>
1849
MEMGroup::GetAttributes(CSLConstList) const
1850
0
{
1851
0
    if (!CheckValidAndErrorOutIfNot())
1852
0
        return {};
1853
0
    std::vector<std::shared_ptr<GDALAttribute>> oRes;
1854
0
    for (const auto &oIter : m_oMapAttributes)
1855
0
    {
1856
0
        oRes.push_back(oIter.second);
1857
0
    }
1858
0
    return oRes;
1859
0
}
1860
1861
/************************************************************************/
1862
/*                           GetDimensions()                            */
1863
/************************************************************************/
1864
1865
std::vector<std::shared_ptr<GDALDimension>>
1866
MEMGroup::GetDimensions(CSLConstList) const
1867
0
{
1868
0
    if (!CheckValidAndErrorOutIfNot())
1869
0
        return {};
1870
0
    std::vector<std::shared_ptr<GDALDimension>> oRes;
1871
0
    for (const auto &oIter : m_oMapDimensions)
1872
0
    {
1873
0
        oRes.push_back(oIter.second);
1874
0
    }
1875
0
    return oRes;
1876
0
}
1877
1878
/************************************************************************/
1879
/*                          CreateAttribute()                           */
1880
/************************************************************************/
1881
1882
std::shared_ptr<GDALAttribute>
1883
MEMGroup::CreateAttribute(const std::string &osName,
1884
                          const std::vector<GUInt64> &anDimensions,
1885
                          const GDALExtendedDataType &oDataType, CSLConstList)
1886
0
{
1887
0
    if (!CheckValidAndErrorOutIfNot())
1888
0
        return nullptr;
1889
0
    if (osName.empty())
1890
0
    {
1891
0
        CPLError(CE_Failure, CPLE_NotSupported,
1892
0
                 "Empty attribute name not supported");
1893
0
        return nullptr;
1894
0
    }
1895
0
    if (m_oMapAttributes.find(osName) != m_oMapAttributes.end())
1896
0
    {
1897
0
        CPLError(CE_Failure, CPLE_AppDefined,
1898
0
                 "An attribute with same name already exists");
1899
0
        return nullptr;
1900
0
    }
1901
0
    auto newAttr(MEMAttribute::Create(
1902
0
        std::dynamic_pointer_cast<MEMGroup>(m_pSelf.lock()), osName,
1903
0
        anDimensions, oDataType));
1904
0
    if (!newAttr)
1905
0
        return nullptr;
1906
0
    m_oMapAttributes[osName] = newAttr;
1907
0
    return newAttr;
1908
0
}
1909
1910
/************************************************************************/
1911
/*                          DeleteAttribute()                           */
1912
/************************************************************************/
1913
1914
bool MEMGroup::DeleteAttribute(const std::string &osName,
1915
                               CSLConstList /*papszOptions*/)
1916
0
{
1917
0
    if (!CheckValidAndErrorOutIfNot())
1918
0
        return false;
1919
0
    auto oIter = m_oMapAttributes.find(osName);
1920
0
    if (oIter == m_oMapAttributes.end())
1921
0
    {
1922
0
        CPLError(CE_Failure, CPLE_AppDefined,
1923
0
                 "Attribute %s is not an attribute of this group",
1924
0
                 osName.c_str());
1925
0
        return false;
1926
0
    }
1927
1928
0
    oIter->second->Deleted();
1929
0
    m_oMapAttributes.erase(oIter);
1930
0
    return true;
1931
0
}
1932
1933
/************************************************************************/
1934
/*                               Rename()                               */
1935
/************************************************************************/
1936
1937
bool MEMGroup::Rename(const std::string &osNewName)
1938
0
{
1939
0
    if (!CheckValidAndErrorOutIfNot())
1940
0
        return false;
1941
0
    if (osNewName.empty())
1942
0
    {
1943
0
        CPLError(CE_Failure, CPLE_NotSupported, "Empty name not supported");
1944
0
        return false;
1945
0
    }
1946
0
    if (m_osName == "/")
1947
0
    {
1948
0
        CPLError(CE_Failure, CPLE_NotSupported, "Cannot rename root group");
1949
0
        return false;
1950
0
    }
1951
0
    auto pParent = m_pParent.lock();
1952
0
    if (pParent)
1953
0
    {
1954
0
        if (pParent->m_oMapGroups.find(osNewName) !=
1955
0
            pParent->m_oMapGroups.end())
1956
0
        {
1957
0
            CPLError(CE_Failure, CPLE_AppDefined,
1958
0
                     "A group with same name already exists");
1959
0
            return false;
1960
0
        }
1961
0
        pParent->m_oMapGroups.erase(pParent->m_oMapGroups.find(m_osName));
1962
0
    }
1963
1964
0
    BaseRename(osNewName);
1965
1966
0
    if (pParent)
1967
0
    {
1968
0
        CPLAssert(m_pSelf.lock());
1969
0
        pParent->m_oMapGroups[m_osName] = m_pSelf.lock();
1970
0
    }
1971
1972
0
    return true;
1973
0
}
1974
1975
/************************************************************************/
1976
/*                      NotifyChildrenOfRenaming()                      */
1977
/************************************************************************/
1978
1979
void MEMGroup::NotifyChildrenOfRenaming()
1980
0
{
1981
0
    for (const auto &oIter : m_oMapGroups)
1982
0
        oIter.second->ParentRenamed(m_osFullName);
1983
0
    for (const auto &oIter : m_oMapMDArrays)
1984
0
        oIter.second->ParentRenamed(m_osFullName);
1985
0
    for (const auto &oIter : m_oMapAttributes)
1986
0
        oIter.second->ParentRenamed(m_osFullName);
1987
0
    for (const auto &oIter : m_oMapDimensions)
1988
0
        oIter.second->ParentRenamed(m_osFullName);
1989
0
}
1990
1991
/************************************************************************/
1992
/*                          RenameDimension()                           */
1993
/************************************************************************/
1994
1995
bool MEMGroup::RenameDimension(const std::string &osOldName,
1996
                               const std::string &osNewName)
1997
0
{
1998
0
    if (m_oMapDimensions.find(osNewName) != m_oMapDimensions.end())
1999
0
    {
2000
0
        CPLError(CE_Failure, CPLE_AppDefined,
2001
0
                 "A dimension with same name already exists");
2002
0
        return false;
2003
0
    }
2004
0
    auto oIter = m_oMapDimensions.find(osOldName);
2005
0
    if (oIter == m_oMapDimensions.end())
2006
0
    {
2007
0
        CPLAssert(false);
2008
0
        return false;
2009
0
    }
2010
0
    auto poDim = std::move(oIter->second);
2011
0
    m_oMapDimensions.erase(oIter);
2012
0
    m_oMapDimensions[osNewName] = std::move(poDim);
2013
0
    return true;
2014
0
}
2015
2016
/************************************************************************/
2017
/*                            RenameArray()                             */
2018
/************************************************************************/
2019
2020
bool MEMGroup::RenameArray(const std::string &osOldName,
2021
                           const std::string &osNewName)
2022
0
{
2023
0
    if (m_oMapMDArrays.find(osNewName) != m_oMapMDArrays.end())
2024
0
    {
2025
0
        CPLError(CE_Failure, CPLE_AppDefined,
2026
0
                 "An array with same name already exists");
2027
0
        return false;
2028
0
    }
2029
0
    auto oIter = m_oMapMDArrays.find(osOldName);
2030
0
    if (oIter == m_oMapMDArrays.end())
2031
0
    {
2032
0
        CPLAssert(false);
2033
0
        return false;
2034
0
    }
2035
0
    auto poArray = std::move(oIter->second);
2036
0
    m_oMapMDArrays.erase(oIter);
2037
0
    m_oMapMDArrays[osNewName] = std::move(poArray);
2038
0
    return true;
2039
0
}
2040
2041
/************************************************************************/
2042
/*                         MEMAbstractMDArray()                         */
2043
/************************************************************************/
2044
2045
MEMAbstractMDArray::MEMAbstractMDArray(
2046
    const std::string &osParentName, const std::string &osName,
2047
    const std::vector<std::shared_ptr<GDALDimension>> &aoDimensions,
2048
    const GDALExtendedDataType &oType)
2049
0
    : GDALAbstractMDArray(osParentName, osName), m_aoDims(aoDimensions),
2050
0
      m_oType(oType)
2051
0
{
2052
0
}
Unexecuted instantiation: MEMAbstractMDArray::MEMAbstractMDArray(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::vector<std::__1::shared_ptr<GDALDimension>, std::__1::allocator<std::__1::shared_ptr<GDALDimension> > > const&, GDALExtendedDataType const&)
Unexecuted instantiation: MEMAbstractMDArray::MEMAbstractMDArray(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::vector<std::__1::shared_ptr<GDALDimension>, std::__1::allocator<std::__1::shared_ptr<GDALDimension> > > const&, GDALExtendedDataType const&)
2053
2054
/************************************************************************/
2055
/*                        ~MEMAbstractMDArray()                         */
2056
/************************************************************************/
2057
2058
MEMAbstractMDArray::~MEMAbstractMDArray()
2059
0
{
2060
0
    FreeArray();
2061
0
}
2062
2063
/************************************************************************/
2064
/*                             FreeArray()                              */
2065
/************************************************************************/
2066
2067
void MEMAbstractMDArray::FreeArray()
2068
0
{
2069
0
    if (m_bOwnArray)
2070
0
    {
2071
0
        if (m_oType.NeedsFreeDynamicMemory())
2072
0
        {
2073
0
            GByte *pabyPtr = m_pabyArray;
2074
0
            GByte *pabyEnd = m_pabyArray + m_nTotalSize;
2075
0
            const auto nDTSize(m_oType.GetSize());
2076
0
            while (pabyPtr < pabyEnd)
2077
0
            {
2078
0
                m_oType.FreeDynamicMemory(pabyPtr);
2079
0
                pabyPtr += nDTSize;
2080
0
            }
2081
0
        }
2082
0
        VSIFree(m_pabyArray);
2083
0
        m_pabyArray = nullptr;
2084
0
        m_nTotalSize = 0;
2085
0
        m_bOwnArray = false;
2086
0
    }
2087
0
}
2088
2089
/************************************************************************/
2090
/*                                Init()                                */
2091
/************************************************************************/
2092
2093
bool MEMAbstractMDArray::Init(GByte *pData,
2094
                              const std::vector<GPtrDiff_t> &anStrides)
2095
0
{
2096
0
    GUInt64 nTotalSize = m_oType.GetSize();
2097
0
    if (!m_aoDims.empty())
2098
0
    {
2099
0
        if (anStrides.empty())
2100
0
        {
2101
0
            m_anStrides.resize(m_aoDims.size());
2102
0
        }
2103
0
        else
2104
0
        {
2105
0
            CPLAssert(anStrides.size() == m_aoDims.size());
2106
0
            m_anStrides = anStrides;
2107
0
        }
2108
2109
        // To compute strides we must proceed from the fastest varying dimension
2110
        // (the last one), and then reverse the result
2111
0
        for (size_t i = m_aoDims.size(); i != 0;)
2112
0
        {
2113
0
            --i;
2114
0
            const auto &poDim = m_aoDims[i];
2115
0
            auto nDimSize = poDim->GetSize();
2116
0
            if (nDimSize == 0)
2117
0
            {
2118
0
                CPLError(CE_Failure, CPLE_IllegalArg,
2119
0
                         "Illegal dimension size 0");
2120
0
                return false;
2121
0
            }
2122
0
            if (nTotalSize > std::numeric_limits<GUInt64>::max() / nDimSize)
2123
0
            {
2124
0
                CPLError(CE_Failure, CPLE_OutOfMemory, "Too big allocation");
2125
0
                return false;
2126
0
            }
2127
0
            auto nNewSize = nTotalSize * nDimSize;
2128
0
            if (anStrides.empty())
2129
0
                m_anStrides[i] = static_cast<size_t>(nTotalSize);
2130
0
            nTotalSize = nNewSize;
2131
0
        }
2132
0
    }
2133
2134
    // We restrict the size of the allocation so that all elements can be
2135
    // indexed by GPtrDiff_t
2136
0
    if (nTotalSize >
2137
0
        static_cast<size_t>(std::numeric_limits<GPtrDiff_t>::max()))
2138
0
    {
2139
0
        CPLError(CE_Failure, CPLE_OutOfMemory, "Too big allocation");
2140
0
        return false;
2141
0
    }
2142
0
    m_nTotalSize = static_cast<size_t>(nTotalSize);
2143
0
    if (pData)
2144
0
    {
2145
0
        m_pabyArray = pData;
2146
0
    }
2147
0
    else
2148
0
    {
2149
0
        m_pabyArray = static_cast<GByte *>(VSI_CALLOC_VERBOSE(1, m_nTotalSize));
2150
0
        m_bOwnArray = true;
2151
0
    }
2152
2153
0
    return m_pabyArray != nullptr;
2154
0
}
2155
2156
/************************************************************************/
2157
/*                              FastCopy()                              */
2158
/************************************************************************/
2159
2160
template <int N>
2161
inline static void FastCopy(size_t nIters, GByte *dstPtr, const GByte *srcPtr,
2162
                            GPtrDiff_t dst_inc_offset,
2163
                            GPtrDiff_t src_inc_offset)
2164
0
{
2165
0
    if (nIters >= 8)
2166
0
    {
2167
0
#define COPY_ELT(i)                                                            \
2168
0
    memcpy(dstPtr + (i) * dst_inc_offset, srcPtr + (i) * src_inc_offset, N)
2169
0
        while (true)
2170
0
        {
2171
0
            COPY_ELT(0);
2172
0
            COPY_ELT(1);
2173
0
            COPY_ELT(2);
2174
0
            COPY_ELT(3);
2175
0
            COPY_ELT(4);
2176
0
            COPY_ELT(5);
2177
0
            COPY_ELT(6);
2178
0
            COPY_ELT(7);
2179
0
            nIters -= 8;
2180
0
            srcPtr += 8 * src_inc_offset;
2181
0
            dstPtr += 8 * dst_inc_offset;
2182
0
            if (nIters < 8)
2183
0
                break;
2184
0
        }
2185
0
        if (nIters == 0)
2186
0
            return;
2187
0
    }
2188
0
    while (true)
2189
0
    {
2190
0
        memcpy(dstPtr, srcPtr, N);
2191
0
        if ((--nIters) == 0)
2192
0
            break;
2193
0
        srcPtr += src_inc_offset;
2194
0
        dstPtr += dst_inc_offset;
2195
0
    }
2196
0
}
Unexecuted instantiation: memdataset.cpp:void FastCopy<1>(unsigned long, unsigned char*, unsigned char const*, long long, long long)
Unexecuted instantiation: memdataset.cpp:void FastCopy<2>(unsigned long, unsigned char*, unsigned char const*, long long, long long)
Unexecuted instantiation: memdataset.cpp:void FastCopy<4>(unsigned long, unsigned char*, unsigned char const*, long long, long long)
Unexecuted instantiation: memdataset.cpp:void FastCopy<8>(unsigned long, unsigned char*, unsigned char const*, long long, long long)
Unexecuted instantiation: memdataset.cpp:void FastCopy<16>(unsigned long, unsigned char*, unsigned char const*, long long, long long)
2197
2198
/************************************************************************/
2199
/*                             ReadWrite()                              */
2200
/************************************************************************/
2201
2202
void MEMAbstractMDArray::ReadWrite(bool bIsWrite, const size_t *count,
2203
                                   std::vector<StackReadWrite> &stack,
2204
                                   const GDALExtendedDataType &srcType,
2205
                                   const GDALExtendedDataType &dstType) const
2206
0
{
2207
0
    const auto nDims = m_aoDims.size();
2208
0
    const auto nDimsMinus1 = nDims - 1;
2209
0
    const bool bBothAreNumericDT = srcType.GetClass() == GEDTC_NUMERIC &&
2210
0
                                   dstType.GetClass() == GEDTC_NUMERIC;
2211
0
    const bool bSameNumericDT =
2212
0
        bBothAreNumericDT &&
2213
0
        srcType.GetNumericDataType() == dstType.GetNumericDataType();
2214
0
    const auto nSameDTSize = bSameNumericDT ? srcType.GetSize() : 0;
2215
0
    const bool bCanUseMemcpyLastDim =
2216
0
        bSameNumericDT &&
2217
0
        stack[nDimsMinus1].src_inc_offset ==
2218
0
            static_cast<GPtrDiff_t>(nSameDTSize) &&
2219
0
        stack[nDimsMinus1].dst_inc_offset ==
2220
0
            static_cast<GPtrDiff_t>(nSameDTSize);
2221
0
    const size_t nCopySizeLastDim =
2222
0
        bCanUseMemcpyLastDim ? nSameDTSize * count[nDimsMinus1] : 0;
2223
0
    const bool bNeedsFreeDynamicMemory =
2224
0
        bIsWrite && dstType.NeedsFreeDynamicMemory();
2225
2226
0
    auto lambdaLastDim = [&](size_t idxPtr)
2227
0
    {
2228
0
        auto srcPtr = stack[idxPtr].src_ptr;
2229
0
        auto dstPtr = stack[idxPtr].dst_ptr;
2230
0
        if (nCopySizeLastDim)
2231
0
        {
2232
0
            memcpy(dstPtr, srcPtr, nCopySizeLastDim);
2233
0
        }
2234
0
        else
2235
0
        {
2236
0
            size_t nIters = count[nDimsMinus1];
2237
0
            const auto dst_inc_offset = stack[nDimsMinus1].dst_inc_offset;
2238
0
            const auto src_inc_offset = stack[nDimsMinus1].src_inc_offset;
2239
0
            if (bSameNumericDT)
2240
0
            {
2241
0
                if (nSameDTSize == 1)
2242
0
                {
2243
0
                    FastCopy<1>(nIters, dstPtr, srcPtr, dst_inc_offset,
2244
0
                                src_inc_offset);
2245
0
                    return;
2246
0
                }
2247
0
                if (nSameDTSize == 2)
2248
0
                {
2249
0
                    FastCopy<2>(nIters, dstPtr, srcPtr, dst_inc_offset,
2250
0
                                src_inc_offset);
2251
0
                    return;
2252
0
                }
2253
0
                if (nSameDTSize == 4)
2254
0
                {
2255
0
                    FastCopy<4>(nIters, dstPtr, srcPtr, dst_inc_offset,
2256
0
                                src_inc_offset);
2257
0
                    return;
2258
0
                }
2259
0
                if (nSameDTSize == 8)
2260
0
                {
2261
0
                    FastCopy<8>(nIters, dstPtr, srcPtr, dst_inc_offset,
2262
0
                                src_inc_offset);
2263
0
                    return;
2264
0
                }
2265
0
                if (nSameDTSize == 16)
2266
0
                {
2267
0
                    FastCopy<16>(nIters, dstPtr, srcPtr, dst_inc_offset,
2268
0
                                 src_inc_offset);
2269
0
                    return;
2270
0
                }
2271
0
                CPLAssert(false);
2272
0
            }
2273
0
            else if (bBothAreNumericDT
2274
0
#if SIZEOF_VOIDP >= 8
2275
0
                     && src_inc_offset <= std::numeric_limits<int>::max() &&
2276
0
                     dst_inc_offset <= std::numeric_limits<int>::max()
2277
0
#endif
2278
0
            )
2279
0
            {
2280
0
                GDALCopyWords64(srcPtr, srcType.GetNumericDataType(),
2281
0
                                static_cast<int>(src_inc_offset), dstPtr,
2282
0
                                dstType.GetNumericDataType(),
2283
0
                                static_cast<int>(dst_inc_offset),
2284
0
                                static_cast<GPtrDiff_t>(nIters));
2285
0
                return;
2286
0
            }
2287
2288
0
            while (true)
2289
0
            {
2290
0
                if (bNeedsFreeDynamicMemory)
2291
0
                {
2292
0
                    dstType.FreeDynamicMemory(dstPtr);
2293
0
                }
2294
0
                GDALExtendedDataType::CopyValue(srcPtr, srcType, dstPtr,
2295
0
                                                dstType);
2296
0
                if ((--nIters) == 0)
2297
0
                    break;
2298
0
                srcPtr += src_inc_offset;
2299
0
                dstPtr += dst_inc_offset;
2300
0
            }
2301
0
        }
2302
0
    };
2303
2304
0
    if (nDims == 1)
2305
0
    {
2306
0
        lambdaLastDim(0);
2307
0
    }
2308
0
    else if (nDims == 2)
2309
0
    {
2310
0
        auto nIters = count[0];
2311
0
        while (true)
2312
0
        {
2313
0
            lambdaLastDim(0);
2314
0
            if ((--nIters) == 0)
2315
0
                break;
2316
0
            stack[0].src_ptr += stack[0].src_inc_offset;
2317
0
            stack[0].dst_ptr += stack[0].dst_inc_offset;
2318
0
        }
2319
0
    }
2320
0
    else if (nDims == 3)
2321
0
    {
2322
0
        stack[0].nIters = count[0];
2323
0
        while (true)
2324
0
        {
2325
0
            stack[1].src_ptr = stack[0].src_ptr;
2326
0
            stack[1].dst_ptr = stack[0].dst_ptr;
2327
0
            auto nIters = count[1];
2328
0
            while (true)
2329
0
            {
2330
0
                lambdaLastDim(1);
2331
0
                if ((--nIters) == 0)
2332
0
                    break;
2333
0
                stack[1].src_ptr += stack[1].src_inc_offset;
2334
0
                stack[1].dst_ptr += stack[1].dst_inc_offset;
2335
0
            }
2336
0
            if ((--stack[0].nIters) == 0)
2337
0
                break;
2338
0
            stack[0].src_ptr += stack[0].src_inc_offset;
2339
0
            stack[0].dst_ptr += stack[0].dst_inc_offset;
2340
0
        }
2341
0
    }
2342
0
    else
2343
0
    {
2344
        // Implementation valid for nDims >= 3
2345
2346
0
        size_t dimIdx = 0;
2347
        // Non-recursive implementation. Hence the gotos
2348
        // It might be possible to rewrite this without gotos, but I find they
2349
        // make it clearer to understand the recursive nature of the code
2350
0
    lbl_next_depth:
2351
0
        if (dimIdx == nDimsMinus1 - 1)
2352
0
        {
2353
0
            auto nIters = count[dimIdx];
2354
0
            while (true)
2355
0
            {
2356
0
                lambdaLastDim(dimIdx);
2357
0
                if ((--nIters) == 0)
2358
0
                    break;
2359
0
                stack[dimIdx].src_ptr += stack[dimIdx].src_inc_offset;
2360
0
                stack[dimIdx].dst_ptr += stack[dimIdx].dst_inc_offset;
2361
0
            }
2362
            // If there was a test if( dimIdx > 0 ), that would be valid for
2363
            // nDims == 2
2364
0
            goto lbl_return_to_caller;
2365
0
        }
2366
0
        else
2367
0
        {
2368
0
            stack[dimIdx].nIters = count[dimIdx];
2369
0
            while (true)
2370
0
            {
2371
0
                dimIdx++;
2372
0
                stack[dimIdx].src_ptr = stack[dimIdx - 1].src_ptr;
2373
0
                stack[dimIdx].dst_ptr = stack[dimIdx - 1].dst_ptr;
2374
0
                goto lbl_next_depth;
2375
0
            lbl_return_to_caller:
2376
0
                dimIdx--;
2377
0
                if ((--stack[dimIdx].nIters) == 0)
2378
0
                    break;
2379
0
                stack[dimIdx].src_ptr += stack[dimIdx].src_inc_offset;
2380
0
                stack[dimIdx].dst_ptr += stack[dimIdx].dst_inc_offset;
2381
0
            }
2382
0
            if (dimIdx > 0)
2383
0
                goto lbl_return_to_caller;
2384
0
        }
2385
0
    }
2386
0
}
2387
2388
/************************************************************************/
2389
/*                               IRead()                                */
2390
/************************************************************************/
2391
2392
bool MEMAbstractMDArray::IRead(const GUInt64 *arrayStartIdx,
2393
                               const size_t *count, const GInt64 *arrayStep,
2394
                               const GPtrDiff_t *bufferStride,
2395
                               const GDALExtendedDataType &bufferDataType,
2396
                               void *pDstBuffer) const
2397
0
{
2398
0
    if (!CheckValidAndErrorOutIfNot())
2399
0
        return false;
2400
2401
0
    const auto nDims = m_aoDims.size();
2402
0
    if (nDims == 0)
2403
0
    {
2404
0
        GDALExtendedDataType::CopyValue(m_pabyArray, m_oType, pDstBuffer,
2405
0
                                        bufferDataType);
2406
0
        return true;
2407
0
    }
2408
0
    std::vector<StackReadWrite> stack(nDims);
2409
0
    const auto nBufferDTSize = bufferDataType.GetSize();
2410
0
    GPtrDiff_t startSrcOffset = 0;
2411
0
    for (size_t i = 0; i < nDims; i++)
2412
0
    {
2413
0
        startSrcOffset +=
2414
0
            static_cast<GPtrDiff_t>(arrayStartIdx[i] * m_anStrides[i]);
2415
0
        stack[i].src_inc_offset =
2416
0
            static_cast<GPtrDiff_t>(arrayStep[i] * m_anStrides[i]);
2417
0
        stack[i].dst_inc_offset =
2418
0
            static_cast<GPtrDiff_t>(bufferStride[i] * nBufferDTSize);
2419
0
    }
2420
0
    stack[0].src_ptr = m_pabyArray + startSrcOffset;
2421
0
    stack[0].dst_ptr = static_cast<GByte *>(pDstBuffer);
2422
2423
0
    ReadWrite(false, count, stack, m_oType, bufferDataType);
2424
0
    return true;
2425
0
}
2426
2427
/************************************************************************/
2428
/*                               IWrite()                               */
2429
/************************************************************************/
2430
2431
bool MEMAbstractMDArray::IWrite(const GUInt64 *arrayStartIdx,
2432
                                const size_t *count, const GInt64 *arrayStep,
2433
                                const GPtrDiff_t *bufferStride,
2434
                                const GDALExtendedDataType &bufferDataType,
2435
                                const void *pSrcBuffer)
2436
0
{
2437
0
    if (!CheckValidAndErrorOutIfNot())
2438
0
        return false;
2439
0
    if (!m_bWritable)
2440
0
    {
2441
0
        CPLError(CE_Failure, CPLE_AppDefined, "Non updatable object");
2442
0
        return false;
2443
0
    }
2444
2445
0
    m_bModified = true;
2446
2447
0
    const auto nDims = m_aoDims.size();
2448
0
    if (nDims == 0)
2449
0
    {
2450
0
        m_oType.FreeDynamicMemory(m_pabyArray);
2451
0
        GDALExtendedDataType::CopyValue(pSrcBuffer, bufferDataType, m_pabyArray,
2452
0
                                        m_oType);
2453
0
        return true;
2454
0
    }
2455
0
    std::vector<StackReadWrite> stack(nDims);
2456
0
    const auto nBufferDTSize = bufferDataType.GetSize();
2457
0
    GPtrDiff_t startDstOffset = 0;
2458
0
    for (size_t i = 0; i < nDims; i++)
2459
0
    {
2460
0
        startDstOffset +=
2461
0
            static_cast<GPtrDiff_t>(arrayStartIdx[i] * m_anStrides[i]);
2462
0
        stack[i].dst_inc_offset =
2463
0
            static_cast<GPtrDiff_t>(arrayStep[i] * m_anStrides[i]);
2464
0
        stack[i].src_inc_offset =
2465
0
            static_cast<GPtrDiff_t>(bufferStride[i] * nBufferDTSize);
2466
0
    }
2467
2468
0
    stack[0].dst_ptr = m_pabyArray + startDstOffset;
2469
0
    stack[0].src_ptr = static_cast<const GByte *>(pSrcBuffer);
2470
2471
0
    ReadWrite(true, count, stack, bufferDataType, m_oType);
2472
0
    return true;
2473
0
}
2474
2475
/************************************************************************/
2476
/*                             MEMMDArray()                             */
2477
/************************************************************************/
2478
2479
MEMMDArray::MEMMDArray(
2480
    const std::string &osParentName, const std::string &osName,
2481
    const std::vector<std::shared_ptr<GDALDimension>> &aoDimensions,
2482
    const GDALExtendedDataType &oType)
2483
0
    : GDALAbstractMDArray(osParentName, osName),
2484
0
      MEMAbstractMDArray(osParentName, osName, aoDimensions, oType),
2485
0
      GDALMDArray(osParentName, osName)
2486
0
{
2487
0
}
Unexecuted instantiation: MEMMDArray::MEMMDArray(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::vector<std::__1::shared_ptr<GDALDimension>, std::__1::allocator<std::__1::shared_ptr<GDALDimension> > > const&, GDALExtendedDataType const&)
Unexecuted instantiation: MEMMDArray::MEMMDArray(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::vector<std::__1::shared_ptr<GDALDimension>, std::__1::allocator<std::__1::shared_ptr<GDALDimension> > > const&, GDALExtendedDataType const&)
2488
2489
/************************************************************************/
2490
/*                            ~MEMMDArray()                             */
2491
/************************************************************************/
2492
2493
MEMMDArray::~MEMMDArray()
2494
0
{
2495
0
    if (m_pabyNoData)
2496
0
    {
2497
0
        m_oType.FreeDynamicMemory(&m_pabyNoData[0]);
2498
0
        CPLFree(m_pabyNoData);
2499
0
    }
2500
2501
0
    for (auto &poDim : GetDimensions())
2502
0
    {
2503
0
        const auto dim = std::dynamic_pointer_cast<MEMDimension>(poDim);
2504
0
        if (dim)
2505
0
            dim->UnRegisterUsingArray(this);
2506
0
    }
2507
0
}
2508
2509
/************************************************************************/
2510
/*                         GetRawNoDataValue()                          */
2511
/************************************************************************/
2512
2513
const void *MEMMDArray::GetRawNoDataValue() const
2514
0
{
2515
0
    return m_pabyNoData;
2516
0
}
2517
2518
/************************************************************************/
2519
/*                         SetRawNoDataValue()                          */
2520
/************************************************************************/
2521
2522
bool MEMMDArray::SetRawNoDataValue(const void *pNoData)
2523
0
{
2524
0
    if (!CheckValidAndErrorOutIfNot())
2525
0
        return false;
2526
0
    if (m_pabyNoData)
2527
0
    {
2528
0
        m_oType.FreeDynamicMemory(&m_pabyNoData[0]);
2529
0
    }
2530
2531
0
    if (pNoData == nullptr)
2532
0
    {
2533
0
        CPLFree(m_pabyNoData);
2534
0
        m_pabyNoData = nullptr;
2535
0
    }
2536
0
    else
2537
0
    {
2538
0
        const auto nSize = m_oType.GetSize();
2539
0
        if (m_pabyNoData == nullptr)
2540
0
        {
2541
0
            m_pabyNoData = static_cast<GByte *>(CPLMalloc(nSize));
2542
0
        }
2543
0
        memset(m_pabyNoData, 0, nSize);
2544
0
        GDALExtendedDataType::CopyValue(pNoData, m_oType, m_pabyNoData,
2545
0
                                        m_oType);
2546
0
    }
2547
0
    return true;
2548
0
}
2549
2550
/************************************************************************/
2551
/*                            GetAttribute()                            */
2552
/************************************************************************/
2553
2554
std::shared_ptr<GDALAttribute>
2555
MEMMDArray::GetAttribute(const std::string &osName) const
2556
0
{
2557
0
    if (!CheckValidAndErrorOutIfNot())
2558
0
        return nullptr;
2559
0
    auto oIter = m_oMapAttributes.find(osName);
2560
0
    if (oIter != m_oMapAttributes.end())
2561
0
        return oIter->second;
2562
0
    return nullptr;
2563
0
}
2564
2565
/************************************************************************/
2566
/*                           GetAttributes()                            */
2567
/************************************************************************/
2568
2569
std::vector<std::shared_ptr<GDALAttribute>>
2570
MEMMDArray::GetAttributes(CSLConstList) const
2571
0
{
2572
0
    if (!CheckValidAndErrorOutIfNot())
2573
0
        return {};
2574
0
    std::vector<std::shared_ptr<GDALAttribute>> oRes;
2575
0
    for (const auto &oIter : m_oMapAttributes)
2576
0
    {
2577
0
        oRes.push_back(oIter.second);
2578
0
    }
2579
0
    return oRes;
2580
0
}
2581
2582
/************************************************************************/
2583
/*                          CreateAttribute()                           */
2584
/************************************************************************/
2585
2586
std::shared_ptr<GDALAttribute>
2587
MEMMDArray::CreateAttribute(const std::string &osName,
2588
                            const std::vector<GUInt64> &anDimensions,
2589
                            const GDALExtendedDataType &oDataType, CSLConstList)
2590
0
{
2591
0
    if (!CheckValidAndErrorOutIfNot())
2592
0
        return nullptr;
2593
0
    if (osName.empty())
2594
0
    {
2595
0
        CPLError(CE_Failure, CPLE_NotSupported,
2596
0
                 "Empty attribute name not supported");
2597
0
        return nullptr;
2598
0
    }
2599
0
    if (m_oMapAttributes.find(osName) != m_oMapAttributes.end())
2600
0
    {
2601
0
        CPLError(CE_Failure, CPLE_AppDefined,
2602
0
                 "An attribute with same name already exists");
2603
0
        return nullptr;
2604
0
    }
2605
0
    auto poSelf = std::dynamic_pointer_cast<MEMMDArray>(m_pSelf.lock());
2606
0
    CPLAssert(poSelf);
2607
0
    auto newAttr(MEMAttribute::Create(poSelf, osName, anDimensions, oDataType));
2608
0
    if (!newAttr)
2609
0
        return nullptr;
2610
0
    m_oMapAttributes[osName] = newAttr;
2611
0
    return newAttr;
2612
0
}
2613
2614
/************************************************************************/
2615
/*                          DeleteAttribute()                           */
2616
/************************************************************************/
2617
2618
bool MEMMDArray::DeleteAttribute(const std::string &osName,
2619
                                 CSLConstList /*papszOptions*/)
2620
0
{
2621
0
    if (!CheckValidAndErrorOutIfNot())
2622
0
        return false;
2623
0
    auto oIter = m_oMapAttributes.find(osName);
2624
0
    if (oIter == m_oMapAttributes.end())
2625
0
    {
2626
0
        CPLError(CE_Failure, CPLE_AppDefined,
2627
0
                 "Attribute %s is not an attribute of this array",
2628
0
                 osName.c_str());
2629
0
        return false;
2630
0
    }
2631
2632
0
    oIter->second->Deleted();
2633
0
    m_oMapAttributes.erase(oIter);
2634
0
    return true;
2635
0
}
2636
2637
/************************************************************************/
2638
/*                       GetCoordinateVariables()                       */
2639
/************************************************************************/
2640
2641
std::vector<std::shared_ptr<GDALMDArray>>
2642
MEMMDArray::GetCoordinateVariables() const
2643
0
{
2644
0
    if (!CheckValidAndErrorOutIfNot())
2645
0
        return {};
2646
0
    std::vector<std::shared_ptr<GDALMDArray>> ret;
2647
0
    const auto poCoordinates = GetAttribute("coordinates");
2648
0
    if (poCoordinates &&
2649
0
        poCoordinates->GetDataType().GetClass() == GEDTC_STRING &&
2650
0
        poCoordinates->GetDimensionCount() == 0)
2651
0
    {
2652
0
        const char *pszCoordinates = poCoordinates->ReadAsString();
2653
0
        if (pszCoordinates)
2654
0
        {
2655
0
            auto poGroup = m_poGroupWeak.lock();
2656
0
            if (!poGroup)
2657
0
            {
2658
0
                CPLError(CE_Failure, CPLE_AppDefined,
2659
0
                         "Cannot access coordinate variables of %s has "
2660
0
                         "belonging group has gone out of scope",
2661
0
                         GetName().c_str());
2662
0
            }
2663
0
            else
2664
0
            {
2665
0
                const CPLStringList aosNames(
2666
0
                    CSLTokenizeString2(pszCoordinates, " ", 0));
2667
0
                for (int i = 0; i < aosNames.size(); i++)
2668
0
                {
2669
0
                    auto poCoordinateVar = poGroup->OpenMDArray(aosNames[i]);
2670
0
                    if (poCoordinateVar)
2671
0
                    {
2672
0
                        ret.emplace_back(poCoordinateVar);
2673
0
                    }
2674
0
                    else
2675
0
                    {
2676
0
                        CPLError(CE_Warning, CPLE_AppDefined,
2677
0
                                 "Cannot find variable corresponding to "
2678
0
                                 "coordinate %s",
2679
0
                                 aosNames[i]);
2680
0
                    }
2681
0
                }
2682
0
            }
2683
0
        }
2684
0
    }
2685
2686
0
    return ret;
2687
0
}
2688
2689
/************************************************************************/
2690
/*                               Resize()                               */
2691
/************************************************************************/
2692
2693
bool MEMMDArray::Resize(const std::vector<GUInt64> &anNewDimSizes,
2694
                        CSLConstList /* papszOptions */)
2695
0
{
2696
0
    return Resize(anNewDimSizes, /*bResizeOtherArrays=*/true);
2697
0
}
2698
2699
bool MEMMDArray::Resize(const std::vector<GUInt64> &anNewDimSizes,
2700
                        bool bResizeOtherArrays)
2701
0
{
2702
0
    if (!CheckValidAndErrorOutIfNot())
2703
0
        return false;
2704
0
    if (!IsWritable())
2705
0
    {
2706
0
        CPLError(CE_Failure, CPLE_AppDefined,
2707
0
                 "Resize() not supported on read-only file");
2708
0
        return false;
2709
0
    }
2710
0
    if (!m_bOwnArray)
2711
0
    {
2712
0
        CPLError(
2713
0
            CE_Failure, CPLE_AppDefined,
2714
0
            "Resize() not supported on an array that does not own its memory");
2715
0
        return false;
2716
0
    }
2717
2718
0
    const auto nDimCount = GetDimensionCount();
2719
0
    if (anNewDimSizes.size() != nDimCount)
2720
0
    {
2721
0
        CPLError(CE_Failure, CPLE_IllegalArg,
2722
0
                 "Not expected number of values in anNewDimSizes.");
2723
0
        return false;
2724
0
    }
2725
2726
0
    auto &dims = GetDimensions();
2727
0
    std::vector<size_t> anDecreasedDimIdx;
2728
0
    std::vector<size_t> anGrownDimIdx;
2729
0
    std::map<GDALDimension *, GUInt64> oMapDimToSize;
2730
0
    for (size_t i = 0; i < nDimCount; ++i)
2731
0
    {
2732
0
        auto oIter = oMapDimToSize.find(dims[i].get());
2733
0
        if (oIter != oMapDimToSize.end() && oIter->second != anNewDimSizes[i])
2734
0
        {
2735
0
            CPLError(CE_Failure, CPLE_AppDefined,
2736
0
                     "Cannot resize a dimension referenced several times "
2737
0
                     "to different sizes");
2738
0
            return false;
2739
0
        }
2740
0
        if (anNewDimSizes[i] != dims[i]->GetSize())
2741
0
        {
2742
0
            if (anNewDimSizes[i] == 0)
2743
0
            {
2744
0
                CPLError(CE_Failure, CPLE_IllegalArg,
2745
0
                         "Illegal dimension size 0");
2746
0
                return false;
2747
0
            }
2748
0
            auto dim = std::dynamic_pointer_cast<MEMDimension>(dims[i]);
2749
0
            if (!dim)
2750
0
            {
2751
0
                CPLError(
2752
0
                    CE_Failure, CPLE_AppDefined,
2753
0
                    "Cannot resize a dimension that is not a MEMDimension");
2754
0
                return false;
2755
0
            }
2756
0
            oMapDimToSize[dim.get()] = anNewDimSizes[i];
2757
0
            if (anNewDimSizes[i] < dims[i]->GetSize())
2758
0
            {
2759
0
                anDecreasedDimIdx.push_back(i);
2760
0
            }
2761
0
            else
2762
0
            {
2763
0
                anGrownDimIdx.push_back(i);
2764
0
            }
2765
0
        }
2766
0
        else
2767
0
        {
2768
0
            oMapDimToSize[dims[i].get()] = dims[i]->GetSize();
2769
0
        }
2770
0
    }
2771
2772
0
    const auto ResizeOtherArrays = [this, &anNewDimSizes, nDimCount, &dims]()
2773
0
    {
2774
0
        std::set<MEMMDArray *> oSetArrays;
2775
0
        std::map<GDALDimension *, GUInt64> oMapNewSize;
2776
0
        for (size_t i = 0; i < nDimCount; ++i)
2777
0
        {
2778
0
            if (anNewDimSizes[i] != dims[i]->GetSize())
2779
0
            {
2780
0
                auto dim = std::dynamic_pointer_cast<MEMDimension>(dims[i]);
2781
0
                if (!dim)
2782
0
                {
2783
0
                    CPLAssert(false);
2784
0
                }
2785
0
                else
2786
0
                {
2787
0
                    oMapNewSize[dims[i].get()] = anNewDimSizes[i];
2788
0
                    for (const auto &poArray : dim->GetUsingArrays())
2789
0
                    {
2790
0
                        if (poArray != this)
2791
0
                            oSetArrays.insert(poArray);
2792
0
                    }
2793
0
                }
2794
0
            }
2795
0
        }
2796
2797
0
        bool bOK = true;
2798
0
        for (auto *poArray : oSetArrays)
2799
0
        {
2800
0
            const auto &apoOtherDims = poArray->GetDimensions();
2801
0
            std::vector<GUInt64> anOtherArrayNewDimSizes(
2802
0
                poArray->GetDimensionCount());
2803
0
            for (size_t i = 0; i < anOtherArrayNewDimSizes.size(); ++i)
2804
0
            {
2805
0
                auto oIter = oMapNewSize.find(apoOtherDims[i].get());
2806
0
                if (oIter != oMapNewSize.end())
2807
0
                    anOtherArrayNewDimSizes[i] = oIter->second;
2808
0
                else
2809
0
                    anOtherArrayNewDimSizes[i] = apoOtherDims[i]->GetSize();
2810
0
            }
2811
0
            if (!poArray->Resize(anOtherArrayNewDimSizes,
2812
0
                                 /*bResizeOtherArrays=*/false))
2813
0
            {
2814
0
                bOK = false;
2815
0
                break;
2816
0
            }
2817
0
        }
2818
0
        if (!bOK)
2819
0
        {
2820
0
            CPLError(CE_Failure, CPLE_AppDefined,
2821
0
                     "Resizing of another array referencing the same dimension "
2822
0
                     "as one modified on the current array failed. All arrays "
2823
0
                     "referencing that dimension will be invalidated.");
2824
0
            Invalidate();
2825
0
            for (auto *poArray : oSetArrays)
2826
0
            {
2827
0
                poArray->Invalidate();
2828
0
            }
2829
0
        }
2830
2831
0
        return bOK;
2832
0
    };
2833
2834
    // Decrease slowest varying dimension
2835
0
    if (anGrownDimIdx.empty() && anDecreasedDimIdx.size() == 1 &&
2836
0
        anDecreasedDimIdx[0] == 0)
2837
0
    {
2838
0
        CPLAssert(m_nTotalSize % dims[0]->GetSize() == 0);
2839
0
        const size_t nNewTotalSize = static_cast<size_t>(
2840
0
            (m_nTotalSize / dims[0]->GetSize()) * anNewDimSizes[0]);
2841
0
        if (m_oType.NeedsFreeDynamicMemory())
2842
0
        {
2843
0
            GByte *pabyPtr = m_pabyArray + nNewTotalSize;
2844
0
            GByte *pabyEnd = m_pabyArray + m_nTotalSize;
2845
0
            const auto nDTSize(m_oType.GetSize());
2846
0
            while (pabyPtr < pabyEnd)
2847
0
            {
2848
0
                m_oType.FreeDynamicMemory(pabyPtr);
2849
0
                pabyPtr += nDTSize;
2850
0
            }
2851
0
        }
2852
        // shrinking... cannot fail, and even if it does, that's ok
2853
0
        GByte *pabyArray = static_cast<GByte *>(
2854
0
            VSI_REALLOC_VERBOSE(m_pabyArray, nNewTotalSize));
2855
0
        if (pabyArray)
2856
0
            m_pabyArray = pabyArray;
2857
0
        m_nTotalSize = nNewTotalSize;
2858
2859
0
        if (bResizeOtherArrays)
2860
0
        {
2861
0
            if (!ResizeOtherArrays())
2862
0
                return false;
2863
2864
0
            auto dim = std::dynamic_pointer_cast<MEMDimension>(dims[0]);
2865
0
            if (dim)
2866
0
            {
2867
0
                dim->SetSize(anNewDimSizes[0]);
2868
0
            }
2869
0
            else
2870
0
            {
2871
0
                CPLAssert(false);
2872
0
            }
2873
0
        }
2874
0
        return true;
2875
0
    }
2876
2877
    // Increase slowest varying dimension
2878
0
    if (anDecreasedDimIdx.empty() && anGrownDimIdx.size() == 1 &&
2879
0
        anGrownDimIdx[0] == 0)
2880
0
    {
2881
0
        CPLAssert(m_nTotalSize % dims[0]->GetSize() == 0);
2882
0
        GUInt64 nNewTotalSize64 = m_nTotalSize / dims[0]->GetSize();
2883
0
        if (nNewTotalSize64 >
2884
0
            std::numeric_limits<GUInt64>::max() / anNewDimSizes[0])
2885
0
        {
2886
0
            CPLError(CE_Failure, CPLE_OutOfMemory, "Too big allocation");
2887
0
            return false;
2888
0
        }
2889
0
        nNewTotalSize64 *= anNewDimSizes[0];
2890
        // We restrict the size of the allocation so that all elements can be
2891
        // indexed by GPtrDiff_t
2892
0
        if (nNewTotalSize64 >
2893
0
            static_cast<size_t>(std::numeric_limits<GPtrDiff_t>::max()))
2894
0
        {
2895
0
            CPLError(CE_Failure, CPLE_OutOfMemory, "Too big allocation");
2896
0
            return false;
2897
0
        }
2898
0
        const size_t nNewTotalSize = static_cast<size_t>(nNewTotalSize64);
2899
0
        GByte *pabyArray = static_cast<GByte *>(
2900
0
            VSI_REALLOC_VERBOSE(m_pabyArray, nNewTotalSize));
2901
0
        if (!pabyArray)
2902
0
            return false;
2903
0
        memset(pabyArray + m_nTotalSize, 0, nNewTotalSize - m_nTotalSize);
2904
0
        m_pabyArray = pabyArray;
2905
0
        m_nTotalSize = nNewTotalSize;
2906
2907
0
        if (bResizeOtherArrays)
2908
0
        {
2909
0
            if (!ResizeOtherArrays())
2910
0
                return false;
2911
2912
0
            auto dim = std::dynamic_pointer_cast<MEMDimension>(dims[0]);
2913
0
            if (dim)
2914
0
            {
2915
0
                dim->SetSize(anNewDimSizes[0]);
2916
0
            }
2917
0
            else
2918
0
            {
2919
0
                CPLAssert(false);
2920
0
            }
2921
0
        }
2922
0
        return true;
2923
0
    }
2924
2925
    // General case where we modify other dimensions that the first one.
2926
2927
    // Create dummy dimensions at the new sizes
2928
0
    std::vector<std::shared_ptr<GDALDimension>> aoNewDims;
2929
0
    for (size_t i = 0; i < nDimCount; ++i)
2930
0
    {
2931
0
        aoNewDims.emplace_back(std::make_shared<MEMDimension>(
2932
0
            std::string(), dims[i]->GetName(), std::string(), std::string(),
2933
0
            anNewDimSizes[i]));
2934
0
    }
2935
2936
    // Create a temporary array
2937
0
    auto poTempMDArray =
2938
0
        Create(std::string(), std::string(), aoNewDims, GetDataType());
2939
0
    if (!poTempMDArray->Init())
2940
0
        return false;
2941
0
    std::vector<GUInt64> arrayStartIdx(nDimCount);
2942
0
    std::vector<size_t> count(nDimCount);
2943
0
    std::vector<GInt64> arrayStep(nDimCount, 1);
2944
0
    std::vector<GPtrDiff_t> bufferStride(nDimCount);
2945
0
    for (size_t i = nDimCount; i > 0;)
2946
0
    {
2947
0
        --i;
2948
0
        if (i == nDimCount - 1)
2949
0
            bufferStride[i] = 1;
2950
0
        else
2951
0
        {
2952
0
            bufferStride[i] = static_cast<GPtrDiff_t>(bufferStride[i + 1] *
2953
0
                                                      dims[i + 1]->GetSize());
2954
0
        }
2955
0
        const auto nCount = std::min(anNewDimSizes[i], dims[i]->GetSize());
2956
0
        count[i] = static_cast<size_t>(nCount);
2957
0
    }
2958
    // Copy the current content into the array with the new layout
2959
0
    if (!poTempMDArray->Write(arrayStartIdx.data(), count.data(),
2960
0
                              arrayStep.data(), bufferStride.data(),
2961
0
                              GetDataType(), m_pabyArray))
2962
0
    {
2963
0
        return false;
2964
0
    }
2965
2966
    // Move content of the temporary array into the current array, and
2967
    // invalidate the temporary array
2968
0
    FreeArray();
2969
0
    m_bOwnArray = true;
2970
0
    m_pabyArray = poTempMDArray->m_pabyArray;
2971
0
    m_nTotalSize = poTempMDArray->m_nTotalSize;
2972
0
    m_anStrides = poTempMDArray->m_anStrides;
2973
2974
0
    poTempMDArray->m_bOwnArray = false;
2975
0
    poTempMDArray->m_pabyArray = nullptr;
2976
0
    poTempMDArray->m_nTotalSize = 0;
2977
2978
0
    if (bResizeOtherArrays && !ResizeOtherArrays())
2979
0
        return false;
2980
2981
    // Update dimension size
2982
0
    for (size_t i = 0; i < nDimCount; ++i)
2983
0
    {
2984
0
        if (anNewDimSizes[i] != dims[i]->GetSize())
2985
0
        {
2986
0
            auto dim = std::dynamic_pointer_cast<MEMDimension>(dims[i]);
2987
0
            if (dim)
2988
0
            {
2989
0
                dim->SetSize(anNewDimSizes[i]);
2990
0
            }
2991
0
            else
2992
0
            {
2993
0
                CPLAssert(false);
2994
0
            }
2995
0
        }
2996
0
    }
2997
2998
0
    return true;
2999
0
}
3000
3001
/************************************************************************/
3002
/*                               Rename()                               */
3003
/************************************************************************/
3004
3005
bool MEMMDArray::Rename(const std::string &osNewName)
3006
0
{
3007
0
    if (!CheckValidAndErrorOutIfNot())
3008
0
        return false;
3009
0
    if (osNewName.empty())
3010
0
    {
3011
0
        CPLError(CE_Failure, CPLE_NotSupported, "Empty name not supported");
3012
0
        return false;
3013
0
    }
3014
3015
0
    if (auto poParentGroup =
3016
0
            std::dynamic_pointer_cast<MEMGroup>(m_poGroupWeak.lock()))
3017
0
    {
3018
0
        if (!poParentGroup->RenameArray(m_osName, osNewName))
3019
0
        {
3020
0
            return false;
3021
0
        }
3022
0
    }
3023
3024
0
    BaseRename(osNewName);
3025
3026
0
    return true;
3027
0
}
3028
3029
/************************************************************************/
3030
/*                      NotifyChildrenOfRenaming()                      */
3031
/************************************************************************/
3032
3033
void MEMMDArray::NotifyChildrenOfRenaming()
3034
0
{
3035
0
    for (const auto &oIter : m_oMapAttributes)
3036
0
        oIter.second->ParentRenamed(m_osFullName);
3037
0
}
3038
3039
/************************************************************************/
3040
/*                      NotifyChildrenOfDeletion()                      */
3041
/************************************************************************/
3042
3043
void MEMMDArray::NotifyChildrenOfDeletion()
3044
0
{
3045
0
    for (const auto &oIter : m_oMapAttributes)
3046
0
        oIter.second->ParentDeleted();
3047
0
}
3048
3049
/************************************************************************/
3050
/*                          BuildDimensions()                           */
3051
/************************************************************************/
3052
3053
static std::vector<std::shared_ptr<GDALDimension>>
3054
BuildDimensions(const std::vector<GUInt64> &anDimensions)
3055
0
{
3056
0
    std::vector<std::shared_ptr<GDALDimension>> res;
3057
0
    for (size_t i = 0; i < anDimensions.size(); i++)
3058
0
    {
3059
0
        res.emplace_back(std::make_shared<GDALDimensionWeakIndexingVar>(
3060
0
            std::string(), CPLSPrintf("dim%u", static_cast<unsigned>(i)),
3061
0
            std::string(), std::string(), anDimensions[i]));
3062
0
    }
3063
0
    return res;
3064
0
}
3065
3066
/************************************************************************/
3067
/*                            MEMAttribute()                            */
3068
/************************************************************************/
3069
3070
MEMAttribute::MEMAttribute(const std::string &osParentName,
3071
                           const std::string &osName,
3072
                           const std::vector<GUInt64> &anDimensions,
3073
                           const GDALExtendedDataType &oType)
3074
0
    : GDALAbstractMDArray(osParentName, osName),
3075
0
      MEMAbstractMDArray(osParentName, osName, BuildDimensions(anDimensions),
3076
0
                         oType),
3077
0
      GDALAttribute(osParentName, osName)
3078
0
{
3079
0
}
Unexecuted instantiation: MEMAttribute::MEMAttribute(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::vector<unsigned long long, std::__1::allocator<unsigned long long> > const&, GDALExtendedDataType const&)
Unexecuted instantiation: MEMAttribute::MEMAttribute(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::vector<unsigned long long, std::__1::allocator<unsigned long long> > const&, GDALExtendedDataType const&)
3080
3081
/************************************************************************/
3082
/*                        MEMAttribute::Create()                        */
3083
/************************************************************************/
3084
3085
std::shared_ptr<MEMAttribute>
3086
MEMAttribute::Create(const std::string &osParentName, const std::string &osName,
3087
                     const std::vector<GUInt64> &anDimensions,
3088
                     const GDALExtendedDataType &oType)
3089
0
{
3090
0
    auto attr(std::shared_ptr<MEMAttribute>(
3091
0
        new MEMAttribute(osParentName, osName, anDimensions, oType)));
3092
0
    attr->SetSelf(attr);
3093
0
    if (!attr->Init())
3094
0
        return nullptr;
3095
0
    return attr;
3096
0
}
3097
3098
/************************************************************************/
3099
/*                        MEMAttribute::Create()                        */
3100
/************************************************************************/
3101
3102
std::shared_ptr<MEMAttribute> MEMAttribute::Create(
3103
    const std::shared_ptr<MEMGroup> &poParentGroup, const std::string &osName,
3104
    const std::vector<GUInt64> &anDimensions, const GDALExtendedDataType &oType)
3105
0
{
3106
0
    const std::string osParentName =
3107
0
        (poParentGroup && poParentGroup->GetName().empty())
3108
0
            ?
3109
            // Case of the ZarrAttributeGroup::m_oGroup fake group
3110
0
            poParentGroup->GetFullName()
3111
0
            : ((poParentGroup == nullptr || poParentGroup->GetFullName() == "/"
3112
0
                    ? "/"
3113
0
                    : poParentGroup->GetFullName() + "/") +
3114
0
               "_GLOBAL_");
3115
0
    auto attr(Create(osParentName, osName, anDimensions, oType));
3116
0
    if (!attr)
3117
0
        return nullptr;
3118
0
    attr->m_poParent = poParentGroup;
3119
0
    return attr;
3120
0
}
3121
3122
/************************************************************************/
3123
/*                        MEMAttribute::Create()                        */
3124
/************************************************************************/
3125
3126
std::shared_ptr<MEMAttribute> MEMAttribute::Create(
3127
    const std::shared_ptr<MEMMDArray> &poParentArray, const std::string &osName,
3128
    const std::vector<GUInt64> &anDimensions, const GDALExtendedDataType &oType)
3129
0
{
3130
0
    auto attr(
3131
0
        Create(poParentArray->GetFullName(), osName, anDimensions, oType));
3132
0
    if (!attr)
3133
0
        return nullptr;
3134
0
    attr->m_poParent = poParentArray;
3135
0
    return attr;
3136
0
}
3137
3138
/************************************************************************/
3139
/*                               Rename()                               */
3140
/************************************************************************/
3141
3142
bool MEMAttribute::Rename(const std::string &osNewName)
3143
0
{
3144
0
    if (!CheckValidAndErrorOutIfNot())
3145
0
        return false;
3146
0
    if (osNewName.empty())
3147
0
    {
3148
0
        CPLError(CE_Failure, CPLE_NotSupported, "Empty name not supported");
3149
0
        return false;
3150
0
    }
3151
3152
0
    if (auto poParent = m_poParent.lock())
3153
0
    {
3154
0
        if (!poParent->RenameAttribute(m_osName, osNewName))
3155
0
        {
3156
0
            return false;
3157
0
        }
3158
0
    }
3159
3160
0
    BaseRename(osNewName);
3161
3162
0
    m_bModified = true;
3163
3164
0
    return true;
3165
0
}
3166
3167
/************************************************************************/
3168
/*                            MEMDimension()                            */
3169
/************************************************************************/
3170
3171
MEMDimension::MEMDimension(const std::string &osParentName,
3172
                           const std::string &osName, const std::string &osType,
3173
                           const std::string &osDirection, GUInt64 nSize)
3174
0
    : GDALDimensionWeakIndexingVar(osParentName, osName, osType, osDirection,
3175
0
                                   nSize)
3176
0
{
3177
0
}
3178
3179
/************************************************************************/
3180
/*                         RegisterUsingArray()                         */
3181
/************************************************************************/
3182
3183
void MEMDimension::RegisterUsingArray(MEMMDArray *poArray)
3184
0
{
3185
0
    m_oSetArrays.insert(poArray);
3186
0
}
3187
3188
/************************************************************************/
3189
/*                        UnRegisterUsingArray()                        */
3190
/************************************************************************/
3191
3192
void MEMDimension::UnRegisterUsingArray(MEMMDArray *poArray)
3193
0
{
3194
0
    m_oSetArrays.erase(poArray);
3195
0
}
3196
3197
/************************************************************************/
3198
/*                               Create()                               */
3199
/************************************************************************/
3200
3201
/* static */
3202
std::shared_ptr<MEMDimension>
3203
MEMDimension::Create(const std::shared_ptr<MEMGroup> &poParentGroup,
3204
                     const std::string &osName, const std::string &osType,
3205
                     const std::string &osDirection, GUInt64 nSize)
3206
0
{
3207
0
    auto newDim(std::make_shared<MEMDimension>(
3208
0
        poParentGroup->GetFullName(), osName, osType, osDirection, nSize));
3209
0
    newDim->m_poParentGroup = poParentGroup;
3210
0
    return newDim;
3211
0
}
3212
3213
/************************************************************************/
3214
/*                          CreateDimension()                           */
3215
/************************************************************************/
3216
3217
std::shared_ptr<GDALDimension>
3218
MEMGroup::CreateDimension(const std::string &osName, const std::string &osType,
3219
                          const std::string &osDirection, GUInt64 nSize,
3220
                          CSLConstList)
3221
0
{
3222
0
    if (osName.empty())
3223
0
    {
3224
0
        CPLError(CE_Failure, CPLE_NotSupported,
3225
0
                 "Empty dimension name not supported");
3226
0
        return nullptr;
3227
0
    }
3228
0
    if (m_oMapDimensions.find(osName) != m_oMapDimensions.end())
3229
0
    {
3230
0
        CPLError(CE_Failure, CPLE_AppDefined,
3231
0
                 "A dimension with same name already exists");
3232
0
        return nullptr;
3233
0
    }
3234
0
    auto newDim(MEMDimension::Create(
3235
0
        std::dynamic_pointer_cast<MEMGroup>(m_pSelf.lock()), osName, osType,
3236
0
        osDirection, nSize));
3237
0
    m_oMapDimensions[osName] = newDim;
3238
0
    return newDim;
3239
0
}
3240
3241
/************************************************************************/
3242
/*                               Rename()                               */
3243
/************************************************************************/
3244
3245
bool MEMDimension::Rename(const std::string &osNewName)
3246
0
{
3247
0
    if (osNewName.empty())
3248
0
    {
3249
0
        CPLError(CE_Failure, CPLE_NotSupported, "Empty name not supported");
3250
0
        return false;
3251
0
    }
3252
3253
0
    if (auto poParentGroup = m_poParentGroup.lock())
3254
0
    {
3255
0
        if (!poParentGroup->RenameDimension(m_osName, osNewName))
3256
0
        {
3257
0
            return false;
3258
0
        }
3259
0
    }
3260
3261
0
    BaseRename(osNewName);
3262
3263
0
    return true;
3264
0
}
3265
3266
/************************************************************************/
3267
/*                       CreateMultiDimensional()                       */
3268
/************************************************************************/
3269
3270
GDALDataset *
3271
MEMDataset::CreateMultiDimensional(const char *pszFilename,
3272
                                   CSLConstList /*papszRootGroupOptions*/,
3273
                                   CSLConstList /*papszOptions*/)
3274
0
{
3275
0
    auto poDS = new MEMDataset();
3276
3277
0
    poDS->SetDescription(pszFilename);
3278
0
    auto poRootGroup = MEMGroup::Create(std::string(), nullptr);
3279
0
    poDS->m_poPrivate->m_poRootGroup = poRootGroup;
3280
3281
0
    return poDS;
3282
0
}
3283
3284
/************************************************************************/
3285
/*                            GetRootGroup()                            */
3286
/************************************************************************/
3287
3288
std::shared_ptr<GDALGroup> MEMDataset::GetRootGroup() const
3289
0
{
3290
0
    return m_poPrivate->m_poRootGroup;
3291
0
}
3292
3293
/************************************************************************/
3294
/*                         MEMDatasetIdentify()                         */
3295
/************************************************************************/
3296
3297
static int MEMDatasetIdentify(GDALOpenInfo *poOpenInfo)
3298
0
{
3299
0
    return (STARTS_WITH(poOpenInfo->pszFilename, "MEM:::") &&
3300
0
            poOpenInfo->fpL == nullptr);
3301
0
}
3302
3303
/************************************************************************/
3304
/*                          MEMDatasetDelete()                          */
3305
/************************************************************************/
3306
3307
static CPLErr MEMDatasetDelete(const char * /* fileName */)
3308
0
{
3309
    /* Null implementation, so that people can Delete("MEM:::") */
3310
0
    return CE_None;
3311
0
}
3312
3313
/************************************************************************/
3314
/*                            CreateLayer()                             */
3315
/************************************************************************/
3316
3317
OGRMemLayer *MEMDataset::CreateLayer(const OGRFeatureDefn &oDefn,
3318
                                     CSLConstList papszOptions)
3319
0
{
3320
0
    auto poLayer = std::make_unique<OGRMemLayer>(oDefn);
3321
3322
0
    if (CPLFetchBool(papszOptions, "ADVERTIZE_UTF8", false))
3323
0
        poLayer->SetAdvertizeUTF8(true);
3324
3325
0
    poLayer->SetDataset(this);
3326
0
    poLayer->SetFIDColumn(CSLFetchNameValueDef(papszOptions, "FID", ""));
3327
3328
    // Add layer to data source layer list.
3329
0
    m_apoLayers.emplace_back(std::move(poLayer));
3330
0
    return m_apoLayers.back().get();
3331
0
}
3332
3333
/************************************************************************/
3334
/*                            ICreateLayer()                            */
3335
/************************************************************************/
3336
3337
OGRLayer *MEMDataset::ICreateLayer(const char *pszLayerName,
3338
                                   const OGRGeomFieldDefn *poGeomFieldDefn,
3339
                                   CSLConstList papszOptions)
3340
0
{
3341
    // Create the layer object.
3342
3343
0
    const auto eType = poGeomFieldDefn ? poGeomFieldDefn->GetType() : wkbNone;
3344
0
    const auto poSRSIn =
3345
0
        poGeomFieldDefn ? poGeomFieldDefn->GetSpatialRef() : nullptr;
3346
3347
0
    OGRSpatialReference *poSRS = nullptr;
3348
0
    if (poSRSIn)
3349
0
    {
3350
0
        poSRS = poSRSIn->Clone();
3351
0
        poSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
3352
0
    }
3353
0
    auto poLayer = std::make_unique<OGRMemLayer>(pszLayerName, poSRS, eType);
3354
0
    if (poSRS)
3355
0
    {
3356
0
        poSRS->Release();
3357
0
    }
3358
3359
0
    if (CPLFetchBool(papszOptions, "ADVERTIZE_UTF8", false))
3360
0
        poLayer->SetAdvertizeUTF8(true);
3361
3362
0
    poLayer->SetDataset(this);
3363
0
    poLayer->SetFIDColumn(CSLFetchNameValueDef(papszOptions, "FID", ""));
3364
3365
    // Add layer to data source layer list.
3366
0
    m_apoLayers.emplace_back(std::move(poLayer));
3367
0
    return m_apoLayers.back().get();
3368
0
}
3369
3370
/************************************************************************/
3371
/*                            DeleteLayer()                             */
3372
/************************************************************************/
3373
3374
OGRErr MEMDataset::DeleteLayer(int iLayer)
3375
3376
0
{
3377
0
    if (iLayer >= 0 && iLayer < static_cast<int>(m_apoLayers.size()))
3378
0
    {
3379
0
        m_apoLayers.erase(m_apoLayers.begin() + iLayer);
3380
0
        return OGRERR_NONE;
3381
0
    }
3382
3383
0
    return OGRERR_FAILURE;
3384
0
}
3385
3386
/************************************************************************/
3387
/*                           TestCapability()                           */
3388
/************************************************************************/
3389
3390
int MEMDataset::TestCapability(const char *pszCap) const
3391
3392
0
{
3393
0
    if (EQUAL(pszCap, ODsCCreateLayer) || EQUAL(pszCap, ODsCDeleteLayer) ||
3394
0
        EQUAL(pszCap, ODsCCreateGeomFieldAfterCreateLayer) ||
3395
0
        EQUAL(pszCap, ODsCCurveGeometries) ||
3396
0
        EQUAL(pszCap, ODsCMeasuredGeometries) ||
3397
0
        EQUAL(pszCap, ODsCZGeometries) || EQUAL(pszCap, ODsCRandomLayerWrite) ||
3398
0
        EQUAL(pszCap, ODsCAddFieldDomain) ||
3399
0
        EQUAL(pszCap, ODsCDeleteFieldDomain) ||
3400
0
        EQUAL(pszCap, ODsCUpdateFieldDomain) ||
3401
0
        EQUAL(pszCap, GDsCAddRelationship) ||
3402
0
        EQUAL(pszCap, GDsCDeleteRelationship) ||
3403
0
        EQUAL(pszCap, GDsCUpdateRelationship))
3404
0
    {
3405
0
        return true;
3406
0
    }
3407
3408
0
    return GDALDataset::TestCapability(pszCap);
3409
0
}
3410
3411
/************************************************************************/
3412
/*                              GetLayer()                              */
3413
/************************************************************************/
3414
3415
const OGRLayer *MEMDataset::GetLayer(int iLayer) const
3416
3417
0
{
3418
0
    if (iLayer < 0 || iLayer >= static_cast<int>(m_apoLayers.size()))
3419
0
        return nullptr;
3420
3421
0
    return m_apoLayers[iLayer].get();
3422
0
}
3423
3424
/************************************************************************/
3425
/*                           AddFieldDomain()                           */
3426
/************************************************************************/
3427
3428
bool MEMDataset::AddFieldDomain(std::unique_ptr<OGRFieldDomain> &&domain,
3429
                                std::string &failureReason)
3430
0
{
3431
0
    if (GetFieldDomain(domain->GetName()) != nullptr)
3432
0
    {
3433
0
        failureReason = "A domain of identical name already exists";
3434
0
        return false;
3435
0
    }
3436
0
    const std::string domainName(domain->GetName());
3437
0
    m_oMapFieldDomains[domainName] = std::move(domain);
3438
0
    return true;
3439
0
}
3440
3441
/************************************************************************/
3442
/*                         DeleteFieldDomain()                          */
3443
/************************************************************************/
3444
3445
bool MEMDataset::DeleteFieldDomain(const std::string &name,
3446
                                   std::string &failureReason)
3447
0
{
3448
0
    const auto iter = m_oMapFieldDomains.find(name);
3449
0
    if (iter == m_oMapFieldDomains.end())
3450
0
    {
3451
0
        failureReason = "Domain does not exist";
3452
0
        return false;
3453
0
    }
3454
3455
0
    m_oMapFieldDomains.erase(iter);
3456
3457
0
    for (auto &poLayer : m_apoLayers)
3458
0
    {
3459
0
        for (int j = 0; j < poLayer->GetLayerDefn()->GetFieldCount(); ++j)
3460
0
        {
3461
0
            OGRLayer *poLayerAsLayer = poLayer.get();
3462
0
            OGRFieldDefn *poFieldDefn =
3463
0
                poLayerAsLayer->GetLayerDefn()->GetFieldDefn(j);
3464
0
            if (poFieldDefn->GetDomainName() == name)
3465
0
            {
3466
0
                whileUnsealing(poFieldDefn)->SetDomainName(std::string());
3467
0
            }
3468
0
        }
3469
0
    }
3470
3471
0
    return true;
3472
0
}
3473
3474
/************************************************************************/
3475
/*                         UpdateFieldDomain()                          */
3476
/************************************************************************/
3477
3478
bool MEMDataset::UpdateFieldDomain(std::unique_ptr<OGRFieldDomain> &&domain,
3479
                                   std::string &failureReason)
3480
0
{
3481
0
    const std::string domainName(domain->GetName());
3482
0
    const auto iter = m_oMapFieldDomains.find(domainName);
3483
0
    if (iter == m_oMapFieldDomains.end())
3484
0
    {
3485
0
        failureReason = "No matching domain found";
3486
0
        return false;
3487
0
    }
3488
0
    m_oMapFieldDomains[domainName] = std::move(domain);
3489
0
    return true;
3490
0
}
3491
3492
/************************************************************************/
3493
/*                        GetRelationshipNames()                        */
3494
/************************************************************************/
3495
3496
std::vector<std::string> MEMDataset::GetRelationshipNames(CSLConstList) const
3497
0
{
3498
0
    std::vector<std::string> ret;
3499
0
    for (const auto &kv : m_poPrivate->m_oMapRelationships)
3500
0
        ret.push_back(kv.first);
3501
0
    return ret;
3502
0
}
3503
3504
/************************************************************************/
3505
/*                          GetRelationship()                           */
3506
/************************************************************************/
3507
3508
const GDALRelationship *
3509
MEMDataset::GetRelationship(const std::string &name) const
3510
0
{
3511
0
    const auto iter = m_poPrivate->m_oMapRelationships.find(name);
3512
0
    if (iter != m_poPrivate->m_oMapRelationships.end())
3513
0
        return iter->second.get();
3514
0
    return nullptr;
3515
0
}
3516
3517
/************************************************************************/
3518
/*                          AddRelationship()                           */
3519
/************************************************************************/
3520
3521
bool MEMDataset::AddRelationship(
3522
    std::unique_ptr<GDALRelationship> &&relationship,
3523
    std::string &failureReason)
3524
0
{
3525
0
    const std::string osName(relationship->GetName());
3526
0
    const auto iter = m_poPrivate->m_oMapRelationships.find(osName);
3527
0
    if (iter != m_poPrivate->m_oMapRelationships.end())
3528
0
    {
3529
0
        failureReason = "A relationship of identical name already exists";
3530
0
        return false;
3531
0
    }
3532
0
    m_poPrivate->m_oMapRelationships[osName] = std::move(relationship);
3533
0
    return true;
3534
0
}
3535
3536
/************************************************************************/
3537
/*                         DeleteRelationship()                         */
3538
/************************************************************************/
3539
3540
bool MEMDataset::DeleteRelationship(const std::string &name,
3541
                                    std::string &failureReason)
3542
0
{
3543
0
    const auto iter = m_poPrivate->m_oMapRelationships.find(name);
3544
0
    if (iter == m_poPrivate->m_oMapRelationships.end())
3545
0
    {
3546
0
        failureReason = "No matching relationship found";
3547
0
        return false;
3548
0
    }
3549
0
    m_poPrivate->m_oMapRelationships.erase(iter);
3550
0
    return true;
3551
0
}
3552
3553
/************************************************************************/
3554
/*                         UpdateRelationship()                         */
3555
/************************************************************************/
3556
3557
bool MEMDataset::UpdateRelationship(
3558
    std::unique_ptr<GDALRelationship> &&relationship,
3559
    std::string &failureReason)
3560
0
{
3561
0
    const std::string osName(relationship->GetName());
3562
0
    const auto iter = m_poPrivate->m_oMapRelationships.find(osName);
3563
0
    if (iter == m_poPrivate->m_oMapRelationships.end())
3564
0
    {
3565
0
        failureReason = "No matching relationship found";
3566
0
        return false;
3567
0
    }
3568
0
    iter->second = std::move(relationship);
3569
0
    return true;
3570
0
}
3571
3572
/************************************************************************/
3573
/*                             ExecuteSQL()                             */
3574
/************************************************************************/
3575
3576
OGRLayer *MEMDataset::ExecuteSQL(const char *pszStatement,
3577
                                 OGRGeometry *poSpatialFilter,
3578
                                 const char *pszDialect)
3579
0
{
3580
0
    if (EQUAL(pszStatement, "PRAGMA read_only=1"))  // as used by VDV driver
3581
0
    {
3582
0
        for (auto &poLayer : m_apoLayers)
3583
0
            poLayer->SetUpdatable(false);
3584
0
        return nullptr;
3585
0
    }
3586
0
    return GDALDataset::ExecuteSQL(pszStatement, poSpatialFilter, pszDialect);
3587
0
}
3588
3589
/************************************************************************/
3590
/*                          GDALRegister_MEM()                          */
3591
/************************************************************************/
3592
3593
void GDALRegister_MEM()
3594
0
{
3595
0
    auto poDM = GetGDALDriverManager();
3596
0
    if (poDM->GetDriverByName("MEM") != nullptr)
3597
0
        return;
3598
3599
0
    GDALDriver *poDriver = new GDALDriver();
3600
3601
0
    poDriver->SetDescription("MEM");
3602
0
    poDriver->SetMetadataItem(GDAL_DCAP_RASTER, "YES");
3603
0
    poDriver->SetMetadataItem(GDAL_DCAP_MULTIDIM_RASTER, "YES");
3604
0
    poDriver->SetMetadataItem(
3605
0
        GDAL_DMD_LONGNAME,
3606
0
        "In Memory raster, vector and multidimensional raster");
3607
0
    poDriver->SetMetadataItem(
3608
0
        GDAL_DMD_CREATIONDATATYPES,
3609
0
        "Byte Int8 Int16 UInt16 Int32 UInt32 Int64 UInt64 Float32 Float64 "
3610
0
        "CInt16 CInt32 CFloat32 CFloat64");
3611
0
    poDriver->SetMetadataItem(GDAL_DCAP_COORDINATE_EPOCH, "YES");
3612
3613
0
    poDriver->SetMetadataItem(
3614
0
        GDAL_DMD_CREATIONOPTIONLIST,
3615
0
        "<CreationOptionList>"
3616
0
        "   <Option name='INTERLEAVE' type='string-select' default='BAND'>"
3617
0
        "       <Value>BAND</Value>"
3618
0
        "       <Value>PIXEL</Value>"
3619
0
        "   </Option>"
3620
0
        "  <Option name='NBITS' type='int' description='Bit depth per band'/>"
3621
0
        "</CreationOptionList>");
3622
3623
0
    poDriver->SetMetadataItem(GDAL_DCAP_VECTOR, "YES");
3624
0
    poDriver->SetMetadataItem(GDAL_DCAP_CREATE_LAYER, "YES");
3625
0
    poDriver->SetMetadataItem(GDAL_DCAP_DELETE_LAYER, "YES");
3626
0
    poDriver->SetMetadataItem(GDAL_DCAP_CREATE_FIELD, "YES");
3627
0
    poDriver->SetMetadataItem(GDAL_DCAP_DELETE_FIELD, "YES");
3628
0
    poDriver->SetMetadataItem(GDAL_DCAP_REORDER_FIELDS, "YES");
3629
0
    poDriver->SetMetadataItem(GDAL_DCAP_CURVE_GEOMETRIES, "YES");
3630
0
    poDriver->SetMetadataItem(GDAL_DCAP_MEASURED_GEOMETRIES, "YES");
3631
0
    poDriver->SetMetadataItem(GDAL_DCAP_Z_GEOMETRIES, "YES");
3632
0
    poDriver->SetMetadataItem(GDAL_DMD_SUPPORTED_SQL_DIALECTS, "OGRSQL SQLITE");
3633
3634
0
    poDriver->SetMetadataItem(GDAL_DCAP_RELATIONSHIPS, "YES");
3635
0
    poDriver->SetMetadataItem(GDAL_DCAP_CREATE_RELATIONSHIP, "YES");
3636
0
    poDriver->SetMetadataItem(GDAL_DCAP_DELETE_RELATIONSHIP, "YES");
3637
0
    poDriver->SetMetadataItem(GDAL_DCAP_UPDATE_RELATIONSHIP, "YES");
3638
0
    poDriver->SetMetadataItem(
3639
0
        GDAL_DMD_RELATIONSHIP_FLAGS,
3640
0
        "OneToOne OneToMany ManyToOne ManyToMany Composite Association "
3641
0
        "Aggregation ForwardPathLabel MultipleFieldKeys BackwardPathLabel");
3642
3643
0
    poDriver->SetMetadataItem(
3644
0
        GDAL_DMD_CREATIONFIELDDATATYPES,
3645
0
        "Integer Integer64 Real String Date DateTime Time IntegerList "
3646
0
        "Integer64List RealList StringList Binary");
3647
0
    poDriver->SetMetadataItem(GDAL_DMD_CREATIONFIELDDATASUBTYPES,
3648
0
                              "Boolean Int16 Float32 JSON UUID");
3649
0
    poDriver->SetMetadataItem(GDAL_DMD_CREATION_FIELD_DEFN_FLAGS,
3650
0
                              "WidthPrecision Nullable Default Unique "
3651
0
                              "Comment AlternativeName Domain");
3652
0
    poDriver->SetMetadataItem(GDAL_DMD_ALTER_FIELD_DEFN_FLAGS,
3653
0
                              "Name Type WidthPrecision Nullable Default "
3654
0
                              "Unique Domain AlternativeName Comment");
3655
3656
0
    poDriver->SetMetadataItem(
3657
0
        GDAL_DS_LAYER_CREATIONOPTIONLIST,
3658
0
        "<LayerCreationOptionList>"
3659
0
        "  <Option name='ADVERTIZE_UTF8' type='boolean' description='Whether "
3660
0
        "the layer will contain UTF-8 strings' default='NO'/>"
3661
0
        "  <Option name='FID' type='string' description="
3662
0
        "'Name of the FID column to create' default='' />"
3663
0
        "</LayerCreationOptionList>");
3664
3665
0
    poDriver->SetMetadataItem(GDAL_DCAP_COORDINATE_EPOCH, "YES");
3666
0
    poDriver->SetMetadataItem(GDAL_DCAP_MULTIPLE_VECTOR_LAYERS, "YES");
3667
3668
0
    poDriver->SetMetadataItem(GDAL_DCAP_FIELD_DOMAINS, "YES");
3669
0
    poDriver->SetMetadataItem(GDAL_DMD_CREATION_FIELD_DOMAIN_TYPES,
3670
0
                              "Coded Range Glob");
3671
3672
0
    poDriver->SetMetadataItem(GDAL_DMD_ALTER_GEOM_FIELD_DEFN_FLAGS,
3673
0
                              "Name Type Nullable SRS CoordinateEpoch");
3674
0
    poDriver->SetMetadataItem(GDAL_DCAP_UPSERT, "YES");
3675
3676
    // Define GDAL_NO_OPEN_FOR_MEM_DRIVER macro to undefine Open() method for
3677
    // MEM driver.  Otherwise, bad user input can trigger easily a GDAL crash
3678
    // as random pointers can be passed as a string.  All code in GDAL tree
3679
    // using the MEM driver use the Create() method only, so Open() is not
3680
    // needed, except for esoteric uses.
3681
0
#ifndef GDAL_NO_OPEN_FOR_MEM_DRIVER
3682
0
    poDriver->pfnOpen = MEMDataset::Open;
3683
0
    poDriver->pfnIdentify = MEMDatasetIdentify;
3684
0
#endif
3685
0
    poDriver->pfnCreate = MEMDataset::CreateBase;
3686
0
    poDriver->pfnCreateMultiDimensional = MEMDataset::CreateMultiDimensional;
3687
0
    poDriver->pfnDelete = MEMDatasetDelete;
3688
3689
0
    poDM->RegisterDriver(poDriver);
3690
0
}