Coverage Report

Created: 2026-04-01 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/apps/ogrinfo_lib.cpp
Line
Count
Source
1
/******************************************************************************
2
 *
3
 * Project:  OpenGIS Simple Features Reference Implementation
4
 * Purpose:  Simple client for viewing OGR driver data.
5
 * Author:   Frank Warmerdam, warmerdam@pobox.com
6
 *
7
 ******************************************************************************
8
 * Copyright (c) 1999, 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 "cpl_json.h"
16
#include "ogrlibjsonutils.h"
17
#include "cpl_string.h"
18
#include "gdal_utils.h"
19
#include "gdal_utils_priv.h"
20
#include "gdal_priv.h"
21
#include "ogr_feature.h"
22
#include "ogrsf_frmts.h"
23
#include "ogr_geometry.h"
24
#include "commonutils.h"
25
#include "gdalargumentparser.h"
26
27
#include <cmath>
28
#include <set>
29
30
/*! output format */
31
typedef enum
32
{
33
    /*! output in text format */ FORMAT_TEXT = 0,
34
    /*! output in json format */ FORMAT_JSON = 1
35
} GDALVectorInfoFormat;
36
37
struct GDALVectorInfoOptions
38
{
39
    GDALVectorInfoFormat eFormat = FORMAT_TEXT;
40
    std::string osWHERE{};
41
    CPLStringList aosLayers{};
42
    std::unique_ptr<OGRGeometry> poSpatialFilter{};
43
    bool bAllLayers = false;
44
    std::string osSQLStatement{};
45
    std::string osDialect{};
46
    std::string osGeomField{};
47
    CPLStringList aosExtraMDDomains{};
48
    bool bListMDD = false;
49
    bool bShowMetadata = true;
50
    bool bFeatureCount = true;
51
    bool bExtent = true;
52
    bool bExtent3D = false;
53
    bool bGeomType = true;
54
    bool bDatasetGetNextFeature = false;
55
    bool bVerbose = true;
56
    bool bSuperQuiet = false;
57
    bool bSummaryOnly = false;
58
    GIntBig nFetchFID = OGRNullFID;
59
    std::string osWKTFormat = "WKT2";
60
    std::string osFieldDomain{};
61
    CPLStringList aosOptions{};
62
    bool bStdoutOutput = false;  // only set by ogrinfo_bin
63
    int nRepeatCount = 1;
64
65
    /*! Maximum number of features, or -1 if no limit. */
66
    GIntBig nLimit = -1;
67
68
    // Only used during argument parsing
69
    bool bSummaryUserRequested = false;
70
    bool bFeaturesUserRequested = false;
71
72
    // Set by gdal vector info
73
    bool bIsCli = false;
74
75
    // Select the OGR_SCHEMA export
76
    bool bExportOgrSchema = false;
77
};
78
79
/************************************************************************/
80
/*                     GDALVectorInfoOptionsFree()                      */
81
/************************************************************************/
82
83
/**
84
 * Frees the GDALVectorInfoOptions struct.
85
 *
86
 * @param psOptions the options struct for GDALVectorInfo().
87
 *
88
 * @since GDAL 3.7
89
 */
90
91
void GDALVectorInfoOptionsFree(GDALVectorInfoOptions *psOptions)
92
0
{
93
0
    delete psOptions;
94
0
}
95
96
/************************************************************************/
97
/*                               Concat()                               */
98
/************************************************************************/
99
100
#ifndef Concat_defined
101
#define Concat_defined
102
static void Concat(CPLString &osRet, bool bStdoutOutput, const char *pszFormat,
103
                   ...) CPL_PRINT_FUNC_FORMAT(3, 4);
104
105
static void Concat(CPLString &osRet, bool bStdoutOutput, const char *pszFormat,
106
                   ...)
107
0
{
108
0
    va_list args;
109
0
    va_start(args, pszFormat);
110
111
0
    if (bStdoutOutput)
112
0
    {
113
0
        vfprintf(stdout, pszFormat, args);
114
0
    }
115
0
    else
116
0
    {
117
0
        try
118
0
        {
119
0
            CPLString osTarget;
120
0
            osTarget.vPrintf(pszFormat, args);
121
122
0
            osRet += osTarget;
123
0
        }
124
0
        catch (const std::bad_alloc &)
125
0
        {
126
0
            CPLError(CE_Failure, CPLE_OutOfMemory, "Out of memory");
127
0
        }
128
0
    }
129
130
0
    va_end(args);
131
0
}
132
#endif
133
134
static void ConcatStr(CPLString &osRet, bool bStdoutOutput, const char *pszStr)
135
0
{
136
0
    if (bStdoutOutput)
137
0
        fwrite(pszStr, 1, strlen(pszStr), stdout);
138
0
    else
139
0
        osRet += pszStr;
140
0
}
141
142
/************************************************************************/
143
/*                         ReportFieldDomain()                          */
144
/************************************************************************/
145
146
static void ReportFieldDomain(CPLString &osRet, CPLJSONObject &oDomains,
147
                              const GDALVectorInfoOptions *psOptions,
148
                              const OGRFieldDomain *poDomain)
149
0
{
150
0
    const bool bJson = psOptions->eFormat == FORMAT_JSON;
151
0
    CPLJSONObject oDomain;
152
0
    oDomains.Add(poDomain->GetName(), oDomain);
153
0
    Concat(osRet, psOptions->bStdoutOutput, "Domain %s:\n",
154
0
           poDomain->GetName().c_str());
155
0
    const std::string &osDesc = poDomain->GetDescription();
156
0
    if (!osDesc.empty())
157
0
    {
158
0
        if (bJson)
159
0
            oDomain.Set("description", osDesc);
160
0
        else
161
0
            Concat(osRet, psOptions->bStdoutOutput, "  Description: %s\n",
162
0
                   osDesc.c_str());
163
0
    }
164
0
    const char *pszType = "";
165
0
    CPL_IGNORE_RET_VAL(pszType);  // Make CSA happy
166
0
    switch (poDomain->GetDomainType())
167
0
    {
168
0
        case OFDT_CODED:
169
0
            pszType = "coded";
170
0
            break;
171
0
        case OFDT_RANGE:
172
0
            pszType = "range";
173
0
            break;
174
0
        case OFDT_GLOB:
175
0
            pszType = "glob";
176
0
            break;
177
0
    }
178
0
    if (bJson)
179
0
    {
180
0
        oDomain.Set("type", pszType);
181
0
    }
182
0
    else
183
0
    {
184
0
        Concat(osRet, psOptions->bStdoutOutput, "  Type: %s\n", pszType);
185
0
    }
186
0
    const char *pszFieldType =
187
0
        OGRFieldDefn::GetFieldTypeName(poDomain->GetFieldType());
188
0
    const char *pszFieldSubType =
189
0
        OGRFieldDefn::GetFieldSubTypeName(poDomain->GetFieldSubType());
190
0
    if (bJson)
191
0
    {
192
0
        oDomain.Set("fieldType", pszFieldType);
193
0
        if (poDomain->GetFieldSubType() != OFSTNone)
194
0
            oDomain.Set("fieldSubType", pszFieldSubType);
195
0
    }
196
0
    else
197
0
    {
198
0
        const char *pszFieldTypeDisplay =
199
0
            (poDomain->GetFieldSubType() != OFSTNone)
200
0
                ? CPLSPrintf("%s(%s)", pszFieldType, pszFieldSubType)
201
0
                : pszFieldType;
202
0
        Concat(osRet, psOptions->bStdoutOutput, "  Field type: %s\n",
203
0
               pszFieldTypeDisplay);
204
0
    }
205
206
0
    const char *pszSplitPolicy = "";
207
0
    CPL_IGNORE_RET_VAL(pszSplitPolicy);  // Make CSA happy
208
0
    switch (poDomain->GetSplitPolicy())
209
0
    {
210
0
        case OFDSP_DEFAULT_VALUE:
211
0
            pszSplitPolicy = "default value";
212
0
            break;
213
0
        case OFDSP_DUPLICATE:
214
0
            pszSplitPolicy = "duplicate";
215
0
            break;
216
0
        case OFDSP_GEOMETRY_RATIO:
217
0
            pszSplitPolicy = "geometry ratio";
218
0
            break;
219
0
    }
220
0
    if (bJson)
221
0
    {
222
0
        oDomain.Set("splitPolicy", pszSplitPolicy);
223
0
    }
224
0
    else
225
0
    {
226
0
        Concat(osRet, psOptions->bStdoutOutput, "  Split policy: %s\n",
227
0
               pszSplitPolicy);
228
0
    }
229
230
0
    const char *pszMergePolicy = "";
231
0
    CPL_IGNORE_RET_VAL(pszMergePolicy);  // Make CSA happy
232
0
    switch (poDomain->GetMergePolicy())
233
0
    {
234
0
        case OFDMP_DEFAULT_VALUE:
235
0
            pszMergePolicy = "default value";
236
0
            break;
237
0
        case OFDMP_SUM:
238
0
            pszMergePolicy = "sum";
239
0
            break;
240
0
        case OFDMP_GEOMETRY_WEIGHTED:
241
0
            pszMergePolicy = "geometry weighted";
242
0
            break;
243
0
    }
244
0
    if (bJson)
245
0
    {
246
0
        oDomain.Set("mergePolicy", pszMergePolicy);
247
0
    }
248
0
    else
249
0
    {
250
0
        Concat(osRet, psOptions->bStdoutOutput, "  Merge policy: %s\n",
251
0
               pszMergePolicy);
252
0
    }
253
254
0
    switch (poDomain->GetDomainType())
255
0
    {
256
0
        case OFDT_CODED:
257
0
        {
258
0
            const auto poCodedFieldDomain =
259
0
                cpl::down_cast<const OGRCodedFieldDomain *>(poDomain);
260
0
            const OGRCodedValue *enumeration =
261
0
                poCodedFieldDomain->GetEnumeration();
262
0
            if (!bJson)
263
0
                Concat(osRet, psOptions->bStdoutOutput, "  Coded values:\n");
264
0
            CPLJSONObject oCodedValues;
265
0
            oDomain.Add("codedValues", oCodedValues);
266
0
            for (int i = 0; enumeration[i].pszCode != nullptr; ++i)
267
0
            {
268
0
                if (enumeration[i].pszValue)
269
0
                {
270
0
                    if (bJson)
271
0
                    {
272
0
                        oCodedValues.Set(enumeration[i].pszCode,
273
0
                                         enumeration[i].pszValue);
274
0
                    }
275
0
                    else
276
0
                    {
277
0
                        Concat(osRet, psOptions->bStdoutOutput, "    %s: %s\n",
278
0
                               enumeration[i].pszCode, enumeration[i].pszValue);
279
0
                    }
280
0
                }
281
0
                else
282
0
                {
283
0
                    if (bJson)
284
0
                    {
285
0
                        oCodedValues.SetNull(enumeration[i].pszCode);
286
0
                    }
287
0
                    else
288
0
                    {
289
0
                        Concat(osRet, psOptions->bStdoutOutput, "    %s\n",
290
0
                               enumeration[i].pszCode);
291
0
                    }
292
0
                }
293
0
            }
294
0
            break;
295
0
        }
296
297
0
        case OFDT_RANGE:
298
0
        {
299
0
            const auto poRangeFieldDomain =
300
0
                cpl::down_cast<const OGRRangeFieldDomain *>(poDomain);
301
0
            bool bMinIsIncluded = false;
302
0
            const OGRField &sMin = poRangeFieldDomain->GetMin(bMinIsIncluded);
303
0
            bool bMaxIsIncluded = false;
304
0
            const OGRField &sMax = poRangeFieldDomain->GetMax(bMaxIsIncluded);
305
0
            if (poDomain->GetFieldType() == OFTInteger)
306
0
            {
307
0
                if (!OGR_RawField_IsUnset(&sMin))
308
0
                {
309
0
                    if (bJson)
310
0
                    {
311
0
                        oDomain.Set("minValue", sMin.Integer);
312
0
                        oDomain.Set("minValueIncluded", bMinIsIncluded);
313
0
                    }
314
0
                    else
315
0
                    {
316
0
                        Concat(osRet, psOptions->bStdoutOutput,
317
0
                               "  Minimum value: %d%s\n", sMin.Integer,
318
0
                               bMinIsIncluded ? "" : " (excluded)");
319
0
                    }
320
0
                }
321
0
                if (!OGR_RawField_IsUnset(&sMax))
322
0
                {
323
0
                    if (bJson)
324
0
                    {
325
0
                        oDomain.Set("maxValue", sMax.Integer);
326
0
                        oDomain.Set("maxValueIncluded", bMaxIsIncluded);
327
0
                    }
328
0
                    else
329
0
                    {
330
0
                        Concat(osRet, psOptions->bStdoutOutput,
331
0
                               "  Maximum value: %d%s\n", sMax.Integer,
332
0
                               bMaxIsIncluded ? "" : " (excluded)");
333
0
                    }
334
0
                }
335
0
            }
336
0
            else if (poDomain->GetFieldType() == OFTInteger64)
337
0
            {
338
0
                if (!OGR_RawField_IsUnset(&sMin))
339
0
                {
340
0
                    if (bJson)
341
0
                    {
342
0
                        oDomain.Set("minValue", sMin.Integer64);
343
0
                        oDomain.Set("minValueIncluded", bMinIsIncluded);
344
0
                    }
345
0
                    else
346
0
                    {
347
0
                        Concat(osRet, psOptions->bStdoutOutput,
348
0
                               "  Minimum value: " CPL_FRMT_GIB "%s\n",
349
0
                               sMin.Integer64,
350
0
                               bMinIsIncluded ? "" : " (excluded)");
351
0
                    }
352
0
                }
353
0
                if (!OGR_RawField_IsUnset(&sMax))
354
0
                {
355
0
                    if (bJson)
356
0
                    {
357
0
                        oDomain.Set("maxValue", sMax.Integer64);
358
0
                        oDomain.Set("maxValueIncluded", bMaxIsIncluded);
359
0
                    }
360
0
                    else
361
0
                    {
362
0
                        Concat(osRet, psOptions->bStdoutOutput,
363
0
                               "  Maximum value: " CPL_FRMT_GIB "%s\n",
364
0
                               sMax.Integer64,
365
0
                               bMaxIsIncluded ? "" : " (excluded)");
366
0
                    }
367
0
                }
368
0
            }
369
0
            else if (poDomain->GetFieldType() == OFTReal)
370
0
            {
371
0
                if (!OGR_RawField_IsUnset(&sMin))
372
0
                {
373
0
                    if (bJson)
374
0
                    {
375
0
                        oDomain.Set("minValue", sMin.Real);
376
0
                        oDomain.Set("minValueIncluded", bMinIsIncluded);
377
0
                    }
378
0
                    else
379
0
                    {
380
0
                        Concat(osRet, psOptions->bStdoutOutput,
381
0
                               "  Minimum value: %g%s\n", sMin.Real,
382
0
                               bMinIsIncluded ? "" : " (excluded)");
383
0
                    }
384
0
                }
385
0
                if (!OGR_RawField_IsUnset(&sMax))
386
0
                {
387
0
                    if (bJson)
388
0
                    {
389
0
                        oDomain.Set("maxValue", sMax.Real);
390
0
                        oDomain.Set("maxValueIncluded", bMaxIsIncluded);
391
0
                    }
392
0
                    else
393
0
                    {
394
0
                        Concat(osRet, psOptions->bStdoutOutput,
395
0
                               "  Maximum value: %g%s\n", sMax.Real,
396
0
                               bMaxIsIncluded ? "" : " (excluded)");
397
0
                    }
398
0
                }
399
0
            }
400
0
            else if (poDomain->GetFieldType() == OFTDateTime)
401
0
            {
402
0
                if (!OGR_RawField_IsUnset(&sMin))
403
0
                {
404
0
                    const char *pszVal = CPLSPrintf(
405
0
                        "%04d-%02d-%02dT%02d:%02d:%02d", sMin.Date.Year,
406
0
                        sMin.Date.Month, sMin.Date.Day, sMin.Date.Hour,
407
0
                        sMin.Date.Minute,
408
0
                        static_cast<int>(sMin.Date.Second + 0.5f));
409
0
                    if (bJson)
410
0
                    {
411
0
                        oDomain.Set("minValue", pszVal);
412
0
                        oDomain.Set("minValueIncluded", bMinIsIncluded);
413
0
                    }
414
0
                    else
415
0
                    {
416
0
                        Concat(osRet, psOptions->bStdoutOutput,
417
0
                               "  Minimum value: %s%s\n", pszVal,
418
0
                               bMinIsIncluded ? "" : " (excluded)");
419
0
                    }
420
0
                }
421
0
                if (!OGR_RawField_IsUnset(&sMax))
422
0
                {
423
0
                    const char *pszVal = CPLSPrintf(
424
0
                        "%04d-%02d-%02dT%02d:%02d:%02d", sMax.Date.Year,
425
0
                        sMax.Date.Month, sMax.Date.Day, sMax.Date.Hour,
426
0
                        sMax.Date.Minute,
427
0
                        static_cast<int>(sMax.Date.Second + 0.5f));
428
0
                    if (bJson)
429
0
                    {
430
0
                        oDomain.Set("maxValue", pszVal);
431
0
                        oDomain.Set("maxValueIncluded", bMaxIsIncluded);
432
0
                    }
433
0
                    else
434
0
                    {
435
0
                        Concat(osRet, psOptions->bStdoutOutput,
436
0
                               "  Maximum value: %s%s\n", pszVal,
437
0
                               bMaxIsIncluded ? "" : " (excluded)");
438
0
                    }
439
0
                }
440
0
            }
441
0
            break;
442
0
        }
443
444
0
        case OFDT_GLOB:
445
0
        {
446
0
            const auto poGlobFieldDomain =
447
0
                cpl::down_cast<const OGRGlobFieldDomain *>(poDomain);
448
0
            if (bJson)
449
0
                oDomain.Set("glob", poGlobFieldDomain->GetGlob());
450
0
            else
451
0
                Concat(osRet, psOptions->bStdoutOutput, "  Glob: %s\n",
452
0
                       poGlobFieldDomain->GetGlob().c_str());
453
0
            break;
454
0
        }
455
0
    }
456
0
}
457
458
/************************************************************************/
459
/*                        ReportRelationships()                         */
460
/************************************************************************/
461
462
static void ReportRelationships(CPLString &osRet, CPLJSONObject &oRoot,
463
                                const GDALVectorInfoOptions *psOptions,
464
                                const GDALDataset *poDS)
465
0
{
466
0
    const bool bJson = psOptions->eFormat == FORMAT_JSON;
467
0
    CPLJSONObject oRelationships;
468
0
    if (bJson)
469
0
        oRoot.Add("relationships", oRelationships);
470
471
0
    const auto aosRelationshipNames = poDS->GetRelationshipNames();
472
0
    for (const std::string &osRelationshipName : aosRelationshipNames)
473
0
    {
474
0
        const auto poRelationship = poDS->GetRelationship(osRelationshipName);
475
0
        if (!poRelationship)
476
0
            continue;
477
478
0
        const char *pszType = "";
479
0
        CPL_IGNORE_RET_VAL(pszType);  // Make CSA happy
480
0
        switch (poRelationship->GetType())
481
0
        {
482
0
            case GRT_COMPOSITE:
483
0
                pszType = "Composite";
484
0
                break;
485
0
            case GRT_ASSOCIATION:
486
0
                pszType = "Association";
487
0
                break;
488
0
            case GRT_AGGREGATION:
489
0
                pszType = "Aggregation";
490
0
                break;
491
0
        }
492
493
0
        const char *pszCardinality = "";
494
0
        CPL_IGNORE_RET_VAL(pszCardinality);  // Make CSA happy
495
0
        switch (poRelationship->GetCardinality())
496
0
        {
497
0
            case GRC_ONE_TO_ONE:
498
0
                pszCardinality = "OneToOne";
499
0
                break;
500
0
            case GRC_ONE_TO_MANY:
501
0
                pszCardinality = "OneToMany";
502
0
                break;
503
0
            case GRC_MANY_TO_ONE:
504
0
                pszCardinality = "ManyToOne";
505
0
                break;
506
0
            case GRC_MANY_TO_MANY:
507
0
                pszCardinality = "ManyToMany";
508
0
                break;
509
0
        }
510
511
0
        const auto &aosLeftTableFields = poRelationship->GetLeftTableFields();
512
0
        const auto &aosRightTableFields = poRelationship->GetRightTableFields();
513
0
        const auto &osMappingTableName = poRelationship->GetMappingTableName();
514
0
        const auto &aosLeftMappingTableFields =
515
0
            poRelationship->GetLeftMappingTableFields();
516
0
        const auto &aosRightMappingTableFields =
517
0
            poRelationship->GetRightMappingTableFields();
518
519
0
        if (bJson)
520
0
        {
521
0
            CPLJSONObject oRelationship;
522
0
            oRelationships.Add(osRelationshipName, oRelationship);
523
524
0
            oRelationship.Add("type", pszType);
525
0
            oRelationship.Add("related_table_type",
526
0
                              poRelationship->GetRelatedTableType());
527
0
            oRelationship.Add("cardinality", pszCardinality);
528
0
            oRelationship.Add("left_table_name",
529
0
                              poRelationship->GetLeftTableName());
530
0
            oRelationship.Add("right_table_name",
531
0
                              poRelationship->GetRightTableName());
532
533
0
            CPLJSONArray oLeftTableFields;
534
0
            oRelationship.Add("left_table_fields", oLeftTableFields);
535
0
            for (const auto &osName : aosLeftTableFields)
536
0
                oLeftTableFields.Add(osName);
537
538
0
            CPLJSONArray oRightTableFields;
539
0
            oRelationship.Add("right_table_fields", oRightTableFields);
540
0
            for (const auto &osName : aosRightTableFields)
541
0
                oRightTableFields.Add(osName);
542
543
0
            if (!osMappingTableName.empty())
544
0
            {
545
0
                oRelationship.Add("mapping_table_name", osMappingTableName);
546
547
0
                CPLJSONArray oLeftMappingTableFields;
548
0
                oRelationship.Add("left_mapping_table_fields",
549
0
                                  oLeftMappingTableFields);
550
0
                for (const auto &osName : aosLeftMappingTableFields)
551
0
                    oLeftMappingTableFields.Add(osName);
552
553
0
                CPLJSONArray oRightMappingTableFields;
554
0
                oRelationship.Add("right_mapping_table_fields",
555
0
                                  oRightMappingTableFields);
556
0
                for (const auto &osName : aosRightMappingTableFields)
557
0
                    oRightMappingTableFields.Add(osName);
558
0
            }
559
560
0
            oRelationship.Add("forward_path_label",
561
0
                              poRelationship->GetForwardPathLabel());
562
0
            oRelationship.Add("backward_path_label",
563
0
                              poRelationship->GetBackwardPathLabel());
564
0
        }
565
0
        else
566
0
        {
567
0
            const auto ConcatStringList =
568
0
                [&osRet, psOptions](const std::vector<std::string> &aosList)
569
0
            {
570
0
                bool bFirstName = true;
571
0
                for (const auto &osName : aosList)
572
0
                {
573
0
                    if (!bFirstName)
574
0
                        ConcatStr(osRet, psOptions->bStdoutOutput, ", ");
575
0
                    bFirstName = false;
576
0
                    ConcatStr(osRet, psOptions->bStdoutOutput, osName.c_str());
577
0
                }
578
0
                Concat(osRet, psOptions->bStdoutOutput, "\n");
579
0
            };
580
581
0
            if (!psOptions->bAllLayers)
582
0
            {
583
0
                Concat(osRet, psOptions->bStdoutOutput,
584
0
                       "Relationship: %s (%s, %s, %s)\n",
585
0
                       osRelationshipName.c_str(), pszType,
586
0
                       poRelationship->GetLeftTableName().c_str(),
587
0
                       poRelationship->GetRightTableName().c_str());
588
0
                continue;
589
0
            }
590
0
            Concat(osRet, psOptions->bStdoutOutput, "\nRelationship: %s\n",
591
0
                   osRelationshipName.c_str());
592
0
            Concat(osRet, psOptions->bStdoutOutput, "  Type: %s\n", pszType);
593
0
            Concat(osRet, psOptions->bStdoutOutput,
594
0
                   "  Related table type: %s\n",
595
0
                   poRelationship->GetRelatedTableType().c_str());
596
0
            Concat(osRet, psOptions->bStdoutOutput, "  Cardinality: %s\n",
597
0
                   pszCardinality);
598
0
            Concat(osRet, psOptions->bStdoutOutput, "  Left table name: %s\n",
599
0
                   poRelationship->GetLeftTableName().c_str());
600
0
            Concat(osRet, psOptions->bStdoutOutput, "  Right table name: %s\n",
601
0
                   poRelationship->GetRightTableName().c_str());
602
0
            Concat(osRet, psOptions->bStdoutOutput, "  Left table fields: ");
603
0
            ConcatStringList(aosLeftTableFields);
604
0
            Concat(osRet, psOptions->bStdoutOutput, "  Right table fields: ");
605
0
            ConcatStringList(aosRightTableFields);
606
607
0
            if (!osMappingTableName.empty())
608
0
            {
609
0
                Concat(osRet, psOptions->bStdoutOutput,
610
0
                       "  Mapping table name: %s\n",
611
0
                       osMappingTableName.c_str());
612
613
0
                Concat(osRet, psOptions->bStdoutOutput,
614
0
                       "  Left mapping table fields: ");
615
0
                ConcatStringList(aosLeftMappingTableFields);
616
617
0
                Concat(osRet, psOptions->bStdoutOutput,
618
0
                       "  Right mapping table fields: ");
619
0
                ConcatStringList(aosRightMappingTableFields);
620
0
            }
621
622
0
            Concat(osRet, psOptions->bStdoutOutput,
623
0
                   "  Forward path label: %s\n",
624
0
                   poRelationship->GetForwardPathLabel().c_str());
625
0
            Concat(osRet, psOptions->bStdoutOutput,
626
0
                   "  Backward path label: %s\n",
627
0
                   poRelationship->GetBackwardPathLabel().c_str());
628
0
        }
629
0
    }
630
0
}
631
632
/************************************************************************/
633
/*                    GDALVectorInfoPrintMetadata()                     */
634
/************************************************************************/
635
636
static void
637
GDALVectorInfoPrintMetadata(CPLString &osRet, CPLJSONObject &oMetadata,
638
                            const GDALVectorInfoOptions *psOptions,
639
                            GDALMajorObjectH hObject, const char *pszDomain,
640
                            const char *pszDisplayedname, const char *pszIndent)
641
0
{
642
0
    const bool bJsonOutput = psOptions->eFormat == FORMAT_JSON;
643
0
    bool bIsxml = false;
644
0
    bool bMDIsJson = false;
645
646
0
    if (pszDomain != nullptr && STARTS_WITH_CI(pszDomain, "xml:"))
647
0
        bIsxml = true;
648
0
    else if (pszDomain != nullptr && STARTS_WITH_CI(pszDomain, "json:"))
649
0
        bMDIsJson = true;
650
651
0
    CSLConstList papszMetadata = GDALGetMetadata(hObject, pszDomain);
652
0
    if (CSLCount(papszMetadata) > 0)
653
0
    {
654
0
        CPLJSONObject oMetadataDomain;
655
0
        if (!bJsonOutput)
656
0
            Concat(osRet, psOptions->bStdoutOutput, "%s%s:\n", pszIndent,
657
0
                   pszDisplayedname);
658
0
        for (int i = 0; papszMetadata[i] != nullptr; i++)
659
0
        {
660
0
            if (bJsonOutput)
661
0
            {
662
0
                if (bIsxml)
663
0
                {
664
0
                    oMetadata.Add(pszDomain, papszMetadata[i]);
665
0
                    return;
666
0
                }
667
0
                else if (bMDIsJson)
668
0
                {
669
0
                    CPLJSONDocument oDoc;
670
0
                    if (oDoc.LoadMemory(papszMetadata[i]))
671
0
                        oMetadata.Add(pszDomain, oDoc.GetRoot());
672
0
                    return;
673
0
                }
674
0
                else
675
0
                {
676
0
                    char *pszKey = nullptr;
677
0
                    const char *pszValue =
678
0
                        CPLParseNameValue(papszMetadata[i], &pszKey);
679
0
                    if (pszKey)
680
0
                    {
681
0
                        oMetadataDomain.Add(pszKey, pszValue);
682
0
                        CPLFree(pszKey);
683
0
                    }
684
0
                }
685
0
            }
686
0
            else if (bIsxml)
687
0
                Concat(osRet, psOptions->bStdoutOutput, "%s%s\n", pszIndent,
688
0
                       papszMetadata[i]);
689
0
            else
690
0
                Concat(osRet, psOptions->bStdoutOutput, "%s  %s\n", pszIndent,
691
0
                       papszMetadata[i]);
692
0
        }
693
0
        if (bJsonOutput)
694
0
        {
695
0
            oMetadata.Add(pszDomain ? pszDomain : "", oMetadataDomain);
696
0
        }
697
0
    }
698
0
}
699
700
/************************************************************************/
701
/*                    GDALVectorInfoReportMetadata()                    */
702
/************************************************************************/
703
704
static void GDALVectorInfoReportMetadata(CPLString &osRet, CPLJSONObject &oRoot,
705
                                         const GDALVectorInfoOptions *psOptions,
706
                                         GDALMajorObject *poMajorObject,
707
                                         bool bListMDD, bool bShowMetadata,
708
                                         CSLConstList papszExtraMDDomains)
709
0
{
710
0
    const char *pszIndent = "";
711
0
    auto hObject = GDALMajorObject::ToHandle(poMajorObject);
712
713
0
    const bool bJson = psOptions->eFormat == FORMAT_JSON;
714
    /* -------------------------------------------------------------------- */
715
    /*      Report list of Metadata domains                                 */
716
    /* -------------------------------------------------------------------- */
717
0
    if (bListMDD)
718
0
    {
719
0
        const CPLStringList aosMDDList(GDALGetMetadataDomainList(hObject));
720
721
0
        CPLJSONArray metadataDomains;
722
723
0
        if (!aosMDDList.empty() && !bJson)
724
0
            Concat(osRet, psOptions->bStdoutOutput, "%sMetadata domains:\n",
725
0
                   pszIndent);
726
0
        for (const char *pszDomain : aosMDDList)
727
0
        {
728
0
            if (EQUAL(pszDomain, ""))
729
0
            {
730
0
                if (bJson)
731
0
                    metadataDomains.Add("");
732
0
                else
733
0
                    Concat(osRet, psOptions->bStdoutOutput, "%s  (default)\n",
734
0
                           pszIndent);
735
0
            }
736
0
            else
737
0
            {
738
0
                if (bJson)
739
0
                    metadataDomains.Add(pszDomain);
740
0
                else
741
0
                    Concat(osRet, psOptions->bStdoutOutput, "%s  %s\n",
742
0
                           pszIndent, pszDomain);
743
0
            }
744
0
        }
745
746
0
        if (bJson)
747
0
            oRoot.Add("metadataDomains", metadataDomains);
748
0
    }
749
750
0
    if (!bShowMetadata)
751
0
        return;
752
753
    /* -------------------------------------------------------------------- */
754
    /*      Report default Metadata domain.                                 */
755
    /* -------------------------------------------------------------------- */
756
0
    CPLJSONObject oMetadata;
757
0
    oRoot.Add("metadata", oMetadata);
758
0
    GDALVectorInfoPrintMetadata(osRet, oMetadata, psOptions, hObject, nullptr,
759
0
                                "Metadata", pszIndent);
760
761
    /* -------------------------------------------------------------------- */
762
    /*      Report extra Metadata domains                                   */
763
    /* -------------------------------------------------------------------- */
764
0
    if (papszExtraMDDomains != nullptr)
765
0
    {
766
0
        CPLStringList aosExtraMDDomainsExpanded;
767
768
0
        if (EQUAL(papszExtraMDDomains[0], "all") &&
769
0
            papszExtraMDDomains[1] == nullptr)
770
0
        {
771
0
            const CPLStringList aosMDDList(GDALGetMetadataDomainList(hObject));
772
0
            for (const char *pszDomain : aosMDDList)
773
0
            {
774
0
                if (!EQUAL(pszDomain, "") && !EQUAL(pszDomain, "SUBDATASETS"))
775
0
                {
776
0
                    aosExtraMDDomainsExpanded.AddString(pszDomain);
777
0
                }
778
0
            }
779
0
        }
780
0
        else
781
0
        {
782
0
            aosExtraMDDomainsExpanded = CSLDuplicate(papszExtraMDDomains);
783
0
        }
784
785
0
        for (const char *pszDomain : aosExtraMDDomainsExpanded)
786
0
        {
787
0
            const std::string osDisplayedName =
788
0
                std::string("Metadata (").append(pszDomain).append(")");
789
0
            GDALVectorInfoPrintMetadata(osRet, oMetadata, psOptions, hObject,
790
0
                                        pszDomain, osDisplayedName.c_str(),
791
0
                                        pszIndent);
792
0
        }
793
0
    }
794
0
    GDALVectorInfoPrintMetadata(osRet, oMetadata, psOptions, hObject,
795
0
                                "SUBDATASETS", "Subdatasets", pszIndent);
796
0
}
797
798
/************************************************************************/
799
/*                           ReportOnLayer()                            */
800
/************************************************************************/
801
802
static void ReportOnLayer(CPLString &osRet, CPLJSONObject &oLayer,
803
                          const GDALVectorInfoOptions *psOptions,
804
                          OGRLayer *poLayer, bool bForceSummary,
805
                          bool bTakeIntoAccountWHERE,
806
                          bool bTakeIntoAccountSpatialFilter,
807
                          bool bTakeIntoAccountGeomField)
808
0
{
809
0
    const bool bJson = psOptions->eFormat == FORMAT_JSON;
810
0
    const bool bIsSummaryCli =
811
0
        psOptions->bIsCli && psOptions->bSummaryUserRequested;
812
0
    const bool bExportOgrSchema = psOptions->bExportOgrSchema;
813
0
    OGRFeatureDefn *poDefn = poLayer->GetLayerDefn();
814
815
0
    oLayer.Set("name", poLayer->GetName());
816
0
    if (bExportOgrSchema)
817
0
    {
818
0
        oLayer.Set("schemaType", "Full");
819
0
    }
820
0
    const int nGeomFieldCount =
821
0
        psOptions->bGeomType ? poLayer->GetLayerDefn()->GetGeomFieldCount() : 0;
822
823
    /* -------------------------------------------------------------------- */
824
    /*      Set filters if provided.                                        */
825
    /* -------------------------------------------------------------------- */
826
0
    if (bTakeIntoAccountWHERE && !psOptions->osWHERE.empty())
827
0
    {
828
0
        if (poLayer->SetAttributeFilter(psOptions->osWHERE.c_str()) !=
829
0
            OGRERR_NONE)
830
0
        {
831
0
            CPLError(CE_Failure, CPLE_AppDefined,
832
0
                     "SetAttributeFilter(%s) failed.",
833
0
                     psOptions->osWHERE.c_str());
834
0
            return;
835
0
        }
836
0
    }
837
838
0
    if (bTakeIntoAccountSpatialFilter && psOptions->poSpatialFilter != nullptr)
839
0
    {
840
0
        if (bTakeIntoAccountGeomField && !psOptions->osGeomField.empty())
841
0
        {
842
0
            const int iGeomField =
843
0
                poDefn->GetGeomFieldIndex(psOptions->osGeomField.c_str());
844
0
            if (iGeomField >= 0)
845
0
                poLayer->SetSpatialFilter(iGeomField,
846
0
                                          psOptions->poSpatialFilter.get());
847
0
            else
848
0
                CPLError(CE_Warning, CPLE_AppDefined,
849
0
                         "Cannot find geometry field %s.",
850
0
                         psOptions->osGeomField.c_str());
851
0
        }
852
0
        else
853
0
        {
854
0
            poLayer->SetSpatialFilter(psOptions->poSpatialFilter.get());
855
0
        }
856
0
    }
857
858
    /* -------------------------------------------------------------------- */
859
    /*      Report various overall information.                             */
860
    /* -------------------------------------------------------------------- */
861
0
    if (!bJson && !psOptions->bSuperQuiet)
862
0
    {
863
0
        Concat(osRet, psOptions->bStdoutOutput, "\n");
864
0
        Concat(osRet, psOptions->bStdoutOutput, "Layer name: %s\n",
865
0
               poLayer->GetName());
866
0
    }
867
868
0
    GDALVectorInfoReportMetadata(osRet, oLayer, psOptions, poLayer,
869
0
                                 !bIsSummaryCli && psOptions->bListMDD,
870
0
                                 !bIsSummaryCli && psOptions->bShowMetadata,
871
0
                                 psOptions->aosExtraMDDomains.List());
872
873
0
    if (psOptions->bVerbose)
874
0
    {
875
876
0
        CPLString osWKTFormat("FORMAT=");
877
0
        osWKTFormat += psOptions->osWKTFormat;
878
0
        const char *const apszWKTOptions[] = {osWKTFormat.c_str(),
879
0
                                              "MULTILINE=YES", nullptr};
880
881
0
        if (bJson || nGeomFieldCount > 1)
882
0
        {
883
0
            CPLJSONArray oGeometryFields;
884
0
            if (bJson)
885
0
                oLayer.Add("geometryFields", oGeometryFields);
886
0
            for (int iGeom = 0; iGeom < nGeomFieldCount; iGeom++)
887
0
            {
888
0
                const OGRGeomFieldDefn *poGFldDefn =
889
0
                    poLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom);
890
0
                if (bJson)
891
0
                {
892
0
                    CPLJSONObject oGeometryField;
893
0
                    oGeometryFields.Add(oGeometryField);
894
0
                    oGeometryField.Set("name", poGFldDefn->GetNameRef());
895
0
                    oGeometryField.Set(
896
0
                        "type", OGRToOGCGeomType(poGFldDefn->GetType(),
897
0
                                                 /*bCamelCase=*/true,
898
0
                                                 /*bAddZm=*/true,
899
0
                                                 /*bSpaceBeforeZM=*/false));
900
0
                    oGeometryField.Set("nullable",
901
0
                                       CPL_TO_BOOL(poGFldDefn->IsNullable()));
902
0
                    if (psOptions->bExtent3D)
903
0
                    {
904
0
                        OGREnvelope3D oExt;
905
0
                        if (poLayer->GetExtent3D(iGeom, &oExt, TRUE) ==
906
0
                            OGRERR_NONE)
907
0
                        {
908
0
                            {
909
0
                                CPLJSONArray oBbox;
910
0
                                oBbox.Add(oExt.MinX);
911
0
                                oBbox.Add(oExt.MinY);
912
0
                                oBbox.Add(oExt.MaxX);
913
0
                                oBbox.Add(oExt.MaxY);
914
0
                                oGeometryField.Add("extent", oBbox);
915
0
                            }
916
0
                            {
917
0
                                CPLJSONArray oBbox;
918
0
                                oBbox.Add(oExt.MinX);
919
0
                                oBbox.Add(oExt.MinY);
920
0
                                if (std::isfinite(oExt.MinZ))
921
0
                                    oBbox.Add(oExt.MinZ);
922
0
                                else
923
0
                                    oBbox.AddNull();
924
0
                                oBbox.Add(oExt.MaxX);
925
0
                                oBbox.Add(oExt.MaxY);
926
0
                                if (std::isfinite(oExt.MaxZ))
927
0
                                    oBbox.Add(oExt.MaxZ);
928
0
                                else
929
0
                                    oBbox.AddNull();
930
0
                                oGeometryField.Add("extent3D", oBbox);
931
0
                            }
932
0
                        }
933
0
                    }
934
0
                    else if (psOptions->bExtent)
935
0
                    {
936
0
                        OGREnvelope oExt;
937
0
                        if (poLayer->GetExtent(iGeom, &oExt, TRUE) ==
938
0
                            OGRERR_NONE)
939
0
                        {
940
0
                            CPLJSONArray oBbox;
941
0
                            oBbox.Add(oExt.MinX);
942
0
                            oBbox.Add(oExt.MinY);
943
0
                            oBbox.Add(oExt.MaxX);
944
0
                            oBbox.Add(oExt.MaxY);
945
0
                            oGeometryField.Add("extent", oBbox);
946
0
                        }
947
0
                    }
948
0
                    const OGRSpatialReference *poSRS =
949
0
                        poGFldDefn->GetSpatialRef();
950
0
                    if (poSRS)
951
0
                    {
952
0
                        CPLJSONObject oCRS;
953
0
                        oGeometryField.Add("coordinateSystem", oCRS);
954
0
                        char *pszWKT = nullptr;
955
0
                        poSRS->exportToWkt(&pszWKT, apszWKTOptions);
956
0
                        if (pszWKT)
957
0
                        {
958
0
                            oCRS.Set("wkt", pszWKT);
959
0
                            CPLFree(pszWKT);
960
0
                        }
961
962
0
                        {
963
0
                            char *pszProjJson = nullptr;
964
                            // PROJJSON requires PROJ >= 6.2
965
0
                            CPLErrorStateBackuper oCPLErrorHandlerPusher(
966
0
                                CPLQuietErrorHandler);
967
0
                            CPL_IGNORE_RET_VAL(
968
0
                                poSRS->exportToPROJJSON(&pszProjJson, nullptr));
969
0
                            if (pszProjJson)
970
0
                            {
971
0
                                CPLJSONDocument oDoc;
972
0
                                if (oDoc.LoadMemory(pszProjJson))
973
0
                                {
974
0
                                    oCRS.Add("projjson", oDoc.GetRoot());
975
0
                                }
976
0
                                CPLFree(pszProjJson);
977
0
                            }
978
0
                        }
979
980
0
                        const auto &anAxes =
981
0
                            poSRS->GetDataAxisToSRSAxisMapping();
982
0
                        CPLJSONArray oAxisMapping;
983
0
                        for (const auto nAxis : anAxes)
984
0
                        {
985
0
                            oAxisMapping.Add(nAxis);
986
0
                        }
987
0
                        oCRS.Add("dataAxisToSRSAxisMapping", oAxisMapping);
988
989
0
                        const double dfCoordinateEpoch =
990
0
                            poSRS->GetCoordinateEpoch();
991
0
                        if (dfCoordinateEpoch > 0)
992
0
                            oCRS.Set("coordinateEpoch", dfCoordinateEpoch);
993
0
                    }
994
0
                    else
995
0
                    {
996
0
                        oGeometryField.SetNull("coordinateSystem");
997
0
                    }
998
999
0
                    const auto &srsList = poLayer->GetSupportedSRSList(iGeom);
1000
0
                    if (!srsList.empty())
1001
0
                    {
1002
0
                        CPLJSONArray oSupportedSRSList;
1003
0
                        for (const auto &poSupportedSRS : srsList)
1004
0
                        {
1005
0
                            const char *pszAuthName =
1006
0
                                poSupportedSRS->GetAuthorityName(nullptr);
1007
0
                            const char *pszAuthCode =
1008
0
                                poSupportedSRS->GetAuthorityCode(nullptr);
1009
0
                            CPLJSONObject oSupportedSRS;
1010
0
                            if (pszAuthName && pszAuthCode)
1011
0
                            {
1012
0
                                CPLJSONObject id;
1013
0
                                id.Set("authority", pszAuthName);
1014
0
                                id.Set("code", pszAuthCode);
1015
0
                                oSupportedSRS.Add("id", id);
1016
0
                                oSupportedSRSList.Add(oSupportedSRS);
1017
0
                            }
1018
0
                            else
1019
0
                            {
1020
0
                                char *pszWKT = nullptr;
1021
0
                                poSupportedSRS->exportToWkt(&pszWKT,
1022
0
                                                            apszWKTOptions);
1023
0
                                if (pszWKT)
1024
0
                                {
1025
0
                                    oSupportedSRS.Add("wkt", pszWKT);
1026
0
                                    oSupportedSRSList.Add(oSupportedSRS);
1027
0
                                }
1028
0
                                CPLFree(pszWKT);
1029
0
                            }
1030
0
                        }
1031
0
                        oGeometryField.Add("supportedSRSList",
1032
0
                                           oSupportedSRSList);
1033
0
                    }
1034
1035
0
                    const auto &oCoordPrec =
1036
0
                        poGFldDefn->GetCoordinatePrecision();
1037
0
                    if (oCoordPrec.dfXYResolution !=
1038
0
                        OGRGeomCoordinatePrecision::UNKNOWN)
1039
0
                    {
1040
0
                        oGeometryField.Add("xyCoordinateResolution",
1041
0
                                           oCoordPrec.dfXYResolution);
1042
0
                    }
1043
0
                    if (oCoordPrec.dfZResolution !=
1044
0
                        OGRGeomCoordinatePrecision::UNKNOWN)
1045
0
                    {
1046
0
                        oGeometryField.Add("zCoordinateResolution",
1047
0
                                           oCoordPrec.dfZResolution);
1048
0
                    }
1049
0
                    if (oCoordPrec.dfMResolution !=
1050
0
                        OGRGeomCoordinatePrecision::UNKNOWN)
1051
0
                    {
1052
0
                        oGeometryField.Add("mCoordinateResolution",
1053
0
                                           oCoordPrec.dfMResolution);
1054
0
                    }
1055
1056
                    // For example set by OpenFileGDB driver
1057
0
                    if (!oCoordPrec.oFormatSpecificOptions.empty())
1058
0
                    {
1059
0
                        CPLJSONObject oFormatSpecificOptions;
1060
0
                        for (const auto &formatOptionsPair :
1061
0
                             oCoordPrec.oFormatSpecificOptions)
1062
0
                        {
1063
0
                            CPLJSONObject oThisFormatSpecificOptions;
1064
0
                            for (const auto &[pszKey, pszValue] :
1065
0
                                 cpl::IterateNameValue(
1066
0
                                     formatOptionsPair.second))
1067
0
                            {
1068
0
                                const auto eValueType =
1069
0
                                    CPLGetValueType(pszValue);
1070
0
                                if (eValueType == CPL_VALUE_INTEGER)
1071
0
                                {
1072
0
                                    oThisFormatSpecificOptions.Add(
1073
0
                                        pszKey, CPLAtoGIntBig(pszValue));
1074
0
                                }
1075
0
                                else if (eValueType == CPL_VALUE_REAL)
1076
0
                                {
1077
0
                                    oThisFormatSpecificOptions.Add(
1078
0
                                        pszKey, CPLAtof(pszValue));
1079
0
                                }
1080
0
                                else
1081
0
                                {
1082
0
                                    oThisFormatSpecificOptions.Add(pszKey,
1083
0
                                                                   pszValue);
1084
0
                                }
1085
0
                            }
1086
0
                            oFormatSpecificOptions.Add(
1087
0
                                formatOptionsPair.first,
1088
0
                                oThisFormatSpecificOptions);
1089
0
                        }
1090
0
                        oGeometryField.Add(
1091
0
                            "coordinatePrecisionFormatSpecificOptions",
1092
0
                            oFormatSpecificOptions);
1093
0
                    }
1094
0
                }
1095
0
                else
1096
0
                {
1097
0
                    Concat(osRet, psOptions->bStdoutOutput,
1098
0
                           "Geometry (%s): %s\n", poGFldDefn->GetNameRef(),
1099
0
                           OGRGeometryTypeToName(poGFldDefn->GetType()));
1100
0
                }
1101
0
            }
1102
0
        }
1103
0
        else if (psOptions->bGeomType)
1104
0
        {
1105
0
            Concat(osRet, psOptions->bStdoutOutput, "Geometry: %s\n",
1106
0
                   OGRGeometryTypeToName(poLayer->GetGeomType()));
1107
0
        }
1108
1109
0
        if (psOptions->bFeatureCount)
1110
0
        {
1111
0
            if (bJson)
1112
0
                oLayer.Set("featureCount", poLayer->GetFeatureCount());
1113
0
            else
1114
0
            {
1115
0
                Concat(osRet, psOptions->bStdoutOutput,
1116
0
                       "Feature Count: " CPL_FRMT_GIB "\n",
1117
0
                       poLayer->GetFeatureCount());
1118
0
            }
1119
0
        }
1120
1121
0
        if (!bJson && psOptions->bExtent && nGeomFieldCount > 1)
1122
0
        {
1123
0
            for (int iGeom = 0; iGeom < nGeomFieldCount; iGeom++)
1124
0
            {
1125
0
                if (psOptions->bExtent3D)
1126
0
                {
1127
0
                    OGREnvelope3D oExt;
1128
0
                    if (poLayer->GetExtent3D(iGeom, &oExt, TRUE) == OGRERR_NONE)
1129
0
                    {
1130
0
                        OGRGeomFieldDefn *poGFldDefn =
1131
0
                            poLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom);
1132
0
                        Concat(osRet, psOptions->bStdoutOutput,
1133
0
                               "Extent (%s): (%f, %f, %s) - (%f, %f, %s)\n",
1134
0
                               poGFldDefn->GetNameRef(), oExt.MinX, oExt.MinY,
1135
0
                               std::isfinite(oExt.MinZ)
1136
0
                                   ? CPLSPrintf("%f", oExt.MinZ)
1137
0
                                   : "none",
1138
0
                               oExt.MaxX, oExt.MaxY,
1139
0
                               std::isfinite(oExt.MaxZ)
1140
0
                                   ? CPLSPrintf("%f", oExt.MaxZ)
1141
0
                                   : "none");
1142
0
                    }
1143
0
                }
1144
0
                else
1145
0
                {
1146
0
                    OGREnvelope oExt;
1147
0
                    if (poLayer->GetExtent(iGeom, &oExt, TRUE) == OGRERR_NONE)
1148
0
                    {
1149
0
                        OGRGeomFieldDefn *poGFldDefn =
1150
0
                            poLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom);
1151
0
                        Concat(osRet, psOptions->bStdoutOutput,
1152
0
                               "Extent (%s): (%f, %f) - (%f, %f)\n",
1153
0
                               poGFldDefn->GetNameRef(), oExt.MinX, oExt.MinY,
1154
0
                               oExt.MaxX, oExt.MaxY);
1155
0
                    }
1156
0
                }
1157
0
            }
1158
0
        }
1159
0
        else if (!bJson && psOptions->bExtent)
1160
0
        {
1161
0
            if (psOptions->bExtent3D)
1162
0
            {
1163
0
                OGREnvelope3D oExt;
1164
0
                if (poLayer->GetExtent3D(0, &oExt, TRUE) == OGRERR_NONE)
1165
0
                {
1166
0
                    Concat(
1167
0
                        osRet, psOptions->bStdoutOutput,
1168
0
                        "Extent: (%f, %f, %s) - (%f, %f, %s)\n", oExt.MinX,
1169
0
                        oExt.MinY,
1170
0
                        std::isfinite(oExt.MinZ) ? CPLSPrintf("%f", oExt.MinZ)
1171
0
                                                 : "none",
1172
0
                        oExt.MaxX, oExt.MaxY,
1173
0
                        std::isfinite(oExt.MaxZ) ? CPLSPrintf("%f", oExt.MaxZ)
1174
0
                                                 : "none");
1175
0
                }
1176
0
            }
1177
0
            else
1178
0
            {
1179
0
                OGREnvelope oExt;
1180
0
                if (poLayer->GetExtent(&oExt, TRUE) == OGRERR_NONE)
1181
0
                {
1182
0
                    Concat(osRet, psOptions->bStdoutOutput,
1183
0
                           "Extent: (%f, %f) - (%f, %f)\n", oExt.MinX,
1184
0
                           oExt.MinY, oExt.MaxX, oExt.MaxY);
1185
0
                }
1186
0
            }
1187
0
        }
1188
1189
0
        const auto displayExtraInfoSRS =
1190
0
            [&osRet, &psOptions](const OGRSpatialReference *poSRS)
1191
0
        {
1192
0
            const double dfCoordinateEpoch = poSRS->GetCoordinateEpoch();
1193
0
            if (dfCoordinateEpoch > 0)
1194
0
            {
1195
0
                std::string osCoordinateEpoch =
1196
0
                    CPLSPrintf("%f", dfCoordinateEpoch);
1197
0
                const size_t nDotPos = osCoordinateEpoch.find('.');
1198
0
                if (nDotPos != std::string::npos)
1199
0
                {
1200
0
                    while (osCoordinateEpoch.size() > nDotPos + 2 &&
1201
0
                           osCoordinateEpoch.back() == '0')
1202
0
                        osCoordinateEpoch.pop_back();
1203
0
                }
1204
0
                Concat(osRet, psOptions->bStdoutOutput,
1205
0
                       "Coordinate epoch: %s\n", osCoordinateEpoch.c_str());
1206
0
            }
1207
1208
0
            const auto &mapping = poSRS->GetDataAxisToSRSAxisMapping();
1209
0
            Concat(osRet, psOptions->bStdoutOutput,
1210
0
                   "Data axis to CRS axis mapping: ");
1211
0
            for (size_t i = 0; i < mapping.size(); i++)
1212
0
            {
1213
0
                if (i > 0)
1214
0
                {
1215
0
                    Concat(osRet, psOptions->bStdoutOutput, ",");
1216
0
                }
1217
0
                Concat(osRet, psOptions->bStdoutOutput, "%d", mapping[i]);
1218
0
            }
1219
0
            Concat(osRet, psOptions->bStdoutOutput, "\n");
1220
0
        };
1221
1222
0
        const auto DisplaySupportedCRSList = [&](int iGeomField)
1223
0
        {
1224
0
            const auto &srsList = poLayer->GetSupportedSRSList(iGeomField);
1225
0
            if (!srsList.empty())
1226
0
            {
1227
0
                Concat(osRet, psOptions->bStdoutOutput, "Supported SRS: ");
1228
0
                bool bFirst = true;
1229
0
                for (const auto &poSupportedSRS : srsList)
1230
0
                {
1231
0
                    const char *pszAuthName =
1232
0
                        poSupportedSRS->GetAuthorityName(nullptr);
1233
0
                    const char *pszAuthCode =
1234
0
                        poSupportedSRS->GetAuthorityCode(nullptr);
1235
0
                    if (!bFirst)
1236
0
                        Concat(osRet, psOptions->bStdoutOutput, ", ");
1237
0
                    bFirst = false;
1238
0
                    if (pszAuthName && pszAuthCode)
1239
0
                    {
1240
0
                        Concat(osRet, psOptions->bStdoutOutput, "%s:%s",
1241
0
                               pszAuthName, pszAuthCode);
1242
0
                    }
1243
0
                    else
1244
0
                    {
1245
0
                        ConcatStr(osRet, psOptions->bStdoutOutput,
1246
0
                                  poSupportedSRS->GetName());
1247
0
                    }
1248
0
                }
1249
0
                Concat(osRet, psOptions->bStdoutOutput, "\n");
1250
0
            }
1251
0
        };
1252
1253
0
        if (!bJson && nGeomFieldCount > 1)
1254
0
        {
1255
1256
0
            for (int iGeom = 0; iGeom < nGeomFieldCount; iGeom++)
1257
0
            {
1258
0
                OGRGeomFieldDefn *poGFldDefn =
1259
0
                    poLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom);
1260
0
                const OGRSpatialReference *poSRS = poGFldDefn->GetSpatialRef();
1261
0
                char *pszWKT = nullptr;
1262
0
                if (poSRS == nullptr)
1263
0
                {
1264
0
                    pszWKT = CPLStrdup("(unknown)");
1265
0
                }
1266
0
                else
1267
0
                {
1268
0
                    poSRS->exportToWkt(&pszWKT, apszWKTOptions);
1269
0
                }
1270
1271
0
                Concat(osRet, psOptions->bStdoutOutput, "SRS WKT (%s):\n%s\n",
1272
0
                       poGFldDefn->GetNameRef(), pszWKT);
1273
0
                CPLFree(pszWKT);
1274
0
                if (poSRS)
1275
0
                {
1276
0
                    displayExtraInfoSRS(poSRS);
1277
0
                }
1278
0
                DisplaySupportedCRSList(iGeom);
1279
0
            }
1280
0
        }
1281
0
        else if (!bJson)
1282
0
        {
1283
0
            char *pszWKT = nullptr;
1284
0
            auto poSRS = poLayer->GetSpatialRef();
1285
0
            if (poSRS == nullptr)
1286
0
            {
1287
0
                pszWKT = CPLStrdup("(unknown)");
1288
0
            }
1289
0
            else
1290
0
            {
1291
0
                poSRS->exportToWkt(&pszWKT, apszWKTOptions);
1292
0
            }
1293
1294
0
            Concat(osRet, psOptions->bStdoutOutput, "Layer SRS WKT:\n%s\n",
1295
0
                   pszWKT);
1296
0
            CPLFree(pszWKT);
1297
0
            if (poSRS)
1298
0
            {
1299
0
                displayExtraInfoSRS(poSRS);
1300
0
            }
1301
0
            DisplaySupportedCRSList(0);
1302
0
        }
1303
1304
0
        const char *pszFIDColumn = poLayer->GetFIDColumn();
1305
0
        if (pszFIDColumn[0] != '\0')
1306
0
        {
1307
0
            if (bJson)
1308
0
                oLayer.Set("fidColumnName", pszFIDColumn);
1309
0
            else
1310
0
            {
1311
0
                Concat(osRet, psOptions->bStdoutOutput, "FID Column = %s\n",
1312
0
                       pszFIDColumn);
1313
0
            }
1314
0
        }
1315
1316
0
        for (int iGeom = 0; !bJson && iGeom < nGeomFieldCount; iGeom++)
1317
0
        {
1318
0
            OGRGeomFieldDefn *poGFldDefn =
1319
0
                poLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom);
1320
0
            if (nGeomFieldCount == 1 && EQUAL(poGFldDefn->GetNameRef(), "") &&
1321
0
                poGFldDefn->IsNullable())
1322
0
                break;
1323
0
            Concat(osRet, psOptions->bStdoutOutput, "Geometry Column ");
1324
0
            if (nGeomFieldCount > 1)
1325
0
                Concat(osRet, psOptions->bStdoutOutput, "%d ", iGeom + 1);
1326
0
            if (!poGFldDefn->IsNullable())
1327
0
                Concat(osRet, psOptions->bStdoutOutput, "NOT NULL ");
1328
0
            Concat(osRet, psOptions->bStdoutOutput, "= %s\n",
1329
0
                   poGFldDefn->GetNameRef());
1330
0
        }
1331
1332
0
        CPLJSONArray oFields;
1333
0
        if (bJson)
1334
0
            oLayer.Add("fields", oFields);
1335
0
        for (int iAttr = 0; iAttr < poDefn->GetFieldCount(); iAttr++)
1336
0
        {
1337
0
            const OGRFieldDefn *poField = poDefn->GetFieldDefn(iAttr);
1338
0
            const char *pszAlias = poField->GetAlternativeNameRef();
1339
0
            const std::string &osDomain = poField->GetDomainName();
1340
0
            const std::string &osComment = poField->GetComment();
1341
0
            const auto eType = poField->GetType();
1342
0
            std::string osTimeZone;
1343
0
            if (eType == OFTTime || eType == OFTDate || eType == OFTDateTime)
1344
0
            {
1345
0
                const int nTZFlag = poField->GetTZFlag();
1346
0
                if (nTZFlag == OGR_TZFLAG_LOCALTIME)
1347
0
                {
1348
0
                    osTimeZone = "localtime";
1349
0
                }
1350
0
                else if (nTZFlag == OGR_TZFLAG_MIXED_TZ)
1351
0
                {
1352
0
                    osTimeZone = "mixed timezones";
1353
0
                }
1354
0
                else if (nTZFlag == OGR_TZFLAG_UTC)
1355
0
                {
1356
0
                    osTimeZone = "UTC";
1357
0
                }
1358
0
                else if (nTZFlag > 0)
1359
0
                {
1360
0
                    char chSign;
1361
0
                    const int nOffset = (nTZFlag - OGR_TZFLAG_UTC) * 15;
1362
0
                    int nHours =
1363
0
                        static_cast<int>(nOffset / 60);  // Round towards zero.
1364
0
                    const int nMinutes = std::abs(nOffset - nHours * 60);
1365
1366
0
                    if (nOffset < 0)
1367
0
                    {
1368
0
                        chSign = '-';
1369
0
                        nHours = std::abs(nHours);
1370
0
                    }
1371
0
                    else
1372
0
                    {
1373
0
                        chSign = '+';
1374
0
                    }
1375
0
                    osTimeZone =
1376
0
                        CPLSPrintf("%c%02d:%02d", chSign, nHours, nMinutes);
1377
0
                }
1378
0
            }
1379
1380
0
            if (bJson)
1381
0
            {
1382
0
                CPLJSONObject oField;
1383
0
                oFields.Add(oField);
1384
0
                oField.Set("name", poField->GetNameRef());
1385
0
                oField.Set("type", OGRFieldDefn::GetFieldTypeName(eType));
1386
0
                if (poField->GetSubType() != OFSTNone)
1387
0
                    oField.Set("subType", OGRFieldDefn::GetFieldSubTypeName(
1388
0
                                              poField->GetSubType()));
1389
0
                if (poField->GetWidth() > 0)
1390
0
                    oField.Set("width", poField->GetWidth());
1391
0
                if (poField->GetPrecision() > 0)
1392
0
                    oField.Set("precision", poField->GetPrecision());
1393
0
                oField.Set("nullable", CPL_TO_BOOL(poField->IsNullable()));
1394
0
                oField.Set("uniqueConstraint",
1395
0
                           CPL_TO_BOOL(poField->IsUnique()));
1396
0
                if (poField->GetDefault() != nullptr)
1397
0
                    oField.Set("defaultValue", poField->GetDefault());
1398
0
                if (pszAlias != nullptr && pszAlias[0])
1399
0
                    oField.Set("alias", pszAlias);
1400
0
                if (!osDomain.empty())
1401
0
                    oField.Set("domainName", osDomain);
1402
0
                if (!osComment.empty())
1403
0
                    oField.Set("comment", osComment);
1404
0
                if (!osTimeZone.empty())
1405
0
                    oField.Set("timezone", osTimeZone);
1406
0
            }
1407
0
            else
1408
0
            {
1409
0
                const char *pszType =
1410
0
                    (poField->GetSubType() != OFSTNone)
1411
0
                        ? CPLSPrintf("%s(%s)",
1412
0
                                     OGRFieldDefn::GetFieldTypeName(
1413
0
                                         poField->GetType()),
1414
0
                                     OGRFieldDefn::GetFieldSubTypeName(
1415
0
                                         poField->GetSubType()))
1416
0
                        : OGRFieldDefn::GetFieldTypeName(poField->GetType());
1417
0
                Concat(osRet, psOptions->bStdoutOutput, "%s: %s",
1418
0
                       poField->GetNameRef(), pszType);
1419
0
                if (eType == OFTTime || eType == OFTDate ||
1420
0
                    eType == OFTDateTime)
1421
0
                {
1422
0
                    if (!osTimeZone.empty())
1423
0
                        Concat(osRet, psOptions->bStdoutOutput, " (%s)",
1424
0
                               osTimeZone.c_str());
1425
0
                }
1426
0
                else
1427
0
                {
1428
0
                    Concat(osRet, psOptions->bStdoutOutput, " (%d.%d)",
1429
0
                           poField->GetWidth(), poField->GetPrecision());
1430
0
                }
1431
0
                if (poField->IsUnique())
1432
0
                    Concat(osRet, psOptions->bStdoutOutput, " UNIQUE");
1433
0
                if (!poField->IsNullable())
1434
0
                    Concat(osRet, psOptions->bStdoutOutput, " NOT NULL");
1435
0
                if (poField->GetDefault() != nullptr)
1436
0
                    Concat(osRet, psOptions->bStdoutOutput, " DEFAULT %s",
1437
0
                           poField->GetDefault());
1438
0
                if (pszAlias != nullptr && pszAlias[0])
1439
0
                    Concat(osRet, psOptions->bStdoutOutput,
1440
0
                           ", alternative name=\"%s\"", pszAlias);
1441
0
                if (!osDomain.empty())
1442
0
                    Concat(osRet, psOptions->bStdoutOutput, ", domain name=%s",
1443
0
                           osDomain.c_str());
1444
0
                if (!osComment.empty())
1445
0
                    Concat(osRet, psOptions->bStdoutOutput, ", comment=%s",
1446
0
                           osComment.c_str());
1447
0
                Concat(osRet, psOptions->bStdoutOutput, "\n");
1448
0
            }
1449
0
        }
1450
0
    }
1451
1452
    /* -------------------------------------------------------------------- */
1453
    /*      Read, and dump features.                                        */
1454
    /* -------------------------------------------------------------------- */
1455
1456
0
    if ((psOptions->nFetchFID == OGRNullFID || bJson) && !bForceSummary &&
1457
0
        ((psOptions->bIsCli && psOptions->bFeaturesUserRequested) ||
1458
0
         (!psOptions->bIsCli && !psOptions->bSummaryOnly)))
1459
0
    {
1460
0
        if (!psOptions->bSuperQuiet)
1461
0
        {
1462
0
            CPLJSONArray oFeatures;
1463
0
            const bool bDisplayFields =
1464
0
                CPLTestBool(psOptions->aosOptions.FetchNameValueDef(
1465
0
                    "DISPLAY_FIELDS", "YES"));
1466
0
            const int nFields =
1467
0
                bDisplayFields ? poLayer->GetLayerDefn()->GetFieldCount() : 0;
1468
0
            const bool bDisplayGeometry =
1469
0
                CPLTestBool(psOptions->aosOptions.FetchNameValueDef(
1470
0
                    "DISPLAY_GEOMETRY", "YES"));
1471
0
            const int nGeomFields =
1472
0
                bDisplayGeometry ? poLayer->GetLayerDefn()->GetGeomFieldCount()
1473
0
                                 : 0;
1474
0
            if (bJson)
1475
0
                oLayer.Add("features", oFeatures);
1476
1477
0
            const auto EmitFeatureJSON =
1478
0
                [poLayer, nFields, nGeomFields,
1479
0
                 &oFeatures](const OGRFeature *poFeature)
1480
0
            {
1481
0
                CPLJSONObject oFeature;
1482
0
                CPLJSONObject oProperties;
1483
0
                oFeatures.Add(oFeature);
1484
0
                oFeature.Add("type", "Feature");
1485
0
                oFeature.Add("properties", oProperties);
1486
0
                oFeature.Add("fid", poFeature->GetFID());
1487
0
                for (int i = 0; i < nFields; ++i)
1488
0
                {
1489
0
                    const auto poFDefn = poFeature->GetFieldDefnRef(i);
1490
0
                    const auto eType = poFDefn->GetType();
1491
0
                    if (!poFeature->IsFieldSet(i))
1492
0
                        continue;
1493
0
                    if (poFeature->IsFieldNull(i))
1494
0
                    {
1495
0
                        oProperties.SetNull(poFDefn->GetNameRef());
1496
0
                    }
1497
0
                    else if (eType == OFTInteger)
1498
0
                    {
1499
0
                        if (poFDefn->GetSubType() == OFSTBoolean)
1500
0
                            oProperties.Add(
1501
0
                                poFDefn->GetNameRef(),
1502
0
                                CPL_TO_BOOL(poFeature->GetFieldAsInteger(i)));
1503
0
                        else
1504
0
                            oProperties.Add(poFDefn->GetNameRef(),
1505
0
                                            poFeature->GetFieldAsInteger(i));
1506
0
                    }
1507
0
                    else if (eType == OFTInteger64)
1508
0
                    {
1509
0
                        oProperties.Add(poFDefn->GetNameRef(),
1510
0
                                        poFeature->GetFieldAsInteger64(i));
1511
0
                    }
1512
0
                    else if (eType == OFTReal)
1513
0
                    {
1514
0
                        oProperties.Add(poFDefn->GetNameRef(),
1515
0
                                        poFeature->GetFieldAsDouble(i));
1516
0
                    }
1517
0
                    else if ((eType == OFTString &&
1518
0
                              poFDefn->GetSubType() != OFSTJSON) ||
1519
0
                             eType == OFTDate || eType == OFTTime ||
1520
0
                             eType == OFTDateTime)
1521
0
                    {
1522
0
                        oProperties.Add(poFDefn->GetNameRef(),
1523
0
                                        poFeature->GetFieldAsString(i));
1524
0
                    }
1525
0
                    else
1526
0
                    {
1527
0
                        char *pszSerialized =
1528
0
                            poFeature->GetFieldAsSerializedJSon(i);
1529
0
                        if (pszSerialized)
1530
0
                        {
1531
0
                            const auto eStrType =
1532
0
                                CPLGetValueType(pszSerialized);
1533
0
                            if (eStrType == CPL_VALUE_INTEGER)
1534
0
                            {
1535
0
                                oProperties.Add(poFDefn->GetNameRef(),
1536
0
                                                CPLAtoGIntBig(pszSerialized));
1537
0
                            }
1538
0
                            else if (eStrType == CPL_VALUE_REAL)
1539
0
                            {
1540
0
                                oProperties.Add(poFDefn->GetNameRef(),
1541
0
                                                CPLAtof(pszSerialized));
1542
0
                            }
1543
0
                            else
1544
0
                            {
1545
0
                                CPLJSONDocument oDoc;
1546
0
                                if (oDoc.LoadMemory(pszSerialized))
1547
0
                                    oProperties.Add(poFDefn->GetNameRef(),
1548
0
                                                    oDoc.GetRoot());
1549
0
                            }
1550
0
                            CPLFree(pszSerialized);
1551
0
                        }
1552
0
                    }
1553
0
                }
1554
1555
0
                const auto GetGeoJSONOptions = [poLayer](int iGeomField)
1556
0
                {
1557
0
                    CPLStringList aosGeoJSONOptions;
1558
0
                    const auto &oCoordPrec = poLayer->GetLayerDefn()
1559
0
                                                 ->GetGeomFieldDefn(iGeomField)
1560
0
                                                 ->GetCoordinatePrecision();
1561
0
                    if (oCoordPrec.dfXYResolution !=
1562
0
                        OGRGeomCoordinatePrecision::UNKNOWN)
1563
0
                    {
1564
0
                        aosGeoJSONOptions.SetNameValue(
1565
0
                            "XY_COORD_PRECISION",
1566
0
                            CPLSPrintf("%d",
1567
0
                                       OGRGeomCoordinatePrecision::
1568
0
                                           ResolutionToPrecision(
1569
0
                                               oCoordPrec.dfXYResolution)));
1570
0
                    }
1571
0
                    if (oCoordPrec.dfZResolution !=
1572
0
                        OGRGeomCoordinatePrecision::UNKNOWN)
1573
0
                    {
1574
0
                        aosGeoJSONOptions.SetNameValue(
1575
0
                            "Z_COORD_PRECISION",
1576
0
                            CPLSPrintf("%d",
1577
0
                                       OGRGeomCoordinatePrecision::
1578
0
                                           ResolutionToPrecision(
1579
0
                                               oCoordPrec.dfZResolution)));
1580
0
                    }
1581
0
                    return aosGeoJSONOptions;
1582
0
                };
1583
1584
0
                if (nGeomFields == 0)
1585
0
                    oFeature.SetNull("geometry");
1586
0
                else
1587
0
                {
1588
0
                    if (const auto poGeom = poFeature->GetGeometryRef())
1589
0
                    {
1590
0
                        char *pszSerialized =
1591
0
                            wkbFlatten(poGeom->getGeometryType()) <=
1592
0
                                    wkbGeometryCollection
1593
0
                                ? poGeom->exportToJson(
1594
0
                                      GetGeoJSONOptions(0).List())
1595
0
                                : nullptr;
1596
0
                        if (pszSerialized)
1597
0
                        {
1598
0
                            CPLJSONDocument oDoc;
1599
0
                            if (oDoc.LoadMemory(pszSerialized))
1600
0
                                oFeature.Add("geometry", oDoc.GetRoot());
1601
0
                            CPLFree(pszSerialized);
1602
0
                        }
1603
0
                        else
1604
0
                        {
1605
0
                            CPLJSONObject oGeometry;
1606
0
                            oFeature.SetNull("geometry");
1607
0
                            oFeature.Add("wkt_geometry", poGeom->exportToWkt());
1608
0
                        }
1609
0
                    }
1610
0
                    else
1611
0
                        oFeature.SetNull("geometry");
1612
1613
0
                    if (nGeomFields > 1)
1614
0
                    {
1615
0
                        CPLJSONArray oGeometries;
1616
0
                        oFeature.Add("geometries", oGeometries);
1617
0
                        for (int i = 0; i < nGeomFields; ++i)
1618
0
                        {
1619
0
                            auto poGeom = poFeature->GetGeomFieldRef(i);
1620
0
                            if (poGeom)
1621
0
                            {
1622
0
                                char *pszSerialized =
1623
0
                                    wkbFlatten(poGeom->getGeometryType()) <=
1624
0
                                            wkbGeometryCollection
1625
0
                                        ? poGeom->exportToJson(
1626
0
                                              GetGeoJSONOptions(i).List())
1627
0
                                        : nullptr;
1628
0
                                if (pszSerialized)
1629
0
                                {
1630
0
                                    CPLJSONDocument oDoc;
1631
0
                                    if (oDoc.LoadMemory(pszSerialized))
1632
0
                                        oGeometries.Add(oDoc.GetRoot());
1633
0
                                    CPLFree(pszSerialized);
1634
0
                                }
1635
0
                                else
1636
0
                                {
1637
0
                                    CPLJSONObject oGeometry;
1638
0
                                    oGeometries.Add(poGeom->exportToWkt());
1639
0
                                }
1640
0
                            }
1641
0
                            else
1642
0
                                oGeometries.AddNull();
1643
0
                        }
1644
0
                    }
1645
0
                }
1646
0
            };
1647
1648
0
            if (psOptions->nFetchFID != OGRNullFID)
1649
0
            {
1650
0
                auto poFeature = std::unique_ptr<OGRFeature>(
1651
0
                    poLayer->GetFeature(psOptions->nFetchFID));
1652
0
                if (poFeature)
1653
0
                {
1654
0
                    EmitFeatureJSON(poFeature.get());
1655
0
                }
1656
0
            }
1657
0
            else
1658
0
            {
1659
0
                GIntBig nFeatureCount = 0;
1660
0
                for (auto &poFeature : poLayer)
1661
0
                {
1662
0
                    if (psOptions->nLimit >= 0 &&
1663
0
                        nFeatureCount >= psOptions->nLimit)
1664
0
                    {
1665
0
                        break;
1666
0
                    }
1667
0
                    ++nFeatureCount;
1668
1669
0
                    if (bJson)
1670
0
                    {
1671
0
                        EmitFeatureJSON(poFeature.get());
1672
0
                    }
1673
0
                    else
1674
0
                    {
1675
0
                        ConcatStr(osRet, psOptions->bStdoutOutput,
1676
0
                                  poFeature
1677
0
                                      ->DumpReadableAsString(
1678
0
                                          psOptions->aosOptions.List())
1679
0
                                      .c_str());
1680
0
                    }
1681
0
                }
1682
0
            }
1683
0
        }
1684
0
    }
1685
0
    else if (!bJson && psOptions->nFetchFID != OGRNullFID)
1686
0
    {
1687
0
        auto poFeature = std::unique_ptr<OGRFeature>(
1688
0
            poLayer->GetFeature(psOptions->nFetchFID));
1689
0
        if (poFeature == nullptr)
1690
0
        {
1691
0
            Concat(osRet, psOptions->bStdoutOutput,
1692
0
                   "Unable to locate feature id " CPL_FRMT_GIB
1693
0
                   " on this layer.\n",
1694
0
                   psOptions->nFetchFID);
1695
0
        }
1696
0
        else
1697
0
        {
1698
0
            ConcatStr(
1699
0
                osRet, psOptions->bStdoutOutput,
1700
0
                poFeature->DumpReadableAsString(psOptions->aosOptions.List())
1701
0
                    .c_str());
1702
0
        }
1703
0
    }
1704
0
}
1705
1706
/************************************************************************/
1707
/*                         PrintLayerSummary()                          */
1708
/************************************************************************/
1709
1710
static void PrintLayerSummary(CPLString &osRet, CPLJSONObject &oLayer,
1711
                              const GDALVectorInfoOptions *psOptions,
1712
                              OGRLayer *poLayer, bool bIsPrivate)
1713
0
{
1714
0
    const bool bJson = psOptions->eFormat == FORMAT_JSON;
1715
0
    const bool bIsSummaryCli = psOptions->bIsCli && psOptions->bSummaryOnly;
1716
0
    if (bJson)
1717
0
    {
1718
0
        oLayer.Set("name", poLayer->GetName());
1719
0
    }
1720
0
    else
1721
0
        ConcatStr(osRet, psOptions->bStdoutOutput, poLayer->GetName());
1722
1723
0
    const char *pszTitle = poLayer->GetMetadataItem("TITLE");
1724
0
    if (pszTitle)
1725
0
    {
1726
0
        if (bJson)
1727
0
            oLayer.Set("title", pszTitle);
1728
0
        else
1729
0
            Concat(osRet, psOptions->bStdoutOutput, " (title: %s)", pszTitle);
1730
0
    }
1731
1732
0
    const int nGeomFieldCount =
1733
0
        psOptions->bGeomType ? poLayer->GetLayerDefn()->GetGeomFieldCount() : 0;
1734
1735
0
    if (bIsSummaryCli && bJson)
1736
0
    {
1737
0
        CPLJSONArray oGeometryTypes;
1738
0
        for (int iGeom = 0; iGeom < nGeomFieldCount; iGeom++)
1739
0
        {
1740
0
            OGRGeomFieldDefn *poGFldDefn =
1741
0
                poLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom);
1742
0
            oGeometryTypes.Add(OGRGeometryTypeToName(poGFldDefn->GetType()));
1743
0
        }
1744
0
        oLayer.Add("geometryType", oGeometryTypes);
1745
0
        return;
1746
0
    }
1747
1748
0
    if (bJson || nGeomFieldCount > 1)
1749
0
    {
1750
0
        if (!bJson)
1751
0
            Concat(osRet, psOptions->bStdoutOutput, " (");
1752
0
        CPLJSONArray oGeometryFields;
1753
0
        oLayer.Add("geometryFields", oGeometryFields);
1754
0
        for (int iGeom = 0; iGeom < nGeomFieldCount; iGeom++)
1755
0
        {
1756
0
            OGRGeomFieldDefn *poGFldDefn =
1757
0
                poLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom);
1758
0
            if (bJson)
1759
0
            {
1760
0
                oGeometryFields.Add(
1761
0
                    OGRGeometryTypeToName(poGFldDefn->GetType()));
1762
0
            }
1763
0
            else
1764
0
            {
1765
0
                if (iGeom > 0)
1766
0
                    Concat(osRet, psOptions->bStdoutOutput, ", ");
1767
0
                ConcatStr(osRet, psOptions->bStdoutOutput,
1768
0
                          OGRGeometryTypeToName(poGFldDefn->GetType()));
1769
0
            }
1770
0
        }
1771
0
        if (!bJson)
1772
0
            Concat(osRet, psOptions->bStdoutOutput, ")");
1773
0
    }
1774
0
    else if (psOptions->bGeomType && poLayer->GetGeomType() != wkbUnknown)
1775
0
        Concat(osRet, psOptions->bStdoutOutput, " (%s)",
1776
0
               OGRGeometryTypeToName(poLayer->GetGeomType()));
1777
1778
0
    if (bIsPrivate)
1779
0
    {
1780
0
        if (bJson)
1781
0
            oLayer.Set("isPrivate", true);
1782
0
        else
1783
0
            Concat(osRet, psOptions->bStdoutOutput, " [private]");
1784
0
    }
1785
1786
0
    if (!bJson)
1787
0
        Concat(osRet, psOptions->bStdoutOutput, "\n");
1788
0
}
1789
1790
/************************************************************************/
1791
/*                      ReportHiearchicalLayers()                       */
1792
/************************************************************************/
1793
1794
static void ReportHiearchicalLayers(CPLString &osRet, CPLJSONObject &oRoot,
1795
                                    const GDALVectorInfoOptions *psOptions,
1796
                                    const GDALGroup *group,
1797
                                    const std::string &indent, bool bGeomType)
1798
0
{
1799
0
    const bool bJson = psOptions->eFormat == FORMAT_JSON;
1800
0
    const auto aosVectorLayerNames = group->GetVectorLayerNames();
1801
0
    CPLJSONArray oLayerNames;
1802
0
    oRoot.Add("layerNames", oLayerNames);
1803
0
    for (const auto &osVectorLayerName : aosVectorLayerNames)
1804
0
    {
1805
0
        OGRLayer *poLayer = group->OpenVectorLayer(osVectorLayerName);
1806
0
        if (poLayer)
1807
0
        {
1808
0
            CPLJSONObject oLayer;
1809
0
            if (!bJson)
1810
0
            {
1811
0
                Concat(osRet, psOptions->bStdoutOutput,
1812
0
                       "%sLayer: ", indent.c_str());
1813
0
                PrintLayerSummary(osRet, oLayer, psOptions, poLayer,
1814
0
                                  /* bIsPrivate=*/false);
1815
0
            }
1816
0
            else
1817
0
            {
1818
0
                oLayerNames.Add(poLayer->GetName());
1819
0
            }
1820
0
        }
1821
0
    }
1822
1823
0
    const std::string subIndent(indent + "  ");
1824
0
    auto aosSubGroupNames = group->GetGroupNames();
1825
0
    CPLJSONArray oGroupArray;
1826
0
    oRoot.Add("groups", oGroupArray);
1827
0
    for (const auto &osSubGroupName : aosSubGroupNames)
1828
0
    {
1829
0
        auto poSubGroup = group->OpenGroup(osSubGroupName);
1830
0
        if (poSubGroup)
1831
0
        {
1832
0
            CPLJSONObject oGroup;
1833
0
            if (!bJson)
1834
0
            {
1835
0
                Concat(osRet, psOptions->bStdoutOutput, "Group %s",
1836
0
                       indent.c_str());
1837
0
                Concat(osRet, psOptions->bStdoutOutput, "%s:\n",
1838
0
                       osSubGroupName.c_str());
1839
0
            }
1840
0
            else
1841
0
            {
1842
0
                oGroupArray.Add(oGroup);
1843
0
                oGroup.Set("name", osSubGroupName);
1844
0
            }
1845
0
            ReportHiearchicalLayers(osRet, oGroup, psOptions, poSubGroup.get(),
1846
0
                                    subIndent, bGeomType);
1847
0
        }
1848
0
    }
1849
0
}
1850
1851
/************************************************************************/
1852
/*                           GDALVectorInfo()                           */
1853
/************************************************************************/
1854
1855
/**
1856
 * Lists various information about a GDAL supported vector dataset.
1857
 *
1858
 * This is the equivalent of the <a href="/programs/ogrinfo.html">ogrinfo</a>
1859
 * utility.
1860
 *
1861
 * GDALVectorInfoOptions* must be allocated and freed with
1862
 * GDALVectorInfoOptionsNew() and GDALVectorInfoOptionsFree() respectively.
1863
 *
1864
 * @param hDataset the dataset handle.
1865
 * @param psOptions the options structure returned by GDALVectorInfoOptionsNew()
1866
 * or NULL.
1867
 * @return string corresponding to the information about the raster dataset
1868
 * (must be freed with CPLFree()), or NULL in case of error.
1869
 *
1870
 * @since GDAL 3.7
1871
 */
1872
char *GDALVectorInfo(GDALDatasetH hDataset,
1873
                     const GDALVectorInfoOptions *psOptions)
1874
0
{
1875
0
    auto poDS = GDALDataset::FromHandle(hDataset);
1876
0
    if (poDS == nullptr)
1877
0
        return nullptr;
1878
1879
0
    const GDALVectorInfoOptions sDefaultOptions;
1880
0
    if (!psOptions)
1881
0
        psOptions = &sDefaultOptions;
1882
1883
0
    GDALDriver *poDriver = poDS->GetDriver();
1884
1885
0
    CPLString osRet;
1886
0
    CPLJSONObject oRoot;
1887
0
    const std::string osFilename(poDS->GetDescription());
1888
1889
0
    const bool bExportOgrSchema = psOptions->bExportOgrSchema;
1890
0
    const bool bJson = psOptions->eFormat == FORMAT_JSON;
1891
0
    const bool bIsSummaryCli =
1892
0
        (psOptions->bIsCli && psOptions->bSummaryUserRequested);
1893
1894
0
    CPLJSONArray oLayerArray;
1895
0
    if (bJson)
1896
0
    {
1897
0
        if (!bExportOgrSchema)
1898
0
        {
1899
0
            oRoot.Set("description", poDS->GetDescription());
1900
0
            if (poDriver)
1901
0
            {
1902
0
                oRoot.Set("driverShortName", poDriver->GetDescription());
1903
0
                oRoot.Set("driverLongName",
1904
0
                          poDriver->GetMetadataItem(GDAL_DMD_LONGNAME));
1905
0
            }
1906
0
        }
1907
0
        oRoot.Add("layers", oLayerArray);
1908
0
    }
1909
1910
    /* -------------------------------------------------------------------- */
1911
    /*      Some information messages.                                      */
1912
    /* -------------------------------------------------------------------- */
1913
0
    if (!bJson && psOptions->bVerbose)
1914
0
    {
1915
0
        Concat(osRet, psOptions->bStdoutOutput,
1916
0
               "INFO: Open of `%s'\n"
1917
0
               "      using driver `%s' successful.\n",
1918
0
               osFilename.c_str(),
1919
0
               poDriver ? poDriver->GetDescription() : "(null)");
1920
0
    }
1921
1922
0
    if (!bJson && psOptions->bVerbose &&
1923
0
        !EQUAL(osFilename.c_str(), poDS->GetDescription()))
1924
0
    {
1925
0
        Concat(osRet, psOptions->bStdoutOutput,
1926
0
               "INFO: Internal data source name `%s'\n"
1927
0
               "      different from user name `%s'.\n",
1928
0
               poDS->GetDescription(), osFilename.c_str());
1929
0
    }
1930
1931
0
    int nRepeatCount = psOptions->nRepeatCount;
1932
1933
0
    if (!bIsSummaryCli && !bExportOgrSchema)
1934
0
    {
1935
0
        GDALVectorInfoReportMetadata(
1936
0
            osRet, oRoot, psOptions, poDS, psOptions->bListMDD,
1937
0
            psOptions->bShowMetadata, psOptions->aosExtraMDDomains.List());
1938
1939
0
        CPLJSONObject oDomains;
1940
0
        oRoot.Add("domains", oDomains);
1941
0
        if (!psOptions->osFieldDomain.empty())
1942
0
        {
1943
0
            auto poDomain = poDS->GetFieldDomain(psOptions->osFieldDomain);
1944
0
            if (poDomain == nullptr)
1945
0
            {
1946
0
                CPLError(CE_Failure, CPLE_AppDefined,
1947
0
                         "Domain %s cannot be found.",
1948
0
                         psOptions->osFieldDomain.c_str());
1949
0
                return nullptr;
1950
0
            }
1951
0
            if (!bJson)
1952
0
                Concat(osRet, psOptions->bStdoutOutput, "\n");
1953
0
            ReportFieldDomain(osRet, oDomains, psOptions, poDomain);
1954
0
            if (!bJson)
1955
0
                Concat(osRet, psOptions->bStdoutOutput, "\n");
1956
0
        }
1957
0
        else if (bJson)
1958
0
        {
1959
0
            for (const auto &osDomainName : poDS->GetFieldDomainNames())
1960
0
            {
1961
0
                auto poDomain = poDS->GetFieldDomain(osDomainName);
1962
0
                if (poDomain)
1963
0
                {
1964
0
                    ReportFieldDomain(osRet, oDomains, psOptions, poDomain);
1965
0
                }
1966
0
            }
1967
0
        }
1968
1969
0
        if (psOptions->bDatasetGetNextFeature)
1970
0
        {
1971
0
            nRepeatCount = 0;  // skip layer reporting.
1972
1973
            /* --------------------------------------------------------------------
1974
             */
1975
            /*      Set filters if provided. */
1976
            /* --------------------------------------------------------------------
1977
             */
1978
0
            if (!psOptions->osWHERE.empty() ||
1979
0
                psOptions->poSpatialFilter != nullptr)
1980
0
            {
1981
0
                for (int iLayer = 0; iLayer < poDS->GetLayerCount(); iLayer++)
1982
0
                {
1983
0
                    OGRLayer *poLayer = poDS->GetLayer(iLayer);
1984
1985
0
                    if (poLayer == nullptr)
1986
0
                    {
1987
0
                        CPLError(CE_Failure, CPLE_AppDefined,
1988
0
                                 "Couldn't fetch advertised layer %d.", iLayer);
1989
0
                        return nullptr;
1990
0
                    }
1991
1992
0
                    if (!psOptions->osWHERE.empty())
1993
0
                    {
1994
0
                        if (poLayer->SetAttributeFilter(
1995
0
                                psOptions->osWHERE.c_str()) != OGRERR_NONE)
1996
0
                        {
1997
0
                            CPLError(
1998
0
                                CE_Warning, CPLE_AppDefined,
1999
0
                                "SetAttributeFilter(%s) failed on layer %s.",
2000
0
                                psOptions->osWHERE.c_str(), poLayer->GetName());
2001
0
                        }
2002
0
                    }
2003
2004
0
                    if (psOptions->poSpatialFilter != nullptr)
2005
0
                    {
2006
0
                        if (!psOptions->osGeomField.empty())
2007
0
                        {
2008
0
                            OGRFeatureDefn *poDefn = poLayer->GetLayerDefn();
2009
0
                            const int iGeomField = poDefn->GetGeomFieldIndex(
2010
0
                                psOptions->osGeomField.c_str());
2011
0
                            if (iGeomField >= 0)
2012
0
                                poLayer->SetSpatialFilter(
2013
0
                                    iGeomField,
2014
0
                                    psOptions->poSpatialFilter.get());
2015
0
                            else
2016
0
                                CPLError(CE_Warning, CPLE_AppDefined,
2017
0
                                         "Cannot find geometry field %s.",
2018
0
                                         psOptions->osGeomField.c_str());
2019
0
                        }
2020
0
                        else
2021
0
                        {
2022
0
                            poLayer->SetSpatialFilter(
2023
0
                                psOptions->poSpatialFilter.get());
2024
0
                        }
2025
0
                    }
2026
0
                }
2027
0
            }
2028
2029
0
            std::set<OGRLayer *> oSetLayers;
2030
0
            while (true)
2031
0
            {
2032
0
                OGRLayer *poLayer = nullptr;
2033
0
                OGRFeature *poFeature =
2034
0
                    poDS->GetNextFeature(&poLayer, nullptr, nullptr, nullptr);
2035
0
                if (poFeature == nullptr)
2036
0
                    break;
2037
0
                if (psOptions->aosLayers.empty() || poLayer == nullptr ||
2038
0
                    CSLFindString(psOptions->aosLayers.List(),
2039
0
                                  poLayer->GetName()) >= 0)
2040
0
                {
2041
0
                    if (psOptions->bVerbose && poLayer != nullptr &&
2042
0
                        oSetLayers.find(poLayer) == oSetLayers.end())
2043
0
                    {
2044
0
                        oSetLayers.insert(poLayer);
2045
0
                        CPLJSONObject oLayer;
2046
0
                        oLayerArray.Add(oLayer);
2047
0
                        ReportOnLayer(
2048
0
                            osRet, oLayer, psOptions, poLayer,
2049
0
                            /*bForceSummary = */ true,
2050
0
                            /*bTakeIntoAccountWHERE = */ false,
2051
0
                            /*bTakeIntoAccountSpatialFilter = */ false,
2052
0
                            /*bTakeIntoAccountGeomField = */ false);
2053
0
                    }
2054
0
                    if (!psOptions->bSuperQuiet && !psOptions->bSummaryOnly)
2055
0
                        poFeature->DumpReadable(
2056
0
                            nullptr,
2057
0
                            const_cast<char **>(psOptions->aosOptions.List()));
2058
0
                }
2059
0
                OGRFeature::DestroyFeature(poFeature);
2060
0
            }
2061
0
        }
2062
2063
        /* -------------------------------------------------------------------- */
2064
        /*      Special case for -sql clause.  No source layers required.       */
2065
        /* -------------------------------------------------------------------- */
2066
0
        else if (!psOptions->osSQLStatement.empty())
2067
0
        {
2068
0
            nRepeatCount = 0;  // skip layer reporting.
2069
2070
0
            if (!bJson && !psOptions->aosLayers.empty())
2071
0
                Concat(osRet, psOptions->bStdoutOutput,
2072
0
                       "layer names ignored in combination with -sql.\n");
2073
2074
0
            CPLErrorReset();
2075
0
            OGRLayer *poResultSet = poDS->ExecuteSQL(
2076
0
                psOptions->osSQLStatement.c_str(),
2077
0
                psOptions->osGeomField.empty()
2078
0
                    ? psOptions->poSpatialFilter.get()
2079
0
                    : nullptr,
2080
0
                psOptions->osDialect.empty() ? nullptr
2081
0
                                             : psOptions->osDialect.c_str());
2082
2083
0
            if (poResultSet != nullptr)
2084
0
            {
2085
0
                if (!psOptions->osWHERE.empty())
2086
0
                {
2087
0
                    if (poResultSet->SetAttributeFilter(
2088
0
                            psOptions->osWHERE.c_str()) != OGRERR_NONE)
2089
0
                    {
2090
0
                        CPLError(CE_Failure, CPLE_AppDefined,
2091
0
                                 "SetAttributeFilter(%s) failed.",
2092
0
                                 psOptions->osWHERE.c_str());
2093
0
                        return nullptr;
2094
0
                    }
2095
0
                }
2096
2097
0
                CPLJSONObject oLayer;
2098
0
                oLayerArray.Add(oLayer);
2099
0
                if (!psOptions->osGeomField.empty())
2100
0
                    ReportOnLayer(osRet, oLayer, psOptions, poResultSet,
2101
0
                                  /*bForceSummary = */ false,
2102
0
                                  /*bTakeIntoAccountWHERE = */ false,
2103
0
                                  /*bTakeIntoAccountSpatialFilter = */ true,
2104
0
                                  /*bTakeIntoAccountGeomField = */ true);
2105
0
                else
2106
0
                    ReportOnLayer(osRet, oLayer, psOptions, poResultSet,
2107
0
                                  /*bForceSummary = */ false,
2108
0
                                  /*bTakeIntoAccountWHERE = */ false,
2109
0
                                  /*bTakeIntoAccountSpatialFilter = */ false,
2110
0
                                  /*bTakeIntoAccountGeomField = */ false);
2111
2112
0
                poDS->ReleaseResultSet(poResultSet);
2113
0
            }
2114
0
            else if (CPLGetLastErrorType() != CE_None)
2115
0
            {
2116
0
                return nullptr;
2117
0
            }
2118
0
        }
2119
0
    }
2120
2121
    // coverity[tainted_data]
2122
0
    auto papszLayers = psOptions->aosLayers.List();
2123
0
    for (int iRepeat = 0; iRepeat < nRepeatCount; iRepeat++)
2124
0
    {
2125
0
        if (papszLayers == nullptr || papszLayers[0] == nullptr)
2126
0
        {
2127
0
            const int nLayerCount = poDS->GetLayerCount();
2128
0
            if (iRepeat == 0)
2129
0
                CPLDebug("OGR", "GetLayerCount() = %d\n", nLayerCount);
2130
2131
0
            bool bDone = false;
2132
0
            auto poRootGroup = poDS->GetRootGroup();
2133
0
            if ((bJson || !psOptions->bAllLayers) && poRootGroup &&
2134
0
                (!poRootGroup->GetGroupNames().empty() ||
2135
0
                 !poRootGroup->GetVectorLayerNames().empty()))
2136
0
            {
2137
0
                CPLJSONObject oGroup;
2138
0
                oRoot.Add("rootGroup", oGroup);
2139
0
                ReportHiearchicalLayers(osRet, oGroup, psOptions,
2140
0
                                        poRootGroup.get(), std::string(),
2141
0
                                        psOptions->bGeomType);
2142
0
                if (!bJson)
2143
0
                    bDone = true;
2144
0
            }
2145
2146
            /* --------------------------------------------------------------------
2147
             */
2148
            /*      Process each data source layer. */
2149
            /* --------------------------------------------------------------------
2150
             */
2151
0
            for (int iLayer = 0; !bDone && iLayer < nLayerCount; iLayer++)
2152
0
            {
2153
0
                OGRLayer *poLayer = poDS->GetLayer(iLayer);
2154
2155
0
                if (poLayer == nullptr)
2156
0
                {
2157
0
                    CPLError(CE_Failure, CPLE_AppDefined,
2158
0
                             "Couldn't fetch advertised layer %d.", iLayer);
2159
0
                    return nullptr;
2160
0
                }
2161
2162
0
                CPLJSONObject oLayer;
2163
0
                oLayerArray.Add(oLayer);
2164
0
                if (!psOptions->bAllLayers || bIsSummaryCli)
2165
0
                {
2166
0
                    if (!bJson)
2167
0
                        Concat(osRet, psOptions->bStdoutOutput,
2168
0
                               "%d: ", iLayer + 1);
2169
0
                    PrintLayerSummary(osRet, oLayer, psOptions, poLayer,
2170
0
                                      poDS->IsLayerPrivate(iLayer));
2171
0
                }
2172
0
                else
2173
0
                {
2174
0
                    if (iRepeat != 0)
2175
0
                        poLayer->ResetReading();
2176
2177
0
                    ReportOnLayer(osRet, oLayer, psOptions, poLayer,
2178
0
                                  /*bForceSummary = */ false,
2179
0
                                  /*bTakeIntoAccountWHERE = */ true,
2180
0
                                  /*bTakeIntoAccountSpatialFilter = */ true,
2181
0
                                  /*bTakeIntoAccountGeomField = */ true);
2182
0
                }
2183
0
            }
2184
0
        }
2185
0
        else
2186
0
        {
2187
            /* --------------------------------------------------------------------
2188
             */
2189
            /*      Process specified data source layers. */
2190
            /* --------------------------------------------------------------------
2191
             */
2192
2193
0
            for (const char *pszLayer : cpl::Iterate(papszLayers))
2194
0
            {
2195
0
                OGRLayer *poLayer = poDS->GetLayerByName(pszLayer);
2196
2197
0
                if (poLayer == nullptr)
2198
0
                {
2199
0
                    CPLError(CE_Failure, CPLE_AppDefined,
2200
0
                             "Couldn't fetch requested layer %s.", pszLayer);
2201
0
                    return nullptr;
2202
0
                }
2203
2204
0
                if (iRepeat != 0)
2205
0
                    poLayer->ResetReading();
2206
2207
0
                CPLJSONObject oLayer;
2208
0
                oLayerArray.Add(oLayer);
2209
0
                ReportOnLayer(osRet, oLayer, psOptions, poLayer,
2210
0
                              /*bForceSummary = */ false,
2211
0
                              /*bTakeIntoAccountWHERE = */ true,
2212
0
                              /*bTakeIntoAccountSpatialFilter = */ true,
2213
0
                              /*bTakeIntoAccountGeomField = */ true);
2214
0
            }
2215
0
        }
2216
0
    }
2217
2218
0
    if (!papszLayers && !bIsSummaryCli && !bExportOgrSchema)
2219
0
    {
2220
0
        ReportRelationships(osRet, oRoot, psOptions, poDS);
2221
0
    }
2222
2223
0
    if (bJson)
2224
0
    {
2225
0
        osRet.clear();
2226
0
        ConcatStr(
2227
0
            osRet, psOptions->bStdoutOutput,
2228
0
            json_object_to_json_string_ext(
2229
0
                static_cast<struct json_object *>(oRoot.GetInternalHandle()),
2230
0
                JSON_C_TO_STRING_PRETTY
2231
0
#ifdef JSON_C_TO_STRING_NOSLASHESCAPE
2232
0
                    | JSON_C_TO_STRING_NOSLASHESCAPE
2233
0
#endif
2234
0
                ));
2235
0
        ConcatStr(osRet, psOptions->bStdoutOutput, "\n");
2236
0
    }
2237
2238
0
    return VSI_STRDUP_VERBOSE(osRet);
2239
0
}
2240
2241
/************************************************************************/
2242
/*                   GDALVectorInfoOptionsGetParser()                   */
2243
/************************************************************************/
2244
2245
static std::unique_ptr<GDALArgumentParser> GDALVectorInfoOptionsGetParser(
2246
    GDALVectorInfoOptions *psOptions,
2247
    GDALVectorInfoOptionsForBinary *psOptionsForBinary)
2248
0
{
2249
0
    auto argParser = std::make_unique<GDALArgumentParser>(
2250
0
        "ogrinfo", /* bForBinary=*/psOptionsForBinary != nullptr);
2251
2252
0
    argParser->add_description(
2253
0
        _("Lists information about an OGR-supported data source."));
2254
2255
0
    argParser->add_epilog(
2256
0
        _("For more details, consult https://gdal.org/programs/ogrinfo.html"));
2257
2258
0
    argParser->add_argument("-json")
2259
0
        .flag()
2260
0
        .action([psOptions](const std::string &)
2261
0
                { psOptions->eFormat = FORMAT_JSON; })
2262
0
        .help(_("Display the output in json format."));
2263
2264
    // Hidden argument to select OGR_SCHEMA output
2265
0
    argParser->add_argument("-schema")
2266
0
        .flag()
2267
0
        .hidden()
2268
0
        .action([psOptions](const std::string &)
2269
0
                { psOptions->bExportOgrSchema = true; })
2270
0
        .help(_("Export the OGR_SCHEMA in json format."));
2271
2272
0
    argParser->add_argument("-ro")
2273
0
        .flag()
2274
0
        .action(
2275
0
            [psOptionsForBinary](const std::string &)
2276
0
            {
2277
0
                if (psOptionsForBinary)
2278
0
                    psOptionsForBinary->bReadOnly = true;
2279
0
            })
2280
0
        .help(_("Open the data source in read-only mode."));
2281
2282
0
    argParser->add_argument("-update")
2283
0
        .flag()
2284
0
        .action(
2285
0
            [psOptionsForBinary](const std::string &)
2286
0
            {
2287
0
                if (psOptionsForBinary)
2288
0
                    psOptionsForBinary->bUpdate = true;
2289
0
            })
2290
0
        .help(_("Open the data source in update mode."));
2291
2292
0
    argParser->add_argument("-q", "--quiet")
2293
0
        .flag()
2294
0
        .action(
2295
0
            [psOptions, psOptionsForBinary](const std::string &)
2296
0
            {
2297
0
                psOptions->bVerbose = false;
2298
0
                if (psOptionsForBinary)
2299
0
                    psOptionsForBinary->bVerbose = false;
2300
0
            })
2301
0
        .help(_("Quiet mode. No progress message is emitted on the standard "
2302
0
                "output."));
2303
2304
#ifdef __AFL_HAVE_MANUAL_CONTROL
2305
    /* Undocumented: mainly only useful for AFL testing */
2306
    argParser->add_argument("-qq")
2307
        .flag()
2308
        .hidden()
2309
        .action(
2310
            [psOptions, psOptionsForBinary](const std::string &)
2311
            {
2312
                psOptions->bVerbose = false;
2313
                if (psOptionsForBinary)
2314
                    psOptionsForBinary->bVerbose = false;
2315
                psOptions->bSuperQuiet = true;
2316
            })
2317
        .help(_("Super quiet mode."));
2318
#endif
2319
2320
0
    argParser->add_argument("-fid")
2321
0
        .metavar("<FID>")
2322
0
        .store_into(psOptions->nFetchFID)
2323
0
        .help(_("Only the feature with this feature id will be reported."));
2324
2325
0
    argParser->add_argument("-spat")
2326
0
        .metavar("<xmin> <ymin> <xmax> <ymax>")
2327
0
        .nargs(4)
2328
0
        .scan<'g', double>()
2329
0
        .help(_("The area of interest. Only features within the rectangle will "
2330
0
                "be reported."));
2331
2332
0
    argParser->add_argument("-geomfield")
2333
0
        .metavar("<field>")
2334
0
        .store_into(psOptions->osGeomField)
2335
0
        .help(_("Name of the geometry field on which the spatial filter "
2336
0
                "operates."));
2337
2338
0
    argParser->add_argument("-where")
2339
0
        .metavar("<restricted_where>")
2340
0
        .store_into(psOptions->osWHERE)
2341
0
        .help(_("An attribute query in a restricted form of the queries used "
2342
0
                "in the SQL WHERE statement."));
2343
2344
0
    {
2345
0
        auto &group = argParser->add_mutually_exclusive_group();
2346
0
        group.add_argument("-sql")
2347
0
            .metavar("<statement|@filename>")
2348
0
            .store_into(psOptions->osSQLStatement)
2349
0
            .help(_(
2350
0
                "Execute the indicated SQL statement and return the result."));
2351
2352
0
        group.add_argument("-rl")
2353
0
            .store_into(psOptions->bDatasetGetNextFeature)
2354
0
            .help(_("Enable random layer reading mode."));
2355
0
    }
2356
2357
0
    argParser->add_argument("-dialect")
2358
0
        .metavar("<dialect>")
2359
0
        .store_into(psOptions->osDialect)
2360
0
        .help(_("SQL dialect."));
2361
2362
    // Only for fuzzing
2363
0
    argParser->add_argument("-rc")
2364
0
        .hidden()
2365
0
        .metavar("<count>")
2366
0
        .store_into(psOptions->nRepeatCount)
2367
0
        .help(_("Repeat count"));
2368
2369
0
    argParser->add_argument("-al")
2370
0
        .store_into(psOptions->bAllLayers)
2371
0
        .help(_("List all layers (used instead of having to give layer names "
2372
0
                "as arguments)."));
2373
2374
0
    {
2375
0
        auto &group = argParser->add_mutually_exclusive_group();
2376
0
        group.add_argument("-so", "-summary")
2377
0
            .store_into(psOptions->bSummaryUserRequested)
2378
0
            .help(_("Summary only: show only summary information like "
2379
0
                    "projection, schema, feature count and extents."));
2380
2381
0
        group.add_argument("-features")
2382
0
            .store_into(psOptions->bFeaturesUserRequested)
2383
0
            .help(_("Enable listing of features."));
2384
0
    }
2385
2386
0
    argParser->add_argument("-limit")
2387
0
        .metavar("<nb_features>")
2388
0
        .store_into(psOptions->nLimit)
2389
0
        .help(_("Limit the number of features per layer."));
2390
2391
0
    argParser->add_argument("-fields")
2392
0
        .choices("YES", "NO")
2393
0
        .metavar("YES|NO")
2394
0
        .action(
2395
0
            [psOptions](const std::string &s)
2396
0
            {
2397
0
                psOptions->aosOptions.SetNameValue("DISPLAY_FIELDS", s.c_str());
2398
0
            })
2399
0
        .help(
2400
0
            _("If set to NO, the feature dump will not display field values."));
2401
2402
0
    argParser->add_argument("-geom")
2403
0
        .choices("YES", "NO", "SUMMARY", "WKT", "ISO_WKT")
2404
0
        .metavar("YES|NO|SUMMARY|WKT|ISO_WKT")
2405
0
        .action(
2406
0
            [psOptions](const std::string &s)
2407
0
            {
2408
0
                psOptions->aosOptions.SetNameValue("DISPLAY_GEOMETRY",
2409
0
                                                   s.c_str());
2410
0
            })
2411
0
        .help(_("How to display geometries in feature dump."));
2412
2413
0
    argParser->add_argument("-oo")
2414
0
        .append()
2415
0
        .metavar("<NAME=VALUE>")
2416
0
        .action(
2417
0
            [psOptionsForBinary](const std::string &s)
2418
0
            {
2419
0
                if (psOptionsForBinary)
2420
0
                    psOptionsForBinary->aosOpenOptions.AddString(s.c_str());
2421
0
            })
2422
0
        .help(_("Dataset open option (format-specific)."));
2423
2424
0
    argParser->add_argument("-nomd")
2425
0
        .flag()
2426
0
        .action([psOptions](const std::string &)
2427
0
                { psOptions->bShowMetadata = false; })
2428
0
        .help(_("Suppress metadata printing."));
2429
2430
0
    argParser->add_argument("-listmdd")
2431
0
        .store_into(psOptions->bListMDD)
2432
0
        .help(_("List all metadata domains available for the dataset."));
2433
2434
0
    argParser->add_argument("-mdd")
2435
0
        .append()
2436
0
        .metavar("<domain>")
2437
0
        .action([psOptions](const std::string &s)
2438
0
                { psOptions->aosExtraMDDomains.AddString(s.c_str()); })
2439
0
        .help(_("List metadata in the specified domain."));
2440
2441
0
    argParser->add_argument("-nocount")
2442
0
        .flag()
2443
0
        .action([psOptions](const std::string &)
2444
0
                { psOptions->bFeatureCount = false; })
2445
0
        .help(_("Suppress feature count printing."));
2446
2447
0
    argParser->add_argument("-noextent")
2448
0
        .flag()
2449
0
        .action([psOptions](const std::string &)
2450
0
                { psOptions->bExtent = false; })
2451
0
        .help(_("Suppress spatial extent printing."));
2452
2453
0
    argParser->add_argument("-extent3D")
2454
0
        .store_into(psOptions->bExtent3D)
2455
0
        .help(_("Request a 3D extent to be reported."));
2456
2457
0
    argParser->add_argument("-nogeomtype")
2458
0
        .flag()
2459
0
        .action([psOptions](const std::string &)
2460
0
                { psOptions->bGeomType = false; })
2461
0
        .help(_("Suppress layer geometry type printing."));
2462
2463
0
    argParser->add_argument("-wkt_format")
2464
0
        .store_into(psOptions->osWKTFormat)
2465
0
        .metavar("WKT1|WKT2|WKT2_2015|WKT2_2019")
2466
0
        .help(_("The WKT format used to display the SRS."));
2467
2468
0
    argParser->add_argument("-fielddomain")
2469
0
        .store_into(psOptions->osFieldDomain)
2470
0
        .metavar("<name>")
2471
0
        .help(_("Display details about a field domain."));
2472
2473
0
    argParser->add_argument("-if")
2474
0
        .append()
2475
0
        .metavar("<format>")
2476
0
        .action(
2477
0
            [psOptionsForBinary](const std::string &s)
2478
0
            {
2479
0
                if (psOptionsForBinary)
2480
0
                {
2481
0
                    if (GDALGetDriverByName(s.c_str()) == nullptr)
2482
0
                    {
2483
0
                        CPLError(CE_Warning, CPLE_AppDefined,
2484
0
                                 "%s is not a recognized driver", s.c_str());
2485
0
                    }
2486
0
                    psOptionsForBinary->aosAllowInputDrivers.AddString(
2487
0
                        s.c_str());
2488
0
                }
2489
0
            })
2490
0
        .help(_("Format/driver name(s) to try when opening the input file."));
2491
2492
0
    argParser->add_argument("-stdout")
2493
0
        .flag()
2494
0
        .store_into(psOptions->bStdoutOutput)
2495
0
        .hidden()
2496
0
        .help(_("Directly output on stdout (format=text mode only)"));
2497
2498
0
    argParser->add_argument("--cli")
2499
0
        .hidden()
2500
0
        .store_into(psOptions->bIsCli)
2501
0
        .help(_("Indicates that this is called from the gdal vector info CLI "
2502
0
                "utility."));
2503
2504
0
    auto &argFilename = argParser->add_argument("filename")
2505
0
                            .action(
2506
0
                                [psOptionsForBinary](const std::string &s)
2507
0
                                {
2508
0
                                    if (psOptionsForBinary)
2509
0
                                        psOptionsForBinary->osFilename = s;
2510
0
                                })
2511
0
                            .help(_("The data source to open."));
2512
0
    if (!psOptionsForBinary)
2513
0
        argFilename.nargs(argparse::nargs_pattern::optional);
2514
2515
0
    argParser->add_argument("layer")
2516
0
        .remaining()
2517
0
        .metavar("<layer_name>")
2518
0
        .help(_("Layer name."));
2519
2520
0
    return argParser;
2521
0
}
2522
2523
/************************************************************************/
2524
/*                    GDALVectorInfoGetParserUsage()                    */
2525
/************************************************************************/
2526
2527
std::string GDALVectorInfoGetParserUsage()
2528
0
{
2529
0
    try
2530
0
    {
2531
0
        GDALVectorInfoOptions sOptions;
2532
0
        GDALVectorInfoOptionsForBinary sOptionsForBinary;
2533
0
        auto argParser =
2534
0
            GDALVectorInfoOptionsGetParser(&sOptions, &sOptionsForBinary);
2535
0
        return argParser->usage();
2536
0
    }
2537
0
    catch (const std::exception &err)
2538
0
    {
2539
0
        CPLError(CE_Failure, CPLE_AppDefined, "Unexpected exception: %s",
2540
0
                 err.what());
2541
0
        return std::string();
2542
0
    }
2543
0
}
2544
2545
/************************************************************************/
2546
/*                      GDALVectorInfoOptionsNew()                      */
2547
/************************************************************************/
2548
2549
/**
2550
 * Allocates a GDALVectorInfoOptions struct.
2551
 *
2552
 * Note that  when this function is used a library function, and not from the
2553
 * ogrinfo utility, a dataset name must be specified if any layer names(s) are
2554
 * specified (if no layer name is specific, passing a dataset name is not
2555
 * needed). That dataset name may be a dummy one, as the dataset taken into
2556
 * account is the hDS parameter passed to GDALVectorInfo().
2557
 * Similarly the -oo switch in a non-ogrinfo context will be ignored, and it
2558
 * is the responsibility of the user to apply them when opening the hDS parameter
2559
 * passed to GDALVectorInfo().
2560
 *
2561
 * @param papszArgv NULL terminated list of options (potentially including
2562
 * filename and open options too), or NULL. The accepted options are the ones of
2563
 * the <a href="/programs/ogrinfo.html">ogrinfo</a> utility.
2564
 * @param psOptionsForBinary (output) may be NULL (and should generally be
2565
 * NULL), otherwise (ogrinfo_bin.cpp use case) must be allocated with
2566
 * GDALVectorInfoOptionsForBinaryNew() prior to this
2567
 * function. Will be filled with potentially present filename, open options,
2568
 * subdataset number...
2569
 * @return pointer to the allocated GDALVectorInfoOptions struct. Must be freed
2570
 * with GDALVectorInfoOptionsFree().
2571
 *
2572
 * @since GDAL 3.7
2573
 */
2574
2575
GDALVectorInfoOptions *
2576
GDALVectorInfoOptionsNew(char **papszArgv,
2577
                         GDALVectorInfoOptionsForBinary *psOptionsForBinary)
2578
0
{
2579
0
    auto psOptions = std::make_unique<GDALVectorInfoOptions>();
2580
2581
0
    try
2582
0
    {
2583
0
        auto argParser =
2584
0
            GDALVectorInfoOptionsGetParser(psOptions.get(), psOptionsForBinary);
2585
2586
        /* Special pre-processing to rewrite -fields=foo as "-fields" "FOO", and
2587
     * same for -geom=foo. */
2588
0
        CPLStringList aosArgv;
2589
0
        for (CSLConstList papszIter = papszArgv; papszIter && *papszIter;
2590
0
             ++papszIter)
2591
0
        {
2592
0
            if (STARTS_WITH(*papszIter, "-fields="))
2593
0
            {
2594
0
                aosArgv.AddString("-fields");
2595
0
                aosArgv.AddString(
2596
0
                    CPLString(*papszIter + strlen("-fields=")).toupper());
2597
0
            }
2598
0
            else if (STARTS_WITH(*papszIter, "-geom="))
2599
0
            {
2600
0
                aosArgv.AddString("-geom");
2601
0
                aosArgv.AddString(
2602
0
                    CPLString(*papszIter + strlen("-geom=")).toupper());
2603
0
            }
2604
0
            else
2605
0
            {
2606
0
                aosArgv.AddString(*papszIter);
2607
0
            }
2608
0
        }
2609
2610
0
        argParser->parse_args_without_binary_name(aosArgv.List());
2611
2612
0
        auto layers = argParser->present<std::vector<std::string>>("layer");
2613
0
        if (layers)
2614
0
        {
2615
0
            for (const auto &layer : *layers)
2616
0
            {
2617
0
                psOptions->aosLayers.AddString(layer.c_str());
2618
0
                psOptions->bAllLayers = false;
2619
0
            }
2620
0
        }
2621
2622
0
        if (auto oSpat = argParser->present<std::vector<double>>("-spat"))
2623
0
        {
2624
0
            const double dfMinX = (*oSpat)[0];
2625
0
            const double dfMinY = (*oSpat)[1];
2626
0
            const double dfMaxX = (*oSpat)[2];
2627
0
            const double dfMaxY = (*oSpat)[3];
2628
2629
0
            auto poPolygon =
2630
0
                std::make_unique<OGRPolygon>(dfMinX, dfMinY, dfMaxX, dfMaxY);
2631
0
            psOptions->poSpatialFilter.reset(poPolygon.release());
2632
0
        }
2633
2634
0
        if (!psOptions->osWHERE.empty() && psOptions->osWHERE[0] == '@')
2635
0
        {
2636
0
            GByte *pabyRet = nullptr;
2637
0
            if (VSIIngestFile(nullptr, psOptions->osWHERE.substr(1).c_str(),
2638
0
                              &pabyRet, nullptr, 10 * 1024 * 1024))
2639
0
            {
2640
0
                GDALRemoveBOM(pabyRet);
2641
0
                psOptions->osWHERE = reinterpret_cast<const char *>(pabyRet);
2642
0
                VSIFree(pabyRet);
2643
0
            }
2644
0
            else
2645
0
            {
2646
0
                CPLError(CE_Failure, CPLE_FileIO, "Cannot open %s",
2647
0
                         psOptions->osWHERE.substr(1).c_str());
2648
0
                return nullptr;
2649
0
            }
2650
0
        }
2651
2652
0
        if (!psOptions->osSQLStatement.empty() &&
2653
0
            psOptions->osSQLStatement[0] == '@')
2654
0
        {
2655
0
            GByte *pabyRet = nullptr;
2656
0
            if (VSIIngestFile(nullptr,
2657
0
                              psOptions->osSQLStatement.substr(1).c_str(),
2658
0
                              &pabyRet, nullptr, 10 * 1024 * 1024))
2659
0
            {
2660
0
                GDALRemoveBOM(pabyRet);
2661
0
                char *pszSQLStatement = reinterpret_cast<char *>(pabyRet);
2662
0
                psOptions->osSQLStatement =
2663
0
                    CPLRemoveSQLComments(pszSQLStatement);
2664
0
                VSIFree(pabyRet);
2665
0
            }
2666
0
            else
2667
0
            {
2668
0
                CPLError(CE_Failure, CPLE_FileIO, "Cannot open %s",
2669
0
                         psOptions->osSQLStatement.substr(1).c_str());
2670
0
                return nullptr;
2671
0
            }
2672
0
        }
2673
2674
0
        if (psOptionsForBinary)
2675
0
        {
2676
0
            psOptions->bStdoutOutput = true;
2677
0
            psOptionsForBinary->osSQLStatement = psOptions->osSQLStatement;
2678
0
        }
2679
2680
0
        if (psOptions->eFormat == FORMAT_JSON)
2681
0
        {
2682
0
            psOptions->bAllLayers = true;
2683
0
            psOptions->bSummaryOnly = true;
2684
0
            if (psOptions->aosExtraMDDomains.empty())
2685
0
                psOptions->aosExtraMDDomains.AddString("all");
2686
0
            psOptions->bStdoutOutput = false;
2687
0
        }
2688
2689
0
        if (psOptions->bSummaryUserRequested)
2690
0
            psOptions->bSummaryOnly = true;
2691
0
        else if (psOptions->bFeaturesUserRequested)
2692
0
            psOptions->bSummaryOnly = false;
2693
2694
0
        if (!psOptions->osDialect.empty() && !psOptions->osWHERE.empty() &&
2695
0
            psOptions->osSQLStatement.empty())
2696
0
        {
2697
0
            CPLError(CE_Warning, CPLE_AppDefined,
2698
0
                     "-dialect is ignored with -where. Use -sql instead");
2699
0
        }
2700
2701
        // Patch options when -schema is set
2702
0
        if (psOptions->bExportOgrSchema)
2703
0
        {
2704
            // TODO: validate and raise an error if incompatible options are set?
2705
            //       not strictly necessary given that -schema is an hidden option
2706
0
            psOptions->eFormat = FORMAT_JSON;
2707
0
            psOptions->bAllLayers = true;
2708
0
            psOptions->bShowMetadata = false;
2709
0
            psOptions->bListMDD = false;
2710
0
            psOptions->bFeatureCount = false;
2711
0
            psOptions->bIsCli = true;
2712
0
            psOptions->bSummaryOnly = false;
2713
0
            psOptions->bExtent = false;
2714
0
            psOptions->bExtent3D = false;
2715
0
        }
2716
2717
0
        return psOptions.release();
2718
0
    }
2719
0
    catch (const std::exception &err)
2720
0
    {
2721
0
        CPLError(CE_Failure, CPLE_AppDefined, "%s", err.what());
2722
0
        return nullptr;
2723
0
    }
2724
0
}