• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OSGeo / gdal / 15899162844

26 Jun 2025 10:14AM UTC coverage: 71.088% (+0.004%) from 71.084%
15899162844

Pull #12623

github

web-flow
Merge c704a8392 into f5cb024d4
Pull Request #12623: gdal raster overview add: add a --overview-src option

209 of 244 new or added lines in 5 files covered. (85.66%)

96 existing lines in 44 files now uncovered.

574014 of 807474 relevant lines covered (71.09%)

250815.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.31
/frmts/xyz/xyzdataset.cpp
1
/******************************************************************************
2
 *
3
 * Project:  XYZ driver
4
 * Purpose:  GDALDataset driver for XYZ dataset.
5
 * Author:   Even Rouault, <even dot rouault at spatialys.com>
6
 *
7
 ******************************************************************************
8
 * Copyright (c) 2010-2013, Even Rouault <even dot rouault at spatialys.com>
9
 *
10
 * SPDX-License-Identifier: MIT
11
 ****************************************************************************/
12

13
#include "cpl_string.h"
14
#include "cpl_vsi_virtual.h"
15
#include "gdal_frmts.h"
16
#include "gdal_pam.h"
17

18
#include <algorithm>
19
#include <cmath>
20
#include <mutex>
21
#include <vector>
22

23
constexpr double RELATIVE_ERROR = 1e-3;
24

25
class XYZDataset;
26

27
// Global cache when we must ingest all grid points
28
static std::mutex gMutex;
29
static XYZDataset *gpoActiveDS = nullptr;
30
static std::vector<short> gasValues;
31
static std::vector<float> gafValues;
32

33
/************************************************************************/
34
/* ==================================================================== */
35
/*                              XYZDataset                              */
36
/* ==================================================================== */
37
/************************************************************************/
38

39
class XYZRasterBand;
40

41
class XYZDataset final : public GDALPamDataset
42
{
43
    friend class XYZRasterBand;
44

45
    VSILFILE *fp;
46
    int bHasHeaderLine;
47
    int nCommentLineCount;
48
    char chDecimalSep;
49
    int nXIndex;
50
    int nYIndex;
51
    int nZIndex;
52
    int nMinTokens;
53
    GIntBig nLineNum;     /* any line */
54
    GIntBig nDataLineNum; /* line with values (header line and empty lines
55
                             ignored) */
56
    GDALGeoTransform m_gt{};
57
    int bSameNumberOfValuesPerLine;
58
    double dfMinZ;
59
    double dfMaxZ;
60
    bool bEOF;
61
    bool bIngestAll = false;
62

63
    static int IdentifyEx(GDALOpenInfo *, int &, int &nCommentLineCount,
64
                          int &nXIndex, int &nYIndex, int &nZIndex);
65

66
    CPL_DISALLOW_COPY_ASSIGN(XYZDataset)
67

68
  public:
69
    XYZDataset();
70
    virtual ~XYZDataset();
71

72
    virtual CPLErr GetGeoTransform(GDALGeoTransform &gt) const override;
73

74
    static GDALDataset *Open(GDALOpenInfo *);
75
    static int Identify(GDALOpenInfo *);
76
    static GDALDataset *CreateCopy(const char *pszFilename,
77
                                   GDALDataset *poSrcDS, int bStrict,
78
                                   char **papszOptions,
79
                                   GDALProgressFunc pfnProgress,
80
                                   void *pProgressData);
81
};
82

83
/************************************************************************/
84
/* ==================================================================== */
85
/*                            XYZRasterBand                             */
86
/* ==================================================================== */
87
/************************************************************************/
88

89
class XYZRasterBand final : public GDALPamRasterBand
90
{
91
    friend class XYZDataset;
92

93
    int nLastYOff;
94

95
  public:
96
    XYZRasterBand(XYZDataset *, int, GDALDataType);
97

98
    virtual CPLErr IReadBlock(int, int, void *) override;
99
    virtual double GetMinimum(int *pbSuccess = nullptr) override;
100
    virtual double GetMaximum(int *pbSuccess = nullptr) override;
101
    virtual double GetNoDataValue(int *pbSuccess = nullptr) override;
102
};
103

104
/************************************************************************/
105
/*                           XYZRasterBand()                            */
106
/************************************************************************/
107

108
XYZRasterBand::XYZRasterBand(XYZDataset *poDSIn, int nBandIn, GDALDataType eDT)
38✔
109
    : nLastYOff(-1)
38✔
110
{
111
    poDS = poDSIn;
38✔
112
    nBand = nBandIn;
38✔
113

114
    eDataType = eDT;
38✔
115

116
    nBlockXSize = poDSIn->GetRasterXSize();
38✔
117
    nBlockYSize = 1;
38✔
118
}
38✔
119

120
/************************************************************************/
121
/*                             IReadBlock()                             */
122
/************************************************************************/
123

124
CPLErr XYZRasterBand::IReadBlock(CPL_UNUSED int nBlockXOff, int nBlockYOff,
318✔
125
                                 void *pImage)
126
{
127
    XYZDataset *poGDS = reinterpret_cast<XYZDataset *>(poDS);
318✔
128

129
    if (poGDS->fp == nullptr)
318✔
130
        return CE_Failure;
×
131

132
    if (poGDS->bIngestAll)
318✔
133
    {
134
        CPLAssert(eDataType == GDT_Int16 || eDataType == GDT_Float32);
12✔
135

136
        std::lock_guard<std::mutex> guard(gMutex);
24✔
137

138
        if (gpoActiveDS != poGDS || (gasValues.empty() && gafValues.empty()))
12✔
139
        {
140
            gpoActiveDS = poGDS;
4✔
141

142
            const int nGridSize = nRasterXSize * nRasterYSize;
4✔
143
            try
144
            {
145
                if (eDataType == GDT_Int16)
4✔
146
                    gasValues.resize(nGridSize);
2✔
147
                else
148
                    gafValues.resize(nGridSize);
2✔
149
            }
150
            catch (const std::exception &)
×
151
            {
152
                CPLError(CE_Failure, CPLE_OutOfMemory, "Cannot allocate grid");
×
153
                return CE_Failure;
×
154
            }
155

156
            poGDS->nDataLineNum = 0;
4✔
157
            poGDS->nLineNum = 0;
4✔
158
            poGDS->bEOF = false;
4✔
159
            VSIFSeekL(poGDS->fp, 0, SEEK_SET);
4✔
160

161
            for (int i = 0; i < poGDS->nCommentLineCount; i++)
4✔
162
            {
163
                if (CPLReadLine2L(poGDS->fp, 100, nullptr) == nullptr)
×
164
                {
165
                    poGDS->bEOF = true;
×
166
                    return CE_Failure;
×
167
                }
168
                poGDS->nLineNum++;
×
169
            }
170

171
            if (poGDS->bHasHeaderLine)
4✔
172
            {
173
                const char *pszLine = CPLReadLine2L(poGDS->fp, 100, nullptr);
4✔
174
                if (pszLine == nullptr)
4✔
175
                {
176
                    poGDS->bEOF = true;
×
177
                    return CE_Failure;
×
178
                }
179
                poGDS->nLineNum++;
4✔
180
            }
181

182
            for (int i = 0; i < nGridSize; i++)
34✔
183
            {
184
                const char *pszLine = CPLReadLine2L(poGDS->fp, 100, nullptr);
30✔
185
                if (pszLine == nullptr)
30✔
186
                {
187
                    poGDS->bEOF = true;
×
188
                    CPLError(CE_Failure, CPLE_AppDefined,
×
189
                             "Cannot read line " CPL_FRMT_GIB,
190
                             poGDS->nLineNum + 1);
×
191
                    return CE_Failure;
×
192
                }
193
                poGDS->nLineNum++;
30✔
194

195
                const char *pszPtr = pszLine;
30✔
196
                char ch;
197
                int nCol = 0;
30✔
198
                bool bLastWasSep = true;
30✔
199
                double dfX = 0.0;
30✔
200
                double dfY = 0.0;
30✔
201
                double dfZ = 0.0;
30✔
202
                int nUsefulColsFound = 0;
30✔
203
                while ((ch = *pszPtr) != '\0')
326✔
204
                {
205
                    if (ch == ' ')
296✔
206
                    {
207
                        if (!bLastWasSep)
60✔
208
                            nCol++;
60✔
209
                        bLastWasSep = true;
60✔
210
                    }
211
                    else if ((ch == ',' && poGDS->chDecimalSep != ',') ||
236✔
212
                             ch == '\t' || ch == ';')
236✔
213
                    {
214
                        nCol++;
×
215
                        bLastWasSep = true;
×
216
                    }
217
                    else
218
                    {
219
                        if (bLastWasSep)
236✔
220
                        {
221
                            if (nCol == poGDS->nXIndex)
90✔
222
                            {
223
                                nUsefulColsFound++;
30✔
224
                                dfX = CPLAtofDelim(pszPtr, poGDS->chDecimalSep);
30✔
225
                            }
226
                            else if (nCol == poGDS->nYIndex)
60✔
227
                            {
228
                                nUsefulColsFound++;
30✔
229
                                dfY = CPLAtofDelim(pszPtr, poGDS->chDecimalSep);
30✔
230
                            }
231
                            else if (nCol == poGDS->nZIndex)
30✔
232
                            {
233
                                nUsefulColsFound++;
30✔
234
                                dfZ = CPLAtofDelim(pszPtr, poGDS->chDecimalSep);
30✔
235
                            }
236
                        }
237
                        bLastWasSep = false;
236✔
238
                    }
239
                    pszPtr++;
296✔
240
                }
241

242
                /* Skip empty line */
243
                if (nCol == 0 && bLastWasSep)
30✔
244
                    continue;
×
245

246
                if (nUsefulColsFound != 3)
30✔
247
                {
248
                    CPLError(
×
249
                        CE_Failure, CPLE_AppDefined,
250
                        "Unexpected number of values at line " CPL_FRMT_GIB,
251
                        poGDS->nLineNum);
252
                    return CE_Failure;
×
253
                }
254

255
                poGDS->nDataLineNum++;
30✔
256

257
                const int nX = static_cast<int>(
258
                    (dfX - 0.5 * poGDS->m_gt[1] - poGDS->m_gt[0]) /
30✔
259
                        poGDS->m_gt[1] +
30✔
260
                    0.5);
30✔
261
                const int nY = static_cast<int>(
262
                    (dfY - 0.5 * poGDS->m_gt[5] - poGDS->m_gt[3]) /
30✔
263
                        poGDS->m_gt[5] +
30✔
264
                    0.5);
30✔
265
                if (nX < 0 || nX >= nRasterXSize)
30✔
266
                {
267
                    CPLError(CE_Failure, CPLE_AppDefined,
×
268
                             "Unexpected X value at line " CPL_FRMT_GIB,
269
                             poGDS->nLineNum);
270
                    return CE_Failure;
×
271
                }
272
                if (nY < 0 || nY >= nRasterYSize)
30✔
273
                {
274
                    CPLError(CE_Failure, CPLE_AppDefined,
×
275
                             "Unexpected Y value at line " CPL_FRMT_GIB,
276
                             poGDS->nLineNum);
277
                    return CE_Failure;
×
278
                }
279
                const int nIdx = nX + nY * nRasterXSize;
30✔
280
                if (eDataType == GDT_Int16)
30✔
281
                    gasValues[nIdx] = static_cast<short>(0.5 + dfZ);
15✔
282
                else
283
                    gafValues[nIdx] = static_cast<float>(dfZ);
15✔
284
            }
285
        }
286

287
        if (eDataType == GDT_Int16)
12✔
288
            memcpy(pImage,
6✔
289
                   &gasValues[static_cast<size_t>(nBlockYOff) * nBlockXSize],
6✔
290
                   sizeof(short) * nBlockXSize);
6✔
291
        else
292
            memcpy(pImage,
6✔
293
                   &gafValues[static_cast<size_t>(nBlockYOff) * nBlockXSize],
6✔
294
                   sizeof(float) * nBlockXSize);
6✔
295
        return CE_None;
12✔
296
    }
297

298
    if (pImage)
306✔
299
    {
300
        int bSuccess = FALSE;
306✔
301
        double dfNoDataValue = GetNoDataValue(&bSuccess);
306✔
302
        if (!bSuccess)
306✔
303
            dfNoDataValue = 0.0;
284✔
304
        GDALCopyWords(&dfNoDataValue, GDT_Float64, 0, pImage, eDataType,
306✔
305
                      GDALGetDataTypeSizeBytes(eDataType), nRasterXSize);
306
    }
307

308
    // Only valid if bSameNumberOfValuesPerLine.
309
    const GIntBig nLineInFile = static_cast<GIntBig>(nBlockYOff) * nBlockXSize;
306✔
310
    if ((poGDS->bSameNumberOfValuesPerLine &&
306✔
311
         poGDS->nDataLineNum > nLineInFile) ||
284✔
312
        (!poGDS->bSameNumberOfValuesPerLine &&
289✔
313
         (nLastYOff == -1 || nBlockYOff == 0)))
22✔
314
    {
315
        poGDS->nDataLineNum = 0;
22✔
316
        poGDS->nLineNum = 0;
22✔
317
        poGDS->bEOF = false;
22✔
318
        VSIFSeekL(poGDS->fp, 0, SEEK_SET);
22✔
319

320
        for (int i = 0; i < poGDS->nCommentLineCount; i++)
22✔
321
        {
322
            if (CPLReadLine2L(poGDS->fp, 100, nullptr) == nullptr)
×
323
            {
324
                poGDS->bEOF = true;
×
325
                return CE_Failure;
×
326
            }
327
            poGDS->nLineNum++;
×
328
        }
329

330
        if (poGDS->bHasHeaderLine)
22✔
331
        {
332
            const char *pszLine = CPLReadLine2L(poGDS->fp, 100, nullptr);
14✔
333
            if (pszLine == nullptr)
14✔
334
            {
335
                poGDS->bEOF = true;
×
336
                return CE_Failure;
×
337
            }
338
            poGDS->nLineNum++;
14✔
339
        }
340
    }
341

342
    if (!poGDS->bSameNumberOfValuesPerLine)
306✔
343
    {
344
        if (nBlockYOff < nLastYOff)
22✔
345
        {
346
            nLastYOff = -1;
×
347
            for (int iY = 0; iY < nBlockYOff; iY++)
×
348
            {
349
                if (IReadBlock(0, iY, nullptr) != CE_None)
×
350
                    return CE_Failure;
×
351
            }
352
        }
353
        else
354
        {
355
            if (poGDS->bEOF)
22✔
356
            {
357
                return CE_Failure;
×
358
            }
359
            for (int iY = nLastYOff + 1; iY < nBlockYOff; iY++)
22✔
360
            {
361
                if (IReadBlock(0, iY, nullptr) != CE_None)
×
362
                    return CE_Failure;
×
363
            }
364
        }
365
    }
366
    else
367
    {
368
        if (poGDS->bEOF)
284✔
369
        {
370
            return CE_Failure;
×
371
        }
372
        while (poGDS->nDataLineNum < nLineInFile)
310✔
373
        {
374
            const char *pszLine = CPLReadLine2L(poGDS->fp, 100, nullptr);
26✔
375
            if (pszLine == nullptr)
26✔
376
            {
377
                poGDS->bEOF = true;
×
378
                return CE_Failure;
×
379
            }
380
            poGDS->nLineNum++;
26✔
381

382
            const char *pszPtr = pszLine;
26✔
383
            char ch;
384
            int nCol = 0;
26✔
385
            bool bLastWasSep = true;
26✔
386
            while ((ch = *pszPtr) != '\0')
146✔
387
            {
388
                if (ch == ' ')
120✔
389
                {
390
                    if (!bLastWasSep)
40✔
391
                        nCol++;
40✔
392
                    bLastWasSep = true;
40✔
393
                }
394
                else if ((ch == ',' && poGDS->chDecimalSep != ',') ||
80✔
395
                         ch == '\t' || ch == ';')
80✔
396
                {
397
                    nCol++;
×
398
                    bLastWasSep = true;
×
399
                }
400
                else
401
                {
402
                    bLastWasSep = false;
80✔
403
                }
404
                pszPtr++;
120✔
405
            }
406

407
            /* Skip empty line */
408
            if (nCol == 0 && bLastWasSep)
26✔
409
                continue;
6✔
410

411
            poGDS->nDataLineNum++;
20✔
412
        }
413
    }
414

415
    const double dfExpectedY =
416
        poGDS->m_gt[3] + (0.5 + nBlockYOff) * poGDS->m_gt[5];
306✔
417

418
    int idx = -1;
306✔
419
    while (true)
420
    {
421
        int nCol;
422
        bool bLastWasSep;
423
        do
7✔
424
        {
425
            const vsi_l_offset nOffsetBefore = VSIFTellL(poGDS->fp);
41,702✔
426
            const char *pszLine = CPLReadLine2L(poGDS->fp, 100, nullptr);
41,702✔
427
            if (pszLine == nullptr)
41,702✔
428
            {
429
                poGDS->bEOF = true;
1✔
430
                if (poGDS->bSameNumberOfValuesPerLine)
1✔
431
                {
432
                    CPLError(CE_Failure, CPLE_AppDefined,
×
433
                             "Cannot read line " CPL_FRMT_GIB,
434
                             poGDS->nLineNum + 1);
×
435
                    return CE_Failure;
×
436
                }
437
                else
438
                {
439
                    nLastYOff = nBlockYOff;
1✔
440
                }
441
                return CE_None;
1✔
442
            }
443
            poGDS->nLineNum++;
41,701✔
444

445
            const char *pszPtr = pszLine;
41,701✔
446
            char ch;
447
            nCol = 0;
41,701✔
448
            bLastWasSep = true;
41,701✔
449
            double dfX = 0.0;
41,701✔
450
            double dfY = 0.0;
41,701✔
451
            double dfZ = 0.0;
41,701✔
452
            int nUsefulColsFound = 0;
41,701✔
453
            while ((ch = *pszPtr) != '\0')
1,353,710✔
454
            {
455
                if (ch == ' ')
1,312,010✔
456
                {
457
                    if (!bLastWasSep)
2,706✔
458
                        nCol++;
2,586✔
459
                    bLastWasSep = true;
2,706✔
460
                }
461
                else if ((ch == ',' && poGDS->chDecimalSep != ',') ||
1,309,300✔
462
                         ch == '\t' || ch == ';')
1,228,500✔
463
                {
464
                    nCol++;
80,802✔
465
                    bLastWasSep = true;
80,802✔
466
                }
467
                else
468
                {
469
                    if (bLastWasSep)
1,228,500✔
470
                    {
471
                        if (nCol == poGDS->nXIndex)
125,082✔
472
                        {
473
                            nUsefulColsFound++;
41,694✔
474
                            if (!poGDS->bSameNumberOfValuesPerLine)
41,694✔
475
                                dfX = CPLAtofDelim(pszPtr, poGDS->chDecimalSep);
36✔
476
                        }
477
                        else if (nCol == poGDS->nYIndex)
83,388✔
478
                        {
479
                            nUsefulColsFound++;
41,694✔
480
                            if (!poGDS->bSameNumberOfValuesPerLine)
41,694✔
481
                                dfY = CPLAtofDelim(pszPtr, poGDS->chDecimalSep);
36✔
482
                        }
483
                        else if (nCol == poGDS->nZIndex)
41,694✔
484
                        {
485
                            nUsefulColsFound++;
41,694✔
486
                            dfZ = CPLAtofDelim(pszPtr, poGDS->chDecimalSep);
41,694✔
487
                        }
488
                    }
489
                    bLastWasSep = false;
1,228,500✔
490
                }
491
                pszPtr++;
1,312,010✔
492
            }
493
            nCol++;
41,701✔
494

495
            if (nUsefulColsFound == 3)
41,701✔
496
            {
497
                if (poGDS->bSameNumberOfValuesPerLine)
41,694✔
498
                {
499
                    idx++;
41,658✔
500
                }
501
                else
502
                {
503
                    if (fabs((dfY - dfExpectedY) / poGDS->m_gt[5]) >
36✔
504
                        RELATIVE_ERROR)
505
                    {
506
                        if (idx < 0)
5✔
507
                        {
508
                            const double dfYDeltaOrigin =
509
                                dfY + 0.5 * poGDS->m_gt[5] - poGDS->m_gt[3];
3✔
510
                            if (!(fabs(dfYDeltaOrigin) > fabs(poGDS->m_gt[5]) &&
6✔
511
                                  fabs(std::round(dfYDeltaOrigin /
6✔
512
                                                  poGDS->m_gt[5]) -
3✔
513
                                       (dfYDeltaOrigin / poGDS->m_gt[5])) <=
3✔
514
                                      RELATIVE_ERROR))
515
                            {
516
                                CPLError(CE_Failure, CPLE_AppDefined,
×
517
                                         "At line " CPL_FRMT_GIB
518
                                         ", found Y=%f instead of %f "
519
                                         "for nBlockYOff = %d",
520
                                         poGDS->nLineNum, dfY, dfExpectedY,
521
                                         nBlockYOff);
522
                                return CE_Failure;
×
523
                            }
524
                        }
525
                        VSIFSeekL(poGDS->fp, nOffsetBefore, SEEK_SET);
5✔
526
                        nLastYOff = nBlockYOff;
5✔
527
                        poGDS->nLineNum--;
5✔
528
                        return CE_None;
5✔
529
                    }
530

531
                    idx = static_cast<int>(
31✔
532
                        (dfX - 0.5 * poGDS->m_gt[1] - poGDS->m_gt[0]) /
31✔
533
                            poGDS->m_gt[1] +
31✔
534
                        0.5);
535
                }
536
                CPLAssert(idx >= 0 && idx < nRasterXSize);
41,689✔
537

538
                if (pImage)
41,689✔
539
                {
540
                    if (eDataType == GDT_Float32)
41,689✔
541
                    {
542
                        reinterpret_cast<float *>(pImage)[idx] =
40,416✔
543
                            static_cast<float>(dfZ);
40,416✔
544
                    }
545
                    else if (eDataType == GDT_Int32)
1,273✔
546
                    {
547
                        reinterpret_cast<GInt32 *>(pImage)[idx] =
400✔
548
                            static_cast<GInt32>(dfZ);
400✔
549
                    }
550
                    else if (eDataType == GDT_Int16)
873✔
551
                    {
552
                        reinterpret_cast<GInt16 *>(pImage)[idx] =
13✔
553
                            static_cast<GInt16>(dfZ);
13✔
554
                    }
555
                    else
556
                    {
557
                        reinterpret_cast<GByte *>(pImage)[idx] =
860✔
558
                            static_cast<GByte>(dfZ);
860✔
559
                    }
560
                }
561
            }
562
            /* Skip empty line */
563
        } while (nCol == 1 && bLastWasSep);
41,696✔
564

565
        poGDS->nDataLineNum++;
41,689✔
566
        if (nCol < poGDS->nMinTokens)
41,689✔
567
            return CE_Failure;
×
568

569
        if (idx + 1 == nRasterXSize)
41,689✔
570
            break;
300✔
571
    }
41,389✔
572

573
    if (poGDS->bSameNumberOfValuesPerLine)
300✔
574
    {
575
        if (poGDS->nDataLineNum !=
284✔
576
            static_cast<GIntBig>(nBlockYOff + 1) * nBlockXSize)
284✔
577
        {
578
            CPLError(CE_Failure, CPLE_AssertionFailed,
×
579
                     "The file does not have the same number of values per "
580
                     "line as initially thought. It must be somehow corrupted");
581
            return CE_Failure;
×
582
        }
583
    }
584

585
    nLastYOff = nBlockYOff;
300✔
586

587
    return CE_None;
300✔
588
}
589

590
/************************************************************************/
591
/*                            GetMinimum()                              */
592
/************************************************************************/
593

594
double XYZRasterBand::GetMinimum(int *pbSuccess)
1✔
595
{
596
    XYZDataset *poGDS = reinterpret_cast<XYZDataset *>(poDS);
1✔
597
    if (pbSuccess)
1✔
598
        *pbSuccess = TRUE;
1✔
599
    return poGDS->dfMinZ;
1✔
600
}
601

602
/************************************************************************/
603
/*                            GetMaximum()                              */
604
/************************************************************************/
605

606
double XYZRasterBand::GetMaximum(int *pbSuccess)
1✔
607
{
608
    XYZDataset *poGDS = reinterpret_cast<XYZDataset *>(poDS);
1✔
609
    if (pbSuccess)
1✔
610
        *pbSuccess = TRUE;
1✔
611
    return poGDS->dfMaxZ;
1✔
612
}
613

614
/************************************************************************/
615
/*                          GetNoDataValue()                            */
616
/************************************************************************/
617

618
double XYZRasterBand::GetNoDataValue(int *pbSuccess)
318✔
619
{
620
    XYZDataset *poGDS = reinterpret_cast<XYZDataset *>(poDS);
318✔
621
    if (!poGDS->bSameNumberOfValuesPerLine && poGDS->dfMinZ > -32768 &&
318✔
622
        eDataType != GDT_Byte)
23✔
623
    {
624
        if (pbSuccess)
×
625
            *pbSuccess = TRUE;
×
626
        return (poGDS->dfMinZ > 0) ? 0 : -32768;
×
627
    }
628
    else if (!poGDS->bSameNumberOfValuesPerLine && poGDS->dfMinZ > 0 &&
318✔
629
             eDataType == GDT_Byte)
23✔
630
    {
631
        if (pbSuccess)
23✔
632
            *pbSuccess = TRUE;
23✔
633
        return 0;
23✔
634
    }
635

636
    return GDALPamRasterBand::GetNoDataValue(pbSuccess);
295✔
637
}
638

639
/************************************************************************/
640
/*                            ~XYZDataset()                            */
641
/************************************************************************/
642

643
XYZDataset::XYZDataset()
38✔
644
    : fp(nullptr), bHasHeaderLine(FALSE), nCommentLineCount(0),
645
      chDecimalSep('.'), nXIndex(-1), nYIndex(-1), nZIndex(-1), nMinTokens(0),
646
      nLineNum(0), nDataLineNum(GINTBIG_MAX), bSameNumberOfValuesPerLine(TRUE),
647
      dfMinZ(0), dfMaxZ(0), bEOF(false)
38✔
648
{
649
}
38✔
650

651
/************************************************************************/
652
/*                            ~XYZDataset()                            */
653
/************************************************************************/
654

655
XYZDataset::~XYZDataset()
76✔
656

657
{
658
    FlushCache(true);
38✔
659
    if (fp)
38✔
660
        VSIFCloseL(fp);
38✔
661

662
    {
663
        std::lock_guard<std::mutex> guard(gMutex);
76✔
664
        if (gpoActiveDS == this)
38✔
665
        {
666
            gpoActiveDS = nullptr;
4✔
667
            gasValues.clear();
4✔
668
            gafValues.clear();
4✔
669
        }
670
    }
671
}
76✔
672

673
/************************************************************************/
674
/*                             Identify()                               */
675
/************************************************************************/
676

677
int XYZDataset::Identify(GDALOpenInfo *poOpenInfo)
56,452✔
678
{
679
    int bHasHeaderLine, nCommentLineCount;
680
    int nXIndex;
681
    int nYIndex;
682
    int nZIndex;
683
    return IdentifyEx(poOpenInfo, bHasHeaderLine, nCommentLineCount, nXIndex,
56,452✔
684
                      nYIndex, nZIndex);
112,882✔
685
}
686

687
/************************************************************************/
688
/*                            IdentifyEx()                              */
689
/************************************************************************/
690

691
int XYZDataset::IdentifyEx(GDALOpenInfo *poOpenInfo, int &bHasHeaderLine,
56,476✔
692
                           int &nCommentLineCount, int &nXIndex, int &nYIndex,
693
                           int &nZIndex)
694

695
{
696
    bHasHeaderLine = FALSE;
56,476✔
697
    nCommentLineCount = 0;
56,476✔
698

699
    CPLString osFilename(poOpenInfo->pszFilename);
112,949✔
700
    if (EQUAL(CPLGetExtensionSafe(osFilename).c_str(), "GRA") &&
56,472✔
UNCOV
701
        !poOpenInfo->IsSingleAllowedDriver("XYZ"))
×
702
    {
703
        // IGNFHeightASCIIGRID .GRA
704
        return FALSE;
×
705
    }
706

707
    std::unique_ptr<GDALOpenInfo> poOpenInfoToDelete;  // keep in this scope
56,475✔
708
    /*  GZipped .xyz files are common, so automagically open them */
709
    /*  if the /vsigzip/ has not been explicitly passed */
710
    if (strlen(poOpenInfo->pszFilename) > 6 &&
56,469✔
711
        EQUAL(poOpenInfo->pszFilename + strlen(poOpenInfo->pszFilename) - 6,
55,268✔
712
              "xyz.gz") &&
×
713
        !STARTS_WITH_CI(poOpenInfo->pszFilename, "/vsigzip/"))
×
714
    {
715
        osFilename = "/vsigzip/";
×
716
        osFilename += poOpenInfo->pszFilename;
×
717
        poOpenInfoToDelete = std::make_unique<GDALOpenInfo>(
×
718
            osFilename.c_str(), GA_ReadOnly, poOpenInfo->GetSiblingFiles());
×
719
        poOpenInfo = poOpenInfoToDelete.get();
×
720
    }
721

722
    if (poOpenInfo->nHeaderBytes == 0)
56,475✔
723
    {
724
        return FALSE;
53,231✔
725
    }
726

727
    /* -------------------------------------------------------------------- */
728
    /*      Check that it looks roughly as an XYZ dataset                   */
729
    /* -------------------------------------------------------------------- */
730
    const char *pszData =
3,244✔
731
        reinterpret_cast<const char *>(poOpenInfo->pabyHeader);
732

733
    if (poOpenInfo->nHeaderBytes >= 4 && STARTS_WITH(pszData, "DSAA") &&
3,244✔
734
        !poOpenInfo->IsSingleAllowedDriver("XYZ"))
×
735
    {
736
        // Do not match GSAG datasets
737
        return FALSE;
×
738
    }
739

740
    /* Skip comments line at the beginning such as in */
741
    /* http://pubs.usgs.gov/of/2003/ofr-03-230/DATA/NSLCU.XYZ */
742
    int i = 0;
3,244✔
743
    if (pszData[i] == '/')
3,244✔
744
    {
745
        nCommentLineCount++;
×
746

747
        i++;
×
748
        for (; i < poOpenInfo->nHeaderBytes; i++)
×
749
        {
750
            const char ch = pszData[i];
×
751
            if (ch == 13 || ch == 10)
×
752
            {
753
                if (ch == 13 && pszData[i + 1] == 10)
×
754
                    i++;
×
755
                if (pszData[i + 1] == '/')
×
756
                {
757
                    nCommentLineCount++;
×
758
                    i++;
×
759
                }
760
                else
761
                    break;
×
762
            }
763
        }
764
    }
765

766
    int iStartLine = i;
3,244✔
767
    for (; i < poOpenInfo->nHeaderBytes; i++)
28,522✔
768
    {
769
        const char ch = pszData[i];
28,470✔
770
        if (ch == 13 || ch == 10)
28,470✔
771
        {
772
            break;
773
        }
774
        else if (ch == ' ' || ch == ',' || ch == '\t' || ch == ';')
28,299✔
775
            ;
776
        else if ((ch >= '0' && ch <= '9') || ch == '.' || ch == '+' ||
17,308✔
777
                 ch == '-' || ch == 'e' || ch == 'E')
11,704✔
778
            ;
779
        else if (ch == '"' || (ch >= 'a' && ch <= 'z') ||
11,030✔
780
                 (ch >= 'A' && ch <= 'Z'))
3,993✔
781
            bHasHeaderLine = TRUE;
8,009✔
782
        else
783
        {
784
            return FALSE;
3,021✔
785
        }
786
    }
787

788
    nXIndex = -1;
223✔
789
    nYIndex = -1;
223✔
790
    nZIndex = -1;
223✔
791
    const char *pszColumnOrder = CSLFetchNameValueDef(
446✔
792
        poOpenInfo->papszOpenOptions, "COLUMN_ORDER", "AUTO");
223✔
793
    if (EQUAL(pszColumnOrder, "XYZ"))
223✔
794
    {
795
        nXIndex = 0;
2✔
796
        nYIndex = 1;
2✔
797
        nZIndex = 2;
2✔
798
        return TRUE;
2✔
799
    }
800
    else if (EQUAL(pszColumnOrder, "YXZ"))
221✔
801
    {
802
        nXIndex = 1;
4✔
803
        nYIndex = 0;
4✔
804
        nZIndex = 2;
4✔
805
        return TRUE;
4✔
806
    }
807
    else if (!EQUAL(pszColumnOrder, "AUTO"))
217✔
808
    {
809
        CPLError(CE_Failure, CPLE_IllegalArg,
1✔
810
                 "Option COLUMN_ORDER can only be XYZ, YXZ and AUTO."
811
                 "%s is not valid",
812
                 pszColumnOrder);
813
        return FALSE;
1✔
814
    }
815

816
    if (bHasHeaderLine)
216✔
817
    {
818
        CPLString osHeaderLine;
118✔
819
        osHeaderLine.assign(pszData + iStartLine, i - iStartLine);
118✔
820
        char **papszTokens =
821
            CSLTokenizeString2(osHeaderLine, " ,\t;", CSLT_HONOURSTRINGS);
118✔
822
        int nTokens = CSLCount(papszTokens);
118✔
823
        for (int iToken = 0; iToken < nTokens; iToken++)
859✔
824
        {
825
            const char *pszToken = papszTokens[iToken];
741✔
826
            if (EQUAL(pszToken, "x") || STARTS_WITH_CI(pszToken, "lon") ||
741✔
827
                STARTS_WITH_CI(pszToken, "east"))
711✔
828
                nXIndex = iToken;
30✔
829
            else if (EQUAL(pszToken, "y") || STARTS_WITH_CI(pszToken, "lat") ||
711✔
830
                     STARTS_WITH_CI(pszToken, "north"))
683✔
831
                nYIndex = iToken;
28✔
832
            else if (EQUAL(pszToken, "z") || STARTS_WITH_CI(pszToken, "alt") ||
683✔
833
                     EQUAL(pszToken, "height"))
655✔
834
                nZIndex = iToken;
28✔
835
        }
836
        CSLDestroy(papszTokens);
118✔
837
        if (nXIndex >= 0 && nYIndex >= 0 && nZIndex >= 0)
118✔
838
        {
839
            return TRUE;
28✔
840
        }
841
    }
842

843
    bool bHasFoundNewLine = false;
188✔
844
    bool bPrevWasSep = true;
188✔
845
    int nCols = 0;
188✔
846
    int nMaxCols = 0;
188✔
847
    for (; i < poOpenInfo->nHeaderBytes; i++)
16,795✔
848
    {
849
        char ch = pszData[i];
16,704✔
850
        if (ch == 13 || ch == 10)
16,704✔
851
        {
852
            bHasFoundNewLine = true;
1,145✔
853
            if (!bPrevWasSep)
1,145✔
854
            {
855
                nCols++;
989✔
856
                if (nCols > nMaxCols)
989✔
857
                    nMaxCols = nCols;
46✔
858
            }
859
            bPrevWasSep = true;
1,145✔
860
            nCols = 0;
1,145✔
861
        }
862
        else if (ch == ' ' || ch == ',' || ch == '\t' || ch == ';')
15,559✔
863
        {
864
            if (!bPrevWasSep)
2,568✔
865
            {
866
                nCols++;
2,507✔
867
                if (nCols > nMaxCols)
2,507✔
868
                    nMaxCols = nCols;
122✔
869
            }
870
            bPrevWasSep = true;
2,568✔
871
        }
872
        else if ((ch >= '0' && ch <= '9') || ch == '.' || ch == '+' ||
12,991✔
873
                 ch == '-' || ch == 'e' || ch == 'E')
97✔
874
        {
875
            bPrevWasSep = false;
12,894✔
876
        }
877
        else
878
        {
879
            return FALSE;
97✔
880
        }
881
    }
882

883
    return bHasFoundNewLine && nMaxCols >= 3;
91✔
884
}
885

886
/************************************************************************/
887
/*                                Open()                                */
888
/************************************************************************/
889

890
GDALDataset *XYZDataset::Open(GDALOpenInfo *poOpenInfo)
25✔
891

892
{
893
    int bHasHeaderLine;
894
    int nCommentLineCount = 0;
25✔
895

896
    int nXIndex = -1;
25✔
897
    int nYIndex = -1;
25✔
898
    int nZIndex = -1;
25✔
899
    if (!IdentifyEx(poOpenInfo, bHasHeaderLine, nCommentLineCount, nXIndex,
25✔
900
                    nYIndex, nZIndex))
901
        return nullptr;
×
902

903
    CPLString osFilename(poOpenInfo->pszFilename);
50✔
904

905
    /*  GZipped .xyz files are common, so automagically open them */
906
    /*  if the /vsigzip/ has not been explicitly passed */
907
    if (strlen(poOpenInfo->pszFilename) > 6 &&
25✔
908
        EQUAL(poOpenInfo->pszFilename + strlen(poOpenInfo->pszFilename) - 6,
25✔
909
              "xyz.gz") &&
×
910
        !STARTS_WITH_CI(poOpenInfo->pszFilename, "/vsigzip/"))
×
911
    {
912
        osFilename = "/vsigzip/";
×
913
        osFilename += poOpenInfo->pszFilename;
×
914
    }
915

916
    /* -------------------------------------------------------------------- */
917
    /*      Find dataset characteristics                                    */
918
    /* -------------------------------------------------------------------- */
919
    VSILFILE *fp = VSIFOpenL(osFilename.c_str(), "rb");
25✔
920
    if (fp == nullptr)
25✔
921
        return nullptr;
×
922

923
    /* For better performance of CPLReadLine2L() we create a buffered reader */
924
    /* (except for /vsigzip/ since it has one internally) */
925
    if (!STARTS_WITH_CI(poOpenInfo->pszFilename, "/vsigzip/"))
25✔
926
        fp = VSICreateBufferedReaderHandle(fp);
25✔
927

928
    int nMinTokens = 0;
25✔
929

930
    for (int i = 0; i < nCommentLineCount; i++)
25✔
931
    {
932
        if (CPLReadLine2L(fp, 100, nullptr) == nullptr)
×
933
        {
934
            VSIFCloseL(fp);
×
935
            return nullptr;
×
936
        }
937
    }
938

939
    /* -------------------------------------------------------------------- */
940
    /*      Parse header line                                               */
941
    /* -------------------------------------------------------------------- */
942
    if (bHasHeaderLine)
25✔
943
    {
944
        const char *pszLine = CPLReadLine2L(fp, 100, nullptr);
17✔
945
        if (pszLine == nullptr)
17✔
946
        {
947
            VSIFCloseL(fp);
×
948
            return nullptr;
×
949
        }
950
        if (nXIndex < 0 || nYIndex < 0 || nZIndex < 0)
17✔
951
        {
952
            CPLError(CE_Warning, CPLE_AppDefined,
1✔
953
                     "Could not find one of the X, Y or Z column names in "
954
                     "header line. Defaulting to the first 3 columns");
955
            nXIndex = 0;
1✔
956
            nYIndex = 1;
1✔
957
            nZIndex = 2;
1✔
958
        }
959
    }
960
    else if (nXIndex < 0 || nYIndex < 0 || nZIndex < 0)
8✔
961
    {
962
        nXIndex = 0;
7✔
963
        nYIndex = 1;
7✔
964
        nZIndex = 2;
7✔
965
    }
966
    nMinTokens = 1 + std::max(std::max(nXIndex, nYIndex), nZIndex);
25✔
967

968
    /* -------------------------------------------------------------------- */
969
    /*      Parse data lines                                                */
970
    /* -------------------------------------------------------------------- */
971

972
    GIntBig nLineNum = 0;
25✔
973
    GIntBig nDataLineNum = 0;
25✔
974
    double dfX = 0.0;
25✔
975
    double dfY = 0.0;
25✔
976
    double dfZ = 0.0;
25✔
977
    double dfMinX = 0.0;
25✔
978
    double dfMinY = 0.0;
25✔
979
    double dfMaxX = 0.0;
25✔
980
    double dfMaxY = 0.0;
25✔
981
    double dfMinZ = 0.0;
25✔
982
    double dfMaxZ = 0.0;
25✔
983
    double dfLastX = 0.0;
25✔
984
    double dfLastY = 0.0;
25✔
985
    std::vector<double> adfStepX;
50✔
986
    std::vector<double> adfStepY;
50✔
987
    GDALDataType eDT = GDT_Byte;
25✔
988
    bool bSameNumberOfValuesPerLine = true;
25✔
989
    char chDecimalSep = '\0';
25✔
990
    int nStepYSign = 0;
25✔
991
    bool bColOrganization = false;
25✔
992

993
    const char *pszLine;
994
    GIntBig nCountStepX = 0;
25✔
995
    GIntBig nCountStepY = 0;
25✔
996
    while ((pszLine = CPLReadLine2L(fp, 100, nullptr)) != nullptr)
41,785✔
997
    {
998
        nLineNum++;
41,760✔
999

1000
        const char *pszPtr = pszLine;
41,760✔
1001
        char ch;
1002
        int nCol = 0;
41,760✔
1003
        bool bLastWasSep = true;
41,760✔
1004
        if (chDecimalSep == '\0')
41,760✔
1005
        {
1006
            int nCountComma = 0;
872✔
1007
            int nCountFieldSep = 0;
872✔
1008
            while ((ch = *pszPtr) != '\0')
15,582✔
1009
            {
1010
                if (ch == '.')
14,722✔
1011
                {
1012
                    chDecimalSep = '.';
12✔
1013
                    break;
12✔
1014
                }
1015
                else if (ch == ',')
14,710✔
1016
                {
1017
                    nCountComma++;
3✔
1018
                    bLastWasSep = false;
3✔
1019
                }
1020
                else if (ch == ' ')
14,707✔
1021
                {
1022
                    if (!bLastWasSep)
1,701✔
1023
                        nCountFieldSep++;
1,698✔
1024
                    bLastWasSep = true;
1,701✔
1025
                }
1026
                else if (ch == '\t' || ch == ';')
13,006✔
1027
                {
1028
                    nCountFieldSep++;
4✔
1029
                    bLastWasSep = true;
4✔
1030
                }
1031
                else
1032
                    bLastWasSep = false;
13,002✔
1033
                pszPtr++;
14,710✔
1034
            }
1035
            if (chDecimalSep == '\0')
872✔
1036
            {
1037
                /* 1,2,3 */
1038
                if (nCountComma >= 2 && nCountFieldSep == 0)
860✔
1039
                    chDecimalSep = '.';
1✔
1040
                /* 23,5;33;45 */
1041
                else if (nCountComma > 0 && nCountFieldSep > 0)
859✔
1042
                    chDecimalSep = ',';
1✔
1043
            }
1044
            pszPtr = pszLine;
872✔
1045
            bLastWasSep = true;
872✔
1046
        }
1047

1048
        char chLocalDecimalSep = chDecimalSep ? chDecimalSep : '.';
41,760✔
1049
        int nUsefulColsFound = 0;
41,760✔
1050
        while ((ch = *pszPtr) != '\0')
1,354,170✔
1051
        {
1052
            if (ch == ' ')
1,312,410✔
1053
            {
1054
                if (!bLastWasSep)
2,784✔
1055
                    nCol++;
2,664✔
1056
                bLastWasSep = true;
2,784✔
1057
            }
1058
            else if ((ch == ',' && chLocalDecimalSep != ',') || ch == '\t' ||
1,309,620✔
1059
                     ch == ';')
1060
            {
1061
                nCol++;
80,826✔
1062
                bLastWasSep = true;
80,826✔
1063
            }
1064
            else
1065
            {
1066
                if (bLastWasSep)
1,228,800✔
1067
                {
1068
                    if (nCol == nXIndex)
125,235✔
1069
                    {
1070
                        nUsefulColsFound++;
41,745✔
1071
                        dfX = CPLAtofDelim(pszPtr, chLocalDecimalSep);
41,745✔
1072
                    }
1073
                    else if (nCol == nYIndex)
83,490✔
1074
                    {
1075
                        nUsefulColsFound++;
41,745✔
1076
                        dfY = CPLAtofDelim(pszPtr, chLocalDecimalSep);
41,745✔
1077
                    }
1078
                    else if (nCol == nZIndex)
41,745✔
1079
                    {
1080
                        nUsefulColsFound++;
41,745✔
1081
                        dfZ = CPLAtofDelim(pszPtr, chLocalDecimalSep);
41,745✔
1082
                        if (nDataLineNum == 0)
41,745✔
1083
                        {
1084
                            dfMinZ = dfZ;
25✔
1085
                            dfMaxZ = dfZ;
25✔
1086
                        }
1087
                        else if (dfZ < dfMinZ)
41,720✔
1088
                        {
1089
                            dfMinZ = dfZ;
22✔
1090
                        }
1091
                        else if (dfZ > dfMaxZ)
41,698✔
1092
                        {
1093
                            dfMaxZ = dfZ;
306✔
1094
                        }
1095

1096
                        if (dfZ < INT_MIN || dfZ > INT_MAX)
41,745✔
1097
                        {
1098
                            eDT = GDT_Float32;
×
1099
                        }
1100
                        else
1101
                        {
1102
                            int nZ = static_cast<int>(dfZ);
41,745✔
1103
                            if (static_cast<double>(nZ) != dfZ)
41,745✔
1104
                            {
1105
                                eDT = GDT_Float32;
28,327✔
1106
                            }
1107
                            else if ((eDT == GDT_Byte || eDT == GDT_Int16)
13,418✔
1108
                                     // cppcheck-suppress
1109
                                     // knownConditionTrueFalse
1110
                                     && (nZ < 0 || nZ > 255))
5,588✔
1111
                            {
1112
                                if (nZ < -32768 || nZ > 32767)
10✔
1113
                                    eDT = GDT_Int32;
×
1114
                                else
1115
                                    eDT = GDT_Int16;
10✔
1116
                            }
1117
                        }
1118
                    }
1119
                }
1120
                bLastWasSep = false;
1,228,800✔
1121
            }
1122
            pszPtr++;
1,312,410✔
1123
        }
1124
        /* skip empty lines */
1125
        if (bLastWasSep && nCol == 0)
41,760✔
1126
        {
1127
            continue;
15✔
1128
        }
1129
        nDataLineNum++;
41,745✔
1130
        nCol++;
41,745✔
1131
        if (nCol < nMinTokens)
41,745✔
1132
        {
1133
            CPLError(CE_Failure, CPLE_AppDefined,
×
1134
                     "At line " CPL_FRMT_GIB
1135
                     ", found %d tokens. Expected %d at least",
1136
                     nLineNum, nCol, nMinTokens);
1137
            VSIFCloseL(fp);
×
1138
            return nullptr;
×
1139
        }
1140
        if (nUsefulColsFound != 3)
41,745✔
1141
        {
1142
            CPLError(CE_Failure, CPLE_AppDefined,
×
1143
                     "At line " CPL_FRMT_GIB
1144
                     ", did not find X, Y and/or Z values",
1145
                     nLineNum);
1146
            VSIFCloseL(fp);
×
1147
            return nullptr;
×
1148
        }
1149

1150
        if (nDataLineNum == 1)
41,745✔
1151
        {
1152
            dfMinX = dfX;
25✔
1153
            dfMaxX = dfX;
25✔
1154
            dfMinY = dfY;
25✔
1155
            dfMaxY = dfY;
25✔
1156
        }
1157
        else if (nDataLineNum == 2 && dfX == dfLastX)
41,720✔
1158
        {
1159
            // Detect datasets organized by columns
1160
            if (dfY == dfLastY)
7✔
1161
            {
1162
                CPLError(CE_Failure, CPLE_AppDefined,
×
1163
                         "Ungridded dataset: At line " CPL_FRMT_GIB
1164
                         ", Failed to detect grid layout.",
1165
                         nLineNum);
1166
                VSIFCloseL(fp);
×
1167
                return nullptr;
×
1168
            }
1169

1170
            bColOrganization = true;
7✔
1171
            const double dfStepY = dfY - dfLastY;
7✔
1172
            adfStepY.push_back(fabs(dfStepY));
7✔
1173
            nStepYSign = dfStepY > 0 ? 1 : -1;
7✔
1174
        }
1175
        else if (bColOrganization)
41,713✔
1176
        {
1177
            const double dfStepX = dfX - dfLastX;
29✔
1178
            if (dfStepX == 0)
29✔
1179
            {
1180
                const double dfStepY = dfY - dfLastY;
19✔
1181
                const double dfExpectedStepY = adfStepY.back() * nStepYSign;
19✔
1182
                if (fabs((dfStepY - dfExpectedStepY) / dfExpectedStepY) >
19✔
1183
                    RELATIVE_ERROR)
1184
                {
1185
                    CPLError(CE_Failure, CPLE_AppDefined,
×
1186
                             "Ungridded dataset: At line " CPL_FRMT_GIB
1187
                             ", Y spacing was %f. Expected %f",
1188
                             nLineNum, dfStepY, dfExpectedStepY);
1189
                    VSIFCloseL(fp);
×
1190
                    return nullptr;
×
1191
                }
1192
            }
1193
            else if (dfStepX > 0)
10✔
1194
            {
1195
                if (adfStepX.empty())
8✔
1196
                {
1197
                    adfStepX.push_back(dfStepX);
5✔
1198
                }
1199
                else if (fabs((dfStepX - adfStepX.back()) / adfStepX.back()) >
3✔
1200
                         RELATIVE_ERROR)
1201
                {
1202
                    CPLError(CE_Failure, CPLE_AppDefined,
×
1203
                             "Ungridded dataset: At line " CPL_FRMT_GIB
1204
                             ", X spacing was %f. Expected %f",
1205
                             nLineNum, dfStepX, adfStepX.back());
×
1206
                    VSIFCloseL(fp);
×
1207
                    return nullptr;
×
1208
                }
1209
            }
1210
            else if (nDataLineNum == 3)
2✔
1211
            {
1212
                const double dfStepY = dfY - dfLastY;
1✔
1213
                const double dfLastSignedStepY = nStepYSign * adfStepY.back();
1✔
1214
                if (dfStepY * dfLastSignedStepY > 0 &&
1✔
1215
                    (fabs(dfStepY - dfLastSignedStepY) <=
1✔
1216
                         RELATIVE_ERROR * fabs(dfLastSignedStepY)
1✔
1217
#ifdef multiple_of_step_y_not_yet_supported
1218
                     ||
1219
                     (fabs(dfStepY) > fabs(dfLastSignedStepY) &&
1220
                      fabs(std::round(dfStepY / dfLastSignedStepY) -
1221
                           (dfStepY / dfLastSignedStepY)) <= RELATIVE_ERROR) ||
1222
                     (fabs(dfLastSignedStepY) > fabs(dfStepY) &&
1223
                      fabs(std::round(dfLastSignedStepY / dfStepY) -
1224
                           (dfLastSignedStepY / dfStepY)) <= RELATIVE_ERROR)
1225
#endif
1226
                         ))
1227
                {
1228
                    // Assume it is a file starting with something like:
1229
                    // 371999.50 5806917.50 41.21
1230
                    // 371999.50 5806918.50 51.99
1231
                    // 371998.50 5806919.50 53.50
1232
                    // 371999.50 5806919.50 53.68
1233
                    adfStepX.push_back(dfLastX - dfX);
1✔
1234
                    bColOrganization = false;
1✔
1235
                }
1236
                else
1237
                {
1238
                    CPLError(CE_Failure, CPLE_AppDefined,
×
1239
                             "Ungridded dataset: At line " CPL_FRMT_GIB
1240
                             ", X spacing was %f. Expected >0 value",
1241
                             nLineNum, dfX - dfLastX);
1242
                    VSIFCloseL(fp);
×
1243
                    return nullptr;
×
1244
                }
1245
            }
1246
            else if (!adfStepX.empty() &&
1✔
1247
                     fabs(std::round(-dfStepX / adfStepX[0]) -
×
1248
                          (-dfStepX / adfStepX[0])) <= RELATIVE_ERROR)
×
1249
            {
1250
                bColOrganization = false;
×
1251
            }
1252
            else if (adfStepX.empty())
1✔
1253
            {
1254
                adfStepX.push_back(fabs(dfStepX));
1✔
1255
                bColOrganization = false;
1✔
1256
            }
1257
            else
1258
            {
1259
                CPLError(CE_Failure, CPLE_AppDefined,
×
1260
                         "Ungridded dataset: At line " CPL_FRMT_GIB
1261
                         ", X spacing was %f. Expected a multiple of %f",
1262
                         nLineNum, dfStepX, adfStepX[0]);
×
1263
                VSIFCloseL(fp);
×
1264
                return nullptr;
×
1265
            }
1266
        }
1267
        else
1268
        {
1269
            double dfStepY = dfY - dfLastY;
41,684✔
1270
            if (dfStepY == 0.0)
41,684✔
1271
            {
1272
                const double dfStepX = dfX - dfLastX;
41,398✔
1273
                if (dfStepX <= 0)
41,398✔
1274
                {
1275
                    CPLError(CE_Failure, CPLE_AppDefined,
×
1276
                             "Ungridded dataset: At line " CPL_FRMT_GIB
1277
                             ", X spacing was %f. Expected >0 value",
1278
                             nLineNum, dfStepX);
1279
                    VSIFCloseL(fp);
×
1280
                    return nullptr;
×
1281
                }
1282
                if (std::find(adfStepX.begin(), adfStepX.end(), dfStepX) ==
41,398✔
1283
                    adfStepX.end())
82,796✔
1284
                {
1285
                    bool bAddNewValue = true;
40✔
1286
                    std::vector<double>::iterator oIter = adfStepX.begin();
40✔
1287
                    std::vector<double> adfStepXNew;
40✔
1288
                    while (oIter != adfStepX.end())
41✔
1289
                    {
1290
                        if (fabs((dfStepX - *oIter) / dfStepX) < RELATIVE_ERROR)
22✔
1291
                        {
1292
                            double dfNewVal = *oIter;
20✔
1293
                            if (nCountStepX > 0)
20✔
1294
                            {
1295
                                // Update mean step
1296
                                /* n * mean(n) = (n-1) * mean(n-1) + val(n)
1297
                                mean(n) = mean(n-1) + (val(n) - mean(n-1)) / n
1298
                              */
1299
                                nCountStepX++;
20✔
1300
                                dfNewVal += (dfStepX - *oIter) / nCountStepX;
20✔
1301
                            }
1302

1303
                            adfStepXNew.push_back(dfNewVal);
20✔
1304
                            bAddNewValue = false;
20✔
1305
                            break;
20✔
1306
                        }
1307
                        else if (dfStepX < *oIter &&
3✔
1308
                                 fabs(*oIter -
1✔
1309
                                      static_cast<int>(*oIter / dfStepX + 0.5) *
1✔
1310
                                          dfStepX) /
1✔
1311
                                         dfStepX <
1✔
1312
                                     RELATIVE_ERROR)
1313
                        {
1314
                            nCountStepX = -1;  // disable update of mean
1✔
1315
                            ++oIter;
1✔
1316
                        }
1317
                        else if (dfStepX > *oIter &&
2✔
1318
                                 fabs(dfStepX -
2✔
1319
                                      static_cast<int>(dfStepX / *oIter + 0.5) *
1✔
1320
                                          (*oIter)) /
1✔
1321
                                         dfStepX <
1✔
1322
                                     RELATIVE_ERROR)
1323
                        {
1324
                            nCountStepX = -1;  // disable update of mean
1✔
1325
                            bAddNewValue = false;
1✔
1326
                            adfStepXNew.push_back(*oIter);
1✔
1327
                            break;
1✔
1328
                        }
1329
                        else
1330
                        {
1331
                            adfStepXNew.push_back(*oIter);
×
1332
                            ++oIter;
×
1333
                        }
1334
                    }
1335
                    adfStepX = std::move(adfStepXNew);
40✔
1336
                    if (bAddNewValue)
40✔
1337
                    {
1338
                        CPLDebug("XYZ", "New stepX=%.15f", dfStepX);
19✔
1339
                        adfStepX.push_back(dfStepX);
19✔
1340
                        if (adfStepX.size() == 1 && nCountStepX == 0)
19✔
1341
                        {
1342
                            nCountStepX++;
18✔
1343
                        }
1344
                        else if (adfStepX.size() == 2)
1✔
1345
                        {
1346
                            nCountStepX = -1;  // disable update of mean
×
1347
                        }
1348
                        else if (adfStepX.size() == 10)
1✔
1349
                        {
1350
                            CPLError(
×
1351
                                CE_Failure, CPLE_AppDefined,
1352
                                "Ungridded dataset: too many stepX values");
1353
                            VSIFCloseL(fp);
×
1354
                            return nullptr;
×
1355
                        }
1356
                    }
1357
                }
1358
            }
1359
            else
1360
            {
1361
                int bNewStepYSign = (dfStepY < 0.0) ? -1 : 1;
286✔
1362
                if (nStepYSign == 0)
286✔
1363
                    nStepYSign = bNewStepYSign;
18✔
1364
                else if (nStepYSign != bNewStepYSign)
268✔
1365
                {
1366
                    CPLError(CE_Failure, CPLE_AppDefined,
×
1367
                             "Ungridded dataset: At line " CPL_FRMT_GIB
1368
                             ", change of Y direction",
1369
                             nLineNum);
1370
                    VSIFCloseL(fp);
×
1371
                    return nullptr;
×
1372
                }
1373
                if (bNewStepYSign < 0)
286✔
1374
                    dfStepY = -dfStepY;
263✔
1375
                nCountStepY++;
286✔
1376
                if (adfStepY.empty())
286✔
1377
                {
1378
                    adfStepY.push_back(dfStepY);
18✔
1379
                }
1380
                else if (fabs((adfStepY[0] - dfStepY) / dfStepY) >
268✔
1381
                         RELATIVE_ERROR)
1382
                {
1383
                    if (dfStepY > adfStepY[0] &&
4✔
1384
                        fabs(std::round(dfStepY / adfStepY[0]) -
2✔
1385
                             (dfStepY / adfStepY[0])) <= RELATIVE_ERROR)
2✔
1386
                    {
1387
                        // The new step is a multiple of the previous one,
1388
                        // which means we have a missing line: OK
1389
                    }
1390
                    else
1391
                    {
1392
                        CPLDebug("XYZ", "New stepY=%.15f prev stepY=%.15f",
×
1393
                                 dfStepY, adfStepY[0]);
×
1394
                        CPLError(CE_Failure, CPLE_AppDefined,
×
1395
                                 "Ungridded dataset: At line " CPL_FRMT_GIB
1396
                                 ", too many stepY values",
1397
                                 nLineNum);
1398
                        VSIFCloseL(fp);
×
1399
                        return nullptr;
×
1400
                    }
1401
                }
1402
                else
1403
                {
1404
                    // Update mean step
1405
                    adfStepY[0] += (dfStepY - adfStepY[0]) / nCountStepY;
266✔
1406
                }
1407
            }
1408
        }
1409

1410
        if (dfX < dfMinX)
41,745✔
1411
            dfMinX = dfX;
4✔
1412
        if (dfX > dfMaxX)
41,745✔
1413
            dfMaxX = dfX;
285✔
1414
        if (dfY < dfMinY)
41,745✔
1415
            dfMinY = dfY;
267✔
1416
        if (dfY > dfMaxY)
41,745✔
1417
            dfMaxY = dfY;
33✔
1418

1419
        dfLastX = dfX;
41,745✔
1420
        dfLastY = dfY;
41,745✔
1421
    }
1422

1423
    if (adfStepX.size() != 1 || adfStepX[0] == 0)
25✔
1424
    {
1425
        CPLError(CE_Failure, CPLE_AppDefined, "Couldn't determine X spacing");
×
1426
        VSIFCloseL(fp);
×
1427
        return nullptr;
×
1428
    }
1429

1430
    if (adfStepY.size() != 1 || adfStepY[0] == 0)
25✔
1431
    {
1432
        CPLError(CE_Failure, CPLE_AppDefined, "Couldn't determine Y spacing");
×
1433
        VSIFCloseL(fp);
×
1434
        return nullptr;
×
1435
    }
1436

1437
    // Decide for a north-up organization
1438
    if (bColOrganization)
25✔
1439
        nStepYSign = -1;
5✔
1440

1441
    const double dfXSize = 1 + ((dfMaxX - dfMinX) / adfStepX[0] + 0.5);
25✔
1442
    const double dfYSize = 1 + ((dfMaxY - dfMinY) / adfStepY[0] + 0.5);
25✔
1443
    // Test written such as to detect NaN values
1444
    if (!(dfXSize > 0 && dfXSize < INT_MAX) ||
25✔
1445
        !(dfYSize > 0 && dfYSize < INT_MAX))
25✔
1446
    {
1447
        CPLError(CE_Failure, CPLE_AppDefined, "Invalid dimensions");
×
1448
        VSIFCloseL(fp);
×
1449
        return nullptr;
×
1450
    }
1451
    const int nXSize = static_cast<int>(dfXSize);
25✔
1452
    const int nYSize = static_cast<int>(dfYSize);
25✔
1453
    const double dfStepX = (dfMaxX - dfMinX) / (nXSize - 1);
25✔
1454
    const double dfStepY = (dfMaxY - dfMinY) / (nYSize - 1) * nStepYSign;
25✔
1455

1456
#ifdef DEBUG_VERBOSE
1457
    CPLDebug("XYZ", "minx=%f maxx=%f stepx=%f", dfMinX, dfMaxX, dfStepX);
1458
    CPLDebug("XYZ", "miny=%f maxy=%f stepy=%f", dfMinY, dfMaxY, dfStepY);
1459
#endif
1460

1461
    if (nDataLineNum != static_cast<GIntBig>(nXSize) * nYSize)
25✔
1462
    {
1463
        if (bColOrganization)
5✔
1464
        {
1465
            CPLError(CE_Failure, CPLE_NotSupported,
×
1466
                     "The XYZ driver does not support datasets organized by "
1467
                     "columns with missing values");
1468
            VSIFCloseL(fp);
×
1469
            return nullptr;
×
1470
        }
1471
        bSameNumberOfValuesPerLine = false;
5✔
1472
    }
1473
    else if (bColOrganization && nDataLineNum > 100 * 1000 * 1000)
20✔
1474
    {
1475
        CPLError(CE_Failure, CPLE_NotSupported,
×
1476
                 "The XYZ driver cannot load datasets organized by "
1477
                 "columns with more than 100 million points");
1478
        VSIFCloseL(fp);
×
1479
        return nullptr;
×
1480
    }
1481

1482
    const bool bIngestAll = bColOrganization;
25✔
1483
    if (bIngestAll)
25✔
1484
    {
1485
        if (eDT == GDT_Int32)
5✔
1486
            eDT = GDT_Float32;
×
1487
        else if (eDT == GDT_Byte)
5✔
1488
            eDT = GDT_Int16;
1✔
1489
        CPLAssert(eDT == GDT_Int16 || eDT == GDT_Float32);
5✔
1490
    }
1491

1492
    if (poOpenInfo->eAccess == GA_Update)
25✔
1493
    {
1494
        ReportUpdateNotSupportedByDriver("XYZ");
×
1495
        VSIFCloseL(fp);
×
1496
        return nullptr;
×
1497
    }
1498

1499
    /* -------------------------------------------------------------------- */
1500
    /*      Create a corresponding GDALDataset.                             */
1501
    /* -------------------------------------------------------------------- */
1502
    XYZDataset *poDS = new XYZDataset();
25✔
1503
    poDS->fp = fp;
25✔
1504
    poDS->bHasHeaderLine = bHasHeaderLine;
25✔
1505
    poDS->nCommentLineCount = nCommentLineCount;
25✔
1506
    poDS->chDecimalSep = chDecimalSep ? chDecimalSep : '.';
25✔
1507
    poDS->nXIndex = nXIndex;
25✔
1508
    poDS->nYIndex = nYIndex;
25✔
1509
    poDS->nZIndex = nZIndex;
25✔
1510
    poDS->nMinTokens = nMinTokens;
25✔
1511
    poDS->nRasterXSize = nXSize;
25✔
1512
    poDS->nRasterYSize = nYSize;
25✔
1513
    poDS->m_gt[0] = dfMinX - dfStepX / 2;
25✔
1514
    poDS->m_gt[1] = dfStepX;
25✔
1515
    poDS->m_gt[3] = (dfStepY < 0) ? dfMaxY - dfStepY / 2 : dfMinY - dfStepY / 2;
25✔
1516
    poDS->m_gt[5] = dfStepY;
25✔
1517
    poDS->bSameNumberOfValuesPerLine = bSameNumberOfValuesPerLine;
25✔
1518
    poDS->dfMinZ = dfMinZ;
25✔
1519
    poDS->dfMaxZ = dfMaxZ;
25✔
1520
    poDS->bIngestAll = bIngestAll;
25✔
1521
#ifdef DEBUG_VERBOSE
1522
    CPLDebug("XYZ", "bSameNumberOfValuesPerLine = %d",
1523
             bSameNumberOfValuesPerLine);
1524
#endif
1525

1526
    if (!GDALCheckDatasetDimensions(poDS->nRasterXSize, poDS->nRasterYSize))
25✔
1527
    {
1528
        delete poDS;
×
1529
        return nullptr;
×
1530
    }
1531

1532
    /* -------------------------------------------------------------------- */
1533
    /*      Create band information objects.                                */
1534
    /* -------------------------------------------------------------------- */
1535
    poDS->nBands = 1;
25✔
1536
    for (int i = 0; i < poDS->nBands; i++)
50✔
1537
        poDS->SetBand(i + 1, new XYZRasterBand(poDS, i + 1, eDT));
25✔
1538

1539
    /* -------------------------------------------------------------------- */
1540
    /*      Initialize any PAM information.                                 */
1541
    /* -------------------------------------------------------------------- */
1542
    poDS->SetDescription(poOpenInfo->pszFilename);
25✔
1543
    poDS->TryLoadXML();
25✔
1544

1545
    /* -------------------------------------------------------------------- */
1546
    /*      Support overviews.                                              */
1547
    /* -------------------------------------------------------------------- */
1548
    poDS->oOvManager.Initialize(poDS, poOpenInfo->pszFilename);
25✔
1549
    return poDS;
25✔
1550
}
1551

1552
/************************************************************************/
1553
/*                             CreateCopy()                             */
1554
/************************************************************************/
1555

1556
GDALDataset *XYZDataset::CreateCopy(const char *pszFilename,
31✔
1557
                                    GDALDataset *poSrcDS, int bStrict,
1558
                                    char **papszOptions,
1559
                                    GDALProgressFunc pfnProgress,
1560
                                    void *pProgressData)
1561
{
1562
    /* -------------------------------------------------------------------- */
1563
    /*      Some some rudimentary checks                                    */
1564
    /* -------------------------------------------------------------------- */
1565
    int nBands = poSrcDS->GetRasterCount();
31✔
1566
    if (nBands == 0)
31✔
1567
    {
1568
        CPLError(
1✔
1569
            CE_Failure, CPLE_NotSupported,
1570
            "XYZ driver does not support source dataset with zero band.\n");
1571
        return nullptr;
1✔
1572
    }
1573

1574
    if (nBands != 1)
30✔
1575
    {
1576
        CPLError((bStrict) ? CE_Failure : CE_Warning, CPLE_NotSupported,
4✔
1577
                 "XYZ driver only uses the first band of the dataset.\n");
1578
        if (bStrict)
4✔
1579
            return nullptr;
4✔
1580
    }
1581

1582
    if (pfnProgress && !pfnProgress(0.0, nullptr, pProgressData))
26✔
1583
        return nullptr;
×
1584

1585
    /* -------------------------------------------------------------------- */
1586
    /*      Get source dataset info                                         */
1587
    /* -------------------------------------------------------------------- */
1588

1589
    int nXSize = poSrcDS->GetRasterXSize();
26✔
1590
    int nYSize = poSrcDS->GetRasterYSize();
26✔
1591
    GDALGeoTransform gt;
26✔
1592
    poSrcDS->GetGeoTransform(gt);
26✔
1593
    if (gt[2] != 0 || gt[4] != 0)
26✔
1594
    {
1595
        CPLError(CE_Failure, CPLE_NotSupported,
×
1596
                 "XYZ driver does not support CreateCopy() from skewed or "
1597
                 "rotated dataset.\n");
1598
        return nullptr;
×
1599
    }
1600

1601
    const GDALDataType eSrcDT = poSrcDS->GetRasterBand(1)->GetRasterDataType();
26✔
1602
    GDALDataType eReqDT;
1603
    if (eSrcDT == GDT_Byte || eSrcDT == GDT_Int16 || eSrcDT == GDT_UInt16 ||
26✔
1604
        eSrcDT == GDT_Int32)
1605
        eReqDT = GDT_Int32;
18✔
1606
    else
1607
        eReqDT = GDT_Float32;
8✔
1608

1609
    /* -------------------------------------------------------------------- */
1610
    /*      Create target file                                              */
1611
    /* -------------------------------------------------------------------- */
1612

1613
    VSILFILE *fp = VSIFOpenL(pszFilename, "wb");
26✔
1614
    if (fp == nullptr)
26✔
1615
    {
1616
        CPLError(CE_Failure, CPLE_AppDefined, "Cannot create %s", pszFilename);
3✔
1617
        return nullptr;
3✔
1618
    }
1619

1620
    /* -------------------------------------------------------------------- */
1621
    /*      Read creation options                                           */
1622
    /* -------------------------------------------------------------------- */
1623
    const char *pszColSep = CSLFetchNameValue(papszOptions, "COLUMN_SEPARATOR");
23✔
1624
    if (pszColSep == nullptr)
23✔
1625
        pszColSep = " ";
22✔
1626
    else if (EQUAL(pszColSep, "COMMA"))
1✔
1627
        pszColSep = ",";
×
1628
    else if (EQUAL(pszColSep, "SPACE"))
1✔
1629
        pszColSep = " ";
×
1630
    else if (EQUAL(pszColSep, "SEMICOLON"))
1✔
1631
        pszColSep = ";";
×
1632
    else if (EQUAL(pszColSep, "\\t") || EQUAL(pszColSep, "TAB"))
1✔
1633
        pszColSep = "\t";
×
1634
#ifdef DEBUG_VERBOSE
1635
    else
1636
        CPLDebug("XYZ", "Using raw column separator: '%s' ", pszColSep);
1637
#endif
1638

1639
    const char *pszAddHeaderLine =
1640
        CSLFetchNameValue(papszOptions, "ADD_HEADER_LINE");
23✔
1641
    if (pszAddHeaderLine != nullptr && CPLTestBool(pszAddHeaderLine))
23✔
1642
    {
1643
        VSIFPrintfL(fp, "X%sY%sZ\n", pszColSep, pszColSep);
1✔
1644
    }
1645

1646
    /* -------------------------------------------------------------------- */
1647
    /*      Copy imagery                                                    */
1648
    /* -------------------------------------------------------------------- */
1649
    char szFormat[50] = {'\0'};
23✔
1650
    if (eReqDT == GDT_Int32)
23✔
1651
        strcpy(szFormat, "%.17g%c%.17g%c%d\n");
15✔
1652
    else
1653
        strcpy(szFormat, "%.17g%c%.17g%c%.17g\n");
8✔
1654
    const char *pszDecimalPrecision =
1655
        CSLFetchNameValue(papszOptions, "DECIMAL_PRECISION");
23✔
1656
    const char *pszSignificantDigits =
1657
        CSLFetchNameValue(papszOptions, "SIGNIFICANT_DIGITS");
23✔
1658
    bool bIgnoreSigDigits = false;
23✔
1659
    if (pszDecimalPrecision && pszSignificantDigits)
23✔
1660
    {
1661
        CPLError(CE_Warning, CPLE_AppDefined,
×
1662
                 "Conflicting precision arguments, using DECIMAL_PRECISION");
1663
        bIgnoreSigDigits = true;
×
1664
    }
1665
    int nPrecision;
1666
    if (pszSignificantDigits && !bIgnoreSigDigits)
23✔
1667
    {
1668
        nPrecision = atoi(pszSignificantDigits);
×
1669
        if (nPrecision >= 0)
×
1670
        {
1671
            if (eReqDT == GDT_Int32)
×
1672
                snprintf(szFormat, sizeof(szFormat), "%%.%dg%%c%%.%dg%%c%%d\n",
×
1673
                         nPrecision, nPrecision);
1674
            else
1675
                snprintf(szFormat, sizeof(szFormat),
×
1676
                         "%%.%dg%%c%%.%dg%%c%%.%dg\n", nPrecision, nPrecision,
1677
                         nPrecision);
1678
        }
1679
        CPLDebug("XYZ", "Setting precision format: %s", szFormat);
×
1680
    }
1681
    else if (pszDecimalPrecision)
23✔
1682
    {
1683
        nPrecision = atoi(pszDecimalPrecision);
×
1684
        if (nPrecision >= 0)
×
1685
        {
1686
            if (eReqDT == GDT_Int32)
×
1687
                snprintf(szFormat, sizeof(szFormat), "%%.%df%%c%%.%df%%c%%d\n",
×
1688
                         nPrecision, nPrecision);
1689
            else
1690
                snprintf(szFormat, sizeof(szFormat),
×
1691
                         "%%.%df%%c%%.%df%%c%%.%df\n", nPrecision, nPrecision,
1692
                         nPrecision);
1693
        }
1694
        CPLDebug("XYZ", "Setting precision format: %s", szFormat);
×
1695
    }
1696
    void *pLineBuffer =
1697
        reinterpret_cast<void *>(CPLMalloc(nXSize * sizeof(int)));
23✔
1698
    CPLErr eErr = CE_None;
23✔
1699
    for (int j = 0; j < nYSize && eErr == CE_None; j++)
405✔
1700
    {
1701
        eErr = poSrcDS->GetRasterBand(1)->RasterIO(GF_Read, 0, j, nXSize, 1,
382✔
1702
                                                   pLineBuffer, nXSize, 1,
1703
                                                   eReqDT, 0, 0, nullptr);
1704
        if (eErr != CE_None)
382✔
1705
            break;
×
1706
        const double dfY = gt[3] + (j + 0.5) * gt[5];
382✔
1707
        CPLString osBuf;
382✔
1708
        for (int i = 0; i < nXSize; i++)
42,747✔
1709
        {
1710
            const double dfX = gt[0] + (i + 0.5) * gt[1];
42,375✔
1711
            char szBuf[256];
1712
            if (eReqDT == GDT_Int32)
42,375✔
1713
                CPLsnprintf(szBuf, sizeof(szBuf), szFormat, dfX, pszColSep[0],
1,274✔
1714
                            dfY, pszColSep[0],
1,274✔
1715
                            reinterpret_cast<int *>(pLineBuffer)[i]);
1,274✔
1716
            else
1717
                CPLsnprintf(szBuf, sizeof(szBuf), szFormat, dfX, pszColSep[0],
41,101✔
1718
                            dfY, pszColSep[0],
41,101✔
1719
                            reinterpret_cast<float *>(pLineBuffer)[i]);
41,101✔
1720
            osBuf += szBuf;
42,375✔
1721
            if ((i & 1023) == 0 || i == nXSize - 1)
42,375✔
1722
            {
1723
                if (VSIFWriteL(osBuf, static_cast<int>(osBuf.size()), 1, fp) !=
760✔
1724
                    1)
1725
                {
1726
                    eErr = CE_Failure;
10✔
1727
                    CPLError(CE_Failure, CPLE_AppDefined,
10✔
1728
                             "Write failed, disk full?\n");
1729
                    break;
10✔
1730
                }
1731
                osBuf = "";
750✔
1732
            }
1733
        }
1734
        if (pfnProgress &&
764✔
1735
            !pfnProgress((j + 1) * 1.0 / nYSize, nullptr, pProgressData))
382✔
1736
        {
1737
            eErr = CE_Failure;
×
1738
            break;
×
1739
        }
1740
    }
1741
    CPLFree(pLineBuffer);
23✔
1742
    VSIFCloseL(fp);
23✔
1743

1744
    if (eErr != CE_None)
23✔
1745
        return nullptr;
10✔
1746

1747
    /* -------------------------------------------------------------------- */
1748
    /*      We don't want to call GDALOpen() since it will be expensive,    */
1749
    /*      so we "hand prepare" an XYZ dataset referencing our file.       */
1750
    /* -------------------------------------------------------------------- */
1751
    XYZDataset *poXYZ_DS = new XYZDataset();
13✔
1752
    poXYZ_DS->nRasterXSize = nXSize;
13✔
1753
    poXYZ_DS->nRasterYSize = nYSize;
13✔
1754
    poXYZ_DS->nBands = 1;
13✔
1755
    poXYZ_DS->SetBand(1, new XYZRasterBand(poXYZ_DS, 1, eReqDT));
13✔
1756
    /* If writing to stdout, we can't reopen it --> silence warning */
1757
    CPLPushErrorHandler(CPLQuietErrorHandler);
13✔
1758
    poXYZ_DS->fp = VSIFOpenL(pszFilename, "rb");
13✔
1759
    CPLPopErrorHandler();
13✔
1760
    poXYZ_DS->m_gt = gt;
13✔
1761
    poXYZ_DS->nXIndex = 0;
13✔
1762
    poXYZ_DS->nYIndex = 1;
13✔
1763
    poXYZ_DS->nZIndex = 2;
13✔
1764
    if (pszAddHeaderLine)
13✔
1765
    {
1766
        poXYZ_DS->nDataLineNum = 1;
1✔
1767
        poXYZ_DS->bHasHeaderLine = TRUE;
1✔
1768
    }
1769

1770
    return poXYZ_DS;
13✔
1771
}
1772

1773
/************************************************************************/
1774
/*                          GetGeoTransform()                           */
1775
/************************************************************************/
1776

1777
CPLErr XYZDataset::GetGeoTransform(GDALGeoTransform &gt) const
19✔
1778

1779
{
1780
    gt = m_gt;
19✔
1781

1782
    return CE_None;
19✔
1783
}
1784

1785
/************************************************************************/
1786
/*                         GDALRegister_XYZ()                           */
1787
/************************************************************************/
1788

1789
void GDALRegister_XYZ()
1,911✔
1790

1791
{
1792
    if (GDALGetDriverByName("XYZ") != nullptr)
1,911✔
1793
        return;
282✔
1794

1795
    GDALDriver *poDriver = new GDALDriver();
1,629✔
1796

1797
    poDriver->SetDescription("XYZ");
1,629✔
1798
    poDriver->SetMetadataItem(GDAL_DCAP_RASTER, "YES");
1,629✔
1799
    poDriver->SetMetadataItem(GDAL_DMD_LONGNAME, "ASCII Gridded XYZ");
1,629✔
1800
    poDriver->SetMetadataItem(GDAL_DMD_HELPTOPIC, "drivers/raster/xyz.html");
1,629✔
1801
    poDriver->SetMetadataItem(GDAL_DMD_EXTENSION, "xyz");
1,629✔
1802
    poDriver->SetMetadataItem(
1,629✔
1803
        GDAL_DMD_CREATIONOPTIONLIST,
1804
        "<CreationOptionList>"
1805
        "   <Option name='COLUMN_SEPARATOR' type='string' default=' ' "
1806
        "description='Separator between fields.'/>"
1807
        "   <Option name='ADD_HEADER_LINE' type='boolean' default='false' "
1808
        "description='Add an header line with column names.'/>"
1809
        "   <Option name='SIGNIFICANT_DIGITS' type='int' description='Number "
1810
        "of significant digits when writing floating-point numbers (%g format; "
1811
        "default with 18).'/>\n"
1812
        "   <Option name='DECIMAL_PRECISION' type='int' description='Number of "
1813
        "decimal places when writing floating-point numbers (%f format).'/>\n"
1814
        "</CreationOptionList>");
1,629✔
1815
    poDriver->SetMetadataItem(
1,629✔
1816
        GDAL_DMD_OPENOPTIONLIST,
1817
        "<OpenOptionList>"
1818
        "   <Option name='COLUMN_ORDER' type='string-select' default='AUTO' "
1819
        "description='Specifies the order of the columns. It overrides the "
1820
        "header.'>"
1821
        "       <Value>AUTO</Value>"
1822
        "       <Value>XYZ</Value>"
1823
        "       <Value>YXZ</Value>"
1824
        "   </Option>"
1825
        "</OpenOptionList>");
1,629✔
1826

1827
    poDriver->SetMetadataItem(GDAL_DCAP_VIRTUALIO, "YES");
1,629✔
1828

1829
    poDriver->pfnOpen = XYZDataset::Open;
1,629✔
1830
    poDriver->pfnIdentify = XYZDataset::Identify;
1,629✔
1831
    poDriver->pfnCreateCopy = XYZDataset::CreateCopy;
1,629✔
1832

1833
    GetGDALDriverManager()->RegisterDriver(poDriver);
1,629✔
1834
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc