Coverage Report

Created: 2026-02-14 06:52

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